PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre5/macros/Text/Toggle_Line_Comment.bsh

#
Unknown | 153 lines | 143 code | 10 blank | 0 comment | 0 complexity | f12102af6f705c3f7d6646626929dc6f MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * Toggle_Line_Comment.bsh - a BeanShell macro script for the
  3. * jEdit text editor - toggles comment at beginning of each
  4. * selected line (jEdit version 3.2 or greater required)
  5. * Copyright (C) 2001 John Gellene
  6. * jgellene@nyc.rr.com
  7. * http://community.jedit.org
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with the jEdit application; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. * This macro is based upon similar code in the TextTools plugin for
  24. * jEdit written by Valery Kondakof. The code presented here is updated to
  25. * use the selection API first included in jEdit 3.2. It also uses the
  26. * buffer's lineComment token instead of blockComment
  27. *
  28. * This macro requires version 3.2 (or greater) of jEdit.
  29. *
  30. * $Id: Toggle_Line_Comment.bsh 3936 2001-12-21 07:02:14Z spestov $
  31. *
  32. * Checked for jEdit 4.0 API
  33. *
  34. */
  35. void toggleLineComment()
  36. {
  37. /*
  38. * Guard for readonly files becuase Buffer.insert()
  39. * ignores the flag
  40. *
  41. */
  42. if(buffer.isReadOnly())
  43. {
  44. Macros.error(view, "This file is read only.");
  45. return;
  46. }
  47. String getRE(String str)
  48. {
  49. commentArray = str.toCharArray();
  50. escapedChars = ".?*()|[]\\";
  51. sbComment = new StringBuffer("^\\s*(");
  52. for (i = 0; i < commentArray.length; ++i)
  53. {
  54. ch = commentArray[i];
  55. if(-1 != escapedChars.indexOf(ch))
  56. sbComment.append('\\');
  57. sbComment.append(ch);
  58. }
  59. sbComment.append(")");
  60. return sbComment.toString();
  61. }
  62. void doComment(int line)
  63. {
  64. offset = textArea.getLineStartOffset(line);
  65. text = textArea.getLineText(line);
  66. try
  67. {
  68. if(text != null)
  69. {
  70. match = super.regExp.getMatch(text);
  71. if (match != null)
  72. {
  73. matchEnd = match.getEndIndex();
  74. replace = match.toString().substring(0,
  75. matchEnd - super.comment.length());
  76. buffer.remove(offset, matchEnd);
  77. buffer.insertString(offset, replace, null);
  78. return;
  79. }
  80. }
  81. buffer.insert(offset, super.comment);
  82. }
  83. catch(javax.swing.text.BadLocationException bl)
  84. {
  85. Log.log(Log.ERROR, this, bl);
  86. }
  87. }
  88. // main routine of method starts here
  89. comment = buffer.getContextSensitiveProperty(textArea.getCaretPosition(),
  90. "lineComment");
  91. if (comment == null)
  92. {
  93. Macros.error(view, "No line comment token to insert");
  94. return;
  95. }
  96. regExpString = getRE(comment);
  97. regExp = null;
  98. try
  99. {
  100. regExp = new gnu.regexp.RE(regExpString);
  101. }
  102. catch (gnu.regexp.REException e)
  103. {
  104. Log.log(Log.ERROR, this, e);
  105. Macros.error(view, "Could not create regexp for locating comments");
  106. return;
  107. }
  108. buffer.beginCompoundEdit();
  109. selections = textArea.getSelection();
  110. if(selections.length == 0)
  111. {
  112. doComment(textArea.getCaretLine());
  113. }
  114. else
  115. {
  116. for( i = 0; i < selections.length; ++i)
  117. {
  118. selItem = selections[i];
  119. startLine = selItem.getStartLine();
  120. endLine = selItem.getEndLine();
  121. for (j = startLine; j <= endLine; ++j)
  122. {
  123. doComment(j);
  124. }
  125. }
  126. }
  127. textArea.selectNone();
  128. }
  129. toggleLineComment();
  130. /*
  131. Macro index data (in DocBook format)
  132. <listitem>
  133. <para><filename>Toggle_Line_Comment.bsh</filename></para>
  134. <abstract><para>
  135. Toggles line comments, alternately inserting and deleting them
  136. at the beginning of each selected line.
  137. </para></abstract>
  138. <para>
  139. If there is no selection, the macro operates on the current line.
  140. </para>
  141. </listitem>
  142. */
  143. // end Toggle_Line_Comment.bsh