Package org.eclipse.m2m.atl.adt.ui.text
Class AtlHeuristicScanner
java.lang.Object
org.eclipse.m2m.atl.adt.ui.text.AtlHeuristicScanner
- All Implemented Interfaces:
Symbols
Utility methods for heuristic based manipulations in an incomplete source file.
An instance holds some internal position in the document and is therefore not threadsafe.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected class
Acts like character match, but skips all scopes introduced by parenthesis, brackets, and braces.static interface
Specifies the stop condition, upon which thescanXXX
methods will decide whether to keep scanning or not. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
Returned by all methods when the requested position could not be found, or if aBadLocationException
was thrown while scanning.static final int
Special bound parameter that means either -1 (backward scanning) orfDocument.getLength()
(forward scanning).Fields inherited from interface org.eclipse.m2m.atl.adt.ui.text.Symbols
TOKEN_BREAK, TOKEN_CASE, TOKEN_CATCH, TOKEN_COLON, TOKEN_COMMA, TOKEN_DEFAULT, TOKEN_DO, TOKEN_ELSE, TOKEN_EOF, TOKEN_EQUAL, TOKEN_FINALLY, TOKEN_FOR, TOKEN_GOTO, TOKEN_IDENT, TOKEN_IF, TOKEN_LBRACE, TOKEN_LBRACKET, TOKEN_LPAREN, TOKEN_NEW, TOKEN_OTHER, TOKEN_QUESTIONMARK, TOKEN_RBRACE, TOKEN_RBRACKET, TOKEN_RETURN, TOKEN_RPAREN, TOKEN_SEMICOLON, TOKEN_STATIC, TOKEN_SWITCH, TOKEN_SYNCHRONIZED, TOKEN_TRY, TOKEN_WHILE
-
Constructor Summary
ConstructorsConstructorDescriptionAtlHeuristicScanner
(org.eclipse.jface.text.IDocument document) Callsthis(document, IAtlPartitions.Atl_PARTITIONING, IDocument.DEFAULT_CONTENT_TYPE)
.AtlHeuristicScanner
(org.eclipse.jface.text.IDocument document, String partitioning, String partition) Creates a new instance. -
Method Summary
Modifier and TypeMethodDescriptionint
findClosingPeer
(int start, char openingPeer, char closingPeer) Returns the position of the closing peer character (forward search).int
findNonWhitespaceBackward
(int position, int bound) Finds the highest position infDocument
such that the position is <=position
and >bound
andCharacter.isWhitespace(fDocument.getChar(pos))
evaluates tofalse
and the position is in the default partition.int
findNonWhitespaceForward
(int position, int bound) Finds the smallest position infDocument
such that the position is >=position
and <bound
andCharacter.isWhitespace(fDocument.getChar(pos))
evaluates tofalse
and the position is in the default partition.int
findNonWhitespaceForwardInAnyPartition
(int position, int bound) Finds the smallest position infDocument
such that the position is >=position
and <bound
andCharacter.isWhitespace(fDocument.getChar(pos))
evaluates tofalse
.int
findOpeningPeer
(int start, char openingPeer, char closingPeer) Returns the position of the opening peer character (backward search).org.eclipse.jface.text.IRegion
findSurroundingBlock
(int offset) Computes the surrounding block aroundoffset
.int
Returns the most recent internal scan position.boolean
isBracelessBlockStart
(int position, int bound) Checks if the line seems to be an open condition not followed by a block (i.e.boolean
isDefaultPartition
(int position) Checks whetherposition
resides in a default (Atl) partition offDocument
.int
nextToken
(int start, int bound) Returns the next token in forward direction, starting atstart
, and not extending further thanbound
.int
previousToken
(int start, int bound) Returns the next token in backward direction, starting atstart
, and not extending further thanbound
.int
scanBackward
(int position, int bound, char ch) Finds the highest position infDocument
such that the position is <=position
and >bound
andfDocument.getChar(position) == ch
evaluates totrue
for at least one ch inchars
and the position is in the default partition.int
scanBackward
(int position, int bound, char[] chars) Finds the highest position infDocument
such that the position is <=position
and >bound
andfDocument.getChar(position) == ch
evaluates totrue
for at least one ch inchars
and the position is in the default partition.int
scanBackward
(int start, int bound, AtlHeuristicScanner.StopCondition condition) Finds the highest positionp
infDocument
such thatbound
<p
<=start
andcondition.stop(fDocument.getChar(p), p)
evaluates totrue
.int
scanForward
(int position, int bound, char ch) Finds the lowest position infDocument
such that the position is >=position
and <bound
andfDocument.getChar(position) == ch
evaluates totrue
and the position is in the default partition.int
scanForward
(int position, int bound, char[] chars) Finds the lowest position infDocument
such that the position is >=position
and <bound
andfDocument.getChar(position) == ch
evaluates totrue
for at least one ch inchars
and the position is in the default partition.int
scanForward
(int start, int bound, AtlHeuristicScanner.StopCondition condition) Finds the lowest positionp
infDocument
such thatstart
<= p <bound
andcondition.stop(fDocument.getChar(p), p)
evaluates totrue
.
-
Field Details
-
NOT_FOUND
public static final int NOT_FOUNDReturned by all methods when the requested position could not be found, or if aBadLocationException
was thrown while scanning.- See Also:
-
UNBOUND
public static final int UNBOUNDSpecial bound parameter that means either -1 (backward scanning) orfDocument.getLength()
(forward scanning).- See Also:
-
-
Constructor Details
-
AtlHeuristicScanner
public AtlHeuristicScanner(org.eclipse.jface.text.IDocument document) Callsthis(document, IAtlPartitions.Atl_PARTITIONING, IDocument.DEFAULT_CONTENT_TYPE)
.- Parameters:
document
- the document to scan.
-
AtlHeuristicScanner
public AtlHeuristicScanner(org.eclipse.jface.text.IDocument document, String partitioning, String partition) Creates a new instance.- Parameters:
document
- the document to scanpartitioning
- the partitioning to use for scanningpartition
- the partition to scan in
-
-
Method Details
-
findClosingPeer
public int findClosingPeer(int start, char openingPeer, char closingPeer) Returns the position of the closing peer character (forward search). Any scopes introduced by opening peers are skipped. All peers accounted for must reside in the default partition.Note that
start
must not point to the opening peer, but to the first character being searched.- Parameters:
start
- the start positionopeningPeer
- the opening peer character (e.g. '{')closingPeer
- the closing peer character (e.g. '}')- Returns:
- the matching peer character position, or
NOT_FOUND
-
findNonWhitespaceBackward
public int findNonWhitespaceBackward(int position, int bound) Finds the highest position infDocument
such that the position is <=position
and >bound
andCharacter.isWhitespace(fDocument.getChar(pos))
evaluates tofalse
and the position is in the default partition.- Parameters:
position
- the first character position infDocument
to be consideredbound
- the first position infDocument
to not consider any more, withbound
<position
, orUNBOUND
- Returns:
- the highest position of a non-whitespace character in (
bound
,position
] that resides in a Atl partition, orNOT_FOUND
if none can be found
-
findNonWhitespaceForward
public int findNonWhitespaceForward(int position, int bound) Finds the smallest position infDocument
such that the position is >=position
and <bound
andCharacter.isWhitespace(fDocument.getChar(pos))
evaluates tofalse
and the position is in the default partition.- Parameters:
position
- the first character position infDocument
to be consideredbound
- the first position infDocument
to not consider any more, withbound
>position
, orUNBOUND
- Returns:
- the smallest position of a non-whitespace character in [
position
,bound
) that resides in a Atl partition, orNOT_FOUND
if none can be found
-
findNonWhitespaceForwardInAnyPartition
public int findNonWhitespaceForwardInAnyPartition(int position, int bound) Finds the smallest position infDocument
such that the position is >=position
and <bound
andCharacter.isWhitespace(fDocument.getChar(pos))
evaluates tofalse
.- Parameters:
position
- the first character position infDocument
to be consideredbound
- the first position infDocument
to not consider any more, withbound
>position
, orUNBOUND
- Returns:
- the smallest position of a non-whitespace character in [
position
,bound
), orNOT_FOUND
if none can be found
-
findOpeningPeer
public int findOpeningPeer(int start, char openingPeer, char closingPeer) Returns the position of the opening peer character (backward search). Any scopes introduced by closing peers are skipped. All peers accounted for must reside in the default partition.Note that
start
must not point to the closing peer, but to the first character being searched.- Parameters:
start
- the start positionopeningPeer
- the opening peer character (e.g. '{')closingPeer
- the closing peer character (e.g. '}')- Returns:
- the matching peer character position, or
NOT_FOUND
-
findSurroundingBlock
public org.eclipse.jface.text.IRegion findSurroundingBlock(int offset) Computes the surrounding block aroundoffset
. The search is started at the beginning ofoffset
, i.e. an opening brace atoffset
will not be part of the surrounding block, but a closing brace will.- Parameters:
offset
- the offset for which the surrounding block is computed- Returns:
- a region describing the surrounding block, or
null
if none can be found
-
getPosition
public int getPosition()Returns the most recent internal scan position.- Returns:
- the most recent internal scan position.
-
isBracelessBlockStart
public boolean isBracelessBlockStart(int position, int bound) Checks if the line seems to be an open condition not followed by a block (i.e. an if, while, or for statement with just one following statement, see example below).if (condition) doStuff();
Algorithm: if the last non-WS, non-Comment code on the line is an if (condition), while (condition), for( expression), do, else, and there is no statement after that
- Parameters:
position
- the insert position of the new characterbound
- the lowest position to consider- Returns:
true
if the code is a conditional statement or loop without a block,false
otherwise
-
isDefaultPartition
public boolean isDefaultPartition(int position) Checks whetherposition
resides in a default (Atl) partition offDocument
.- Parameters:
position
- the position to be checked- Returns:
true
ifposition
is in the default partition offDocument
,false
otherwise
-
nextToken
public int nextToken(int start, int bound) Returns the next token in forward direction, starting atstart
, and not extending further thanbound
. The return value is one of the constants defined inSymbols
. After a call,getPosition()
will return the position just after the scanned token (i.e. the next position that will be scanned).- Parameters:
start
- the first character position in the document to considerbound
- the first position not to consider any more- Returns:
- a constant from
Symbols
describing the next token
-
previousToken
public int previousToken(int start, int bound) Returns the next token in backward direction, starting atstart
, and not extending further thanbound
. The return value is one of the constants defined inSymbols
. After a call,getPosition()
will return the position just before the scanned token starts (i.e. the next position that will be scanned).- Parameters:
start
- the first character position in the document to considerbound
- the first position not to consider any more- Returns:
- a constant from
Symbols
describing the previous token
-
scanBackward
public int scanBackward(int position, int bound, char ch) Finds the highest position infDocument
such that the position is <=position
and >bound
andfDocument.getChar(position) == ch
evaluates totrue
for at least one ch inchars
and the position is in the default partition.- Parameters:
position
- the first character position infDocument
to be consideredbound
- the first position infDocument
to not consider any more, withbound
<position
, orUNBOUND
ch
- thechar
to search for- Returns:
- the highest position of one element in
chars
in (bound
,position
] that resides in a Atl partition, orNOT_FOUND
if none can be found
-
scanBackward
public int scanBackward(int position, int bound, char[] chars) Finds the highest position infDocument
such that the position is <=position
and >bound
andfDocument.getChar(position) == ch
evaluates totrue
for at least one ch inchars
and the position is in the default partition.- Parameters:
position
- the first character position infDocument
to be consideredbound
- the first position infDocument
to not consider any more, withbound
<position
, orUNBOUND
chars
- an array ofchar
to search for- Returns:
- the highest position of one element in
chars
in (bound
,position
] that resides in a Atl partition, orNOT_FOUND
if none can be found
-
scanBackward
Finds the highest positionp
infDocument
such thatbound
<p
<=start
andcondition.stop(fDocument.getChar(p), p)
evaluates totrue
.- Parameters:
start
- the first character position infDocument
to be consideredbound
- the first position infDocument
to not consider any more, withbound
<start
, orUNBOUND
condition
- theStopCondition
to check- Returns:
- the highest position in (
bound
,start
for whichcondition
holds, orNOT_FOUND
if none can be found
-
scanForward
public int scanForward(int position, int bound, char ch) Finds the lowest position infDocument
such that the position is >=position
and <bound
andfDocument.getChar(position) == ch
evaluates totrue
and the position is in the default partition.- Parameters:
position
- the first character position infDocument
to be consideredbound
- the first position infDocument
to not consider any more, withbound
>position
, orUNBOUND
ch
- thechar
to search for- Returns:
- the lowest position of
ch
in (bound
,position
] that resides in a Atl partition, orNOT_FOUND
if none can be found
-
scanForward
public int scanForward(int position, int bound, char[] chars) Finds the lowest position infDocument
such that the position is >=position
and <bound
andfDocument.getChar(position) == ch
evaluates totrue
for at least one ch inchars
and the position is in the default partition.- Parameters:
position
- the first character position infDocument
to be consideredbound
- the first position infDocument
to not consider any more, withbound
>position
, orUNBOUND
chars
- an array ofchar
to search for- Returns:
- the lowest position of a non-whitespace character in [
position
,bound
) that resides in a Atl partition, orNOT_FOUND
if none can be found
-
scanForward
Finds the lowest positionp
infDocument
such thatstart
<= p <bound
andcondition.stop(fDocument.getChar(p), p)
evaluates totrue
.- Parameters:
start
- the first character position infDocument
to be consideredbound
- the first position infDocument
to not consider any more, withbound
>start
, orUNBOUND
condition
- theStopCondition
to check- Returns:
- the lowest position in [
start
,bound
) for whichcondition
holds, orNOT_FOUND
if none can be found
-