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 javax.swing.text.StyledDocument ; 27 import org.netbeans.api.languages.Context; 28 import org.netbeans.api.languages.LanguagesManager; 29 import org.netbeans.api.languages.ParseException; 30 import org.netbeans.api.lexer.TokenHierarchy; 31 import org.netbeans.api.lexer.TokenSequence; 32 import org.netbeans.editor.BaseDocument; 33 import org.netbeans.editor.ext.ExtKit.ExtDefaultKeyTypedAction; 34 import org.netbeans.modules.languages.Feature; 35 import org.netbeans.modules.languages.Feature.Type; 36 import org.netbeans.modules.languages.Language; 37 import org.netbeans.modules.languages.LanguagesManagerImpl; 38 import org.openide.ErrorManager; 39 import org.openide.text.NbDocument; 40 41 45 public class BraceCompletionInsertAction extends ExtDefaultKeyTypedAction { 46 47 protected void insertString ( 48 BaseDocument doc, int dotPos, 49 Caret caret, String str, 50 boolean overwrite 51 ) throws BadLocationException { 52 try { 53 String mimeType = (String ) doc.getProperty ("mimeType"); 54 TokenHierarchy th = TokenHierarchy.get (doc); 55 TokenSequence ts = th.tokenSequence (); 56 while (true) { 57 ts.move (caret.getDot ()); 58 if (!ts.moveNext ()) { 59 super.insertString (doc, dotPos, caret, str, overwrite); 60 return; 61 } 62 TokenSequence ts2 = ts.embedded (); 63 if (ts2 == null) break; 64 ts = ts2; 65 } 66 mimeType = ts.language ().mimeType (); 67 Language l = ((LanguagesManagerImpl) LanguagesManager.getDefault ()).getLanguage (mimeType); 68 List <Feature> completes = l.getFeatures ("COMPLETE"); 69 if (completes == null) { 70 super.insertString (doc, dotPos, caret, str, overwrite); 71 return; 72 } 73 Feature methodCall = null; 74 Iterator <Feature> it = completes.iterator (); 75 while (it.hasNext ()) { 76 Feature complete = it.next (); 77 if (complete.getType () == Type.METHOD_CALL) { 78 methodCall = complete; 79 continue; 80 } 81 String s = (String ) complete.getValue (); 82 int i = s.indexOf (':'); 83 String ss = doc.getText ( 84 caret.getDot (), 85 s.length () - i - 1 86 ); 87 if (s.endsWith (ss) && str.equals (ss)) { 88 caret.setDot (caret.getDot () + 1); 90 return; 91 } 92 } 93 boolean beg = ts.offset () == caret.getDot (); 94 95 super.insertString (doc, dotPos, caret, str, overwrite); 96 97 th = TokenHierarchy.get (doc); 98 ts = th.tokenSequence (); 99 while (true) { 100 ts.move (caret.getDot ()); 101 if (!ts.moveNext ()) { 102 return; 103 } 104 TokenSequence ts2 = ts.embedded (); 105 if (ts2 == null) break; 106 ts = ts2; 107 } 108 if (methodCall != null) { 109 if (caret.getDot () < doc.getLength ()) 110 ts.movePrevious (); 111 String s = (String ) methodCall.getValue (Context.create (doc, ts)); 112 if (s != null) { 113 int pos = caret.getDot (); 114 doc.insertString (pos, s, null); 115 caret.setDot (pos); 116 } 117 } 118 if (!beg && 119 ts.token ().id ().name ().indexOf ("whitespace") < 0 120 ) return; 121 StyledDocument sdoc = (StyledDocument ) doc; 122 int ln = NbDocument.findLineNumber (sdoc, caret.getDot ()); 123 int ls = NbDocument.findLineOffset (sdoc, ln); 124 String text = sdoc.getText (ls, caret.getDot () - ls); 125 it = completes.iterator (); 126 while (it.hasNext ()) { 127 Feature complete = it.next (); 128 if (complete.getType () == Type.METHOD_CALL) continue; 129 String s = (String ) complete.getValue (); 130 int i = s.indexOf (':'); 131 if (text.endsWith (s.substring (0, i))) { 132 int pos = caret.getDot (); 133 doc.insertString (pos, s.substring (i + 1), null); 134 caret.setDot (pos); 135 return; 136 } 137 } 138 } catch (ParseException ex) { 139 ErrorManager.getDefault ().notify (ex); 140 } 141 } 142 154 } 155 | Popular Tags |