Changeset 1137


Ignore:
Timestamp:
Jul 2, 2011, 1:28:21 PM (6 years ago)
Author:
stefan
Message:

improved math environment detection and error highlighting

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/CHANGELOG

    r1134 r1137  
    1515* major improvements:
    1616  - replaced buggy Java open dialog with a completely new one - better looking and better usability
     17  - improved math environment detection and error highlighting
    1718* minor enhancements:
    1819  - prettied up tabs and icons in compile tree
  • trunk/src/jlatexeditor/syntaxhighlighting/LatexSyntaxHighlighting.java

    r1136 r1137  
    192192          // search for a backslash '\'
    193193          if (c == '\\') {
     194            // check if next char is any a kind of brace
     195            if (char_nr + 1 < row.length) {
     196              switch (chars[char_nr+1].character) {
     197                case '(':
     198                  MathMode mathMode = new MathMode(MathMode.Type.openingParenthesis);
     199                  char_nr = processMathMode(stateStack, state, chars, char_nr, 2, mathMode);
     200                  state = stateStack.peek();
     201                  continue;
     202                case ')':
     203                  mathMode = new MathMode(MathMode.Type.closingParenthesis);
     204                  char_nr = processMathMode(stateStack, state, chars, char_nr, 2, mathMode);
     205                  state = stateStack.peek();
     206                  continue;
     207                case '[':
     208                  mathMode = new MathMode(MathMode.Type.openingBracket);
     209                  char_nr = processMathMode(stateStack, state, chars, char_nr, 2, mathMode);
     210                  state = stateStack.peek();
     211                  continue;
     212                case ']':
     213                  mathMode = new MathMode(MathMode.Type.closingBracket);
     214                  char_nr = processMathMode(stateStack, state, chars, char_nr, 2, mathMode);
     215                  state = stateStack.peek();
     216                  continue;
     217              }
     218            }
     219
    194220            String command = getWord(row, char_nr + 1, true);
    195221
     
    208234          // search for '$' and "$$"
    209235          if (c == '$') {
    210             sce_char.style = stateStyles[LatexStyles.MATH];
     236            int charCount = 1;
    211237
    212238            MathMode.Type type = row.length > char_nr+1 && chars[char_nr+1].character == '$' ? MathMode.Type.doubled : MathMode.Type.simple;
    213239            if (type == MathMode.Type.doubled) {
    214               char_nr++;
    215               chars[char_nr].style = stateStyles[LatexStyles.MATH];
     240              charCount = 2;
    216241            }
    217242
    218243            MathMode mathMode = new MathMode(type);
    219 
    220             // if active math mode -> close; otherwise open
    221             if (state instanceof MathMode) {
    222               if (state.equals(mathMode)) {
    223                 stateStack.pop();
    224                 state = stateStack.peek();
    225               } else {
    226                 chars[char_nr].style = stateStyles[LatexStyles.ERROR];
    227               }
    228             } else {
    229               stateStack.push(state = mathMode);
    230             }
     244            char_nr = processMathMode(stateStack, state, chars, char_nr, charCount, mathMode);
     245            state = stateStack.peek();
    231246
    232247            argumentsIterator = null;
     
    367382  }
    368383
     384  private int processMathMode(ParserStateStack stateStack, ParserState state, SCEDocumentChar[] chars, int char_nr, int charCount, MathMode mathMode) {
     385    // if active math mode -> close; otherwise open
     386    byte style = LatexStyles.MATH;
     387    if (state instanceof MathMode) {
     388      if (mathMode.closes((MathMode) state)) {
     389        stateStack.pop();
     390      } else {
     391        style = LatexStyles.ERROR;
     392      }
     393    } else {
     394      if (mathMode.mayBeOpening()) {
     395        stateStack.push(mathMode);
     396      } else {
     397        style = LatexStyles.ERROR;
     398      }
     399    }
     400    char_nr = setStyle(charCount, style, chars, char_nr);
     401    return char_nr;
     402  }
     403
    369404  private void matchAndStyle(int start, SCEDocumentChar[] chars, String param, Pattern pattern, Function1<String, Byte> styleFunc) {
    370405    Matcher matcher = pattern.matcher(param);
  • trunk/src/jlatexeditor/syntaxhighlighting/states/MathMode.java

    r1136 r1137  
    88 */
    99public class MathMode implements ParserState {
    10   public enum Type { simple, doubled, bracket, parenthesis }
     10  public enum Type { simple, doubled, openingBracket, closingBracket, openingParenthesis, closingParenthesis, openingEnv, closingEnv }
    1111
    1212  private static byte[] styles = new byte[256];
     
    2020
    2121  private Type type;
     22  private boolean envBased;
     23  /** Name of the math environment if envBased. */
     24  private String envName;
    2225
    2326  public MathMode(Type type) {
    2427    this.type = type;
     28  }
     29
     30  public MathMode(Type type, String envName) {
     31    this.type = type;
     32    this.envBased = true;
     33    this.envName = envName;
    2534  }
    2635
     
    2938    if (obj instanceof MathMode) {
    3039      MathMode that = (MathMode) obj;
    31       return this.type.equals(that.type);
     40      if (this.type.equals(that.type)) {
     41        return !envBased || this.envName.equals(that.envName);
     42      }
    3243    }
    3344    return false;
    3445  }
     46
     47  /**
     48   * Returns true if this math mode closes that given one.
     49   *
     50   * @param that previous math mode
     51   * @return true if this math mode closes that given one
     52   */
     53  public boolean closes(MathMode that) {
     54    switch (that.type) {
     55      case simple:
     56        return this.type == Type.simple;
     57      case doubled:
     58        return this.type == Type.doubled;
     59      case openingBracket:
     60        return this.type == Type.closingBracket;
     61      case openingParenthesis:
     62        return this.type == Type.closingParenthesis;
     63      case openingEnv:
     64        return this.type == Type.closingEnv && this.envName.equals(that.envName);
     65      default:
     66        return false;
     67    }
     68  }
     69
     70  public boolean mayBeOpening() {
     71    switch (type) {
     72      case closingBracket:
     73      case closingParenthesis:
     74      case closingEnv:
     75        return false;
     76      default:
     77        return true;
     78    }
     79  }
    3580
    3681  public ParserState copy() {
Note: See TracChangeset for help on using the changeset viewer.