Skip to content

Commit e361e96

Browse files
committed
Format with Syntax Tree
1 parent 9cc2e15 commit e361e96

16 files changed

+566
-246
lines changed

.rubocop.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ Style/GuardClause:
4949
Style/IdenticalConditionalBranches:
5050
Enabled: false
5151

52+
Style/IfInsideElse:
53+
Enabled: false
54+
5255
Style/KeywordParametersOrder:
5356
Enabled: false
5457

Rakefile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# frozen_string_literal: true
22

3-
require 'bundler/gem_tasks'
4-
require 'rake/testtask'
3+
require "bundler/gem_tasks"
4+
require "rake/testtask"
55

66
Rake::TestTask.new(:test) do |t|
7-
t.libs << 'test'
8-
t.libs << 'lib'
9-
t.test_files = FileList['test/**/*_test.rb']
7+
t.libs << "test"
8+
t.libs << "lib"
9+
t.test_files = FileList["test/**/*_test.rb"]
1010
end
1111

1212
task default: :test

lib/syntax_tree/cli.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,7 @@ def run(handler, filepath, source)
8383
warning = "[#{Color.yellow("warn")}] #{filepath}"
8484
formatted = handler.format(source)
8585

86-
if formatted != handler.format(formatted)
87-
raise NonIdempotentFormatError
88-
end
86+
raise NonIdempotentFormatError if formatted != handler.format(formatted)
8987
rescue StandardError
9088
warn(warning)
9189
raise
@@ -300,13 +298,15 @@ def run(argv)
300298
def each_file(arguments)
301299
if $stdin.tty?
302300
arguments.each do |pattern|
303-
Dir.glob(pattern).each do |filepath|
304-
next unless File.file?(filepath)
305-
306-
handler = HANDLERS[File.extname(filepath)]
307-
source = handler.read(filepath)
308-
yield handler, filepath, source
309-
end
301+
Dir
302+
.glob(pattern)
303+
.each do |filepath|
304+
next unless File.file?(filepath)
305+
306+
handler = HANDLERS[File.extname(filepath)]
307+
source = handler.read(filepath)
308+
yield handler, filepath, source
309+
end
310310
end
311311
else
312312
yield HANDLERS[".rb"], :stdin, $stdin.read

lib/syntax_tree/formatter.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,12 @@ def format(node, stackable: true)
4141

4242
# If the node has a stree-ignore comment right before it, then we're
4343
# going to just print out the node as it was seen in the source.
44-
if leading.last&.ignore?
45-
doc = text(source[node.location.start_char...node.location.end_char])
46-
else
47-
doc = node.format(self)
48-
end
44+
doc =
45+
if leading.last&.ignore?
46+
text(source[node.location.start_char...node.location.end_char])
47+
else
48+
node.format(self)
49+
end
4950

5051
# Print all comments that were found after the node.
5152
trailing.each do |comment|

lib/syntax_tree/language_server.rb

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,37 @@ def run
3434
in { method: "shutdown" }
3535
store.clear
3636
return
37-
in { method: "textDocument/didChange", params: { textDocument: { uri: }, contentChanges: [{ text: }, *] } }
37+
in {
38+
method: "textDocument/didChange",
39+
params: { textDocument: { uri: }, contentChanges: [{ text: }, *] }
40+
}
3841
store[uri] = text
39-
in { method: "textDocument/didOpen", params: { textDocument: { uri:, text: } } }
42+
in {
43+
method: "textDocument/didOpen",
44+
params: { textDocument: { uri:, text: } }
45+
}
4046
store[uri] = text
41-
in { method: "textDocument/didClose", params: { textDocument: { uri: } } }
47+
in {
48+
method: "textDocument/didClose", params: { textDocument: { uri: } }
49+
}
4250
store.delete(uri)
43-
in { method: "textDocument/formatting", id:, params: { textDocument: { uri: } } }
51+
in {
52+
method: "textDocument/formatting",
53+
id:,
54+
params: { textDocument: { uri: } }
55+
}
4456
write(id: id, result: [format(store[uri])])
45-
in { method: "textDocument/inlayHints", id:, params: { textDocument: { uri: } } }
57+
in {
58+
method: "textDocument/inlayHints",
59+
id:,
60+
params: { textDocument: { uri: } }
61+
}
4662
write(id: id, result: inlay_hints(store[uri]))
47-
in { method: "syntaxTree/visualizing", id:, params: { textDocument: { uri: } } }
63+
in {
64+
method: "syntaxTree/visualizing",
65+
id:,
66+
params: { textDocument: { uri: } }
67+
}
4868
output = []
4969
PP.pp(SyntaxTree.parse(store[uri]), output)
5070
write(id: id, result: output.join)
@@ -61,15 +81,24 @@ def run
6181
def capabilities
6282
{
6383
documentFormattingProvider: true,
64-
textDocumentSync: { change: 1, openClose: true }
84+
textDocumentSync: {
85+
change: 1,
86+
openClose: true
87+
}
6588
}
6689
end
6790

6891
def format(source)
6992
{
7093
range: {
71-
start: { line: 0, character: 0 },
72-
end: { line: source.lines.size + 1, character: 0 }
94+
start: {
95+
line: 0,
96+
character: 0
97+
},
98+
end: {
99+
line: source.lines.size + 1,
100+
character: 0
101+
}
73102
},
74103
newText: SyntaxTree.format(source)
75104
}

0 commit comments

Comments
 (0)