From c5847a71c08043cb3c4d86dcb6a6c474e2244588 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Sun, 2 Jul 2023 13:36:34 +0900 Subject: [PATCH 1/3] signature helps are not shown if the target code has comments SignatureHelpProvider fails to detect the cursor position correctly because it uses source code from AST node instead of original source code. Therefore it misdetects the cursor position if the source code contains comments. As a result, it does not show signature helps it the source code contains comments. --- lib/steep/server/interaction_worker.rb | 2 +- lib/steep/services/signature_help_provider.rb | 9 ++------- sig/steep/services/signature_help_provider.rbs | 2 +- test/signature_help_provider_test.rb | 10 ++++++---- 4 files changed, 10 insertions(+), 13 deletions(-) diff --git a/lib/steep/server/interaction_worker.rb b/lib/steep/server/interaction_worker.rb index 3e38effa3..fb003c0c9 100644 --- a/lib/steep/server/interaction_worker.rb +++ b/lib/steep/server/interaction_worker.rb @@ -391,7 +391,7 @@ def process_signature_help(job) subtyping = service.signature_services[target.name].current_subtyping or return source = Source.parse(file.content, path: file.path, factory: subtyping.factory) - provider = Services::SignatureHelpProvider.new(source: source, subtyping: subtyping) + provider = Services::SignatureHelpProvider.new(source: source, content: file.content, subtyping: subtyping) if (items, index = provider.run(line: job.line, column: job.column)) signatures = items.map do |item| diff --git a/lib/steep/services/signature_help_provider.rb b/lib/steep/services/signature_help_provider.rb index 515a4c985..89c5224a4 100644 --- a/lib/steep/services/signature_help_provider.rb +++ b/lib/steep/services/signature_help_provider.rb @@ -11,15 +11,10 @@ def env subtyping.factory.env end - def initialize(source:, subtyping:) + def initialize(source:, content:, subtyping:) @source = source @subtyping = subtyping - - text = - if source.node - source.node.loc.expression.source - end - @buffer = RBS::Buffer.new(name: source.path, content: text || "") + @buffer = RBS::Buffer.new(name: source.path, content: content) end def run(line:, column:) diff --git a/sig/steep/services/signature_help_provider.rbs b/sig/steep/services/signature_help_provider.rbs index 9f9201024..5822f36fb 100644 --- a/sig/steep/services/signature_help_provider.rbs +++ b/sig/steep/services/signature_help_provider.rbs @@ -23,7 +23,7 @@ module Steep def env: () -> Environment - def initialize: (source: Source, subtyping: Subtyping::Check) -> void + def initialize: (source: Source, content: String, subtyping: Subtyping::Check) -> void # 1-origin line, 0-origin column # diff --git a/test/signature_help_provider_test.rb b/test/signature_help_provider_test.rb index 22d0df66a..03255fa2e 100644 --- a/test/signature_help_provider_test.rb +++ b/test/signature_help_provider_test.rb @@ -14,11 +14,12 @@ class TestClass def self.foo: (String, Integer) -> Array[Symbol] end RBS - source = Source.parse(<<~RUBY, path: Pathname("a.rb"), factory: checker.factory) + content = <<~RUBY TestClass.foo() RUBY + source = Source.parse(content, path: Pathname("a.rb"), factory: checker.factory) - SignatureHelpProvider.new(source: source, subtyping: checker).tap do |provider| + SignatureHelpProvider.new(source: source, content: content, subtyping: checker).tap do |provider| items, index = provider.run(line: 1, column: 14) assert_nil index @@ -34,11 +35,12 @@ def self.foo: (String, Integer) -> Array[Symbol] | () -> Array[Symbol] end RBS - source = Source.parse(<<~RUBY, path: Pathname("a.rb"), factory: checker.factory) + content = <<~RUBY TestClass.foo("", 123) RUBY + source = Source.parse(content, path: Pathname("a.rb"), factory: checker.factory) - SignatureHelpProvider.new(source: source, subtyping: checker).tap do |provider| + SignatureHelpProvider.new(source: source, content: content, subtyping: checker).tap do |provider| items, index = provider.run(line: 1, column: 14) assert_equal 0, index From 8b8c7c3fc0fce6fb75779d80f5567f7287843c69 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Mon, 3 Jul 2023 22:22:04 +0900 Subject: [PATCH 2/3] Use Source#buffer to get the original source code on SignatureHelpProvider --- lib/steep/server/interaction_worker.rb | 2 +- lib/steep/services/signature_help_provider.rb | 4 ++-- sig/steep/services/signature_help_provider.rbs | 2 +- test/signature_help_provider_test.rb | 10 ++++------ 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/lib/steep/server/interaction_worker.rb b/lib/steep/server/interaction_worker.rb index fb003c0c9..3e38effa3 100644 --- a/lib/steep/server/interaction_worker.rb +++ b/lib/steep/server/interaction_worker.rb @@ -391,7 +391,7 @@ def process_signature_help(job) subtyping = service.signature_services[target.name].current_subtyping or return source = Source.parse(file.content, path: file.path, factory: subtyping.factory) - provider = Services::SignatureHelpProvider.new(source: source, content: file.content, subtyping: subtyping) + provider = Services::SignatureHelpProvider.new(source: source, subtyping: subtyping) if (items, index = provider.run(line: job.line, column: job.column)) signatures = items.map do |item| diff --git a/lib/steep/services/signature_help_provider.rb b/lib/steep/services/signature_help_provider.rb index 89c5224a4..b5a12dd38 100644 --- a/lib/steep/services/signature_help_provider.rb +++ b/lib/steep/services/signature_help_provider.rb @@ -11,10 +11,10 @@ def env subtyping.factory.env end - def initialize(source:, content:, subtyping:) + def initialize(source:, subtyping:) @source = source @subtyping = subtyping - @buffer = RBS::Buffer.new(name: source.path, content: content) + @buffer = source.buffer end def run(line:, column:) diff --git a/sig/steep/services/signature_help_provider.rbs b/sig/steep/services/signature_help_provider.rbs index 5822f36fb..9f9201024 100644 --- a/sig/steep/services/signature_help_provider.rbs +++ b/sig/steep/services/signature_help_provider.rbs @@ -23,7 +23,7 @@ module Steep def env: () -> Environment - def initialize: (source: Source, content: String, subtyping: Subtyping::Check) -> void + def initialize: (source: Source, subtyping: Subtyping::Check) -> void # 1-origin line, 0-origin column # diff --git a/test/signature_help_provider_test.rb b/test/signature_help_provider_test.rb index 03255fa2e..22d0df66a 100644 --- a/test/signature_help_provider_test.rb +++ b/test/signature_help_provider_test.rb @@ -14,12 +14,11 @@ class TestClass def self.foo: (String, Integer) -> Array[Symbol] end RBS - content = <<~RUBY + source = Source.parse(<<~RUBY, path: Pathname("a.rb"), factory: checker.factory) TestClass.foo() RUBY - source = Source.parse(content, path: Pathname("a.rb"), factory: checker.factory) - SignatureHelpProvider.new(source: source, content: content, subtyping: checker).tap do |provider| + SignatureHelpProvider.new(source: source, subtyping: checker).tap do |provider| items, index = provider.run(line: 1, column: 14) assert_nil index @@ -35,12 +34,11 @@ def self.foo: (String, Integer) -> Array[Symbol] | () -> Array[Symbol] end RBS - content = <<~RUBY + source = Source.parse(<<~RUBY, path: Pathname("a.rb"), factory: checker.factory) TestClass.foo("", 123) RUBY - source = Source.parse(content, path: Pathname("a.rb"), factory: checker.factory) - SignatureHelpProvider.new(source: source, content: content, subtyping: checker).tap do |provider| + SignatureHelpProvider.new(source: source, subtyping: checker).tap do |provider| items, index = provider.run(line: 1, column: 14) assert_equal 0, index From 233e23cf929180904152bea4209d7d81ffe76d42 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Tue, 4 Jul 2023 02:50:48 +0900 Subject: [PATCH 3/3] Update testcase for SignatureHelpProvider --- test/signature_help_provider_test.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/signature_help_provider_test.rb b/test/signature_help_provider_test.rb index 22d0df66a..e1f4257e1 100644 --- a/test/signature_help_provider_test.rb +++ b/test/signature_help_provider_test.rb @@ -35,11 +35,12 @@ def self.foo: (String, Integer) -> Array[Symbol] end RBS source = Source.parse(<<~RUBY, path: Pathname("a.rb"), factory: checker.factory) + # Show signature help for a commented method call TestClass.foo("", 123) RUBY SignatureHelpProvider.new(source: source, subtyping: checker).tap do |provider| - items, index = provider.run(line: 1, column: 14) + items, index = provider.run(line: 2, column: 14) assert_equal 0, index assert_equal ["(::String, ::Integer) -> ::Array[::Symbol]", "() -> ::Array[::Symbol]"], items.map(&:method_type).map(&:to_s)