KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > indent > CloseBracketIndentRule


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
23 package org.gjt.sp.jedit.indent;
24
25 import java.util.List JavaDoc;
26 import org.gjt.sp.jedit.buffer.JEditBuffer;
27 import org.gjt.sp.jedit.TextUtilities;
28
29 /**
30  * @author Slava Pestov
31  * @version $Id: CloseBracketIndentRule.java 5579 2006-07-13 10:53:15Z kpouer $
32  */

33 public class CloseBracketIndentRule extends BracketIndentRule
34 {
35     //{{{ CloseBracketIndentRule constructor
36
public CloseBracketIndentRule(char closeBracket, boolean aligned)
37     {
38         super(TextUtilities.getComplementaryBracket(closeBracket,
39             new boolean[1]),closeBracket);
40         this.aligned = aligned;
41     } //}}}
42

43     //{{{ apply() method
44
public void apply(JEditBuffer buffer, int thisLineIndex,
45         int prevLineIndex, int prevPrevLineIndex,
46         List JavaDoc<IndentAction> indentActions)
47     {
48         int index;
49         if(aligned)
50             index = thisLineIndex;
51         else
52             index = prevLineIndex;
53
54         if(index == -1)
55             return;
56
57         String JavaDoc line = buffer.getLineText(index);
58
59         int offset = line.lastIndexOf(closeBracket);
60         if(offset == -1)
61             return;
62
63         int closeCount = getBrackets(line).closeCount;
64         if(closeCount != 0)
65         {
66             IndentAction.AlignBracket alignBracket
67                 = new IndentAction.AlignBracket(
68                 buffer,index,offset);
69             /*
70             Consider the following Common Lisp code (with one more opening
71             bracket than closing):
72
73             (defun emit-push-long (arg)
74               (cond ((eql arg 0)
75                   (emit 'lconst_0))
76                 ((eql arg 1)
77                   (emit 'lconst_1)))
78
79             even though we have a closing bracket match on line 3,
80             the next line must be indented relative to the
81             corresponding opening bracket from line 1.
82             */

83             String JavaDoc openLine = alignBracket.getOpenBracketLine();
84             int column = alignBracket.getOpenBracketColumn();
85             if(openLine != null)
86             {
87                 String JavaDoc leadingBrackets = openLine.substring(0,column);
88                 alignBracket.setExtraIndent(getBrackets(leadingBrackets)
89                     .openCount);
90             }
91
92             indentActions.add(alignBracket);
93         }
94     } //}}}
95

96     //{{{ isMatch() method
97
public boolean isMatch(String JavaDoc line)
98     {
99         return getBrackets(line).closeCount != 0;
100     } //}}}
101

102     private boolean aligned;
103 }
104
Popular Tags