Skip to content

Update Hover and CodeLens as per refactored responses in Ruby LSP #253

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
Feb 13, 2024
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
6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ PATH
actionpack (>= 6.0)
activerecord (>= 6.0)
railties (>= 6.0)
ruby-lsp (>= 0.13.0, < 0.14.0)
ruby-lsp (>= 0.14.0, < 0.15.0)
sorbet-runtime (>= 0.5.9897)

GEM
Expand Down Expand Up @@ -221,9 +221,9 @@ GEM
rubocop (~> 1.51)
rubocop-sorbet (0.7.6)
rubocop (>= 0.90.0)
ruby-lsp (0.13.4)
ruby-lsp (0.14.0)
language_server-protocol (~> 3.17.0)
prism (>= 0.19.0, < 0.20)
prism (>= 0.19.0, < 0.22)
sorbet-runtime (>= 0.5.10782)
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5)
Expand Down
14 changes: 8 additions & 6 deletions lib/ruby_lsp/ruby_lsp_rails/addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,25 @@ def deactivate; end
# Creates a new CodeLens listener. This method is invoked on every CodeLens request
sig do
override.params(
response_builder: ResponseBuilders::CollectionResponseBuilder[Interface::CodeLens],
uri: URI::Generic,
dispatcher: Prism::Dispatcher,
).returns(T.nilable(Listener[T::Array[Interface::CodeLens]]))
).void
end
def create_code_lens_listener(uri, dispatcher)
CodeLens.new(uri, dispatcher)
def create_code_lens_listener(response_builder, uri, dispatcher)
CodeLens.new(response_builder, uri, dispatcher)
end

sig do
override.params(
response_builder: ResponseBuilders::Hover,
nesting: T::Array[String],
index: RubyIndexer::Index,
dispatcher: Prism::Dispatcher,
).returns(T.nilable(Listener[T.nilable(Interface::Hover)]))
).void
end
def create_hover_listener(nesting, index, dispatcher)
Hover.new(client, nesting, index, dispatcher)
def create_hover_listener(response_builder, nesting, index, dispatcher)
Hover.new(client, response_builder, nesting, index, dispatcher)
end

sig { override.returns(String) }
Expand Down
28 changes: 14 additions & 14 deletions lib/ruby_lsp/ruby_lsp_rails/code_lens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,26 @@ module Rails
# ````
#
# The code lenses will be displayed above the class and above each test method.
class CodeLens < ::RubyLsp::Listener
class CodeLens
extend T::Sig
extend T::Generic
include Requests::Support::Common

ResponseType = type_member { { fixed: T::Array[::RubyLsp::Interface::CodeLens] } }
BASE_COMMAND = "bin/rails test"

sig { override.returns(ResponseType) }
attr_reader :_response

sig { params(uri: URI::Generic, dispatcher: Prism::Dispatcher).void }
def initialize(uri, dispatcher)
@_response = T.let([], ResponseType)
sig do
params(
response_builder: ResponseBuilders::CollectionResponseBuilder[Interface::CodeLens],
uri: URI::Generic,
dispatcher: Prism::Dispatcher,
).void
end
def initialize(response_builder, uri, dispatcher)
@response_builder = response_builder
@path = T.let(uri.to_standardized_path, T.nilable(String))
@group_id = T.let(1, Integer)
@group_id_stack = T.let([], T::Array[Integer])

dispatcher.register(self, :on_call_node_enter, :on_class_node_enter, :on_def_node_enter, :on_class_node_leave)

super(dispatcher)
end

sig { params(node: Prism::CallNode).void }
Expand Down Expand Up @@ -131,23 +131,23 @@ def add_test_code_lens(node, name:, command:, kind:)
grouping_data = { group_id: @group_id_stack.last, kind: kind }
grouping_data[:id] = @group_id if kind == :group

@_response << create_code_lens(
@response_builder << create_code_lens(
node,
title: "Run",
command_name: "rubyLsp.runTest",
arguments: arguments,
data: { type: "test", **grouping_data },
)

@_response << create_code_lens(
@response_builder << create_code_lens(
node,
title: "Run In Terminal",
command_name: "rubyLsp.runTestInTerminal",
arguments: arguments,
data: { type: "test_in_terminal", **grouping_data },
)

@_response << create_code_lens(
@response_builder << create_code_lens(
node,
title: "Debug",
command_name: "rubyLsp.debugTest",
Expand Down
57 changes: 24 additions & 33 deletions lib/ruby_lsp/ruby_lsp_rails/hover.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,22 @@ module Rails
# User.all
# # ^ hovering here will show information about the User model
# ```
class Hover < ::RubyLsp::Listener
class Hover
extend T::Sig
extend T::Generic

ResponseType = type_member { { fixed: T.nilable(::RubyLsp::Interface::Hover) } }

sig { override.returns(ResponseType) }
attr_reader :_response
include Requests::Support::Common

sig do
params(
client: RailsClient,
response_builder: ResponseBuilders::Hover,
nesting: T::Array[String],
index: RubyIndexer::Index,
dispatcher: Prism::Dispatcher,
).void
end
def initialize(client, nesting, index, dispatcher)
super(dispatcher)

@_response = T.let(nil, ResponseType)
def initialize(client, response_builder, nesting, index, dispatcher)
@client = client
@response_builder = response_builder
@nesting = nesting
@index = index
dispatcher.register(self, :on_constant_path_node_enter, :on_constant_read_node_enter, :on_call_node_enter)
Expand All @@ -49,28 +43,18 @@ def on_constant_path_node_enter(node)
return unless entries

name = T.must(entries.first).name
content = +""
column_info = generate_column_content(name)
content << column_info if column_info

urls = Support::RailsDocumentClient.generate_rails_document_urls(name)
content << urls.join("\n\n") unless urls.empty?
return if content.empty?
generate_column_content(name)

contents = RubyLsp::Interface::MarkupContent.new(kind: "markdown", value: content)
@_response = RubyLsp::Interface::Hover.new(range: range_from_location(node.location), contents: contents)
generate_rails_document_link_hover(name, node.location)
end

sig { params(node: Prism::ConstantReadNode).void }
def on_constant_read_node_enter(node)
entries = @index.resolve(node.name.to_s, @nesting)
return unless entries

content = generate_column_content(T.must(entries.first).name)
return unless content

contents = RubyLsp::Interface::MarkupContent.new(kind: "markdown", value: content)
@_response = RubyLsp::Interface::Hover.new(range: range_from_location(node.location), contents: contents)
generate_column_content(T.must(entries.first).name)
end

sig { params(node: Prism::CallNode).void }
Expand All @@ -80,30 +64,37 @@ def on_call_node_enter(node)

return unless message_value && message_loc

@_response = generate_rails_document_link_hover(message_value, message_loc)
generate_rails_document_link_hover(message_value, message_loc)
end

private

sig { params(name: String).returns(T.nilable(String)) }
sig { params(name: String).void }
def generate_column_content(name)
model = @client.model(name)
return if model.nil?

schema_file = model[:schema_file]
content = +""
content << "[Schema](#{URI::Generic.build(scheme: "file", path: schema_file)})\n\n" if schema_file
content << model[:columns].map { |name, type| "**#{name}**: #{type}\n" }.join("\n")
content

@response_builder.push(
"[Schema](#{URI::Generic.build(scheme: "file", path: schema_file)})",
category: :links,
) if schema_file

@response_builder.push(
model[:columns].map do |name, type|
"**#{name}**: #{type}\n"
end.join("\n"),
category: :documentation,
)
end

sig { params(name: String, location: Prism::Location).returns(T.nilable(Interface::Hover)) }
sig { params(name: String, location: Prism::Location).void }
def generate_rails_document_link_hover(name, location)
urls = Support::RailsDocumentClient.generate_rails_document_urls(name)
return if urls.empty?

contents = RubyLsp::Interface::MarkupContent.new(kind: "markdown", value: urls.join("\n\n"))
RubyLsp::Interface::Hover.new(range: range_from_location(location), contents: contents)
@response_builder.push(urls.join("\n\n"), category: :links)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion ruby-lsp-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ Gem::Specification.new do |spec|
spec.add_dependency("actionpack", ">= 6.0")
spec.add_dependency("activerecord", ">= 6.0")
spec.add_dependency("railties", ">= 6.0")
spec.add_dependency("ruby-lsp", ">= 0.13.0", "< 0.14.0")
spec.add_dependency("ruby-lsp", ">= 0.14.0", "< 0.15.0")
spec.add_dependency("sorbet-runtime", ">= 0.5.9897")
end
Loading