Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit bf0b343

Browse files
committed
The new SyntaxTree.register_handler hook for plugins.
1 parent fc93026 commit bf0b343

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
88

99
## [2.0.0] - 2022-03-30
1010

11+
### Added
12+
13+
- The new `SyntaxTree.register_handler` hook for plugins.
14+
1115
### Changed
1216

1317
- Changed `SyntaxTree` from being a class to being a module. The parser functionality is moved into `SyntaxTree::Parser`.

lib/syntax_tree/cli.rb

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22

33
module SyntaxTree
44
module CLI
5+
# This holds references to objects that respond to both #parse and #format
6+
# so that we can use them in the CLI.
7+
HANDLERS = {}
8+
HANDLERS.default = SyntaxTree
9+
10+
# This is a hook provided so that plugins can register themselves as the
11+
# handler for a particular file type.
12+
def self.register_handler(extension, handler)
13+
HANDLERS[extension] = handler
14+
end
15+
516
# A utility wrapper around colored strings in the output.
617
class Color
718
attr_reader :value, :code
@@ -34,7 +45,7 @@ def self.yellow(value)
3445

3546
# The parent action class for the CLI that implements the basics.
3647
class Action
37-
def run(filepath, source)
48+
def run(handler, filepath, source)
3849
end
3950

4051
def success
@@ -46,8 +57,8 @@ def failure
4657

4758
# An action of the CLI that prints out the AST for the given source.
4859
class AST < Action
49-
def run(filepath, source)
50-
pp SyntaxTree.parse(source)
60+
def run(handler, filepath, source)
61+
pp handler.parse(source)
5162
end
5263
end
5364

@@ -57,8 +68,8 @@ class Check < Action
5768
class UnformattedError < StandardError
5869
end
5970

60-
def run(filepath, source)
61-
raise UnformattedError if source != SyntaxTree.format(source)
71+
def run(handler, filepath, source)
72+
raise UnformattedError if source != handler.format(source)
6273
rescue StandardError
6374
warn("[#{Color.yellow("warn")}] #{filepath}")
6475
raise
@@ -79,11 +90,11 @@ class Debug < Action
7990
class NonIdempotentFormatError < StandardError
8091
end
8192

82-
def run(filepath, source)
93+
def run(handler, filepath, source)
8394
warning = "[#{Color.yellow("warn")}] #{filepath}"
84-
formatted = SyntaxTree.format(source)
95+
formatted = handler.format(source)
8596

86-
if formatted != SyntaxTree.format(formatted)
97+
if formatted != handler.format(formatted)
8798
raise NonIdempotentFormatError
8899
end
89100
rescue StandardError
@@ -102,28 +113,28 @@ def failure
102113

103114
# An action of the CLI that prints out the doc tree IR for the given source.
104115
class Doc < Action
105-
def run(filepath, source)
116+
def run(handler, filepath, source)
106117
formatter = Formatter.new([])
107-
SyntaxTree.parse(source).format(formatter)
118+
handler.parse(source).format(formatter)
108119
pp formatter.groups.first
109120
end
110121
end
111122

112123
# An action of the CLI that formats the input source and prints it out.
113124
class Format < Action
114-
def run(filepath, source)
115-
puts SyntaxTree.format(source)
125+
def run(handler, filepath, source)
126+
puts handler.format(source)
116127
end
117128
end
118129

119130
# An action of the CLI that formats the input source and writes the
120131
# formatted output back to the file.
121132
class Write < Action
122-
def run(filepath, source)
133+
def run(handler, filepath, source)
123134
print filepath
124135
start = Time.now
125136

126-
formatted = SyntaxTree.format(source)
137+
formatted = handler.format(source)
127138
File.write(filepath, formatted)
128139

129140
color = source == formatted ? Color.gray(filepath) : filepath
@@ -214,10 +225,12 @@ def run(argv)
214225
patterns.each do |pattern|
215226
Dir.glob(pattern).each do |filepath|
216227
next unless File.file?(filepath)
228+
229+
handler = HANDLERS[File.extname(filepath)]
217230
source = SyntaxTree.read(filepath)
218231

219232
begin
220-
action.run(filepath, source)
233+
action.run(handler, filepath, source)
221234
rescue ParseError => error
222235
warn("Error: #{error.message}")
223236

0 commit comments

Comments
 (0)