Skip to content
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

WIP: Refactor folding ranges to use emitter pattern (alternative approach) #750

Closed
wants to merge 4 commits into from
Closed
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
12 changes: 3 additions & 9 deletions lib/ruby_lsp/executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def run(request)
when "textDocument/selectionRange"
selection_range(uri, request.dig(:params, :positions))
when "textDocument/documentSymbol", "textDocument/documentLink", "textDocument/codeLens",
"textDocument/semanticTokens/full"
"textDocument/semanticTokens/full", "textDocument/foldingRange"
document = @store.get(uri)

# If the response has already been cached by another request, return it
Expand All @@ -97,7 +97,7 @@ def run(request)
document_symbol = Requests::DocumentSymbol.new(emitter, @message_queue)
document_link = Requests::DocumentLink.new(uri, emitter, @message_queue)
code_lens = Requests::CodeLens.new(uri, emitter, @message_queue, @test_library)

folding_ranges = Requests::FoldingRanges.new(emitter, @message_queue)
semantic_highlighting = Requests::SemanticHighlighting.new(emitter, @message_queue)
emitter.visit(document.tree) if document.parsed?

Expand All @@ -112,6 +112,7 @@ def run(request)
"textDocument/semanticTokens/full",
Requests::Support::SemanticTokenEncoder.new.encode(semantic_highlighting.response),
)
document.cache_set("textDocument/foldingRange", folding_ranges.response)
document.cache_get(request[:method])
when "textDocument/semanticTokens/range"
semantic_tokens_range(uri, request.dig(:params, :range))
Expand Down Expand Up @@ -187,13 +188,6 @@ def definition(uri, position)
base_listener.response
end

sig { params(uri: String).returns(T::Array[Interface::FoldingRange]) }
def folding_range(uri)
@store.cache_fetch(uri, "textDocument/foldingRange") do |document|
Requests::FoldingRanges.new(document).run
end
end

sig do
params(
uri: String,
Expand Down
42 changes: 21 additions & 21 deletions lib/ruby_lsp/requests/folding_ranges.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ module Requests
# puts "Hello"
# end # <-- folding range end
# ```
class FoldingRanges < BaseRequest
class FoldingRanges < Listener
extend T::Sig

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

SIMPLE_FOLDABLES = T.let(
[
SyntaxTree::ArrayLiteral,
Expand Down Expand Up @@ -60,27 +63,24 @@ class FoldingRanges < BaseRequest
)
end

sig { params(document: Document).void }
def initialize(document)
sig { params(emitter: EventEmitter, message_queue: Thread::Queue).void }
def initialize(emitter, message_queue)
super

@ranges = T.let([], T::Array[Interface::FoldingRange])
@response = T.let([], ResponseType)
@partial_range = T.let(nil, T.nilable(PartialRange))
end

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

@ranges
sig { params(node: T.nilable(SyntaxTree::Node)).returns(T.untyped) } # TODO: fix sig
def on_node(node)
visit(node)
emit_partial_range
end

private
# private

sig { override.params(node: T.nilable(SyntaxTree::Node)).void }
sig { params(node: T.nilable(SyntaxTree::Node)).void }
def visit(node)
return unless handle_partial_range(node)

Expand All @@ -98,7 +98,7 @@ def visit(node)
add_lines_range(location.start_line, location.end_line - 1)
else
add_call_range(node)
return
nil
end
when SyntaxTree::Command
unless same_lines_for_command_and_block?(node)
Expand All @@ -109,10 +109,8 @@ def visit(node)
add_def_range(node)
when SyntaxTree::StringConcat
add_string_concat(node)
return
nil
end

super
end

# This is to prevent duplicate ranges
Expand Down Expand Up @@ -215,7 +213,9 @@ def partial_range_kind(node)
def emit_partial_range
return if @partial_range.nil?

@ranges << @partial_range.to_range if @partial_range.multiline?
if @partial_range.multiline?
@response << @partial_range.to_range
end
@partial_range = nil
end

Expand Down Expand Up @@ -296,7 +296,7 @@ def add_string_concat(node)
def add_lines_range(start_line, end_line)
return if start_line >= end_line

@ranges << Interface::FoldingRange.new(
@response << Interface::FoldingRange.new(
start_line: start_line - 1,
end_line: end_line - 1,
kind: "region",
Expand Down
Loading