Skip to content

Commit

Permalink
Merge pull request #703 from Shopify/vs/migrate_inlay_hint_to_listener
Browse files Browse the repository at this point in the history
Migrate inlay hint to listener
  • Loading branch information
vinistock authored May 4, 2023
2 parents 3fb51d1 + ccdfac4 commit 6d63b6e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
6 changes: 6 additions & 0 deletions lib/ruby_lsp/event_emitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,11 @@ def visit_comment(node)
@listeners[:on_comment]&.each { |l| T.unsafe(l).on_comment(node) }
super
end

sig { override.params(node: SyntaxTree::Rescue).void }
def visit_rescue(node)
@listeners[:on_rescue]&.each { |l| T.unsafe(l).on_rescue(node) }
super
end
end
end
9 changes: 7 additions & 2 deletions lib/ruby_lsp/executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,18 @@ def document_highlight(uri, position)
Requests::DocumentHighlight.new(@store.get(uri), position).run
end

sig { params(uri: String, range: Document::RangeShape).returns(T::Array[Interface::InlayHint]) }
sig { params(uri: String, range: Document::RangeShape).returns(T.nilable(T::Array[Interface::InlayHint])) }
def inlay_hint(uri, range)
document = @store.get(uri)
return if document.syntax_error?

start_line = range.dig(:start, :line)
end_line = range.dig(:end, :line)

Requests::InlayHints.new(document, start_line..end_line).run
emitter = EventEmitter.new
listener = Requests::InlayHints.new(start_line..end_line, emitter, @message_queue)
emitter.visit(document.tree)
listener.response
end

sig do
Expand Down
32 changes: 17 additions & 15 deletions lib/ruby_lsp/requests/inlay_hints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,41 @@ module Requests
# puts "handle some rescue"
# end
# ```
class InlayHints < BaseRequest
class InlayHints < Listener
extend T::Sig
extend T::Generic

ResponseType = type_member { { fixed: T::Array[Interface::InlayHint] } }

RESCUE_STRING_LENGTH = T.let("rescue".length, Integer)

sig { params(document: Document, range: T::Range[Integer]).void }
def initialize(document, range)
super(document)
sig { override.returns(ResponseType) }
attr_reader :response

sig { params(range: T::Range[Integer], emitter: EventEmitter, message_queue: Thread::Queue).void }
def initialize(range, emitter, message_queue)
super(emitter, message_queue)

@hints = T.let([], T::Array[Interface::InlayHint])
@response = T.let([], ResponseType)
@range = range
end

sig { override.returns(T.all(T::Array[Interface::InlayHint], Object)) }
def run
visit(@document.tree) if @document.parsed?
@hints
emitter.register(self, :on_rescue)
end

sig { override.params(node: SyntaxTree::Rescue).void }
def visit_rescue(node)
sig { params(node: SyntaxTree::Rescue).void }
def on_rescue(node)
exception = node.exception
return unless exception.nil? || exception.exceptions.nil?

loc = node.location
return unless visible?(node, @range)

@hints << Interface::InlayHint.new(
@response << Interface::InlayHint.new(
position: { line: loc.start_line - 1, character: loc.start_column + RESCUE_STRING_LENGTH },
label: "StandardError",
padding_left: true,
tooltip: "StandardError is implied in a bare rescue",
)

super
end
end
end
Expand Down
14 changes: 14 additions & 0 deletions test/requests/inlay_hints_expectations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@
class InlayHintsExpectationsTest < ExpectationsTestRunner
expectations_tests RubyLsp::Requests::InlayHints, "inlay_hints"

def run_expectations(source)
message_queue = Thread::Queue.new
params = @__params&.any? ? @__params : default_args
uri = "file://#{@_path}"
document = RubyLsp::Document.new(source: source, version: 1, uri: uri)

emitter = RubyLsp::EventEmitter.new
listener = RubyLsp::Requests::InlayHints.new(params.first, emitter, message_queue)
emitter.visit(document.tree)
listener.response
ensure
T.must(message_queue).close
end

def default_args
[0..20]
end
Expand Down

0 comments on commit 6d63b6e

Please sign in to comment.