java - Custom Script Editor -


i'm making own script editor (in case arma: cold war assault) because want learn , challenging enough.

let me out of way: please don't tell me should easier things. want anyway.

so, have simple gui working new/open/save file menu.

i've managed highlight words different colors (because want tackle hardest part first) it's not efficient.

i've come several ideas algorithm (didn't implement them all), want know you'do, if there's way , doing wrong.

this happens inside jtextpane class.


the arrays containing reserved words:

collections.addall(keywords, "private", "public", "if", "not", "then", "else", "else if");           collections.addall(operators, "+", "-", "=", "==", "?", "!","(", ")","{", "}", "_", "-", "^", "<", ">");              arraylist<string> keywords = new arraylist<string>(); arraylist<string> operators = new arraylist<string>(); 

everytime user makes update document, get's updated:

@override public void insertupdate(documentevent e) {     update(); }  @override public void removeupdate(documentevent e) {     update(); } 

when user stops typing, waits 500 ms update screen:

timer t;  /**  * updates text when user stops typing  */ public void update(){     if (t != null) {         if (t.isrunning())             t.stop();     }      t = new timer(500, new actionlistener() {          @override         public void actionperformed(actionevent e) {              long start = system.currenttimemillis();                  string text = gettext();             int length = text.length();              simpleattributeset attrs = new simpleattributeset();             styleconstants.setforeground(attrs, color.black);             styleddocument doc = getstyleddocument();                    doc.setcharacterattributes(0, length, attrs, true);              int c = 0, carriage = 0;              while ( (c < length ) ){                                              if(text.codepointat(c) == 10){                     carriage += 1;                 }                 (string s : keywords) {                     if (text.startswith(s, c)) {                         styleconstants.setforeground(attrs, color.blue);                         doc.setcharacterattributes(                                 c - carriage, s.length(), attrs, false);                     }                 }                  (string s : operators) {                     if (text.startswith(s, c)) {                         styleconstants.setforeground(attrs, color.red);                         doc.setcharacterattributes(                                 c - carriage, s.length(), attrs, false);                     }                 }                     c++;             }                                system.out.println("iterations took: " + (system.currenttimemillis() - start) + " ms");             t.stop();         }     });     t.start(); } 

how go doing more efficiently?

image

here code:

http://www.codeproject.com/articles/161871/fast-colored-textbox-for-syntax-highlighting

it looks want 'idea[s] [an] algorithm.' therefore, difference in language should not matter much.


Comments