PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/indent/RegexpIndentRule.java

#
Java | 162 lines | 103 code | 14 blank | 45 comment | 17 complexity | 0fcb16cd52d95ad2640dc3bfb9c136ac 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. * RegexpIndentRule.java
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2005 Slava Pestov
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package org.gjt.sp.jedit.indent;
  23. import java.util.List;
  24. import java.util.regex.Matcher;
  25. import java.util.regex.Pattern;
  26. import java.util.regex.PatternSyntaxException;
  27. import javax.swing.text.Segment;
  28. import org.gjt.sp.jedit.buffer.JEditBuffer;
  29. import org.gjt.sp.jedit.syntax.Token;
  30. import org.gjt.sp.jedit.syntax.TokenHandler;
  31. import org.gjt.sp.jedit.syntax.TokenMarker;
  32. /**
  33. * @author Slava Pestov
  34. * @version $Id: RegexpIndentRule.java 18919 2010-11-04 10:52:55Z kpouer $
  35. */
  36. public class RegexpIndentRule implements IndentRule
  37. {
  38. //{{{ RegexpIndentRule constructor
  39. /**
  40. * @param collapse If true, then if the next indent rule is
  41. * an opening bracket, this rule will not increase indent.
  42. */
  43. public RegexpIndentRule(String regexp, IndentAction prevPrev,
  44. IndentAction prev, IndentAction thisLine, boolean collapse)
  45. throws PatternSyntaxException
  46. {
  47. prevPrevAction = prevPrev;
  48. prevAction = prev;
  49. thisAction = thisLine;
  50. this.regexp = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE );
  51. this.collapse = collapse;
  52. } //}}}
  53. //{{{ apply() method
  54. public void apply(JEditBuffer buffer, int thisLineIndex,
  55. int prevLineIndex, int prevPrevLineIndex,
  56. List<IndentAction> indentActions)
  57. {
  58. if(thisAction != null
  59. && lineMatches(buffer, thisLineIndex))
  60. {
  61. indentActions.add(thisAction);
  62. }
  63. if(prevAction != null
  64. && prevLineIndex != -1
  65. && lineMatches(buffer, prevLineIndex))
  66. {
  67. indentActions.add(prevAction);
  68. if (collapse)
  69. indentActions.add(IndentAction.PrevCollapse);
  70. }
  71. if(prevPrevAction != null
  72. && prevPrevLineIndex != -1
  73. && lineMatches(buffer, prevPrevLineIndex))
  74. {
  75. indentActions.add(prevPrevAction);
  76. if (collapse)
  77. indentActions.add(IndentAction.PrevPrevCollapse);
  78. }
  79. } //}}}
  80. //{{{ toString() method
  81. public String toString()
  82. {
  83. return getClass().getName() + '[' + regexp + ']';
  84. } //}}}
  85. private IndentAction prevPrevAction, prevAction, thisAction;
  86. private Pattern regexp;
  87. private boolean collapse;
  88. //{{{ class TokenFilter
  89. /**
  90. * A filter which removes non syntactic characters in comments
  91. * or literals which might confuse regexp matchings for indent.
  92. */
  93. private static class TokenFilter implements TokenHandler
  94. {
  95. public StringBuilder result;
  96. public TokenFilter(int originalLength)
  97. {
  98. result = new StringBuilder(originalLength);
  99. }
  100. public void handleToken(Segment seg
  101. , byte id, int offset, int length
  102. , TokenMarker.LineContext context)
  103. {
  104. // Avoid replacing an empty token into a non empty
  105. // string.
  106. if (length <= 0)
  107. {
  108. return;
  109. }
  110. switch (id)
  111. {
  112. case Token.COMMENT1:
  113. case Token.COMMENT2:
  114. case Token.COMMENT3:
  115. case Token.COMMENT4:
  116. // Replace any comments to a white space
  117. // so that they are simply ignored.
  118. result.append(' ');
  119. break;
  120. case Token.LITERAL1:
  121. case Token.LITERAL2:
  122. case Token.LITERAL3:
  123. case Token.LITERAL4:
  124. // Replace any literals to a '0' which means
  125. // a simple integer literal in most programming
  126. // languages.
  127. result.append('0');
  128. break;
  129. default:
  130. result.append(seg.array
  131. , seg.offset + offset
  132. , length);
  133. break;
  134. }
  135. }
  136. public void setLineContext(TokenMarker.LineContext lineContext)
  137. {
  138. }
  139. } //}}}
  140. //{{{ lineMatches() method
  141. private boolean lineMatches(JEditBuffer buffer, int lineIndex)
  142. {
  143. TokenFilter filter
  144. = new TokenFilter(buffer.getLineLength(lineIndex));
  145. buffer.markTokens(lineIndex, filter);
  146. return regexp.matcher(filter.result).matches();
  147. } //}}}
  148. }