Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Trim down even further
  • Loading branch information
kddnewton committed Oct 29, 2025
commit 8518688aa9cbc64febd6bce29e37ac19ad049a35
456 changes: 3 additions & 453 deletions README.md

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

require "bundler/setup"
require "syntax_tree"
require "syntax_tree/reflection"

require "irb"
IRB.start(__FILE__)
55 changes: 0 additions & 55 deletions lib/syntax_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,7 @@
# tools necessary to inspect and manipulate that syntax tree. It can be used to
# build formatters, linters, language servers, and more.
module SyntaxTree
# Syntax Tree the library has many features that aren't always used by the
# CLI. Requiring those features takes time, so we autoload as many constants
# as possible in order to keep the CLI as fast as possible.

autoload :FieldVisitor, "syntax_tree/field_visitor"
autoload :Index, "syntax_tree/index"
autoload :JSONVisitor, "syntax_tree/json_visitor"
autoload :LanguageServer, "syntax_tree/language_server"
autoload :MatchVisitor, "syntax_tree/match_visitor"
autoload :Mermaid, "syntax_tree/mermaid"
autoload :MermaidVisitor, "syntax_tree/mermaid_visitor"
autoload :MutationVisitor, "syntax_tree/mutation_visitor"
autoload :Pattern, "syntax_tree/pattern"
autoload :PrettyPrintVisitor, "syntax_tree/pretty_print_visitor"
autoload :Search, "syntax_tree/search"

# This holds references to objects that respond to both #parse and #format
# so that we can use them in the CLI.
Expand Down Expand Up @@ -92,39 +78,13 @@ def self.format_node(
formatter.output.join
end

# Indexes the given source code to return a list of all class, module, and
# method definitions. Used to quickly provide indexing capability for IDEs or
# documentation generation.
def self.index(source)
Index.index(source)
end

# Indexes the given file to return a list of all class, module, and method
# definitions. Used to quickly provide indexing capability for IDEs or
# documentation generation.
def self.index_file(filepath)
Index.index_file(filepath)
end

# A convenience method for creating a new mutation visitor.
def self.mutation
visitor = MutationVisitor.new
yield visitor
visitor
end

# Parses the given source and returns the syntax tree.
def self.parse(source)
parser = Parser.new(source)
response = parser.parse
response unless parser.error?
end

# Parses the given file and returns the syntax tree.
def self.parse_file(filepath)
parse(read(filepath))
end

# Returns the source from the given filepath taking into account any potential
# magic encoding comments.
def self.read(filepath)
Expand All @@ -145,19 +105,4 @@ def self.read(filepath)
def self.register_handler(extension, handler)
HANDLERS[extension] = handler
end

# Searches through the given source using the given pattern and yields each
# node in the tree that matches the pattern to the given block.
def self.search(source, query, &block)
pattern = Pattern.new(query).compile
program = parse(source)

Search.new(pattern).scan(program, &block)
end

# Searches through the given file using the given pattern and yields each
# node in the tree that matches the pattern to the given block.
def self.search_file(filepath, query, &block)
search(read(filepath), query, &block)
end
end
91 changes: 0 additions & 91 deletions lib/syntax_tree/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,21 +211,6 @@ def run(item)
end
end

# An action of the CLI that outputs a pattern-matching Ruby expression that
# would match the first expression of the input given.
class Expr < Action
def run(item)
program = item.handler.parse(item.source)

if (expressions = program.statements.body) && expressions.size == 1
puts expressions.first.construct_keys
else
warn("The input to `stree expr` must be a single expression.")
exit(1)
end
end
end

# An action of the CLI that formats the input source and prints it out.
class Format < Action
def run(item)
Expand All @@ -240,61 +225,6 @@ def run(item)
end
end

# An action of the CLI that converts the source into its equivalent JSON
# representation.
class Json < Action
def run(item)
object = item.handler.parse(item.source).accept(JSONVisitor.new)
puts JSON.pretty_generate(object)
end
end

# An action of the CLI that outputs a pattern-matching Ruby expression that
# would match the input given.
class Match < Action
def run(item)
puts item.handler.parse(item.source).construct_keys
end
end

# An action of the CLI that searches for the given pattern matching pattern
# in the given files.
class Search < Action
attr_reader :search

def initialize(query)
query = File.read(query) if File.readable?(query)
pattern =
begin
Pattern.new(query).compile
rescue Pattern::CompilationError => error
warn(error.message)
exit(1)
end

@search = SyntaxTree::Search.new(pattern)
end

def run(item)
search.scan(item.handler.parse(item.source)) do |node|
location = node.location
line = location.start_line

bold_range =
if line == location.end_line
location.start_column...location.end_column
else
location.start_column..
end

source = item.source.lines[line - 1].chomp
source[bold_range] = Color.bold(source[bold_range]).to_s

puts("#{item.filepath}:#{line}:#{location.start_column}: #{source}")
end
end
end

# An action of the CLI that formats the input source and writes the
# formatted output back to the file.
class Write < Action
Expand Down Expand Up @@ -338,28 +268,15 @@ def run(item)
#{Color.bold("stree doc [--plugins=...] [-e SCRIPT] FILE")}
Print out the doc tree that would be used to format the given files

#{Color.bold("stree expr [-e SCRIPT] FILE")}
Print out a pattern-matching Ruby expression that would match the first
expression of the given files

#{Color.bold("stree format [--plugins=...] [--print-width=NUMBER] [-e SCRIPT] FILE")}
Print out the formatted version of the given files

#{Color.bold("stree json [--plugins=...] [-e SCRIPT] FILE")}
Print out the JSON representation of the given files

#{Color.bold("stree match [--plugins=...] [-e SCRIPT] FILE")}
Print out a pattern-matching Ruby expression that would match the given files

#{Color.bold("stree help")}
Display this help message

#{Color.bold("stree lsp [--plugins=...] [--print-width=NUMBER]")}
Run syntax tree in language server mode

#{Color.bold("stree search PATTERN [-e SCRIPT] FILE")}
Search for the given pattern in the given files

#{Color.bold("stree version")}
Output the current version of syntax tree

Expand Down Expand Up @@ -542,25 +459,17 @@ def run(argv)
Debug.new(options)
when "doc"
Doc.new(options)
when "e", "expr"
Expr.new(options)
when "f", "format"
Format.new(options)
when "help"
puts HELP
return 0
when "j", "json"
Json.new(options)
when "lsp"
LanguageServer.new(
print_width: options.print_width,
ignore_files: options.ignore_files
).run
return 0
when "m", "match"
Match.new(options)
when "s", "search"
Search.new(arguments.shift)
when "version"
puts SyntaxTree::VERSION
return 0
Expand Down
Loading