Skip to content

Commit af53edb

Browse files
committed
Remove inlay hints
1 parent 8518688 commit af53edb

File tree

4 files changed

+2
-295
lines changed

4 files changed

+2
-295
lines changed

README.md

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ It is built with only standard library dependencies. It additionally ships with
2020
- [Globbing](#globbing)
2121
- [Language server](#language-server)
2222
- [textDocument/formatting](#textdocumentformatting)
23-
- [textDocument/inlayHint](#textdocumentinlayhint)
2423
- [Customization](#customization)
2524
- [Ignoring code](#ignoring-code)
2625
- [Plugins](#plugins)
@@ -176,31 +175,13 @@ stree write "**/{[!schema]*,*}.rb"
176175

177176
## Language server
178177

179-
Syntax Tree additionally ships with a language server conforming to the [language server protocol](https://microsoft.github.io/language-server-protocol/). It can be invoked through the CLI by running:
178+
Syntax Tree additionally ships with a minimal language server conforming to the [language server protocol](https://microsoft.github.io/language-server-protocol/) that registers a formatter for the Ruby language. It can be invoked through the CLI by running:
180179

181180
```sh
182181
stree lsp
183182
```
184183

185-
By default, the language server is relatively minimal, mostly meant to provide a registered formatter for the Ruby language. However there are a couple of additional niceties baked in. There are related projects that configure and use this language server within IDEs. For example, to use this code with VSCode, see [ruby-syntax-tree/vscode-syntax-tree](https://github.com/ruby-syntax-tree/vscode-syntax-tree).
186-
187-
### textDocument/formatting
188-
189-
As mentioned above, the language server responds to formatting requests with the formatted document. It typically responds on the order of tens of milliseconds, so it should be fast enough for any IDE.
190-
191-
### textDocument/inlayHint
192-
193-
The language server also responds to the relatively new inlay hints request. This request allows the language server to define additional information that should exist in the source code as helpful hints to the developer. In our case we use it to display things like implicit parentheses. For example, if you had the following code:
194-
195-
```ruby
196-
1 + 2 * 3
197-
```
198-
199-
Implicitly, the `2 * 3` is going to be executed first because the `*` operator has higher precedence than the `+` operator. To ease mental overhead, our language server includes small parentheses to make this explicit, as in:
200-
201-
```ruby
202-
1 +2 * 3
203-
```
184+
There are related projects that configure and use this language server within IDEs. For example, to use this code with VSCode, see [ruby-syntax-tree/vscode-syntax-tree](https://github.com/ruby-syntax-tree/vscode-syntax-tree).
204185

205186
## Customization
206187

lib/syntax_tree/language_server.rb

Lines changed: 0 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -12,162 +12,6 @@ module SyntaxTree
1212
# stree lsp
1313
#
1414
class LanguageServer
15-
# This class provides inlay hints for the language server. For more
16-
# information, see the spec here:
17-
# https://github.com/microsoft/language-server-protocol/issues/956.
18-
class InlayHints < Visitor
19-
# This represents a hint that is going to be displayed in the editor.
20-
class Hint
21-
attr_reader :line, :character, :label
22-
23-
def initialize(line:, character:, label:)
24-
@line = line
25-
@character = character
26-
@label = label
27-
end
28-
29-
# This is the shape that the LSP expects.
30-
def to_json(*opts)
31-
{
32-
position: {
33-
line: line,
34-
character: character
35-
},
36-
label: label
37-
}.to_json(*opts)
38-
end
39-
end
40-
41-
attr_reader :stack, :hints
42-
43-
def initialize
44-
@stack = []
45-
@hints = []
46-
end
47-
48-
def visit(node)
49-
stack << node
50-
result = super
51-
stack.pop
52-
result
53-
end
54-
55-
visit_methods do
56-
# Adds parentheses around assignments contained within the default
57-
# values of parameters. For example,
58-
#
59-
# def foo(a = b = c)
60-
# end
61-
#
62-
# becomes
63-
#
64-
# def foo(a = ₍b = c₎)
65-
# end
66-
#
67-
def visit_assign(node)
68-
parentheses(node.location) if stack[-2].is_a?(Params)
69-
super
70-
end
71-
72-
# Adds parentheses around binary expressions to make it clear which
73-
# subexpression will be evaluated first. For example,
74-
#
75-
# a + b * c
76-
#
77-
# becomes
78-
#
79-
# a + ₍b * c₎
80-
#
81-
def visit_binary(node)
82-
case stack[-2]
83-
when Assign, OpAssign
84-
parentheses(node.location)
85-
when Binary
86-
parentheses(node.location) if stack[-2].operator != node.operator
87-
end
88-
89-
super
90-
end
91-
92-
# Adds parentheses around ternary operators contained within certain
93-
# expressions where it could be confusing which subexpression will get
94-
# evaluated first. For example,
95-
#
96-
# a ? b : c ? d : e
97-
#
98-
# becomes
99-
#
100-
# a ? b : ₍c ? d : e₎
101-
#
102-
def visit_if_op(node)
103-
case stack[-2]
104-
when Assign, Binary, IfOp, OpAssign
105-
parentheses(node.location)
106-
end
107-
108-
super
109-
end
110-
111-
# Adds the implicitly rescued StandardError into a bare rescue clause.
112-
# For example,
113-
#
114-
# begin
115-
# rescue
116-
# end
117-
#
118-
# becomes
119-
#
120-
# begin
121-
# rescue StandardError
122-
# end
123-
#
124-
def visit_rescue(node)
125-
if node.exception.nil?
126-
hints << Hint.new(
127-
line: node.location.start_line - 1,
128-
character: node.location.start_column + "rescue".length,
129-
label: " StandardError"
130-
)
131-
end
132-
133-
super
134-
end
135-
136-
# Adds parentheses around unary statements using the - operator that are
137-
# contained within Binary nodes. For example,
138-
#
139-
# -a + b
140-
#
141-
# becomes
142-
#
143-
# ₍-a₎ + b
144-
#
145-
def visit_unary(node)
146-
if stack[-2].is_a?(Binary) && (node.operator == "-")
147-
parentheses(node.location)
148-
end
149-
150-
super
151-
end
152-
end
153-
154-
private
155-
156-
def parentheses(location)
157-
hints << Hint.new(
158-
line: location.start_line - 1,
159-
character: location.start_column,
160-
label: "₍"
161-
)
162-
163-
hints << Hint.new(
164-
line: location.end_line - 1,
165-
character: location.end_column,
166-
label: "₎"
167-
)
168-
end
169-
end
170-
17115
# This is a small module that effectively mirrors pattern matching. We're
17216
# using it so that we can support truffleruby without having to ignore the
17317
# language server.
@@ -263,10 +107,6 @@ def run
263107
end
264108
contents = store[uri]
265109
write(id: request[:id], result: contents && !ignore ? format(contents, uri.split(".").last) : nil)
266-
when Request[method: "textDocument/inlayHint", id: :any, params: { textDocument: { uri: :any } }]
267-
uri = request.dig(:params, :textDocument, :uri)
268-
contents = store[uri]
269-
write(id: request[:id], result: contents ? inlay_hints(contents) : nil)
270110
when Request[method: %r{\$/.+}]
271111
# ignored
272112
when Request[method: "textDocument/documentColor", params: { textDocument: { uri: :any } }]
@@ -283,9 +123,6 @@ def run
283123
def capabilities
284124
{
285125
documentFormattingProvider: true,
286-
inlayHintProvider: {
287-
resolveProvider: false
288-
},
289126
textDocumentSync: {
290127
change: 1,
291128
openClose: true
@@ -317,16 +154,6 @@ def format(source, extension)
317154
nil
318155
end
319156

320-
def inlay_hints(source)
321-
visitor = InlayHints.new
322-
SyntaxTree.parse(source).accept(visitor)
323-
visitor.hints
324-
rescue Parser::ParseError
325-
# If there is a parse error, then we're not going to return any inlay
326-
# hints for this source.
327-
[]
328-
end
329-
330157
def write(value)
331158
response = value.merge(jsonrpc: "2.0").to_json
332159
output.print("Content-Length: #{response.bytesize}\r\n\r\n#{response}")

test/language_server/inlay_hints_test.rb

Lines changed: 0 additions & 43 deletions
This file was deleted.

test/language_server_test.rb

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -97,23 +97,6 @@ def to_hash
9797
end
9898
end
9999

100-
class TextDocumentInlayHint
101-
attr_reader :id, :uri
102-
103-
def initialize(id, uri)
104-
@id = id
105-
@uri = uri
106-
end
107-
108-
def to_hash
109-
{
110-
method: "textDocument/inlayHint",
111-
id: id,
112-
params: { textDocument: { uri: uri } }
113-
}
114-
end
115-
end
116-
117100
def test_formatting
118101
responses = run_server([
119102
Initialize.new(1),
@@ -190,47 +173,6 @@ def test_formatting_print_width
190173
assert_equal(contents, responses.dig(1, :result, 0, :newText))
191174
end
192175

193-
def test_inlay_hint
194-
responses = run_server([
195-
Initialize.new(1),
196-
TextDocumentDidOpen.new("file:///path/to/file.rb", <<~RUBY),
197-
begin
198-
1 + 2 * 3
199-
rescue
200-
end
201-
RUBY
202-
TextDocumentInlayHint.new(2, "file:///path/to/file.rb"),
203-
Shutdown.new(3)
204-
])
205-
206-
shape = LanguageServer::Request[[
207-
{ id: 1, result: { capabilities: Hash } },
208-
{ id: 2, result: :any },
209-
{ id: 3, result: {} }
210-
]]
211-
212-
assert_operator(shape, :===, responses)
213-
assert_equal(3, responses.dig(1, :result).size)
214-
end
215-
216-
def test_inlay_hint_invalid
217-
responses = run_server([
218-
Initialize.new(1),
219-
TextDocumentDidOpen.new("file:///path/to/file.rb", "<>"),
220-
TextDocumentInlayHint.new(2, "file:///path/to/file.rb"),
221-
Shutdown.new(3)
222-
])
223-
224-
shape = LanguageServer::Request[[
225-
{ id: 1, result: { capabilities: Hash } },
226-
{ id: 2, result: :any },
227-
{ id: 3, result: {} }
228-
]]
229-
230-
assert_operator(shape, :===, responses)
231-
assert_equal(0, responses.dig(1, :result).size)
232-
end
233-
234176
def test_reading_file
235177
Tempfile.open(%w[test- .rb]) do |file|
236178
file.write("class Foo; end")

0 commit comments

Comments
 (0)