KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > TclMode


1 /*
2  * TclMode.java
3  *
4  * Copyright (C) 2002 Peter Graves
5  * $Id: TclMode.java,v 1.1.1.1 2002/09/24 16:08:09 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.awt.event.KeyEvent JavaDoc;
25
26 public final class TclMode extends AbstractMode implements Constants, Mode
27 {
28     private static final int STATE_NEUTRAL = 0;
29     private static final int STATE_SINGLEQUOTE = 1;
30     private static final int STATE_DOUBLEQUOTE = 2;
31
32     private static final TclMode mode = new TclMode();
33
34     private TclMode()
35     {
36         super(TCL_MODE, TCL_MODE_NAME);
37         keywords = new Keywords(this);
38     }
39
40     public static TclMode getMode()
41     {
42         return mode;
43     }
44
45     public String JavaDoc getCommentStart()
46     {
47         return "# ";
48     }
49
50     public Formatter getFormatter(Buffer buffer)
51     {
52         return new TclFormatter(buffer);
53     }
54
55     protected void setKeyMapDefaults(KeyMap km)
56     {
57         km.mapKey('{', "electricOpenBrace");
58         km.mapKey('}', "electricCloseBrace");
59         km.mapKey(KeyEvent.VK_TAB, CTRL_MASK, "insertTab");
60         km.mapKey(KeyEvent.VK_TAB, 0, "tab");
61         km.mapKey(KeyEvent.VK_ENTER, 0, "newlineAndIndent");
62         km.mapKey(KeyEvent.VK_T, CTRL_MASK, "findTag");
63         km.mapKey(KeyEvent.VK_PERIOD, ALT_MASK, "findTagAtDot");
64         km.mapKey(KeyEvent.VK_OPEN_BRACKET, CTRL_MASK | SHIFT_MASK, "insertBraces");
65         // Duplicate mapping for 1.4.
66
km.mapKey(KeyEvent.VK_BRACELEFT, CTRL_MASK | SHIFT_MASK, "insertBraces");
67         km.mapKey(KeyEvent.VK_F12, 0, "wrapComment");
68         // Duplicate mapping to support IBM 1.3 for Linux.
69
km.mapKey(0xffc9, 0, "wrapComment"); // F12
70
}
71
72     public boolean isTaggable()
73     {
74         return true;
75     }
76
77     public Tagger getTagger(SystemBuffer buffer)
78     {
79         return new TclTagger(buffer);
80     }
81
82     public boolean canIndent()
83     {
84         return true;
85     }
86
87     public int getCorrectIndentation(Line line, Buffer buffer)
88     {
89         final int indentSize = buffer.getIndentSize();
90
91         // Is the current line a continuation line?
92
Line previous = line.previous();
93         if (previous == null)
94             return 0;
95         if (isContinued(previous)) {
96             // The current line is a continuation line. We need to find the
97
// first line in the sequence of continued lines.
98
while (true) {
99                 Line maybe = previous.previous();
100                 if (maybe != null && isContinued(maybe))
101                     previous = maybe;
102                 else
103                     break;
104             }
105             return buffer.getIndentation(previous) + indentSize;
106         }
107
108         // Current line is not a continuation line.
109
final String JavaDoc trim = line.getText().trim();
110         if (trim.startsWith("}")) {
111             Position pos = matchClosingBrace(new Position(line, 0));
112             return buffer.getIndentation(pos.getLine());
113         }
114         final Line model = findModel(line);
115         if (model == null)
116             return 0;
117         final int modelIndent = buffer.getIndentation(model);
118         final String JavaDoc modelTrim = trimSyntacticWhitespace(model);
119
120         if (modelTrim.endsWith("{")) {
121             if (buffer.getBooleanProperty(Property.INDENT_AFTER_BRACE))
122                 return modelIndent + indentSize;
123             else
124                 return modelIndent;
125         }
126
127         return modelIndent;
128     }
129
130     private static Line findModel(Line line)
131     {
132         Line model = line.previous();
133         if (model == null)
134             return null;
135         // Check for continuation line.
136
if (isContinued(model)) {
137             while (true) {
138                 Line previous = model.previous();
139                 if (previous != null && isContinued(previous))
140                     model = previous;
141                 else
142                     break;
143             }
144             return model;
145         }
146         // Otherwise any non-blank line will do.
147
while (model != null && model.isBlank())
148             model = model.previous();
149         if (model == null)
150             return null;
151         // If the model we've found is itself a continuation line, we need to
152
// find the first line in the sequence of continued lines.
153
while (true) {
154             Line previous = model.previous();
155             if (previous != null && isContinued(previous))
156                 model = previous;
157             else
158                 break;
159         }
160         return model;
161     }
162
163     private static boolean isContinued(Line line)
164     {
165         return line.getText().endsWith("\\");
166     }
167
168     private static Position matchClosingBrace(Position start)
169     {
170         int count = 1;
171         TclSyntaxIterator it = new TclSyntaxIterator(start);
172         char c;
173         while ((c = it.prevChar()) != SyntaxIterator.DONE) {
174             if (c == '}')
175                 ++count;
176             else if (c == '{') {
177                 --count;
178                 if (count == 0) // Found it!
179
break;
180             }
181         }
182         return it.getPosition();
183     }
184
185     // Replaces syntactic whitespace (quotes and comments) with actual space
186
// characters and returns trimmed string.
187
private static String JavaDoc trimSyntacticWhitespace(Line line)
188     {
189         TclSyntaxIterator it = new TclSyntaxIterator(null);
190         return new String JavaDoc(it.hideSyntacticWhitespace(line.getText())).trim();
191     }
192
193     public boolean isIdentifierStart(char c)
194     {
195         return !Character.isWhitespace(c);
196     }
197
198     public boolean isIdentifierPart(char c)
199     {
200         return !Character.isWhitespace(c);
201     }
202 }
203
Popular Tags