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

java.lang.Object
  extended by edu.rice.cs.drjava.model.definitions.reducedmodel.ReducedModelControl
All Implemented Interfaces:
BraceReduction

public class ReducedModelControl
extends Object
implements BraceReduction

This class provides an implementation of the BraceReduction interface for brace matching. In order to correctly match, this class keeps track of what is commented (line and block) and what is inside double quotes (strings). To avoid unnecessary complication, this class maintains a few invariants for its consistent states, i.e., between top-level function calls.

  1. The cursor offset is never at the end of a brace. If movement or insertion puts it there, the cursor is updated to point to the 0 offset of the next brace. (token?)
  2. Quoting information is invalid inside valid comments. When part of the document becomes uncommented, the reduced model must update the quoting information linearly in the newly revealed code.
  3. Quote shadowing and comment shadowing are mutually exclusive.
  4. There is no nesting of comment open characters. If // is encountered in the middle of a comment, it is treated as two separate slashes. Similarly for /*.
All of the code in the class assumes that the document read lock and the lock on this are held.

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

Constructor Summary
ReducedModelControl()
          Standard constructor.
 
Method Summary
 BraceInfo _getEnclosingBrace()
          Gets info about the brace enclosing this location.
 BraceInfo _getLineEnclosingBrace()
          Gets info about the brace enclosing the beginning of this line.
 int absOffset()
          Gets the absolute character offset into the document represented by the reduced model.
 int balanceBackward()
          Finds the open brace that matches the previous significant brace iff that brace is an closing brace.
 int balanceForward()
          Finds the closing brace that matches the next significant brace iff that brace is an open brace.
 int braceCursorOffset()
           
 int commentCursorOffset()
           
 ReducedToken currentToken()
          Gets the token currently pointed at by the cursor.
 void delete(int count)
          Updates the BraceReduction to reflect text deletion.
 int getDistToEnclosingBraceStart()
           
 int getDistToIdentNewline()
           
 int getDistToNextNewline()
          Gets distance to next new line.
 int getDistToStart()
          Gets distance to the new newline character (not including the newline).
 int getDistToStart(int relLoc)
          Gets distance to previous newline character (not including the newline).
 ArrayList<HighlightStatus> getHighlightStatus(int start, int length)
          Return all highlight status info for text between the current location and current location + length.
 ReducedModelState getStateAtCurrent()
          Gets the shadowing state at the current caret position.
 void insertChar(char ch)
          Insert a character into the BraceReduction.
 boolean isShadowed()
          Determines if cursor position is shadowed by comment or string (does not include opening comment or quotation "brace").
 boolean isWeaklyShadowed()
          Determines if current token is either shadowed or an opening comment brace ("//" or "/*").
 void move(int count)
          Updates the BraceReduction to reflect cursor movement.
 ReducedModelState moveWalkerGetState(int relDistance)
          Returns the state at the relDistance, where relDistance is relative to the last time it was called.
 ReducedToken nextItem()
          Get the next token.
 ReducedToken prevItem()
          Get the previous token.
 void resetLocation()
          This function resets the location of the walker in the comment list to where the current cursor is.
 String simpleString()
          A toString() substitute.
 int walkerOffset()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReducedModelControl

public ReducedModelControl()
Standard constructor.

Method Detail

braceCursorOffset

public int braceCursorOffset()
Returns:
the absolute offset of the cursor in the brace model, which is expensive. Used for testing purposes only. This value should agree with commentCursorOffset().

commentCursorOffset

public int commentCursorOffset()
Returns:
the absolute offset of the cursor in the comment model, which is expensive. Used for testing purposes only. This value should agree with braceCursorOffset().

walkerOffset

public int walkerOffset()
Returns:
the absolute offset of the walker in the comment model, which is expensive. Used for testing purposes only.

insertChar

public void insertChar(char ch)
Description copied from interface: BraceReduction
Insert a character into the BraceReduction.

Specified by:
insertChar in interface BraceReduction
Parameters:
ch - the character to be inserted

move

public void move(int count)
Updates the BraceReduction to reflect cursor movement. Negative values move left; positive values move right. ASSUMES that count is within range, i.e. that 0 <= absOffset() + count <= getLength NOTE: this method does NOT move the _walker in ReduceModelComment.

Specified by:
move in interface BraceReduction
Parameters:
count - indicates the direction and magnitude of cursor movement

delete

public void delete(int count)
Updates the BraceReduction to reflect text deletion. Assumes that count is within range!

Specified by:
delete in interface BraceReduction
Parameters:
count - A number indicating the size and direction of text deletion. Negative values delete text to the left of the cursor, positive values delete text to the right.

isShadowed

public boolean isShadowed()
Determines if cursor position is shadowed by comment or string (does not include opening comment or quotation "brace").


isWeaklyShadowed

public boolean isWeaklyShadowed()
Determines if current token is either shadowed or an opening comment brace ("//" or "/*").


balanceForward

public int balanceForward()
Finds the closing brace that matches the next significant brace iff that brace is an open brace. Fails when brace is shadowed.

Specified by:
balanceForward in interface BraceReduction
Returns:
the distance until the matching closing brace. On failure, returns -1.
See Also:
balanceBackward()

balanceBackward

public int balanceBackward()
Finds the open brace that matches the previous significant brace iff that brace is an closing brace. Fails when brace is shadowed.

Specified by:
balanceBackward in interface BraceReduction
Returns:
the distance until the matching open brace. On failure, returns -1.
See Also:
balanceForward()

moveWalkerGetState

public ReducedModelState moveWalkerGetState(int relDistance)
Returns the state at the relDistance, where relDistance is relative to the last time it was called. You can reset the last call to the current offset using resetLocation.

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

resetLocation

public void resetLocation()
This function resets the location of the walker in the comment list to where the current cursor is. This allows the walker to keep walking and using relative distance instead of having to rewalk the same distance every call to stateAtRelLocation. It is an optimization.

Specified by:
resetLocation in interface BraceReduction

currentToken

public ReducedToken currentToken()
Gets the token currently pointed at by the cursor. Because the reduced model is split into two reduced sub-models, we have to check each sub-model first as each one has unique information. If we find a non-gap token in either sub-model we want to return that. Otherwise, we want to return a sort of hybrid Gap of the two, i.e., a Gap where there are neither special comment/quote tokens nor parens/squigglies/brackets.

Specified by:
currentToken in interface BraceReduction
Returns:
a ReducedToken representative of the unified reduced model

getStateAtCurrent

public ReducedModelState getStateAtCurrent()
Gets the shadowing state at the current caret position.

Specified by:
getStateAtCurrent in interface BraceReduction
Returns:
FREE|INSIDE_LINE_COMMENT|INSIDE_BLOCK_COMMENT| INSIDE_SINGLE_QUOTE|INSIDE_DOUBLE_QUOTE

prevItem

public ReducedToken prevItem()
Get the previous token.


nextItem

public ReducedToken nextItem()
Get the next token.


absOffset

public int absOffset()
Gets the absolute character offset into the document represented by the reduced model.

Specified by:
absOffset in interface BraceReduction

simpleString

public String simpleString()
A toString() substitute.

Specified by:
simpleString in interface BraceReduction

getDistToIdentNewline

public int getDistToIdentNewline()

getDistToEnclosingBraceStart

public int getDistToEnclosingBraceStart()

_getLineEnclosingBrace

public BraceInfo _getLineEnclosingBrace()
Gets info about the brace enclosing the beginning of this line.


_getEnclosingBrace

public BraceInfo _getEnclosingBrace()
Gets info about the brace enclosing this location.


getDistToStart

public int getDistToStart()
Gets distance to the new newline character (not including the newline).


getDistToStart

public int getDistToStart(int relLoc)
Gets distance to previous newline character (not including the newline).

Specified by:
getDistToStart in interface BraceReduction

getDistToNextNewline

public int getDistToNextNewline()
Description copied from interface: BraceReduction
Gets distance to next new line.

Specified by:
getDistToNextNewline in interface BraceReduction

getHighlightStatus

public ArrayList<HighlightStatus> getHighlightStatus(int start,
                                                     int length)
Return all highlight status info for text between the current location and current location + length. This should collapse adjoining blocks with the same status into one. Assumes read and reduced locks are already held.

Specified by:
getHighlightStatus in interface BraceReduction
Parameters:
start - The start location of the area being inspected. The reduced model cursor is already set at this position, but this value is needed to compute the absolute positions of HighlightStatus objects.
length - The length of the text segment for which status information must be generated.