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

java.lang.Object
  |
  +--edu.rice.cs.drjava.model.definitions.reducedmodel.AbstractReducedModel
        |
        +--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,v 1.25 2002/08/10 23:52:51 cmcgraw Exp $
Author:
JavaPLT

Field Summary
private  ReducedModelControl _parent
           
 
Fields inherited from class edu.rice.cs.drjava.model.definitions.reducedmodel.AbstractReducedModel
_cursor, _tokens, 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
private  void _insertBrace(String text)
          Helper function for top level brace insert functions.
private  boolean _isCurrentBraceMatchable()
          If the current brace is a /, a *, a // or a \n, it's not matchable.
 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.
protected  void getDistToEnclosingBrace(IndentInfo braceInfo)
           
protected  void getDistToEnclosingBraceCurrent(IndentInfo braceInfo)
          Find the enclosing brace enclosing our current location.
 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.
 void move(int count)
          Updates ReducedModelBrace to reflect cursor movement.
 int nextBrace()
          Goes to the location before the brace.
 boolean openBraceImmediatelyRight()
           
 int previousBrace()
          Returns distance from current location of cursor to the location of the previous significant brace.
protected  void resetLocation()
          Resets the walker to the current position in document
protected  ReducedModelState stateAtRelLocation(int relDistance)
          Returns the state at the relLocation, where relLocation is the location relative to the walker
 
Methods inherited from class edu.rice.cs.drjava.model.definitions.reducedmodel.AbstractReducedModel
_augmentCurrentGap, _augmentGapToLeft, _gapToLeft, _gapToRight, _insertGap, _insertNewGap, absOffset, current, getBlockOffset, getStateAtCurrent, makeCopyCursor, next, prev, setBlockOffset, simpleString
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

_parent

private ReducedModelControl _parent
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.
Overrides:
insertChar in class AbstractReducedModel

_insertBrace

private void _insertBrace(String text)
Helper function for top level brace insert functions.
  1. at Head: not special case
  2. at Tail: not special case
  3. between two things (offset is 0):
    1. insert brace
    2. move next
    3. offset = 0
  4. inside gap:
    1. shrink gap to size of gap - offset.
    2. insert brace
    3. insert gap the size of offset.
    4. move next twice
    5. offset = 0
  5. inside multiple char brace:
    1. break
    2. insert brace
Parameters:
text - the String type of the brace to insert

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 the comment braces and escape sequences, we just throw an exception since the condition in insertGap that spawns this method doesn't arise.
Overrides:
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.
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.

_isCurrentBraceMatchable

private boolean _isCurrentBraceMatchable()
If the current brace is a /, a *, a // or a \n, it's not matchable. This means it is ignored on balancing and on next/prev brace finding. All other braces are matchable.

previousBrace

public int previousBrace()
Returns distance from current location of cursor to the location of the previous significant brace. ex. (...|) where | signifies the cursor. previousBrace returns 4 because it goes to the spot behind the (. /|* returns this brace since you're in the middle of it and going backward can find it.

nextBrace

public int nextBrace()
Goes to the location before the brace. |...( where | is the cursor, returns three since it is three moves to the location of 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.

openBraceImmediatelyRight

public boolean openBraceImmediatelyRight()

closedBraceImmediatelyLeft

public boolean closedBraceImmediatelyLeft()

balanceBackward

public int balanceBackward()

stateAtRelLocation

protected ReducedModelState stateAtRelLocation(int relDistance)
Description copied from class: AbstractReducedModel
Returns the state at the relLocation, where relLocation is the location relative to the walker
Overrides:
stateAtRelLocation in class AbstractReducedModel
Following copied from class: edu.rice.cs.drjava.model.definitions.reducedmodel.AbstractReducedModel
Parameters:
relLocation - distance from walker to get state at.

resetLocation

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

getDistToEnclosingBrace

protected void getDistToEnclosingBrace(IndentInfo braceInfo)

getDistToEnclosingBraceCurrent

protected void getDistToEnclosingBraceCurrent(IndentInfo braceInfo)
Find the enclosing brace enclosing our current location.