Package spoon.support.sniper.internal
Class SourceFragmentContextNormal
java.lang.Object
spoon.support.sniper.internal.SourceFragmentContextNormal
- All Implemented Interfaces:
SourceFragmentPrinter
Knows how to print modified
CtElement
by the way that origin formatting is kept as much as possible.
There are two streams of source fragments
1) stream of origin source fragments
2) stream of tokens produced by DefaultJavaPrettyPrinter
in spaces between printing of child CtElement
s
Note: some fragments are optional and some are mandatory. For example:
<T> void method(T p);
and after remove of type parameter and method parameter....
void method();
The type parameter brackets were removed, while parameter brackets are kept.
Only DJPP knows whether it has to be displayed or not. So algorithm is like this:
- print origin source token only after this token is printed by DJPP
- if DJPP doesn't prints some token then the same token must be ignored in origin source code too
- spaces belong to both surrounding elements. So they are printed only if both elements are printed
- if the elements are moved, then spaces are moved together with the follow up element
- if the elements E1 and E2 are separated by sequence of E1, Spaces1, Separator, Spaces2, E2 then Spaces1 belongs to E1 and Spaces2 belongs to element E2.
-
Field Summary
Modifier and TypeFieldDescriptionprotected ChangeResolver
protected int
protected List<SourceFragment>
protected MutableTokenWriter
-
Constructor Summary
ConstructorDescriptionSourceFragmentContextNormal(MutableTokenWriter mutableTokenWriter, ElementSourceFragment rootFragment, ChangeResolver changeResolver)
-
Method Summary
Modifier and TypeMethodDescriptionprotected int
Looks for the child token which fits toPrinterEvent
`event`protected int
looks for next child token which contains expected token typeprotected int
looks for next child token which contains exactly same tokenprotected int
looks for next child token, which has element `elementprotected int
findIndexOfNextChildTokenOfRole(int start, CtRole role)
looks for next child token which has role `role`protected boolean
protected ModificationStatus
isFragmentModified(SourceFragment fragment)
boolean
knowsHowToPrint(PrinterEvent event)
void
Called when printing using this context is going to finishvoid
onPush()
called when pushed on the stackvoid
print(PrinterEvent event)
Called whenDefaultJavaPrettyPrinter
starts an operationprotected void
printOriginSpacesUntilFragmentIndex(int fromIndex, int toIndex)
Prints origin whitespaces including comments which prefixes the fragment on index `index`, starting with fragment on `fromIndex`protected void
printSpaces(int fromIndex, int fragmentIndex)
Prints spaces before fragment with index `fragmentIndex`protected void
print all tokens, which represents white spaces and then forget them, so we can collect next white spacesprotected void
setChildFragmentIdx(int idx)
Remembers index of last processed fragmentint
update(PrinterEvent event)
Update the internal state of this printer for this event but does not print anything.
-
Field Details
-
mutableTokenWriter
-
childFragments
-
changeResolver
-
childFragmentIdx
protected int childFragmentIdx -
separatorActions
-
-
Constructor Details
-
SourceFragmentContextNormal
public SourceFragmentContextNormal(MutableTokenWriter mutableTokenWriter, ElementSourceFragment rootFragment, ChangeResolver changeResolver)- Parameters:
mutableTokenWriter
-MutableTokenWriter
, which is used for printingrootFragment
- theElementSourceFragment
, which represents whole elements. E.g. whole type or methodchangeResolver
-
-
-
Method Details
-
knowsHowToPrint
- Returns:
- true if this printer is able to handle this event
That is that we can safely call
SourceFragmentPrinter.print(PrinterEvent)
after having called this one.
-
onFinished
public void onFinished()Description copied from interface:SourceFragmentPrinter
Called when printing using this context is going to finish- Specified by:
onFinished
in interfaceSourceFragmentPrinter
-
print
Description copied from interface:SourceFragmentPrinter
Called whenDefaultJavaPrettyPrinter
starts an operation- Specified by:
print
in interfaceSourceFragmentPrinter
- Parameters:
event
- theDefaultJavaPrettyPrinter
event
-
update
Description copied from interface:SourceFragmentPrinter
Update the internal state of this printer for this event but does not print anything. Returns the index of the fragment corresponding to this event.- Specified by:
update
in interfaceSourceFragmentPrinter
-
printSpaces
protected void printSpaces(int fromIndex, int fragmentIndex)Prints spaces before fragment with index `fragmentIndex`- Parameters:
fragmentIndex
- index of fragment whose prefix spaces has to be printed or -1 if origin source fragment was not found
-
isFragmentModified
- Parameters:
fragment
-- Returns:
- true if at least part of `fragment` is modified. false if whole `fragment` is not modified. ModificationStatus.UNKNOWN if it is not possible to detect it here. Then it will be detected later.
-
printOriginSpacesUntilFragmentIndex
protected void printOriginSpacesUntilFragmentIndex(int fromIndex, int toIndex)Prints origin whitespaces including comments which prefixes the fragment on index `index`, starting with fragment on `fromIndex`- Parameters:
fromIndex
- index of first processed fragmenttoIndex
- index of first not processed fragment.
-
setChildFragmentIdx
protected void setChildFragmentIdx(int idx)Remembers index of last processed fragment- Parameters:
idx
- index of last processed fragment
-
hasNextChildToken
protected boolean hasNextChildToken()- Returns:
- true if there is more child tokens to be processed. false if all tokens were processed
-
findIndexOfNextChildTokenByValue
looks for next child token which contains exactly same token- Parameters:
token
- String of searched token- Returns:
- index of first same token or -1 if not found
-
findIndexOfNextChildTokenByType
looks for next child token which contains expected token type- Parameters:
type
-TokenType
of search token- Returns:
- index of token with same type or -1 if not found
-
findIndexOfNextChildTokenOfRole
looks for next child token which has role `role`- Parameters:
role
-CtRole
of searched token- Returns:
- index of first token with same role or -1 if not found
-
findIndexOfNextChildTokenOfElement
looks for next child token, which has element `element- Parameters:
element
- element of searched token- Returns:
- index of first token with same element or -1 if not found
-
findIFragmentIndexCorrespondingToEvent
Looks for the child token which fits toPrinterEvent
`event`- Parameters:
event
-PrinterEvent
whose token it searches for- Returns:
- index of first token which fits to
PrinterEvent
or -1 if not found
-
printStandardSpaces
protected void printStandardSpaces()print all tokens, which represents white spaces and then forget them, so we can collect next white spaces -
onPush
public void onPush()Description copied from interface:SourceFragmentPrinter
called when pushed on the stack- Specified by:
onPush
in interfaceSourceFragmentPrinter
-