Changeset 441 for trunk/org.modelica.mdt.ui/src/org/modelica/mdt/ui
- Timestamp:
- 04/04/06 09:20:16 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/org.modelica.mdt.ui/src/org/modelica/mdt/ui/editor/ModelicaAutoIndentStrategy.java
r440 r441 2 2 * This file is part of Modelica Development Tooling. 3 3 * 4 * Copyright (c) 2005, Link �pings universitet, Department of4 * Copyright (c) 2005, Linköpings universitet, Department of 5 5 * Computer and Information Science, PELAB 6 6 * … … 23 23 * distribution. 24 24 * 25 * * Neither the name of Link �pings universitet nor the names of its25 * * Neither the name of Link�pings universitet nor the names of its 26 26 * contributors may be used to endorse or promote products derived from 27 27 * this software without specific prior written permission. … … 40 40 */ 41 41 42 43 42 package org.modelica.mdt.ui.editor; 44 43 … … 50 49 import org.eclipse.jface.text.TextUtilities; 51 50 52 51 /** 52 * This class implements the auto indent strategy for modelica source 53 * code editor. 54 * 55 * @author: Adrian Pop 56 */ 53 57 54 58 class ModelicaAutoIdentStrategy implements IAutoEditStrategy 55 56 59 { 57 60 61 static public class key 62 { 63 protected String name; 64 protected boolean indentPlus; 65 protected boolean indentMinus; 66 public key(String name, boolean indentPlus, boolean indentMinus) 67 { 68 this.name = name; 69 this.indentPlus = indentPlus; /* should we indent++ */ 70 this.indentMinus = indentMinus; /* should we indent-- */ 71 } 72 73 String getName() { return name; } 74 boolean getIndentPlus() { return indentPlus; } 75 boolean getIndentMinus() { return indentPlus; } 76 } 77 58 78 /* keywords that influence indenting */ 59 60 static public String[] keywords = 61 { 62 63 "algorithm", "and", "annotation", "assert", "block", "break", 64 "class", "connect", "connector", "constant", "der","discrete", 65 "each", "else", "elseif", "elsewhen", "encapsulated", "end", 66 "enumeration", "equation", "extends", "external", "false", "final", 67 "flow", "for", "function", "if", "import", "in", "initial", "inner", 68 "input", "loop", "model", "not", "or", "outer", "output", 69 "overload", "package", "parameter", "partial", "protected", 70 "public", "record", "redeclare", "replacable", "return", 71 "terminate", "then", "time", "true", "type", "when", "while", 72 "within", 79 static public key[] keywords = 80 { 81 new key("algorithm", true, false), 82 new key("and", false, false), 83 new key("annotation", false, false), 84 new key("assert", false, false), 85 new key("block", true, false), 86 new key("break", false, false), 87 new key("class", true, false), 88 new key("connect", false, false), 89 new key("connector", true, false), 90 new key("constant", false, false), 91 new key("der", false, false), 92 new key("discrete", false, false), 93 new key("each", false, false), 94 new key("else", false, false), 95 new key("elseif", false, false), 96 new key("elsewhen", false, false), 97 new key("encapsulated", false, false), 98 new key("end", false, true), 99 new key("enumeration", false, false), 100 new key("equation", true, false), 101 new key("extends", false, false), 102 new key("external", false, false), 103 new key("false", false, false), 104 new key("final", false, false), 105 new key("flow", false, false), 106 new key("for", false, false), 107 new key("function", true, false), 108 new key("if", false, false), 109 new key("import", false, false), 110 new key("in", false, false), 111 new key("initial", false, false), 112 new key("inner", false, false), 113 new key("input", false, false), 114 new key("loop", false, false), 115 new key("model", true, false), 116 new key("not", false, false), 117 new key("or", false, false), 118 new key("outer", false, false), 119 new key("output", false, false), 120 new key("overload", false, false), 121 new key("package", true, false), 122 new key("parameter", false, false), 123 new key("partial", false, false), 124 new key("protected", false, false), 125 new key("public", false, false), 126 new key("record", true, false), 127 new key("redeclare", false, false), 128 new key("replacable", false, false), 129 new key("return", false, false), 130 new key("terminate", false, false), 131 new key("then", true, false), 132 new key("time", false, false), 133 new key("true", false, false), 134 new key("type", true, false), 135 new key("when", false, false), 136 new key("while", true, false), 137 new key("within", true, false), 73 138 74 139 /* The following are Meta-Modelica Keywords */ 75 "as", "case", "equality", "failure", "list", "local", "match", 76 "matchcontinue" , "tuple", "uniontype" 140 new key("as", false, false), 141 new key("case", true, false), 142 new key("equality", false, false), 143 new key("failure", false, false), 144 new key("list", false, false), 145 new key("local", true, false), 146 new key("match", true, false), 147 new key("matchcontinue", true, false), 148 new key("tuple", false, false), 149 new key("uniontype", true, false) 77 150 }; 151 78 152 79 153 /** … … 95 169 */ 96 170 171 97 172 protected int findEndOfWhiteSpace(IDocument document, int offset, int end) 98 173 throws BadLocationException … … 102 177 { 103 178 char c= document.getChar(offset); 179 104 180 105 181 if (c != ' ' && c != '\t') … … 108 184 } 109 185 186 110 187 offset++; 111 188 } … … 114 191 } 115 192 116 117 193 118 194 /** … … 130 206 * the command to deal with 131 207 */ 132 133 208 private void autoIndentAfterNewLine(IDocument d, DocumentCommand c) 134 209 { … … 139 214 } 140 215 216 141 217 try 142 218 { 143 219 /* find start of line */ 220 144 221 145 222 int p = (c.offset == d.getLength() ? c.offset - 1 : c.offset); … … 147 224 int start = info.getOffset(); 148 225 226 149 227 /* find white spaces */ 150 228 151 229 int end = findEndOfWhiteSpace(d, start, c.offset); 152 230 231 153 232 StringBuffer buf = new StringBuffer(c.text); 233 154 234 155 235 if (end > start) 156 236 { 237 157 238 158 239 /* append to input */ 159 240 buf.append(d.get(start, end - start)); 160 241 } 161 242 243 /* adrpo -- 244 * we copy the indentation from line above. 245 * and now we need to decide if we increase/decrease 246 * the indent 247 */ 248 /* take the text appearing in this line and do some 249 * checking on it. 250 */ 251 if ( c.offset > end ) 252 { 253 String lineStr = d.get(end, c.offset - end); 254 255 for (int i=0; i < keywords.length; i++ ) 256 { 257 if (keywords[i].indentPlus && lineStr.startsWith(keywords[i].name)) 258 { 259 buf.append(" "); 260 } 261 else if (keywords[i].indentMinus && lineStr.startsWith(keywords[i].name)) 262 { 263 int l = buf.length(); 264 /* decrease indent */ 265 if (l >= 2 && buf.charAt(l-1) == ' ' && buf.charAt(l-2) == ' ') 266 buf.delete(l-2, l); 267 } 268 } 269 } 162 270 c.text = buf.toString(); 163 271 } … … 166 274 167 275 /* stop work */ 168 } 169 170 } 276 excp.printStackTrace(); 277 } 278 } 279 171 280 172 281 private boolean isLineDelimiter(IDocument document, String text) 173 282 { 174 String[] delimiters= document.getLegalLineDelimiters(); 283 String[] delimiters = document.getLegalLineDelimiters(); 284 175 285 176 286 if (delimiters != null) … … 179 289 } 180 290 291 181 292 return false; 182 293 } 294 183 295 184 296 private void smartIndentOnTab(IDocument document, DocumentCommand command) … … 193 305 } 194 306 195 private void smartIndentOnKeypress(IDocument document, DocumentCommand command) 307 308 private void smartIndentOnKeypress(IDocument document, 309 DocumentCommand command) 196 310 { 197 311 switch (command.text.charAt(0)) … … 201 315 break; 202 316 317 203 318 /* add more here if needed */ 204 319 /* ok */ 205 320 } 206 321 } 207 208 322 209 323 private void smartPaste(IDocument document, DocumentCommand command) 210 324 { 325 211 326 212 327 /* … … 216 331 */ 217 332 } 333 218 334 219 335 /** … … 227 343 return; 228 344 } 345 229 346 230 347 if (c.length == 0 && c.text != null && isLineDelimiter(d, c.text)) … … 232 349 autoIndentAfterNewLine(d, c); 233 350 } 351 234 352 else if (c.text.length() == 1) 235 353 { 354 236 355 smartIndentOnKeypress(d, c); 237 356 } 357 238 358 else if (c.text.length() > 1) 239 359 { 360 240 361 smartPaste(d, c); 241 362 } … … 243 364 } 244 365 366 367 368
Note: See TracChangeset
for help on using the changeset viewer.