Skip to content

Commit 509c29e

Browse files
committed
Allow specifying --print-width in the CLI
1 parent b5024c3 commit 509c29e

File tree

4 files changed

+102
-34
lines changed

4 files changed

+102
-34
lines changed

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ If there are files with unformatted code, you will receive:
118118
The listed files did not match the expected format.
119119
```
120120

121+
To change the print width that you are checking against, specify the `--print-width` option, as in:
122+
123+
```sh
124+
stree check --print-width=100 path/to/file.rb
125+
```
126+
121127
### format
122128

123129
This command will output the formatted version of each of the listed files. Importantly, it will not write that content back to the source files. It is meant to display the formatted version only.
@@ -132,6 +138,12 @@ For a file that contains `1 + 1`, you will receive:
132138
1 + 1
133139
```
134140

141+
To change the print width that you are formatting with, specify the `--print-width` option, as in:
142+
143+
```sh
144+
stree format --print-width=100 path/to/file.rb
145+
```
146+
135147
### json
136148

137149
This command will output a JSON representation of the syntax tree that is functionally equivalent to the input. This is mostly used in contexts where you need to access the tree from JavaScript or serialize it over a network.
@@ -213,6 +225,12 @@ This will list every file that is being formatted. It will output light gray if
213225
path/to/file.rb 0ms
214226
```
215227

228+
To change the print width that you are writing with, specify the `--print-width` option, as in:
229+
230+
```sh
231+
stree write --print-width=100 path/to/file.rb
232+
```
233+
216234
## Library
217235

218236
Syntax Tree can be used as a library to access the syntax tree underlying Ruby source code.

lib/syntax_tree.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ module SyntaxTree
2929
HANDLERS = {}
3030
HANDLERS.default = SyntaxTree
3131

32+
# This is the default print width when formatting. It can be overridden in the
33+
# CLI by passing the --print-width option or here in the API by passing the
34+
# optional second argument to ::format.
35+
DEFAULT_PRINT_WIDTH = 80
36+
3237
# This is a hook provided so that plugins can register themselves as the
3338
# handler for a particular file type.
3439
def self.register_handler(extension, handler)
@@ -43,7 +48,7 @@ def self.parse(source)
4348
end
4449

4550
# Parses the given source and returns the formatted source.
46-
def self.format(source, maxwidth = 80)
51+
def self.format(source, maxwidth = DEFAULT_PRINT_WIDTH)
4752
formatter = Formatter.new(source, [], maxwidth)
4853
parse(source).format(formatter)
4954

lib/syntax_tree/cli.rb

+70-33
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,15 @@ class Check < Action
9191
class UnformattedError < StandardError
9292
end
9393

94+
attr_reader :print_width
95+
96+
def initialize(print_width:)
97+
@print_width = print_width
98+
end
99+
94100
def run(item)
95101
source = item.source
96-
raise UnformattedError if source != item.handler.format(source)
102+
raise UnformattedError if source != item.handler.format(source, print_width)
97103
rescue StandardError
98104
warn("[#{Color.yellow("warn")}] #{item.filepath}")
99105
raise
@@ -114,13 +120,19 @@ class Debug < Action
114120
class NonIdempotentFormatError < StandardError
115121
end
116122

123+
attr_reader :print_width
124+
125+
def initialize(print_width:)
126+
@print_width = print_width
127+
end
128+
117129
def run(item)
118130
handler = item.handler
119131

120132
warning = "[#{Color.yellow("warn")}] #{item.filepath}"
121-
formatted = handler.format(item.source)
133+
formatted = handler.format(item.source, print_width)
122134

123-
raise NonIdempotentFormatError if formatted != handler.format(formatted)
135+
raise NonIdempotentFormatError if formatted != handler.format(formatted, print_width)
124136
rescue StandardError
125137
warn(warning)
126138
raise
@@ -148,8 +160,14 @@ def run(item)
148160

149161
# An action of the CLI that formats the input source and prints it out.
150162
class Format < Action
163+
attr_reader :print_width
164+
165+
def initialize(print_width:)
166+
@print_width = print_width
167+
end
168+
151169
def run(item)
152-
puts item.handler.format(item.source)
170+
puts item.handler.format(item.source, print_width)
153171
end
154172
end
155173

@@ -173,12 +191,18 @@ def run(item)
173191
# An action of the CLI that formats the input source and writes the
174192
# formatted output back to the file.
175193
class Write < Action
194+
attr_reader :print_width
195+
196+
def initialize(print_width:)
197+
@print_width = print_width
198+
end
199+
176200
def run(item)
177201
filepath = item.filepath
178202
start = Time.now
179203

180204
source = item.source
181-
formatted = item.handler.format(source)
205+
formatted = item.handler.format(source, print_width)
182206
File.write(filepath, formatted) if filepath != :stdin
183207

184208
color = source == formatted ? Color.gray(filepath) : filepath
@@ -194,63 +218,76 @@ def run(item)
194218
# The help message displayed if the input arguments are not correctly
195219
# ordered or formatted.
196220
HELP = <<~HELP
197-
#{Color.bold("stree ast [OPTIONS] [FILE]")}
221+
#{Color.bold("stree ast [--plugins=...] [--print-width=NUMBER] FILE")}
198222
Print out the AST corresponding to the given files
199223
200-
#{Color.bold("stree check [OPTIONS] [FILE]")}
224+
#{Color.bold("stree check [--plugins=...] [--print-width=NUMBER] FILE")}
201225
Check that the given files are formatted as syntax tree would format them
202226
203-
#{Color.bold("stree debug [OPTIONS] [FILE]")}
227+
#{Color.bold("stree debug [--plugins=...] [--print-width=NUMBER] FILE")}
204228
Check that the given files can be formatted idempotently
205229
206-
#{Color.bold("stree doc [OPTIONS] [FILE]")}
230+
#{Color.bold("stree doc [--plugins=...] FILE")}
207231
Print out the doc tree that would be used to format the given files
208232
209-
#{Color.bold("stree format [OPTIONS] [FILE]")}
233+
#{Color.bold("stree format [--plugins=...] [--print-width=NUMBER] FILE")}
210234
Print out the formatted version of the given files
211235
212-
#{Color.bold("stree json [OPTIONS] [FILE]")}
236+
#{Color.bold("stree json [--plugins=...] FILE")}
213237
Print out the JSON representation of the given files
214238
215-
#{Color.bold("stree match [OPTIONS] [FILE]")}
239+
#{Color.bold("stree match [--plugins=...] FILE")}
216240
Print out a pattern-matching Ruby expression that would match the given files
217241
218242
#{Color.bold("stree help")}
219243
Display this help message
220244
221-
#{Color.bold("stree lsp [OPTIONS]")}
245+
#{Color.bold("stree lsp [--plugins=...]")}
222246
Run syntax tree in language server mode
223247
224248
#{Color.bold("stree version")}
225249
Output the current version of syntax tree
226250
227-
#{Color.bold("stree write [OPTIONS] [FILE]")}
251+
#{Color.bold("stree write [--plugins=...] [--print-width=NUMBER] FILE")}
228252
Read, format, and write back the source of the given files
229253
230-
[OPTIONS]
231-
232254
--plugins=...
233255
A comma-separated list of plugins to load.
256+
257+
--print-width=NUMBER
258+
The maximum line width to use when formatting.
234259
HELP
235260

236261
class << self
237262
# Run the CLI over the given array of strings that make up the arguments
238263
# passed to the invocation.
239264
def run(argv)
240265
name, *arguments = argv
241-
242-
# If there are any plugins specified on the command line, then load them
243-
# by requiring them here. We do this by transforming something like
244-
#
245-
# stree format --plugins=haml template.haml
246-
#
247-
# into
248-
#
249-
# require "syntax_tree/haml"
250-
#
251-
if arguments.first&.start_with?("--plugins=")
252-
plugins = arguments.shift[/^--plugins=(.*)$/, 1]
253-
plugins.split(",").each { |plugin| require "syntax_tree/#{plugin}" }
266+
print_width = DEFAULT_PRINT_WIDTH
267+
268+
while arguments.first&.start_with?("--")
269+
case (argument = arguments.shift)
270+
when /^--plugins=(.+)$/
271+
# If there are any plugins specified on the command line, then load
272+
# them by requiring them here. We do this by transforming something
273+
# like
274+
#
275+
# stree format --plugins=haml template.haml
276+
#
277+
# into
278+
#
279+
# require "syntax_tree/haml"
280+
#
281+
$1.split(",").each { |plugin| require "syntax_tree/#{plugin}" }
282+
when /^--print-width=(\d+)$/
283+
# If there is a print width specified on the command line, then
284+
# parse that out here and use it when formatting.
285+
print_width = Integer($1)
286+
else
287+
warn("Unknown CLI option: #{argument}")
288+
warn(HELP)
289+
return 1
290+
end
254291
end
255292

256293
case name
@@ -271,19 +308,19 @@ def run(argv)
271308
when "a", "ast"
272309
AST.new
273310
when "c", "check"
274-
Check.new
311+
Check.new(print_width: print_width)
275312
when "debug"
276-
Debug.new
313+
Debug.new(print_width: print_width)
277314
when "doc"
278315
Doc.new
279316
when "j", "json"
280317
Json.new
281318
when "m", "match"
282319
Match.new
283320
when "f", "format"
284-
Format.new
321+
Format.new(print_width: print_width)
285322
when "w", "write"
286-
Write.new
323+
Write.new(print_width: print_width)
287324
else
288325
warn(HELP)
289326
return 1

test/cli_test.rb

+8
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ def test_check_unformatted
5252
assert_includes(result.stderr, "expected")
5353
end
5454

55+
def test_check_print_width
56+
file = Tempfile.new(%w[test- .rb])
57+
file.write("#{"a" * 40} + #{"b" * 40}\n")
58+
59+
result = run_cli("check", "--print-width=100", file: file)
60+
assert_includes(result.stdio, "match")
61+
end
62+
5563
def test_debug
5664
result = run_cli("debug")
5765
assert_includes(result.stdio, "idempotently")

0 commit comments

Comments
 (0)