KickJava   Java API By Example, From Geeks To Geeks.

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


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

33 public interface IndentAction
34 {
35     /**
36      * @param buffer The buffer
37      * @param line The line number that matched the rule; not necessarily
38      * the line being indented.
39      * @param oldIndent Original indent.
40      * @param newIndent The new indent -- ie, indent returned by previous
41      * indent action.
42      */

43     int calculateIndent(JEditBuffer buffer, int line, int oldIndent,
44         int newIndent);
45
46     /**
47      * @return true if the indent engine should keep processing after
48      * this rule.
49      */

50     boolean keepChecking();
51
52     /**
53      * This handles the following Java code:
54      * if(something)
55      * { // no indentation on this line, even though previous matches a rule
56      */

57     class Collapse implements IndentAction
58     {
59         /**
60          * This does nothing; it is merely a sentinel for the
61          * <code>OpenBracketIndentRule</code>.
62          */

63         public int calculateIndent(JEditBuffer buffer, int line, int oldIndent,
64             int newIndent)
65         {
66             return newIndent;
67         }
68         
69         public boolean keepChecking()
70         {
71             return true;
72         }
73         
74         public boolean equals(Object JavaDoc o)
75         {
76             return (o instanceof Collapse);
77         }
78     }
79
80     class Reset implements IndentAction
81     {
82         public int calculateIndent(JEditBuffer buffer, int line, int oldIndent,
83             int newIndent)
84         {
85             return oldIndent;
86         }
87         
88         public boolean keepChecking()
89         {
90             return true;
91         }
92     }
93
94     class Increase implements IndentAction
95     {
96         private int amount;
97         
98         public Increase()
99         {
100             amount = 1;
101         }
102         
103         public Increase(int amount)
104         {
105             this.amount = amount;
106         }
107         
108         public int calculateIndent(JEditBuffer buffer, int line, int oldIndent,
109             int newIndent)
110         {
111             return newIndent + buffer.getIndentSize() * amount;
112         }
113         
114         public boolean keepChecking()
115         {
116             return true;
117         }
118         
119         public boolean equals(Object JavaDoc o)
120         {
121             if(o instanceof Increase)
122                 return ((Increase)o).amount == amount;
123             else
124                 return false;
125         }
126     }
127
128     class Decrease implements IndentAction
129     {
130         public int calculateIndent(JEditBuffer buffer, int line, int oldIndent,
131             int newIndent)
132         {
133             return newIndent - buffer.getIndentSize();
134         }
135         
136         public boolean keepChecking()
137         {
138             return true;
139         }
140     }
141
142     class AlignBracket implements IndentAction
143     {
144         private int line, offset;
145         private int openBracketLine;
146         private int openBracketColumn;
147         private String JavaDoc openBracketLineText;
148         private int extraIndent;
149
150         public AlignBracket(JEditBuffer buffer, int line, int offset)
151         {
152             this.line = line;
153             this.offset = offset;
154             
155             int openBracketIndex = TextUtilities.findMatchingBracket(
156                 buffer,this.line,this.offset);
157             if(openBracketIndex == -1)
158                 openBracketLine = -1;
159             else
160             {
161                 openBracketLine = buffer.getLineOfOffset(openBracketIndex);
162                 openBracketColumn = openBracketIndex -
163                     buffer.getLineStartOffset(openBracketLine);
164                 openBracketLineText = buffer.getLineText(openBracketLine);
165             }
166         }
167
168         public int getExtraIndent()
169         {
170             return extraIndent;
171         }
172         
173         public void setExtraIndent(int extraIndent)
174         {
175             this.extraIndent = extraIndent;
176         }
177         
178         public int getOpenBracketColumn()
179         {
180             return openBracketColumn;
181         }
182         
183         public String JavaDoc getOpenBracketLine()
184         {
185             return openBracketLineText;
186         }
187
188         public int calculateIndent(JEditBuffer buffer, int line, int oldIndent,
189             int newIndent)
190         {
191             if(openBracketLineText == null)
192                 return newIndent;
193             else
194             {
195                 return StandardUtilities.getLeadingWhiteSpaceWidth(
196                     openBracketLineText,buffer.getTabSize())
197                     + (extraIndent * buffer.getIndentSize());
198             }
199         }
200         
201         public boolean keepChecking()
202         {
203             return false;
204         }
205     }
206
207     /**
208     * @author Matthieu Casanova
209     */

210     class AlignOffset implements IndentAction
211     {
212         private int offset;
213         
214         public AlignOffset(int offset)
215         {
216             this.offset = offset;
217         }
218
219         public int calculateIndent(JEditBuffer buffer, int line, int oldIndent,
220             int newIndent)
221         {
222             return offset;
223         }
224
225         public boolean keepChecking()
226         {
227             return false;
228         }
229     }
230
231     /**
232     * Indent action used for deep indent.
233     * @author Matthieu Casanova
234     */

235     class AlignParameter implements IndentAction
236     {
237         private int openParensColumn;
238         private String JavaDoc openParensLineText;
239
240         public AlignParameter(int openParensColumn, String JavaDoc openParensLineText)
241         {
242             this.openParensLineText = openParensLineText;
243             this.openParensColumn = openParensColumn;
244         }
245
246         public int calculateIndent(JEditBuffer buffer, int line, int oldIndent,
247                      int newIndent)
248         {
249             return openParensLineText == null ? newIndent : openParensColumn + 1;
250         }
251
252         public boolean keepChecking()
253         {
254             return false;
255         }
256     }
257 }
258
Popular Tags