PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 168 lines | 119 code | 16 blank | 33 comment | 14 complexity | 8273c1dfed1b0b7e9771a9c073be3cd4 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. * BracketIndentRule.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 javax.swing.text.Segment;
  24. import org.gjt.sp.jedit.buffer.JEditBuffer;
  25. import org.gjt.sp.jedit.syntax.Token;
  26. import org.gjt.sp.jedit.syntax.TokenHandler;
  27. import org.gjt.sp.jedit.syntax.TokenMarker;
  28. /**
  29. * @author Slava Pestov
  30. * @version $Id: BracketIndentRule.java 18919 2010-11-04 10:52:55Z kpouer $
  31. */
  32. public abstract class BracketIndentRule implements IndentRule
  33. {
  34. //{{{ BracketIndentRule constructor
  35. public BracketIndentRule(char openBracket, char closeBracket)
  36. {
  37. this.openBracket = openBracket;
  38. this.closeBracket = closeBracket;
  39. } //}}}
  40. //{{{ Brackets class
  41. public static class Brackets
  42. {
  43. int openCount;
  44. int closeCount;
  45. } //}}}
  46. //{{{ getBrackets() method
  47. public Brackets getBrackets(JEditBuffer buffer, int lineIndex)
  48. {
  49. return getBrackets(buffer, lineIndex,
  50. 0, buffer.getLineLength(lineIndex));
  51. } //}}}
  52. //{{{ getBrackets() method
  53. public Brackets getBrackets(JEditBuffer buffer, int lineIndex,
  54. int begin, int end)
  55. {
  56. LineScanner scanner = new LineScanner(begin, end);
  57. buffer.markTokens(lineIndex, scanner);
  58. return scanner.result;
  59. } //}}}
  60. //{{{ toString() method
  61. public String toString()
  62. {
  63. return getClass().getName() + "[" + openBracket + ","
  64. + closeBracket + "]";
  65. } //}}}
  66. protected char openBracket, closeBracket;
  67. //{{{ class LineScanner
  68. private class LineScanner implements TokenHandler
  69. {
  70. public final Brackets result;
  71. private int scannedIndex;
  72. private final int beginIndex;
  73. private final int endIndex;
  74. public LineScanner(int begin, int end)
  75. {
  76. this.result = new Brackets();
  77. this.scannedIndex = 0;
  78. this.beginIndex = begin;
  79. this.endIndex = end;
  80. }
  81. boolean rejectsToken(byte id)
  82. {
  83. // Rejects comments and literals.
  84. // Accepts all others.
  85. switch (id)
  86. {
  87. case Token.COMMENT1:
  88. case Token.COMMENT2:
  89. case Token.COMMENT3:
  90. case Token.COMMENT4:
  91. case Token.LITERAL1:
  92. case Token.LITERAL2:
  93. case Token.LITERAL3:
  94. case Token.LITERAL4:
  95. return true;
  96. default:
  97. return false;
  98. }
  99. }
  100. private void scan(Segment seg, int offset, int length)
  101. {
  102. int index = scannedIndex;
  103. if (index >= endIndex)
  104. {
  105. return;
  106. }
  107. if (index < beginIndex)
  108. {
  109. int numToSkip = beginIndex - index;
  110. if (numToSkip >= length)
  111. {
  112. return;
  113. }
  114. offset += numToSkip;
  115. length -= numToSkip;
  116. index = beginIndex;
  117. }
  118. if (index + length > endIndex)
  119. {
  120. length = endIndex - index;
  121. }
  122. for (int i = 0; i < length; ++i)
  123. {
  124. char c = seg.array[seg.offset + offset + i];
  125. if(c == openBracket)
  126. {
  127. result.openCount++;
  128. }
  129. else if(c == closeBracket)
  130. {
  131. if(result.openCount != 0)
  132. result.openCount--;
  133. else
  134. result.closeCount++;
  135. }
  136. }
  137. }
  138. public void handleToken(Segment seg
  139. , byte id, int offset, int length
  140. , TokenMarker.LineContext context)
  141. {
  142. if (!rejectsToken(id))
  143. {
  144. scan(seg, offset, length);
  145. }
  146. scannedIndex += length;
  147. }
  148. public void setLineContext(TokenMarker.LineContext lineContext)
  149. {
  150. }
  151. } //}}}
  152. }