Skip to content

Add inlay hint support per LSP specification. #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 21 additions & 6 deletions lib/syntax_tree/language_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,19 @@ def run
}
write(id: id, result: [format(store[uri])])
in {
# official RPC in LSP spec 3.17
method: "textDocument/inlayHint",
id:,
params: { textDocument: { uri: } }
}
write(id: id, result: inlay_hints(store[uri], false))
in {
# proprietary RPC (deprecated) between this gem and vscode-syntax-tree
method: "textDocument/inlayHints",
id:,
params: { textDocument: { uri: } }
}
write(id: id, result: inlay_hints(store[uri]))
write(id: id, result: inlay_hints(store[uri], true))
in {
method: "syntaxTree/visualizing",
id:,
Expand All @@ -85,6 +93,9 @@ def run
def capabilities
{
documentFormattingProvider: true,
inlayHintProvider: {
resolveProvider: false
},
textDocumentSync: {
change: 1,
openClose: true
Expand All @@ -108,14 +119,18 @@ def format(source)
}
end

def inlay_hints(source)
def inlay_hints(source, proprietary)
inlay_hints = InlayHints.find(SyntaxTree.parse(source))
serialize = ->(position, text) { { position: position, text: text } }

{
before: inlay_hints.before.map(&serialize),
after: inlay_hints.after.map(&serialize)
}
if proprietary
{
before: inlay_hints.before.map(&serialize),
after: inlay_hints.after.map(&serialize)
}
else
inlay_hints.all
end
rescue Parser::ParseError
# If there is a parse error, then we're not going to return any inlay
# hints for this source.
Expand Down
30 changes: 26 additions & 4 deletions lib/syntax_tree/language_server/inlay_hints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

module SyntaxTree
class LanguageServer
# This class provides inlay hints for the language server. It is loosely
# designed around the LSP spec, but existed before the spec was finalized so
# is a little different for now.
# This class provides inlay hints for the language server. It existed
# before the spec was finalized so, so it provides two result formats:
# aligned with the spec (`#all`) and proprietary (`#before` and `#after`).
#
# For more information, see the spec here:
# https://github.com/microsoft/language-server-protocol/issues/956.
#
class InlayHints < Visitor
attr_reader :stack, :before, :after
attr_reader :stack, :all, :before, :after

def initialize
@stack = []
@all = []
@before = Hash.new { |hash, key| hash[key] = +"" }
@after = Hash.new { |hash, key| hash[key] = +"" }
end
Expand Down Expand Up @@ -98,6 +99,13 @@ def visit_if_op(node)
def visit_rescue(node)
if node.exception.nil?
after[node.location.start_char + "rescue".length] << " StandardError"
all << {
position: {
line: node.location.start_line - 1,
character: node.location.start_column + "rescue".length
},
label: " StandardError"
}
end

super
Expand Down Expand Up @@ -129,6 +137,20 @@ def self.find(program)
private

def parentheses(location)
all << {
position: {
line: location.start_line - 1,
character: location.start_column
},
label: "₍"
}
all << {
position: {
line: location.end_line - 1,
character: location.end_column
},
label: "₎"
}
before[location.start_char] << "₍"
after[location.end_char] << "₎"
end
Expand Down
6 changes: 6 additions & 0 deletions test/language_server/inlay_hints_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,46 @@ def test_assignments_in_parameters

assert_equal(1, hints.before.length)
assert_equal(1, hints.after.length)
assert_equal(2, hints.all.length)
end

def test_operators_in_binaries
hints = find("1 + 2 * 3")

assert_equal(1, hints.before.length)
assert_equal(1, hints.after.length)
assert_equal(2, hints.all.length)
end

def test_binaries_in_assignments
hints = find("a = 1 + 2")

assert_equal(1, hints.before.length)
assert_equal(1, hints.after.length)
assert_equal(2, hints.all.length)
end

def test_nested_ternaries
hints = find("a ? b : c ? d : e")

assert_equal(1, hints.before.length)
assert_equal(1, hints.after.length)
assert_equal(2, hints.all.length)
end

def test_bare_rescue
hints = find("begin; rescue; end")

assert_equal(1, hints.after.length)
assert_equal(1, hints.all.length)
end

def test_unary_in_binary
hints = find("-a + b")

assert_equal(1, hints.before.length)
assert_equal(1, hints.after.length)
assert_equal(2, hints.all.length)
end

private
Expand Down