-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for completionItem/resolve (#1798)
* Add support for completionItem/resolve This dramatically improves editor performance in certain situations by omitting details and documentation from completion results. Instead, the documentation and details are provided when the client resolves the selected completion item. * Add some documentation
- Loading branch information
Showing
7 changed files
with
118 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
module RubyLsp | ||
module Requests | ||
# The [completionItem/resolve](https://microsoft.github.io/language-server-protocol/specification#completionItem_resolve) | ||
# request provides additional information about the currently selected completion. Specifically, the `labelDetails` | ||
# and `documentation` fields are provided, which are omitted from the completion items returned by | ||
# `textDocument/completion`. | ||
# | ||
# The `labelDetails` field lists the files where the completion item is defined, and the `documentation` field | ||
# includes any available documentation for those definitions. | ||
# | ||
# At most 10 definitions are included, to ensure low latency during request processing and rendering the completion | ||
# item. | ||
class CompletionResolve < Request | ||
extend T::Sig | ||
include Requests::Support::Common | ||
|
||
# set a limit on the number of documentation entries returned, to avoid rendering performance issues | ||
# https://github.com/Shopify/ruby-lsp/pull/1798 | ||
MAX_DOCUMENTATION_ENTRIES = 10 | ||
|
||
sig { params(index: RubyIndexer::Index, item: T::Hash[Symbol, T.untyped]).void } | ||
def initialize(index, item) | ||
super() | ||
@index = index | ||
@item = item | ||
end | ||
|
||
sig { override.returns(Interface::CompletionItem) } | ||
def perform | ||
label = @item[:label] | ||
entries = @index[label] || [] | ||
Interface::CompletionItem.new( | ||
label: label, | ||
label_details: Interface::CompletionItemLabelDetails.new( | ||
description: entries.take(MAX_DOCUMENTATION_ENTRIES).map(&:file_name).join(","), | ||
), | ||
documentation: Interface::MarkupContent.new( | ||
kind: "markdown", | ||
value: markdown_from_index_entries(label, entries, MAX_DOCUMENTATION_ENTRIES), | ||
), | ||
) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# typed: true | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class CompletionResolveTest < Minitest::Test | ||
include RubyLsp::Requests::Support::Common | ||
|
||
Interface = LanguageServer::Protocol::Interface | ||
Constant = LanguageServer::Protocol::Constant | ||
|
||
def test_completion_resolve_for_constant | ||
stub_no_typechecker | ||
source = +<<~RUBY | ||
# This is a class that does things | ||
class Foo | ||
end | ||
RUBY | ||
|
||
with_server(source) do |server, uri| | ||
server.process_message(id: 1, method: "completionItem/resolve", params: { | ||
label: "Foo", | ||
}) | ||
|
||
result = server.pop_response.response | ||
|
||
expected = Interface::CompletionItem.new( | ||
label: "Foo", | ||
label_details: Interface::CompletionItemLabelDetails.new( | ||
description: "fake.rb", | ||
), | ||
documentation: Interface::MarkupContent.new( | ||
kind: "markdown", | ||
value: markdown_from_index_entries("Foo", T.must(server.index["Foo"])), | ||
), | ||
) | ||
assert_match(/This is a class that does things/, result.documentation.value) | ||
assert_equal(expected.to_json, result.to_json) | ||
end | ||
end | ||
end |