PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Java | 222 lines | 126 code | 29 blank | 67 comment | 2 complexity | 294c32ed2eb380fbf751e61546f430ff 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. * IndentAction.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 org.gjt.sp.jedit.buffer.JEditBuffer;
  24. import org.gjt.sp.util.StandardUtilities;
  25. /**
  26. * @author Slava Pestov
  27. * @version $Id: IndentAction.java 15614 2009-06-30 07:21:56Z voituk $
  28. */
  29. public interface IndentAction
  30. {
  31. /**
  32. * @param buffer The buffer
  33. * @param line The line number that matched the rule; not necessarily
  34. * the line being indented.
  35. * @param oldIndent Original indent.
  36. * @param newIndent The new indent -- ie, indent returned by previous
  37. * indent action.
  38. */
  39. int calculateIndent(JEditBuffer buffer, int line, int oldIndent,
  40. int newIndent);
  41. /**
  42. * @return true if the indent engine should keep processing after
  43. * this rule.
  44. */
  45. boolean keepChecking();
  46. /** See comments for each instance of this class below. */
  47. class Collapse implements IndentAction
  48. {
  49. /**
  50. * This does nothing; it is merely a sentinel for the
  51. * <code>OpenBracketIndentRule</code>.
  52. */
  53. public int calculateIndent(JEditBuffer buffer, int line, int oldIndent,
  54. int newIndent)
  55. {
  56. return newIndent;
  57. }
  58. public boolean keepChecking()
  59. {
  60. return true;
  61. }
  62. private Collapse()
  63. {
  64. }
  65. }
  66. class Reset implements IndentAction
  67. {
  68. public int calculateIndent(JEditBuffer buffer, int line, int oldIndent,
  69. int newIndent)
  70. {
  71. return oldIndent;
  72. }
  73. public boolean keepChecking()
  74. {
  75. return true;
  76. }
  77. }
  78. class Increase implements IndentAction
  79. {
  80. private int amount;
  81. public Increase()
  82. {
  83. amount = 1;
  84. }
  85. public Increase(int amount)
  86. {
  87. this.amount = amount;
  88. }
  89. public int calculateIndent(JEditBuffer buffer, int line, int oldIndent,
  90. int newIndent)
  91. {
  92. return newIndent + buffer.getIndentSize() * amount;
  93. }
  94. public boolean keepChecking()
  95. {
  96. return true;
  97. }
  98. public boolean equals(Object o)
  99. {
  100. if(o instanceof Increase)
  101. return ((Increase)o).amount == amount;
  102. else
  103. return false;
  104. }
  105. }
  106. class Decrease implements IndentAction
  107. {
  108. public int calculateIndent(JEditBuffer buffer, int line, int oldIndent,
  109. int newIndent)
  110. {
  111. return newIndent - buffer.getIndentSize();
  112. }
  113. public boolean keepChecking()
  114. {
  115. return true;
  116. }
  117. }
  118. /**
  119. * @author Matthieu Casanova
  120. */
  121. class AlignOffset implements IndentAction
  122. {
  123. private int offset;
  124. public AlignOffset(int offset)
  125. {
  126. this.offset = offset;
  127. }
  128. public int calculateIndent(JEditBuffer buffer, int line, int oldIndent,
  129. int newIndent)
  130. {
  131. return offset;
  132. }
  133. public boolean keepChecking()
  134. {
  135. return false;
  136. }
  137. }
  138. /**
  139. * Indent action used for deep indent.
  140. * @author Matthieu Casanova
  141. */
  142. class AlignParameter implements IndentAction
  143. {
  144. private int openParensColumn;
  145. public AlignParameter(int openParensColumn)
  146. {
  147. this.openParensColumn = openParensColumn;
  148. }
  149. public int calculateIndent(JEditBuffer buffer, int line, int oldIndent,
  150. int newIndent)
  151. {
  152. return openParensColumn + 1;
  153. }
  154. public boolean keepChecking()
  155. {
  156. return false;
  157. }
  158. }
  159. /**
  160. * Used to cancel increases in indentation.
  161. *
  162. * @author Marcelo Vanzin
  163. */
  164. class NoIncrease implements IndentAction
  165. {
  166. public int calculateIndent(JEditBuffer buffer, int line, int oldIndent,
  167. int newIndent)
  168. {
  169. int current = StandardUtilities.getLeadingWhiteSpaceWidth(
  170. buffer.getLineSegment(line),buffer.getTabSize());
  171. return (current < newIndent) ? current : newIndent;
  172. }
  173. public boolean keepChecking()
  174. {
  175. return true;
  176. }
  177. }
  178. /**
  179. * This handles the following Java code:
  180. * if(something)
  181. * { // no indentation on this line, even though previous matches a rule
  182. */
  183. Collapse PrevCollapse = new Collapse();
  184. /**
  185. * This handles cases like:
  186. * if (foo)
  187. * bar;
  188. * for (something; condition; action) {
  189. * }
  190. * Without this the "for" line would be incorrectly indented.
  191. */
  192. Collapse PrevPrevCollapse = new Collapse();
  193. }