1 21 22 package org.armedbear.j; 23 24 import java.awt.event.KeyEvent ; 25 26 public final class VerilogMode extends AbstractMode implements Constants, Mode 27 { 28 private static final VerilogMode mode = new VerilogMode(); 29 30 private VerilogMode() 31 { 32 super(VERILOG_MODE, VERILOG_MODE_NAME); 33 keywords = new Keywords(this); 34 } 35 36 public static VerilogMode getMode() 37 { 38 return mode; 39 } 40 41 public String getCommentStart() 42 { 43 return "// "; 44 } 45 46 public Formatter getFormatter(Buffer buffer) 47 { 48 return new VerilogFormatter(buffer); 49 } 50 51 protected void setKeyMapDefaults(KeyMap km) 52 { 53 km.mapKey(KeyEvent.VK_TAB, CTRL_MASK, "insertTab"); 54 km.mapKey(KeyEvent.VK_TAB, 0, "tab"); 55 km.mapKey(KeyEvent.VK_ENTER, 0, "newlineAndIndent"); 56 km.mapKey(KeyEvent.VK_T, CTRL_MASK, "findTag"); 57 km.mapKey(KeyEvent.VK_PERIOD, ALT_MASK, "findTagAtDot"); 58 km.mapKey(KeyEvent.VK_F12, 0, "wrapComment"); 59 km.mapKey(0xffc9, 0, "wrapComment"); } 62 63 public boolean isTaggable() 64 { 65 return true; 66 } 67 68 public Tagger getTagger(SystemBuffer buffer) 69 { 70 return new VerilogTagger(buffer); 71 } 72 73 public boolean canIndent() 74 { 75 return true; 76 } 77 78 public int getCorrectIndentation(Line line, Buffer buffer) 79 { 80 final int indentSize = buffer.getIndentSize(); 81 final Line model = findModel(line); 82 if (model == null) 83 return 0; 84 final String trim = line.getText().trim(); 85 final int modelIndent = buffer.getIndentation(model); 86 final String modelTrim = model.getText().trim(); 87 if (line.flags() == STATE_COMMENT) { 88 if (modelTrim.startsWith("/*") && trim.startsWith("*")) 89 return modelIndent + 1; 90 else 91 return modelIndent; 92 } 93 if (modelTrim.endsWith("(")) 94 return modelIndent + indentSize; 95 final String modelIdentifier = 96 Utilities.getFirstIdentifier(modelTrim, this); 97 if (Utilities.isOneOf(modelIdentifier, alwaysIndentAfter)) 98 return modelIndent + indentSize; 99 if (Utilities.isOneOf(modelIdentifier, maybeIndentAfter)) { 100 if (modelTrim.endsWith(";")) 101 return modelIndent; 102 else 103 return modelIndent + indentSize; 104 } 105 final String identifier = Utilities.getFirstIdentifier(trim, this); 106 if ("end".equals(identifier)) { 107 Line beginLine = findBeginLine(line); 108 if (beginLine != null) 109 return buffer.getIndentation(beginLine); 110 } 111 return modelIndent; 112 } 113 114 private static Line findModel(Line line) 115 { 116 Line model = line.previous(); 117 if (line.flags() == STATE_COMMENT) { 118 while (model != null && model.isBlank()) 120 model = model.previous(); 121 } else { 122 while (model != null && !isAcceptableModel(model)) 123 model = model.previous(); 124 } 125 return model; 126 } 127 128 private static boolean isAcceptableModel(Line line) 129 { 130 if (line.isBlank()) 131 return false; 132 if (line.flags() == STATE_COMMENT) 133 return false; 134 135 return true; 136 } 137 138 private Line findBeginLine(Line line) 139 { 140 int count = 1; 141 while (true) { 142 line = line.previous(); 143 if (line == null) 144 return null; 145 String identifier = Utilities.getFirstIdentifier(line.trim(), this); 146 if (identifier != null) { 147 if (identifier.equals("begin")) { 148 if (--count == 0) 149 return line; 150 } else if (identifier.equals("end")) 151 ++count; 152 } 153 } 154 } 155 156 public boolean isIdentifierStart(char c) 157 { 158 return startChars.indexOf(c) >= 0; 159 } 160 161 public boolean isIdentifierPart(char c) 162 { 163 return partChars.indexOf(c) >= 0; 164 } 165 166 private static final String startChars = 167 "`ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_"; 168 169 private static final String partChars = 170 "`ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_$"; 171 172 private static final String [] alwaysIndentAfter = { 173 "begin", 174 "case", 175 "casex", 176 "casez", 177 "fork", 178 "function", 179 "generate", 180 "module", 181 "primitive", 182 "specify", 183 "table", 184 "task" 185 }; 186 187 private static final String [] maybeIndentAfter = { 188 "always", 189 "else", 190 "for", 191 "forever", 192 "if", 193 "initial", 194 "repeat", 195 "while" 196 }; 197 } 198 | Popular Tags |