1717# tools necessary to inspect and manipulate that syntax tree. It can be used to
1818# build formatters, linters, language servers, and more.
1919module SyntaxTree
20- # Syntax Tree the library has many features that aren't always used by the
21- # CLI. Requiring those features takes time, so we autoload as many constants
22- # as possible in order to keep the CLI as fast as possible.
23-
24- autoload :FieldVisitor , "syntax_tree/field_visitor"
25- autoload :Index , "syntax_tree/index"
26- autoload :JSONVisitor , "syntax_tree/json_visitor"
2720 autoload :LanguageServer , "syntax_tree/language_server"
28- autoload :MatchVisitor , "syntax_tree/match_visitor"
29- autoload :Mermaid , "syntax_tree/mermaid"
30- autoload :MermaidVisitor , "syntax_tree/mermaid_visitor"
31- autoload :MutationVisitor , "syntax_tree/mutation_visitor"
32- autoload :Pattern , "syntax_tree/pattern"
33- autoload :PrettyPrintVisitor , "syntax_tree/pretty_print_visitor"
34- autoload :Search , "syntax_tree/search"
3521
3622 # This holds references to objects that respond to both #parse and #format
3723 # so that we can use them in the CLI.
@@ -92,39 +78,13 @@ def self.format_node(
9278 formatter . output . join
9379 end
9480
95- # Indexes the given source code to return a list of all class, module, and
96- # method definitions. Used to quickly provide indexing capability for IDEs or
97- # documentation generation.
98- def self . index ( source )
99- Index . index ( source )
100- end
101-
102- # Indexes the given file to return a list of all class, module, and method
103- # definitions. Used to quickly provide indexing capability for IDEs or
104- # documentation generation.
105- def self . index_file ( filepath )
106- Index . index_file ( filepath )
107- end
108-
109- # A convenience method for creating a new mutation visitor.
110- def self . mutation
111- visitor = MutationVisitor . new
112- yield visitor
113- visitor
114- end
115-
11681 # Parses the given source and returns the syntax tree.
11782 def self . parse ( source )
11883 parser = Parser . new ( source )
11984 response = parser . parse
12085 response unless parser . error?
12186 end
12287
123- # Parses the given file and returns the syntax tree.
124- def self . parse_file ( filepath )
125- parse ( read ( filepath ) )
126- end
127-
12888 # Returns the source from the given filepath taking into account any potential
12989 # magic encoding comments.
13090 def self . read ( filepath )
@@ -145,19 +105,4 @@ def self.read(filepath)
145105 def self . register_handler ( extension , handler )
146106 HANDLERS [ extension ] = handler
147107 end
148-
149- # Searches through the given source using the given pattern and yields each
150- # node in the tree that matches the pattern to the given block.
151- def self . search ( source , query , &block )
152- pattern = Pattern . new ( query ) . compile
153- program = parse ( source )
154-
155- Search . new ( pattern ) . scan ( program , &block )
156- end
157-
158- # Searches through the given file using the given pattern and yields each
159- # node in the tree that matches the pattern to the given block.
160- def self . search_file ( filepath , query , &block )
161- search ( read ( filepath ) , query , &block )
162- end
163108end
0 commit comments