Skip to content

Commit 8518688

Browse files
committed
Trim down even further
1 parent 3a60b73 commit 8518688

25 files changed

+8
-4839
lines changed

README.md

Lines changed: 3 additions & 453 deletions
Large diffs are not rendered by default.

bin/console

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
require "bundler/setup"
55
require "syntax_tree"
6-
require "syntax_tree/reflection"
76

87
require "irb"
98
IRB.start(__FILE__)

lib/syntax_tree.rb

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,7 @@
1717
# tools necessary to inspect and manipulate that syntax tree. It can be used to
1818
# build formatters, linters, language servers, and more.
1919
module 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
163108
end

lib/syntax_tree/cli.rb

Lines changed: 0 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -211,21 +211,6 @@ def run(item)
211211
end
212212
end
213213

214-
# An action of the CLI that outputs a pattern-matching Ruby expression that
215-
# would match the first expression of the input given.
216-
class Expr < Action
217-
def run(item)
218-
program = item.handler.parse(item.source)
219-
220-
if (expressions = program.statements.body) && expressions.size == 1
221-
puts expressions.first.construct_keys
222-
else
223-
warn("The input to `stree expr` must be a single expression.")
224-
exit(1)
225-
end
226-
end
227-
end
228-
229214
# An action of the CLI that formats the input source and prints it out.
230215
class Format < Action
231216
def run(item)
@@ -240,61 +225,6 @@ def run(item)
240225
end
241226
end
242227

243-
# An action of the CLI that converts the source into its equivalent JSON
244-
# representation.
245-
class Json < Action
246-
def run(item)
247-
object = item.handler.parse(item.source).accept(JSONVisitor.new)
248-
puts JSON.pretty_generate(object)
249-
end
250-
end
251-
252-
# An action of the CLI that outputs a pattern-matching Ruby expression that
253-
# would match the input given.
254-
class Match < Action
255-
def run(item)
256-
puts item.handler.parse(item.source).construct_keys
257-
end
258-
end
259-
260-
# An action of the CLI that searches for the given pattern matching pattern
261-
# in the given files.
262-
class Search < Action
263-
attr_reader :search
264-
265-
def initialize(query)
266-
query = File.read(query) if File.readable?(query)
267-
pattern =
268-
begin
269-
Pattern.new(query).compile
270-
rescue Pattern::CompilationError => error
271-
warn(error.message)
272-
exit(1)
273-
end
274-
275-
@search = SyntaxTree::Search.new(pattern)
276-
end
277-
278-
def run(item)
279-
search.scan(item.handler.parse(item.source)) do |node|
280-
location = node.location
281-
line = location.start_line
282-
283-
bold_range =
284-
if line == location.end_line
285-
location.start_column...location.end_column
286-
else
287-
location.start_column..
288-
end
289-
290-
source = item.source.lines[line - 1].chomp
291-
source[bold_range] = Color.bold(source[bold_range]).to_s
292-
293-
puts("#{item.filepath}:#{line}:#{location.start_column}: #{source}")
294-
end
295-
end
296-
end
297-
298228
# An action of the CLI that formats the input source and writes the
299229
# formatted output back to the file.
300230
class Write < Action
@@ -338,28 +268,15 @@ def run(item)
338268
#{Color.bold("stree doc [--plugins=...] [-e SCRIPT] FILE")}
339269
Print out the doc tree that would be used to format the given files
340270
341-
#{Color.bold("stree expr [-e SCRIPT] FILE")}
342-
Print out a pattern-matching Ruby expression that would match the first
343-
expression of the given files
344-
345271
#{Color.bold("stree format [--plugins=...] [--print-width=NUMBER] [-e SCRIPT] FILE")}
346272
Print out the formatted version of the given files
347273
348-
#{Color.bold("stree json [--plugins=...] [-e SCRIPT] FILE")}
349-
Print out the JSON representation of the given files
350-
351-
#{Color.bold("stree match [--plugins=...] [-e SCRIPT] FILE")}
352-
Print out a pattern-matching Ruby expression that would match the given files
353-
354274
#{Color.bold("stree help")}
355275
Display this help message
356276
357277
#{Color.bold("stree lsp [--plugins=...] [--print-width=NUMBER]")}
358278
Run syntax tree in language server mode
359279
360-
#{Color.bold("stree search PATTERN [-e SCRIPT] FILE")}
361-
Search for the given pattern in the given files
362-
363280
#{Color.bold("stree version")}
364281
Output the current version of syntax tree
365282
@@ -542,25 +459,17 @@ def run(argv)
542459
Debug.new(options)
543460
when "doc"
544461
Doc.new(options)
545-
when "e", "expr"
546-
Expr.new(options)
547462
when "f", "format"
548463
Format.new(options)
549464
when "help"
550465
puts HELP
551466
return 0
552-
when "j", "json"
553-
Json.new(options)
554467
when "lsp"
555468
LanguageServer.new(
556469
print_width: options.print_width,
557470
ignore_files: options.ignore_files
558471
).run
559472
return 0
560-
when "m", "match"
561-
Match.new(options)
562-
when "s", "search"
563-
Search.new(arguments.shift)
564473
when "version"
565474
puts SyntaxTree::VERSION
566475
return 0

0 commit comments

Comments
 (0)