001: /*
002: * CloseBracketIndentRule.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 java.util.List;
026: import org.gjt.sp.jedit.buffer.JEditBuffer;
027: import org.gjt.sp.jedit.TextUtilities;
028: import org.gjt.sp.util.StandardUtilities;
029:
030: /**
031: * @author Slava Pestov
032: * @version $Id: CloseBracketIndentRule.java 9524 2007-05-10 14:51:01Z k_satoda $
033: */
034: public class CloseBracketIndentRule extends BracketIndentRule {
035: //{{{ CloseBracketIndentRule constructor
036: public CloseBracketIndentRule(char closeBracket, boolean aligned) {
037: super (
038: TextUtilities.getComplementaryBracket(closeBracket,
039: null), closeBracket);
040: this .aligned = aligned;
041: } //}}}
042:
043: //{{{ apply() method
044: public void apply(JEditBuffer buffer, int this LineIndex,
045: int prevLineIndex, int prevPrevLineIndex,
046: List<IndentAction> indentActions) {
047: int index;
048: if (aligned)
049: index = this LineIndex;
050: else
051: index = prevLineIndex;
052:
053: if (index == -1)
054: return;
055:
056: int offset = buffer.getLineText(index)
057: .lastIndexOf(closeBracket);
058: if (offset == -1)
059: return;
060:
061: int closeCount = getBrackets(buffer, index).closeCount;
062: if (closeCount != 0) {
063: AlignBracket alignBracket = new AlignBracket(buffer, index,
064: offset);
065: /*
066: Consider the following Common Lisp code (with one more opening
067: bracket than closing):
068:
069: (defun emit-push-long (arg)
070: (cond ((eql arg 0)
071: (emit 'lconst_0))
072: ((eql arg 1)
073: (emit 'lconst_1)))
074:
075: even though we have a closing bracket match on line 3,
076: the next line must be indented relative to the
077: corresponding opening bracket from line 1.
078: */
079: int openLine = alignBracket.getOpenBracketLine();
080: if (openLine != -1) {
081: int column = alignBracket.getOpenBracketColumn();
082: alignBracket.setExtraIndent(getBrackets(buffer,
083: openLine, 0, column).openCount);
084: }
085:
086: indentActions.add(alignBracket);
087: }
088: } //}}}
089:
090: //{{{ isMatch() method
091: /**
092: * @deprecated
093: * This method calls BracketIndentRule#getBrackets(String)
094: * which has been deprecated.
095: */
096: @Deprecated
097: public boolean isMatch(String line) {
098: return getBrackets(line).closeCount != 0;
099: } //}}}
100:
101: private boolean aligned;
102:
103: //{{{ AlignBracket class
104: private static class AlignBracket implements IndentAction {
105: private int line, offset;
106: private int openBracketLine;
107: private int openBracketColumn;
108: private String openBracketLineText;
109: private int extraIndent;
110:
111: public AlignBracket(JEditBuffer buffer, int line, int offset) {
112: this .line = line;
113: this .offset = offset;
114:
115: int openBracketIndex = TextUtilities.findMatchingBracket(
116: buffer, this .line, this .offset);
117: if (openBracketIndex == -1)
118: openBracketLine = -1;
119: else {
120: openBracketLine = buffer
121: .getLineOfOffset(openBracketIndex);
122: openBracketColumn = openBracketIndex
123: - buffer.getLineStartOffset(openBracketLine);
124: openBracketLineText = buffer
125: .getLineText(openBracketLine);
126: }
127: }
128:
129: public int getExtraIndent() {
130: return extraIndent;
131: }
132:
133: public void setExtraIndent(int extraIndent) {
134: this .extraIndent = extraIndent;
135: }
136:
137: public int getOpenBracketColumn() {
138: return openBracketColumn;
139: }
140:
141: public int getOpenBracketLine() {
142: return openBracketLine;
143: }
144:
145: public int calculateIndent(JEditBuffer buffer, int line,
146: int oldIndent, int newIndent) {
147: if (openBracketLineText == null)
148: return newIndent;
149: else {
150: return StandardUtilities.getLeadingWhiteSpaceWidth(
151: openBracketLineText, buffer.getTabSize())
152: + (extraIndent * buffer.getIndentSize());
153: }
154: }
155:
156: public boolean keepChecking() {
157: return false;
158: }
159: } //}}}
160: }
|