PageRenderTime 34ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Java | 167 lines | 107 code | 18 blank | 42 comment | 16 complexity | 4879e85c032b7df502a4e36696c0c1cc 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. * CloseBracketIndentRule.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 org.gjt.sp.jedit.buffer.JEditBuffer;
  25. import org.gjt.sp.jedit.TextUtilities;
  26. import org.gjt.sp.util.StandardUtilities;
  27. /**
  28. * @author Slava Pestov
  29. * @version $Id: CloseBracketIndentRule.java 18919 2010-11-04 10:52:55Z kpouer $
  30. */
  31. public class CloseBracketIndentRule extends BracketIndentRule
  32. {
  33. //{{{ CloseBracketIndentRule constructor
  34. public CloseBracketIndentRule(char closeBracket, boolean aligned)
  35. {
  36. super(TextUtilities.getComplementaryBracket(closeBracket,null),
  37. closeBracket);
  38. this.aligned = aligned;
  39. } //}}}
  40. //{{{ apply() method
  41. public void apply(JEditBuffer buffer, int thisLineIndex,
  42. int prevLineIndex, int prevPrevLineIndex,
  43. List<IndentAction> indentActions)
  44. {
  45. int index;
  46. if(aligned)
  47. index = thisLineIndex;
  48. else
  49. index = prevLineIndex;
  50. if(index == -1)
  51. return;
  52. CharSequence lineText = buffer.getLineSegment(index);
  53. int offset;
  54. for (offset = lineText.length() - 1; offset >= 0; offset--)
  55. {
  56. if (lineText.charAt(offset) == closeBracket)
  57. break;
  58. }
  59. if(offset == -1)
  60. return;
  61. int closeCount = getBrackets(buffer, index).closeCount;
  62. if(closeCount != 0)
  63. {
  64. AlignBracket alignBracket
  65. = new AlignBracket(buffer,index,offset);
  66. /*
  67. Consider the following Common Lisp code (with one more opening
  68. bracket than closing):
  69. (defun emit-push-long (arg)
  70. (cond ((eql arg 0)
  71. (emit 'lconst_0))
  72. ((eql arg 1)
  73. (emit 'lconst_1)))
  74. even though we have a closing bracket match on line 3,
  75. the next line must be indented relative to the
  76. corresponding opening bracket from line 1.
  77. */
  78. int openLine = alignBracket.getOpenBracketLine();
  79. if(openLine != -1)
  80. {
  81. int column = alignBracket.getOpenBracketColumn();
  82. alignBracket.setExtraIndent(
  83. getBrackets(buffer, openLine,
  84. 0, column).openCount);
  85. }
  86. indentActions.add(alignBracket);
  87. }
  88. } //}}}
  89. private boolean aligned;
  90. //{{{ AlignBracket class
  91. private static class AlignBracket implements IndentAction
  92. {
  93. private int line, offset;
  94. private int openBracketLine;
  95. private int openBracketColumn;
  96. private CharSequence openBracketLineText;
  97. private int extraIndent;
  98. public AlignBracket(JEditBuffer buffer, int line, int offset)
  99. {
  100. this.line = line;
  101. this.offset = offset;
  102. int openBracketIndex = TextUtilities.findMatchingBracket(
  103. buffer,this.line,this.offset);
  104. if(openBracketIndex == -1)
  105. openBracketLine = -1;
  106. else
  107. {
  108. openBracketLine = buffer.getLineOfOffset(openBracketIndex);
  109. openBracketColumn = openBracketIndex -
  110. buffer.getLineStartOffset(openBracketLine);
  111. openBracketLineText = buffer.getLineSegment(openBracketLine);
  112. }
  113. }
  114. public int getExtraIndent()
  115. {
  116. return extraIndent;
  117. }
  118. public void setExtraIndent(int extraIndent)
  119. {
  120. this.extraIndent = extraIndent;
  121. }
  122. public int getOpenBracketColumn()
  123. {
  124. return openBracketColumn;
  125. }
  126. public int getOpenBracketLine()
  127. {
  128. return openBracketLine;
  129. }
  130. public int calculateIndent(JEditBuffer buffer, int line, int oldIndent,
  131. int newIndent)
  132. {
  133. if(openBracketLineText == null)
  134. return newIndent;
  135. else
  136. {
  137. return StandardUtilities.getLeadingWhiteSpaceWidth(
  138. openBracketLineText,buffer.getTabSize())
  139. + (extraIndent * buffer.getIndentSize());
  140. }
  141. }
  142. public boolean keepChecking()
  143. {
  144. return false;
  145. }
  146. } //}}}
  147. }