1 21 22 package org.armedbear.j; 23 24 import java.awt.event.KeyEvent ; 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 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 km.mapKey(KeyEvent.VK_BRACELEFT, CTRL_MASK | SHIFT_MASK, "insertBraces"); 67 km.mapKey(KeyEvent.VK_F12, 0, "wrapComment"); 68 km.mapKey(0xffc9, 0, "wrapComment"); } 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 Line previous = line.previous(); 93 if (previous == null) 94 return 0; 95 if (isContinued(previous)) { 96 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 final String 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 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 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 while (model != null && model.isBlank()) 148 model = model.previous(); 149 if (model == null) 150 return null; 151 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) break; 180 } 181 } 182 return it.getPosition(); 183 } 184 185 private static String trimSyntacticWhitespace(Line line) 188 { 189 TclSyntaxIterator it = new TclSyntaxIterator(null); 190 return new String (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 |