Class LexicalPreservingPrinter
- java.lang.Object
-
- com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter
-
public class LexicalPreservingPrinter extends java.lang.ObjectThe LexicalPreservingPrinter is responsible for maintaining the original formatting and layout of Java source code when the Abstract Syntax Tree (AST) is modified. This printer works by: 1. Storing the original textual representation alongside the AST nodes 2. Tracking changes made to the AST through a sophisticated change tracking system 3. Applying only the necessary modifications while preserving unchanged portions 4. Maintaining original spacing, comments, and formatting wherever possible The lexical preservation is essential for code refactoring tools that need to modify code while maintaining its original style and readability. Usage Pattern: 1. Setup lexical preservation on a CompilationUnit before making changes 2. Modify the AST as needed 3. Use this printer to generate the modified source code with preserved formatting
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classLexicalPreservingPrinter.Observer
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.StringJAVA_UTIL_OPTIONALprivate static java.lang.StringJAVAPARSER_AST_NODELISTprivate static LexicalDifferenceCalculatorLEXICAL_DIFFERENCE_CALCULATORstatic DataKey<NodeText>NODE_TEXT_DATAThe nodetext for a node is stored in the node's data field.private static AstObserverobserverObserver tracks changes made to AST nodes.
-
Constructor Summary
Constructors Constructor Description LexicalPreservingPrinter()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static AstObservercreateObserver()(package private) static java.util.List<TextElement>findIndentation(Node node)private static ObservablePropertyfindNodeListName(NodeList<?> nodeList)(package private) static NodeTextgetOrCreateNodeText(Node node)private static NodeTextinterpret(Node node, CsmElement csm, NodeText nodeText)TODO: Process CsmIndent and CsmUnindent before reaching this pointstatic booleanisAvailableOn(Node node)private static booleanisReturningOptionalNodeList(java.lang.reflect.Method m)private static voidprettyPrintingTextNode(Node node, NodeText nodeText)static java.lang.Stringprint(Node node)Prints the given node to a string while preserving the original formatting where possible and applying changes where the AST has been modified.static <N extends Node>
Nsetup(N node)Initializes lexical preservation for the given CompilationUnit.private static voidstoreInitialText(Node root)Stores the initial textual representation for all nodes in the AST.private static voidstoreInitialTextForOneNode(Node node, java.util.List<JavaToken> nodeTokens)Recursively stores initial text representation for all child nodes of the given parent node.private static java.util.Iterator<TokenTextElement>tokensPreceeding(Node node)
-
-
-
Field Detail
-
JAVA_UTIL_OPTIONAL
private static java.lang.String JAVA_UTIL_OPTIONAL
-
JAVAPARSER_AST_NODELIST
private static java.lang.String JAVAPARSER_AST_NODELIST
-
observer
private static AstObserver observer
Observer tracks changes made to AST nodes. The observer is responsible for detecting modifications to specific types of nodes and updating the lexical preservation data accordingly.
-
NODE_TEXT_DATA
public static final DataKey<NodeText> NODE_TEXT_DATA
The nodetext for a node is stored in the node's data field. This is the key to set and retrieve it.
-
LEXICAL_DIFFERENCE_CALCULATOR
private static final LexicalDifferenceCalculator LEXICAL_DIFFERENCE_CALCULATOR
-
-
Method Detail
-
setup
public static <N extends Node> N setup(N node)
Initializes lexical preservation for the given CompilationUnit. This method must be called before making any modifications to the AST if you want to preserve the original formatting. The setup process involves: 1. Parsing the original source code into tokens 2. Creating NodeText representations for each AST node 3. Establishing relationships between tokens and nodes 4. Setting up change observers to track future modifications- Parameters:
cu- The CompilationUnit to setup for lexical preservation- Returns:
- The same CompilationUnit with lexical preservation enabled
-
isAvailableOn
public static boolean isAvailableOn(Node node)
-
createObserver
private static AstObserver createObserver()
-
storeInitialText
private static void storeInitialText(Node root)
Stores the initial textual representation for all nodes in the AST. This method creates a NodeText object for each node that captures: - The original tokens associated with the node - Whitespace and formatting information - Comment associations - Token positions and relationships This initial storage is crucial for later determining what has changed and what should be preserved during printing.- Parameters:
cu- The root node to process
-
storeInitialTextForOneNode
private static void storeInitialTextForOneNode(Node node, java.util.List<JavaToken> nodeTokens)
Recursively stores initial text representation for all child nodes of the given parent node. This method ensures that every node in the AST has its original textual representation captured for lexical preservation.- Parameters:
node- The parent node whose children should be processed
-
tokensPreceeding
private static java.util.Iterator<TokenTextElement> tokensPreceeding(Node node)
-
print
public static java.lang.String print(Node node)
Prints the given node to a string while preserving the original formatting where possible and applying changes where the AST has been modified. This method analyzes the changes made to the AST since lexical preservation was setup and generates output that: - Preserves original formatting for unchanged portions - Applies appropriate formatting for new or modified content - Maintains consistency with the original code style- Parameters:
node- The node to print (typically a CompilationUnit)- Returns:
- A string representation of the node with preserved formatting
-
interpret
private static NodeText interpret(Node node, CsmElement csm, NodeText nodeText)
TODO: Process CsmIndent and CsmUnindent before reaching this point
-
findIndentation
static java.util.List<TextElement> findIndentation(Node node)
-
isReturningOptionalNodeList
private static boolean isReturningOptionalNodeList(java.lang.reflect.Method m)
-
findNodeListName
private static ObservableProperty findNodeListName(NodeList<?> nodeList)
-
-