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

java.lang.Object
  |
  +--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 and keeps this in mind when matching. 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.
  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. Similar for /*.

Version:
$Id: ReducedModelControl.java,v 1.25 2002/07/23 20:16:22 jhsia Exp $
Author:
JavaPLT

Field Summary
(package private)  int _offset
           
(package private)  ReducedModelBrace rmb
           
(package private)  ReducedModelComment rmc
           
 
Constructor Summary
ReducedModelControl()
           
 
Method Summary
 int absOffset()
          Gets the absolute character offset into the document represented by the reduced model.
(package private)  boolean atEnd()
          Determines if the cursor is at the end of the reduced model.
(package private)  boolean atStart()
          Determines if the cursor is at the start of 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.
 ReducedToken currentToken()
          Get the token currently pointed at by the cursor.
 void delete(int count)
          Update the BraceReduction to reflect text deletion.
(package private)  int getBlockOffset()
          Gets the offset within the current token.
 int getDistToNextNewline()
          Gets distance to next new line.
 int getDistToPreviousNewline(int relLoc)
          Gets distance to end of line on the line previous.
 gj.util.Vector getHighlightStatus(int start, int length)
          Return all highlight status info for text between the current location and current location + end.
 IndentInfo getIndentInformation()
          Returns an IndentInfo containing the following information: - distance to the previous newline ( start of this line) - distance to the brace enclosing the beginning of the current line - distance to the beginning of the line containing that brace
(package private)  int getSize()
          Gets the size of the current token.
(package private)  int getSize(ReducedToken rmbToken, ReducedToken rmcToken)
           
 ReducedModelState getStateAtCurrent()
          Get the shadowing state at the current caret position.
(package private)  String getType()
          Get a string representation of the current token's type.
 void insertChar(char ch)
          Insert a character into the BraceReduction.
 void move(int count)
          Updates the BraceReduction to reflect cursor movement.
(package private)  void next()
          Move the reduced model to the next token and update the cursor information.
 ReducedToken nextItem()
          Get the next token.
(package private)  void prev()
          Move the reduced model to the previous token and update the cursor information.
 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.
 ReducedModelState stateAtRelLocation(int relDistance)
          This function returns the state at the relDistance, where relDistance is relative to the last time it was called.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

rmb

ReducedModelBrace rmb

rmc

ReducedModelComment rmc

_offset

int _offset
Constructor Detail

ReducedModelControl

public ReducedModelControl()
Method Detail

insertChar

public void insertChar(char ch)
Description copied from interface: BraceReduction
Insert a character into the BraceReduction.
Specified by:
insertChar in interface BraceReduction
Following copied from interface: edu.rice.cs.drjava.model.definitions.reducedmodel.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 from the cursor, positive values move right.

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

delete

public void delete(int count)

Update the BraceReduction to reflect text deletion.

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

balanceForward

public int balanceForward()

Finds the closing brace that matches the next significant brace iff that brace is an open brace.

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

balanceBackward

public int balanceBackward()

Finds the open brace that matches the previous significant brace iff that brace is an closing brace.

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

stateAtRelLocation

public ReducedModelState stateAtRelLocation(int relDistance)
This function 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:
stateAtRelLocation in interface BraceReduction
Following copied from interface: edu.rice.cs.drjava.model.definitions.reducedmodel.BraceReduction
Parameters:
relLocation - 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()
Get 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()
Get 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

getType

String getType()
Get a string representation of the current token's type.
Returns:
"" if current is a Gap, otherwise, use ReducedToken.getType()

getSize

int getSize()
Gets the size of the current token. It checks both the brace and comment sub-models to find the size of the current token. If the current token is a Gap, we have to reconcile the information of both sub-models in order to get the correct size of the current token as seen by the outside world.
Returns:
the number of characters represented by the current token

getSize

int getSize(ReducedToken rmbToken,
            ReducedToken rmcToken)

next

void next()
Move the reduced model to the next token and update the cursor information.

prev

void prev()
Move the reduced model to the previous token and update the cursor information.

prevItem

public ReducedToken prevItem()
Get the previous token.

nextItem

public ReducedToken nextItem()
Get the next token.

atEnd

boolean atEnd()
Determines if the cursor is at the end of the reduced model.

atStart

boolean atStart()
Determines if the cursor is at the start of the reduced model.

getBlockOffset

int getBlockOffset()
Gets the offset within the current 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

getIndentInformation

public IndentInfo getIndentInformation()
Returns an IndentInfo containing the following information: - distance to the previous newline ( start of this line) - distance to the brace enclosing the beginning of the current line - distance to the beginning of the line containing that brace
Specified by:
getIndentInformation in interface BraceReduction

getDistToPreviousNewline

public int getDistToPreviousNewline(int relLoc)
Gets distance to end of line on the line previous.
Specified by:
getDistToPreviousNewline 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 gj.util.Vector getHighlightStatus(int start,
                                         int length)
Return all highlight status info for text between the current location and current location + end. This should collapse adjoining blocks with the same status into one.
Specified by:
getHighlightStatus in interface BraceReduction
Parameters:
start - The starting location of the area we want to get status of. The reduced model is already at this position, but the parameter is needed to determine the absolute positions needed in the HighlightStatus objects we return.
length - How far should we generate info for?