edu.rice.cs.drjava.model.definitions.reducedmodel
Class ReducedModelBrace

java.lang.Object
  extended by edu.rice.cs.drjava.model.definitions.reducedmodel.AbstractReducedModel
      extended by edu.rice.cs.drjava.model.definitions.reducedmodel.ReducedModelBrace
All Implemented Interfaces:
ReducedModelStates

public class ReducedModelBrace
extends AbstractReducedModel

Keeps track of the true braces (i.e., "() {}[]"). This reduced sub-model is used to balance braces for both indenting and highlighting purposes. For example, when the user's caret is immediately after a closing brace, this allows the DefinitionsPane to produced a highlight extending from the closing brace to its match.

Version:
$Id: ReducedModelBrace.java 4691 2008-12-02 23:33:27Z dlsmith $
Author:
JavaPLT

Field Summary
 
Fields inherited from class edu.rice.cs.drjava.model.definitions.reducedmodel.AbstractReducedModel
PTR_CHAR
 
Fields inherited from interface edu.rice.cs.drjava.model.definitions.reducedmodel.ReducedModelStates
FREE, INSIDE_BLOCK_COMMENT, INSIDE_DOUBLE_QUOTE, INSIDE_LINE_COMMENT, INSIDE_SINGLE_QUOTE, STUTTER
 
Constructor Summary
ReducedModelBrace(ReducedModelControl parent)
           
 
Method Summary
protected  BraceInfo _getEnclosingBrace()
          Determines the brace enclosing the current location.
 BraceInfo _getLineEnclosingBrace()
          Determines the brace (type and distance) enclosing the beginning of the current line (except the first line).
 int balanceBackward()
           
 int balanceForward()
          If the current ReducedToken is an open significant brace and the offset is 0 (i.e., if we're immediately left of said brace), push the current Brace onto a Stack and iterate forwards, keeping track of the distance covered.
 boolean closedBraceImmediatelyLeft()
           
 void delete(int count)
          Updates ReducedModelBrace to reflect text deletion.
 void insertChar(char ch)
          Inserts a character into the reduced model.
protected  void insertGapBetweenMultiCharBrace(int length)
          Inserts a gap between the characters in a multiple character brace.
 boolean isShadowed()
           
 void move(int count)
          Updates ReducedModelBrace to reflect cursor movement.
protected  ReducedModelState moveWalkerGetState(int relDistance)
          Returns the state at the relLocation, where relLocation is the location relative to the walker.
 int nextBrace()
          Determines the distance to the location before the next open brace.
 boolean openBraceImmediatelyLeft()
           
 int previousBrace()
          Returns distance from current location of cursor to the location of the previous significant brace including opening comment braces (but not closing ones since they are not "FREE").
protected  void resetWalkerLocationToCursor()
          Resets the walker to the current position in document.
 
Methods inherited from class edu.rice.cs.drjava.model.definitions.reducedmodel.AbstractReducedModel
_augmentCurrentGap, _augmentGapToLeft, _gapToLeft, _gapToRight, _insertGap, _insertNewGap, absOffset, absOffset, current, getLength, getState, makeCopyCursor, next, prev, simpleString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReducedModelBrace

public ReducedModelBrace(ReducedModelControl parent)
Method Detail

insertChar

public void insertChar(char ch)
Description copied from class: AbstractReducedModel
Inserts a character into the reduced model. A method to be implemented in each specific reduced sub-model.

Specified by:
insertChar in class AbstractReducedModel

insertGapBetweenMultiCharBrace

protected void insertGapBetweenMultiCharBrace(int length)
Inserts a gap between the characters in a multiple character brace. However, since ReducedModelBrace doesn't keep track of comment braces and escape sequences, we just throw an exception since the condition in insertGap that spawns this method doesn't arise.

Specified by:
insertGapBetweenMultiCharBrace in class AbstractReducedModel

move

public void move(int count)
Updates ReducedModelBrace to reflect cursor movement. Negative values move left from the cursor, positive values move right. All functionality has been refactored into TokenList. ASSUMES that count is within range.

Parameters:
count - indicates the direction and magnitude of cursor movement

delete

public void delete(int count)
Updates ReducedModelBrace to reflect text deletion. Negative values mean text left of the cursor, positive values mean text to the right. All functionality has been refactored into TokenList. Assumes that count is with range.


isShadowed

public boolean isShadowed()

previousBrace

public int previousBrace()
Returns distance from current location of cursor to the location of the previous significant brace including opening comment braces (but not closing ones since they are not "FREE"). For example, given "(...|)" where | signifies the cursor, previousBrace returns 4 because it goes to the position preceding the (. Given "* /|*", it returns 1 (the distance to the position of this brace) since you're in the middle of it and going backward finds it.


nextBrace

public int nextBrace()
Determines the distance to the location before the next open brace. For example, |...( where | is the cursor, returns 3 since it is 3 moves to the position preceding the (. NOTE: /|* returns the next brace. It does not return this brace because you are past it.


balanceForward

public int balanceForward()
If the current ReducedToken is an open significant brace and the offset is 0 (i.e., if we're immediately left of said brace), push the current Brace onto a Stack and iterate forwards, keeping track of the distance covered. - For every closed significant Brace, if it matches the top of the Stack, pop the Stack. Increase the distance by the size of the Brace. If the Stack is Empty, we have a balance. Return distance. If the closed Brace does not match the top of the Stack, return -1; We have an unmatched open Brace at the top of the Stack. - For every open significant Brace, push onto the Stack. Increase distance by size of the Brace, continue. - Anything else, increase distance by size of the ReducedToken, continue.


openBraceImmediatelyLeft

public boolean openBraceImmediatelyLeft()

closedBraceImmediatelyLeft

public boolean closedBraceImmediatelyLeft()

balanceBackward

public int balanceBackward()

moveWalkerGetState

protected ReducedModelState moveWalkerGetState(int relDistance)
Description copied from class: AbstractReducedModel
Returns the state at the relLocation, where relLocation is the location relative to the walker.

Specified by:
moveWalkerGetState in class AbstractReducedModel
Parameters:
relDistance - distance from walker to get state at.

resetWalkerLocationToCursor

protected void resetWalkerLocationToCursor()
Description copied from class: AbstractReducedModel
Resets the walker to the current position in document.

Specified by:
resetWalkerLocationToCursor in class AbstractReducedModel

_getLineEnclosingBrace

public BraceInfo _getLineEnclosingBrace()
Determines the brace (type and distance) enclosing the beginning of the current line (except the first line). The matching brace obviously must appear on the preceding line or before. To find the enclosing brace one must first move past this newline. If there is not a line enclosing brace, returns BraceInfo.NULL. Usually called when distToStart() = 0.


_getEnclosingBrace

protected BraceInfo _getEnclosingBrace()
Determines the brace enclosing the current location.