Changeset 601

Show
Ignore:
Timestamp:
01/31/10 14:47:12 (3 years ago)
Author:
stefan
Message:

added syntax highlighting for gobal.properties

Location:
trunk
Files:
2 modified
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/data/gproperties/styles/user.xml

    r600 r601  
    1919 
    2020  <element name="number" style="plain"> 
    21     <foreground><color r="0" g="0" b="0"/></foreground> 
     21    <foreground><color r="0" g="128" b="0"/></foreground> 
    2222  </element> 
    2323 
  • trunk/src/jlatexeditor/gproperties/GPropertiesSyntaxHighlighting.java

    r535 r601  
    44 */ 
    55 
    6 package jlatexeditor.syntaxhighlighting; 
    7  
     6package jlatexeditor.gproperties; 
     7 
     8import jlatexeditor.syntaxhighlighting.LatexStyles; 
     9import jlatexeditor.syntaxhighlighting.StyleableTerm; 
    810import jlatexeditor.syntaxhighlighting.states.MathMode; 
    911import jlatexeditor.syntaxhighlighting.states.RootState; 
     
    1820import java.util.regex.Pattern; 
    1921 
    20 public class LatexSyntaxHighlighting extends SyntaxHighlighting implements SCEDocumentListener{ 
     22public class GPropertiesSyntaxHighlighting extends SyntaxHighlighting implements SCEDocumentListener{ 
    2123  private static final Pattern TERM_PATTERN = Pattern.compile("(\\\\?[\\w_\\-\\^]+)"); 
    2224  private static final Pattern BAD_TERM_CHARS = Pattern.compile("[\\\\\\d_\\-\\^]"); 
     
    3032  private boolean currentlyChanging = false; 
    3133 
    32   private static Aspell aspell; 
    33   static { 
    34     try { 
    35       aspell = Aspell.getInstance(); 
    36     } catch (IOException e) { 
    37       e.printStackTrace(); 
    38     } 
    39   } 
    40  
    41   public LatexSyntaxHighlighting(SCEPane pane){ 
     34  public GPropertiesSyntaxHighlighting(SCEPane pane){ 
    4235    this.pane = pane; 
    4336    document = pane.getDocument(); 
     
    129122  private void parseRow(int row_nr, int rowsCount, SCEDocumentRow rows[]){ 
    130123    boolean ready = false; 
    131     LatexStyles.CommandStyle lastCommandStyle = null; 
    132124 
    133125    while(!ready && row_nr < rowsCount){ 
     
    144136 
    145137      // parse the row 
     138      boolean parsingKey = true; 
    146139      SCEDocumentChar chars[] = row.chars; 
    147140      for(int char_nr = 0; char_nr < row.length; char_nr++){ 
     
    151144        byte[] stateStyles = state.getStyles(); 
    152145 
    153         // search for a backslash '\' 
    154         if(c == '\\'){ 
    155           String command = getCommandString(row, char_nr + 1); 
    156  
    157           if(command == null){ 
    158             byte styleText = stateStyles[LatexStyles.TEXT]; 
    159             // if there is no command -> '\' escapes the next character -> set style text 
    160             if(char_nr < row.length - 1){ 
    161               sce_char.style = styleText; 
    162               chars[char_nr + 1].style = styleText; 
    163               char_nr += 1; 
    164             }else{ 
    165               sce_char.style = stateStyles[LatexStyles.ERROR]; 
    166             } 
    167             lastCommandStyle = null; 
    168           }else{ 
    169             lastCommandStyle = LatexStyles.getCommandStyle(command); 
    170             // highlight the command 
    171             byte commandStyle = stateStyles[lastCommandStyle.commandStyle]; 
    172             for(int i = 0; i <= command.length(); i++){ 
    173               chars[char_nr + i].style = commandStyle; 
    174             } 
    175             char_nr += command.length(); 
    176           } 
    177  
    178           continue; 
    179         } 
    180  
    181         // search for '$' and "$$" 
    182         if(c == '$'){ 
    183           sce_char.style = stateStyles[LatexStyles.MATH]; 
    184  
    185           boolean doubleMath = chars[char_nr + 1].character == '$'; 
    186           if (doubleMath) { 
    187             char_nr++; 
    188             chars[char_nr].style = stateStyles[LatexStyles.MATH]; 
    189           } 
    190  
    191           // if active math mode -> close; otherwise open 
    192           if (state instanceof MathMode) { 
    193             stateStack.pop(); 
    194             state = stateStack.peek(); 
    195           } else { 
    196             stateStack.push(state = new MathMode(doubleMath)); 
    197           } 
    198  
    199           continue; 
    200         } 
     146        // search for '#' (comment) 
     147        if(c == '#'){ 
     148          byte commentStyle = stateStyles[LatexStyles.COMMENT]; 
     149          while(char_nr < row.length) chars[char_nr++].style = commentStyle; 
     150          continue; 
     151        } 
     152 
     153        if (parsingKey) { 
     154          // search for a backslash '\' 
     155          if(c == '='){ 
     156            sce_char.style = stateStyles[GPropertiesStyles.TEXT]; 
     157            parsingKey = false; 
     158            continue; 
     159          } else { 
     160            sce_char.style = stateStyles[GPropertiesStyles.KEY]; 
     161            continue; 
     162          } 
     163        } 
    201164 
    202165        // search for '{' and '}' 
    203166        if(c == '{'){ 
    204           sce_char.style = stateStyles[LatexStyles.BRACKET]; 
     167          sce_char.style = stateStyles[GPropertiesStyles.BRACKET]; 
    205168          continue; 
    206169        } 
    207170        if(c == '}'){ 
    208           sce_char.style = stateStyles[LatexStyles.BRACKET]; 
    209           continue; 
    210         } 
    211  
    212         // search for '%' (comment) 
    213         if(c == '%'){ 
    214           byte commentStyle = stateStyles[LatexStyles.COMMENT]; 
    215           while(char_nr < row.length) chars[char_nr++].style = commentStyle; 
     171          sce_char.style = stateStyles[GPropertiesStyles.BRACKET]; 
    216172          continue; 
    217173        } 
     
    219175        // default style is text or number 
    220176        if(c >= '0' && c <= '9'){ 
    221           sce_char.style = stateStyles[LatexStyles.NUMBER]; 
     177          sce_char.style = stateStyles[GPropertiesStyles.NUMBER]; 
    222178        }else{ 
    223           sce_char.style = stateStyles[LatexStyles.TEXT]; 
    224         } 
    225       } 
    226  
    227       // extract word from row that shell be checked for misspellings 
    228       String rowString = document.getRow(row_nr); 
    229  
    230       // for each term in this row 
    231       Matcher matcher = TERM_PATTERN.matcher(rowString); 
    232       while(matcher.find()) { 
    233         // check if it's not a tex command and does not contain formula specific characters ("_" and numbers) 
    234         String termString = matcher.group(1); 
    235         StyleableTerm term; 
    236         if (BAD_TERM_CHARS.matcher(termString).find()) { 
    237           term = new StyleableTerm(termString, chars, matcher.start(1), LatexStyles.U_NORMAL); 
    238         } else { 
    239           // spell check 
    240           try { 
    241             Aspell.Result aspellResult = aspell.check(termString); 
    242             term = new StyleableTerm(termString, chars, matcher.start(1), aspellResult.isCorrect() ? LatexStyles.U_NORMAL : LatexStyles.U_MISSPELLED); 
    243           } catch (IOException e) { 
    244             term = new StyleableTerm(termString, chars, matcher.start(1), LatexStyles.U_NORMAL); 
    245             e.printStackTrace(); 
    246           } 
    247         } 
    248  
    249         term.applyStyleToDoc(); 
    250       } 
     179          sce_char.style = stateStyles[GPropertiesStyles.TEXT]; 
     180        } 
     181      } 
    251182 
    252183      // go to the next row 
     
    264195  } 
    265196 
    266   /** 
    267    * Returns the command name found at the given position (after '\'). 
    268    * 
    269    * @param row the row 
    270    * @param offset the offset 
    271    * @return the command string 
    272    */ 
    273   private String getCommandString(SCEDocumentRow row, int offset){ 
    274     String command = null; 
    275  
    276     SCEDocumentChar chars[] = row.chars; 
    277     int end_offset = offset; 
    278     for(; end_offset < row.length; end_offset++){ 
    279       char character = chars[end_offset].character; 
    280  
    281       // only letters are allowed 
    282       if(character >= 'a' && character <= 'z') continue; 
    283       if(character >= 'A' && character <= 'Z') continue; 
    284  
    285       // we found the end of the command 
    286       break; 
    287     } 
    288     // command consists of at least 1 char 
    289     if (end_offset == offset) end_offset++; 
    290  
    291     // did we find a command? 
    292     if(offset != end_offset){ 
    293       SCEString sceCommand = new SCEString(chars, offset, end_offset - offset); 
    294       command = sceCommand.toString(); 
    295     } 
    296  
    297     return command; 
    298   } 
    299  
    300197  // SCEDocumentListener methods 
    301198  public void documentChanged(SCEDocument sender, SCEDocumentEvent event){ 
  • trunk/src/jlatexeditor/JLatexEditorJFrame.java

    r600 r601  
    1111import jlatexeditor.errorhighlighting.LatexErrorHighlighting; 
    1212import jlatexeditor.gproperties.GPropertiesStyles; 
     13import jlatexeditor.gproperties.GPropertiesSyntaxHighlighting; 
    1314import jlatexeditor.gui.AboutDialog; 
    1415import jlatexeditor.gui.LocalHistory; 
     
    438439 
    439440    // syntax highlighting 
    440 //    SyntaxHighlighting syntaxHighlighting = new LatexSyntaxHighlighting(scePane); 
    441 //    syntaxHighlighting.start(); 
     441    GPropertiesSyntaxHighlighting syntaxHighlighting = new GPropertiesSyntaxHighlighting(scePane); 
     442    syntaxHighlighting.start(); 
    442443 
    443444    // code completion and quick help