From 143c0ce5f7b83f81d1d109f168a9111ac7730c12 Mon Sep 17 00:00:00 2001 From: Haider Date: Wed, 7 Feb 2024 08:42:51 +0500 Subject: [PATCH 1/2] node.slice replaced with node.full_name --- lib/ruby_lsp/listeners/completion.rb | 4 ++-- lib/ruby_lsp/listeners/definition.rb | 4 ++-- lib/ruby_lsp/listeners/hover.rb | 4 ++-- lib/ruby_lsp/requests/support/common.rb | 7 +++++++ 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/ruby_lsp/listeners/completion.rb b/lib/ruby_lsp/listeners/completion.rb index 260d7046a..fb445ded3 100644 --- a/lib/ruby_lsp/listeners/completion.rb +++ b/lib/ruby_lsp/listeners/completion.rb @@ -43,7 +43,7 @@ def on_string_node_enter(node) def on_constant_read_node_enter(node) return if DependencyDetector.instance.typechecker - name = node.slice + name = constant_name(node) candidates = @index.prefix_search(name, @nesting) candidates.each do |entries| complete_name = T.must(entries.first).name @@ -62,7 +62,7 @@ def on_constant_read_node_enter(node) def on_constant_path_node_enter(node) return if DependencyDetector.instance.typechecker - name = node.slice + name = constant_name(node) top_level_reference = if name.start_with?("::") name = name.delete_prefix("::") diff --git a/lib/ruby_lsp/listeners/definition.rb b/lib/ruby_lsp/listeners/definition.rb index e0f1b1374..c1d4f3df7 100644 --- a/lib/ruby_lsp/listeners/definition.rb +++ b/lib/ruby_lsp/listeners/definition.rb @@ -45,12 +45,12 @@ def on_call_node_enter(node) sig { params(node: Prism::ConstantPathNode).void } def on_constant_path_node_enter(node) - find_in_index(node.slice) + find_in_index(constant_name(node)) end sig { params(node: Prism::ConstantReadNode).void } def on_constant_read_node_enter(node) - find_in_index(node.slice) + find_in_index(constant_name(node)) end private diff --git a/lib/ruby_lsp/listeners/hover.rb b/lib/ruby_lsp/listeners/hover.rb index 10efba331..063cf861c 100644 --- a/lib/ruby_lsp/listeners/hover.rb +++ b/lib/ruby_lsp/listeners/hover.rb @@ -55,7 +55,7 @@ def initialize(response_builder, uri, nesting, index, dispatcher, typechecker_en def on_constant_read_node_enter(node) return if @typechecker_enabled - generate_hover(node.slice, node.location) + generate_hover(constant_name(node), node.location) end sig { params(node: Prism::ConstantWriteNode).void } @@ -69,7 +69,7 @@ def on_constant_write_node_enter(node) def on_constant_path_node_enter(node) return if DependencyDetector.instance.typechecker - generate_hover(node.slice, node.location) + generate_hover(constant_name(node), node.location) end sig { params(node: Prism::CallNode).void } diff --git a/lib/ruby_lsp/requests/support/common.rb b/lib/ruby_lsp/requests/support/common.rb index 7f37c741c..aa33ed90d 100644 --- a/lib/ruby_lsp/requests/support/common.rb +++ b/lib/ruby_lsp/requests/support/common.rb @@ -116,6 +116,13 @@ def markdown_from_index_entries(title, entries) #{content} MARKDOWN end + + sig { params(node: Prism::Node).returns(T.nilable(String)) } + def constant_name(node) + node.full_name + rescue Prism::ConstantPathNode::DynamicPartsInConstantPathError + nil + end end end end From 835505e75b14113151356c33983226d633005bc2 Mon Sep 17 00:00:00 2001 From: Haider Date: Fri, 9 Feb 2024 23:32:26 +0500 Subject: [PATCH 2/2] type errors resolved --- lib/ruby_lsp/listeners/completion.rb | 3 +++ lib/ruby_lsp/listeners/definition.rb | 10 ++++++++-- lib/ruby_lsp/listeners/hover.rb | 10 ++++++++-- lib/ruby_lsp/requests/support/common.rb | 10 +++++++++- 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/lib/ruby_lsp/listeners/completion.rb b/lib/ruby_lsp/listeners/completion.rb index fb445ded3..f98bee511 100644 --- a/lib/ruby_lsp/listeners/completion.rb +++ b/lib/ruby_lsp/listeners/completion.rb @@ -44,6 +44,8 @@ def on_constant_read_node_enter(node) return if DependencyDetector.instance.typechecker name = constant_name(node) + return if name.nil? + candidates = @index.prefix_search(name, @nesting) candidates.each do |entries| complete_name = T.must(entries.first).name @@ -63,6 +65,7 @@ def on_constant_path_node_enter(node) return if DependencyDetector.instance.typechecker name = constant_name(node) + return if name.nil? top_level_reference = if name.start_with?("::") name = name.delete_prefix("::") diff --git a/lib/ruby_lsp/listeners/definition.rb b/lib/ruby_lsp/listeners/definition.rb index c1d4f3df7..4e1aeec6c 100644 --- a/lib/ruby_lsp/listeners/definition.rb +++ b/lib/ruby_lsp/listeners/definition.rb @@ -45,12 +45,18 @@ def on_call_node_enter(node) sig { params(node: Prism::ConstantPathNode).void } def on_constant_path_node_enter(node) - find_in_index(constant_name(node)) + name = constant_name(node) + return if name.nil? + + find_in_index(name) end sig { params(node: Prism::ConstantReadNode).void } def on_constant_read_node_enter(node) - find_in_index(constant_name(node)) + name = constant_name(node) + return if name.nil? + + find_in_index(name) end private diff --git a/lib/ruby_lsp/listeners/hover.rb b/lib/ruby_lsp/listeners/hover.rb index 063cf861c..708be1275 100644 --- a/lib/ruby_lsp/listeners/hover.rb +++ b/lib/ruby_lsp/listeners/hover.rb @@ -55,7 +55,10 @@ def initialize(response_builder, uri, nesting, index, dispatcher, typechecker_en def on_constant_read_node_enter(node) return if @typechecker_enabled - generate_hover(constant_name(node), node.location) + name = constant_name(node) + return if name.nil? + + generate_hover(name, node.location) end sig { params(node: Prism::ConstantWriteNode).void } @@ -69,7 +72,10 @@ def on_constant_write_node_enter(node) def on_constant_path_node_enter(node) return if DependencyDetector.instance.typechecker - generate_hover(constant_name(node), node.location) + name = constant_name(node) + return if name.nil? + + generate_hover(name, node.location) end sig { params(node: Prism::CallNode).void } diff --git a/lib/ruby_lsp/requests/support/common.rb b/lib/ruby_lsp/requests/support/common.rb index aa33ed90d..bf8e6e4e4 100644 --- a/lib/ruby_lsp/requests/support/common.rb +++ b/lib/ruby_lsp/requests/support/common.rb @@ -117,7 +117,15 @@ def markdown_from_index_entries(title, entries) MARKDOWN end - sig { params(node: Prism::Node).returns(T.nilable(String)) } + sig do + params( + node: T.any( + Prism::ConstantPathNode, + Prism::ConstantReadNode, + Prism::ConstantPathTargetNode, + ), + ).returns(T.nilable(String)) + end def constant_name(node) node.full_name rescue Prism::ConstantPathNode::DynamicPartsInConstantPathError