1 19 20 package org.netbeans.modules.languages.features; 21 22 import java.util.Iterator ; 23 import java.util.List ; 24 import javax.swing.text.BadLocationException ; 25 import javax.swing.text.Caret ; 26 import org.netbeans.api.languages.LanguagesManager; 27 import org.netbeans.api.languages.ParseException; 28 import org.netbeans.api.lexer.Token; 29 import org.netbeans.api.lexer.TokenHierarchy; 30 import org.netbeans.api.lexer.TokenSequence; 31 import org.netbeans.editor.BaseDocument; 32 import org.netbeans.editor.ext.ExtKit.ExtDeleteCharAction; 33 import org.netbeans.modules.languages.Feature; 34 import org.netbeans.modules.languages.Language; 35 import org.netbeans.modules.languages.LanguagesManagerImpl; 36 import org.openide.ErrorManager; 37 38 42 public class BraceCompletionDeleteAction extends ExtDeleteCharAction { 43 44 public BraceCompletionDeleteAction () { 45 super ("delete-previous", false); 46 } 47 48 protected void charBackspaced ( 49 BaseDocument doc, int dotPos, Caret caret, char ch 50 ) throws BadLocationException { 51 try { 52 String mimeType = (String ) doc.getProperty ("mimeType"); 53 TokenHierarchy th = TokenHierarchy.get (doc); 54 TokenSequence ts = th.tokenSequence (); 55 while (true) { 56 ts.move (caret.getDot ()); 57 if (!ts.moveNext ()) return; 58 TokenSequence ts2 = ts.embedded (); 59 if (ts2 == null) break; 60 ts = ts2; 61 } 62 mimeType = ts.language ().mimeType (); 63 Language l = ((LanguagesManagerImpl) LanguagesManager.getDefault ()).getLanguage (mimeType); 64 List <Feature> completes = l.getFeatures ("COMPLETE"); 65 Iterator <Feature> it = completes.iterator (); 66 while (it.hasNext ()) { 67 Feature complete = it.next (); 68 if (complete.getType () != Feature.Type.STRING) 69 continue; 70 String s = (String ) complete.getValue (); 71 int i = s.indexOf (':'); 72 if (i != 1) continue; 73 String ss = doc.getText ( 74 caret.getDot (), 75 s.length () - i - 1 76 ); 77 if (s.endsWith (ss) && 78 s.charAt (0) == ch 79 ) { 80 doc.remove (caret.getDot (), s.length () - i - 1); 81 return; 82 } 83 } 84 } catch (ParseException ex) { 85 ErrorManager.getDefault ().notify (ex); 86 } 87 } 88 } 89 | Popular Tags |