1 19 20 package org.netbeans.modules.editor.html; 21 22 23 import javax.swing.text.BadLocationException ; 24 import javax.swing.text.Caret ; 25 26 import org.netbeans.api.html.lexer.HTMLTokenId; 27 28 import org.netbeans.api.lexer.Token; 29 import org.netbeans.api.lexer.TokenHierarchy; 30 import org.netbeans.api.lexer.TokenSequence; 31 32 import org.netbeans.editor.BaseDocument; 33 import org.netbeans.editor.ext.ExtSyntaxSupport; 34 35 42 class HTMLAutoCompletion { 43 44 private static int equalsSignInsertedOffset = -1; 46 47 59 static void charInserted(BaseDocument doc, 60 int dotPos, 61 Caret caret, 62 char ch) throws BadLocationException { 63 if (doc.getSyntaxSupport() instanceof ExtSyntaxSupport) { 64 if (ch == '=') { 65 completeQuotes(doc, dotPos, caret); 66 } else if(ch == '"') { 67 handleQuotationMark(doc, dotPos, caret); 69 } else { 70 equalsSignInsertedOffset = -1; 72 } 73 } 74 } 75 76 static void charDeleted(BaseDocument doc, int dotPos, Caret caret, char ch) { 78 equalsSignInsertedOffset = -1; 79 } 80 81 private static void handleQuotationMark(BaseDocument doc, int dotPos, Caret caret) throws BadLocationException { 82 if(equalsSignInsertedOffset != -1) { 83 if(dotPos == (equalsSignInsertedOffset + ("=\"".length()))) { 87 doc.remove(dotPos,1); 89 caret.setDot(dotPos); 90 } 91 92 } else { 93 doc.readLock(); 95 try { 96 TokenHierarchy hi = TokenHierarchy.get(doc); 97 TokenSequence ts = hi.tokenSequence(); 98 99 ts.move(dotPos); 100 if(!ts.moveNext()) { 101 return ; } 103 104 Token token = ts.token(); 105 if(token.id() == HTMLTokenId.VALUE) { 106 if("\"\"".equals(doc.getText(dotPos , 2))) { 112 doc.remove(dotPos,1); 113 caret.setDot(dotPos+1); 114 } 115 } 116 }finally { 117 doc.readUnlock(); 118 } 119 } 120 equalsSignInsertedOffset = -1; 122 } 123 124 private static void completeQuotes(BaseDocument doc, int dotPos, Caret caret) throws BadLocationException { 125 doc.readLock(); 126 try { 127 TokenHierarchy hi = TokenHierarchy.get(doc); 128 TokenSequence ts = hi.tokenSequence(); 129 130 ts.move(dotPos); 131 if(!ts.moveNext()) { 132 return ; } 134 135 Token token = ts.token(); 136 137 int dotPosAfterTypedChar = dotPos + 1; 138 if(token != null && 139 token.id() == HTMLTokenId.ARGUMENT) { 140 doc.insertString( dotPosAfterTypedChar, "\"\"" , null); 141 caret.setDot(dotPosAfterTypedChar + 1); 142 equalsSignInsertedOffset = dotPos; 144 } 145 146 }finally { 147 doc.readUnlock(); 148 } 149 150 } 151 152 153 } 154 | Popular Tags |