Further improve correction of whitespace during difference application #4316
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #1638 .
Lexical preservation is based on decomposing AST nodes into tokens. Spaces, tabs and end-of-line characters are also represented in the form of tokens. When lexical preservation is enabled, each operation on the AST updates this list of tokens, which are then printed to restore the lines of code.
The list of tokens analysed is defined from the parent node of the node on which the operation is performed. In the example provided, this is the list of tokens representing the declaration of the method on which the annotation is removed.
The problem you describe arises when you try to delete the first element in the list, as the prior indentation is generally carried by the parent node of the element in consideration (in this case, the annotation). Here, the tokens representing the method declaration are not sufficient as they do not contain the indentation information because they start with the element representing the annotation. We therefore need to analyse the parent node of the method declaration, then search the list of tokens for the element corresponding to the method declaration and finally determine whether an indentation is present and delete it if necessary.