001: /*
002: * IndentAction.java
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2005 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit.indent;
024:
025: import org.gjt.sp.jedit.buffer.JEditBuffer;
026: import org.gjt.sp.jedit.TextUtilities;
027: import org.gjt.sp.util.StandardUtilities;
028:
029: /**
030: * @author Slava Pestov
031: * @version $Id: IndentAction.java 9524 2007-05-10 14:51:01Z k_satoda $
032: */
033: public interface IndentAction {
034: /**
035: * @param buffer The buffer
036: * @param line The line number that matched the rule; not necessarily
037: * the line being indented.
038: * @param oldIndent Original indent.
039: * @param newIndent The new indent -- ie, indent returned by previous
040: * indent action.
041: */
042: int calculateIndent(JEditBuffer buffer, int line, int oldIndent,
043: int newIndent);
044:
045: /**
046: * @return true if the indent engine should keep processing after
047: * this rule.
048: */
049: boolean keepChecking();
050:
051: /** See comments for each instance of this class below. */
052: class Collapse implements IndentAction {
053: /**
054: * This does nothing; it is merely a sentinel for the
055: * <code>OpenBracketIndentRule</code>.
056: */
057: public int calculateIndent(JEditBuffer buffer, int line,
058: int oldIndent, int newIndent) {
059: return newIndent;
060: }
061:
062: public boolean keepChecking() {
063: return true;
064: }
065:
066: private Collapse() {
067: }
068: }
069:
070: class Reset implements IndentAction {
071: public int calculateIndent(JEditBuffer buffer, int line,
072: int oldIndent, int newIndent) {
073: return oldIndent;
074: }
075:
076: public boolean keepChecking() {
077: return true;
078: }
079: }
080:
081: class Increase implements IndentAction {
082: private int amount;
083:
084: public Increase() {
085: amount = 1;
086: }
087:
088: public Increase(int amount) {
089: this .amount = amount;
090: }
091:
092: public int calculateIndent(JEditBuffer buffer, int line,
093: int oldIndent, int newIndent) {
094: return newIndent + buffer.getIndentSize() * amount;
095: }
096:
097: public boolean keepChecking() {
098: return true;
099: }
100:
101: public boolean equals(Object o) {
102: if (o instanceof Increase)
103: return ((Increase) o).amount == amount;
104: else
105: return false;
106: }
107: }
108:
109: class Decrease implements IndentAction {
110: public int calculateIndent(JEditBuffer buffer, int line,
111: int oldIndent, int newIndent) {
112: return newIndent - buffer.getIndentSize();
113: }
114:
115: public boolean keepChecking() {
116: return true;
117: }
118: }
119:
120: /**
121: * @author Matthieu Casanova
122: */
123: class AlignOffset implements IndentAction {
124: private int offset;
125:
126: public AlignOffset(int offset) {
127: this .offset = offset;
128: }
129:
130: public int calculateIndent(JEditBuffer buffer, int line,
131: int oldIndent, int newIndent) {
132: return offset;
133: }
134:
135: public boolean keepChecking() {
136: return false;
137: }
138: }
139:
140: /**
141: * Indent action used for deep indent.
142: * @author Matthieu Casanova
143: */
144: class AlignParameter implements IndentAction {
145: private int openParensColumn;
146: private String openParensLineText;
147:
148: public AlignParameter(int openParensColumn,
149: String openParensLineText) {
150: this .openParensLineText = openParensLineText;
151: this .openParensColumn = openParensColumn;
152: }
153:
154: public int calculateIndent(JEditBuffer buffer, int line,
155: int oldIndent, int newIndent) {
156: return openParensLineText == null ? newIndent
157: : openParensColumn + 1;
158: }
159:
160: public boolean keepChecking() {
161: return false;
162: }
163: }
164:
165: /**
166: * Used to cancel increases in indentation.
167: *
168: * @author Marcelo Vanzin
169: */
170: class NoIncrease implements IndentAction {
171: public int calculateIndent(JEditBuffer buffer, int line,
172: int oldIndent, int newIndent) {
173: int current = StandardUtilities.getLeadingWhiteSpaceWidth(
174: buffer.getLineText(line), buffer.getTabSize());
175: return (current < newIndent) ? current : newIndent;
176: }
177:
178: public boolean keepChecking() {
179: return true;
180: }
181: }
182:
183: /**
184: * This handles the following Java code:
185: * if(something)
186: * { // no indentation on this line, even though previous matches a rule
187: */
188: Collapse PrevCollapse = new Collapse();
189: /**
190: * This handles cases like:
191: * if (foo)
192: * bar;
193: * for (something; condition; action) {
194: * }
195: * Without this the "for" line would be incorrectly indented.
196: */
197: Collapse PrevPrevCollapse = new Collapse();
198: }
|