Skip to content

Commit

Permalink
Support LSP workspace/symbol feature
Browse files Browse the repository at this point in the history
  • Loading branch information
soutaro committed Dec 8, 2020
1 parent 05d7bc0 commit d28f4b0
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 9 deletions.
6 changes: 5 additions & 1 deletion lib/steep/server/master.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ def process_message_from_client(message)
hover_provider: true,
completion_provider: LSP::Interface::CompletionOptions.new(
trigger_characters: [".", "@"]
)
),
workspace_symbol_provider: true
)
)
}
Expand Down Expand Up @@ -143,6 +144,9 @@ def process_message_from_client(message)
when "textDocument/open"
# Ignores open notification

when "workspace/symbol"
signature_worker << message

when "shutdown"
queue << { id: id, result: nil }
@shutdown_request_id = id
Expand Down
69 changes: 63 additions & 6 deletions lib/steep/server/signature_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ def validate_signature_if_required(request)
def enqueue_target(target:, timestamp:)
Steep.logger.debug "queueing target #{target.name}@#{timestamp}"
last_target_validated_at[target] = timestamp
queue << [target, timestamp]
queue << [:validate, [target, timestamp]]
end

def enqueue_symbol(id:, query:)
Steep.logger.debug "queueing symbol #{query} (#{id})"
queue << [:symbol, [id, query]]
end

def handle_request(request)
Expand All @@ -37,6 +42,8 @@ def handle_request(request)
when "textDocument/didChange"
update_source(request)
validate_signature_if_required(request)
when "workspace/symbol"
enqueue_symbol(query: request[:params][:query], id: request[:id])
end
end

Expand Down Expand Up @@ -138,13 +145,63 @@ def active_job?(target, timestamp)
end
end

def handle_workspace_symbol(query:, id:)
provider = Index::SignatureSymbolProvider.new()

project.targets.each do |target|
case target.status
when Project::Target::TypeCheckStatus
index = Index::RBSIndex.new()

builder = Index::RBSIndex::Builder.new(index: index)
builder.env(target.status.environment)

provider.indexes << index
end
end

symbols = provider.query_symbol(query)

result = symbols.map do |symbol|
{
name: symbol.name.to_s,
kind: symbol.kind,
deprecated: false,
containerName: symbol.container_name.to_s,
location: {
uri: URI.parse(project.absolute_path(symbol.location.buffer.name).to_s),
range: {
start: LSP::Interface::Position.new(
line: symbol.location.start_line - 1,
character: symbol.location.start_column,
),
end: LSP::Interface::Position.new(
line: symbol.location.end_line - 1,
character: symbol.location.end_column
)
}
}
}
end

writer.write(id: id, result: result)
end

def handle_job(job)
target, timestamp = job
action, data = job

case action
when :validate
target, timestamp = data

if active_job?(target, timestamp)
validate_signature(target, timestamp: timestamp)
else
Steep.logger.info "Skipping signature validation: #{target.name}, queued timestamp=#{timestamp}, latest timestamp=#{last_target_validated_at[target]}"
if active_job?(target, timestamp)
validate_signature(target, timestamp: timestamp)
else
Steep.logger.info "Skipping signature validation: #{target.name}, queued timestamp=#{timestamp}, latest timestamp=#{last_target_validated_at[target]}"
end
when :symbol
id, query = data
handle_workspace_symbol(query: query, id: id)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions test/signature_worker_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class Foo
}
)

assert_equal [lib_target, test_target], worker.queue.map(&:first)
assert_equal [lib_target, test_target], worker.queue.map {|_, pair| pair.first }
assert_instance_of Time, worker.last_target_validated_at[lib_target]
assert_instance_of Time, worker.last_target_validated_at[test_target]

Expand All @@ -127,7 +127,7 @@ module TestHelper
}
)

assert_equal [test_target], worker.queue.map(&:first)
assert_equal [test_target], worker.queue.map {|_, pair| pair.first }
end
end

Expand Down

0 comments on commit d28f4b0

Please sign in to comment.