KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * DeepIndentRule.java
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2006 Matthieu Casanova
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 org.gjt.sp.jedit.TextUtilities;
26 import org.gjt.sp.jedit.buffer.JEditBuffer;
27
28 import java.util.List JavaDoc;
29
30 /**
31  * Deep indent rule.
32  *
33  * @author Matthieu Casanova
34  * @version $Id: DeepIndentRule.java 6922 2006-09-11 14:44:30Z kpouer $
35  */

36 public class DeepIndentRule implements IndentRule
37 {
38     //{{{ getLastParens() method
39
/**
40      * Return the indexes of the last closing and the last opening parens in a line
41      *
42      * @param s the line text
43      * @param pos the starting pos from the end (or -1 for entire line)
44      *
45      * @return the last pos of the parens in the line
46      */

47     private static Parens getLastParens(String JavaDoc s, int pos)
48     {
49         int lastClose;
50         int lastOpen;
51         if (pos == -1)
52         {
53             lastClose = s.lastIndexOf(')');
54             lastOpen = s.lastIndexOf('(');
55         }
56         else
57         {
58             lastClose = s.lastIndexOf(')', pos);
59             lastOpen = s.lastIndexOf('(', pos);
60         }
61         return new Parens(lastOpen, lastClose);
62     } //}}}
63

64     //{{{ apply() method
65
public void apply(JEditBuffer buffer, int thisLineIndex,
66               int prevLineIndex, int prevPrevLineIndex,
67               List JavaDoc<IndentAction> indentActions)
68     {
69         if (prevLineIndex == -1)
70             return;
71         
72         int lineIndex = prevLineIndex;
73         int oldLineIndex = lineIndex;
74         String JavaDoc lineText = buffer.getLineText(lineIndex);
75         int searchPos = -1;
76         while (true)
77         {
78             if (lineIndex != oldLineIndex)
79             {
80                 lineText = buffer.getLineText(lineIndex);
81                 oldLineIndex = lineIndex;
82             }
83             Parens parens = getLastParens(lineText, searchPos);
84             if (parens.openOffset > parens.closeOffset)
85             {
86                 // recalculate column (when using tabs instead of spaces)
87
int indent = parens.openOffset + TextUtilities.tabsToSpaces(lineText, buffer.getTabSize()).length() - lineText.length();
88                 indentActions.add(new IndentAction.AlignParameter(indent, lineText));
89                 return;
90             }
91             
92             // No parens on prev line
93
if (parens.openOffset == -1 && parens.closeOffset == -1)
94             {
95                 return;
96             }
97             int openParenOffset = TextUtilities.findMatchingBracket(buffer, lineIndex, parens.closeOffset);
98             if (openParenOffset >= 0)
99             {
100                 lineIndex = buffer.getLineOfOffset(openParenOffset);
101                 searchPos = openParenOffset - buffer.getLineStartOffset(lineIndex) - 1;
102                 if (searchPos < 0)
103                     break;
104             }
105             else
106                 break;
107         }
108     } //}}}
109

110     //{{{ Parens() class
111
private static class Parens
112     {
113         final int openOffset;
114         final int closeOffset;
115         
116         Parens(int openOffset, int closeOffset)
117         {
118             this.openOffset = openOffset;
119             this.closeOffset = closeOffset;
120         }
121         
122         @Override JavaDoc
123         public String JavaDoc toString()
124         {
125             return "Parens(" + openOffset + ',' + closeOffset + ')';
126         }
127     } //}}}
128
}
129
130
Popular Tags