Skip to content

Commit

Permalink
Use language_server-protocol gem's SymbolKind constants (#947)
Browse files Browse the repository at this point in the history
Instead of maintaining our own list of symbol kinds, we can use the
language_server-protocol gem's SymbolKind constants, which will reduce a
layer of conversion (symbol to integer) as well.
  • Loading branch information
st0012 authored and andyw8 committed Sep 5, 2023
1 parent ffa0f42 commit 339aa3a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 59 deletions.
2 changes: 1 addition & 1 deletion lib/ruby_lsp/executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ def initialize_request(options)
Interface::DocumentSymbolClientCapabilities.new(
hierarchical_document_symbol_support: true,
symbol_kind: {
value_set: Requests::DocumentSymbol::SYMBOL_KIND.values,
value_set: (Constant::SymbolKind::FILE..Constant::SymbolKind::TYPE_PARAMETER).to_a,
},
)
end
Expand Down
82 changes: 25 additions & 57 deletions lib/ruby_lsp/requests/document_symbol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,38 +32,6 @@ class DocumentSymbol < Listener

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

SYMBOL_KIND = T.let(
{
file: 1,
module: 2,
namespace: 3,
package: 4,
class: 5,
method: 6,
property: 7,
field: 8,
constructor: 9,
enum: 10,
interface: 11,
function: 12,
variable: 13,
constant: 14,
string: 15,
number: 16,
boolean: 17,
array: 18,
object: 19,
key: 20,
null: 21,
enummember: 22,
struct: 23,
event: 24,
operator: 25,
typeparameter: 26,
}.freeze,
T::Hash[Symbol, Integer],
)

ATTR_ACCESSORS = T.let(["attr_reader", "attr_writer", "attr_accessor"].freeze, T::Array[String])

class SymbolHierarchyRoot
Expand Down Expand Up @@ -112,7 +80,7 @@ def initialize(emitter, message_queue)
def on_class(node)
@stack << create_document_symbol(
name: node.constant_path.location.slice,
kind: :class,
kind: Constant::SymbolKind::CLASS,
range_node: node,
selection_range_node: node.constant_path,
)
Expand All @@ -135,7 +103,7 @@ def on_call(node)

create_document_symbol(
name: argument.value,
kind: :field,
kind: Constant::SymbolKind::FIELD,
range_node: argument,
selection_range_node: argument,
)
Expand All @@ -146,7 +114,7 @@ def on_call(node)
def on_constant_path_write(node)
create_document_symbol(
name: node.target.location.slice,
kind: :constant,
kind: Constant::SymbolKind::CONSTANT,
range_node: node,
selection_range_node: node.target,
)
Expand All @@ -156,22 +124,37 @@ def on_constant_path_write(node)
def on_constant_write(node)
create_document_symbol(
name: node.name,
kind: :constant,
kind: Constant::SymbolKind::CONSTANT,
range_node: node,
selection_range_node: node.name_loc,
)
end

sig { params(node: YARP::DefNode).void }
def after_def(node)
@stack.pop
end

sig { params(node: YARP::ModuleNode).void }
def on_module(node)
@stack << create_document_symbol(
name: node.constant_path.location.slice,
kind: Constant::SymbolKind::MODULE,
range_node: node,
selection_range_node: node.constant_path,
)
end

sig { params(node: YARP::DefNode).void }
def on_def(node)
receiver = node.receiver

if receiver.is_a?(YARP::SelfNode)
name = "self.#{node.name}"
kind = :method
kind = Constant::SymbolKind::METHOD
else
name = node.name
kind = name == "initialize" ? :constructor : :method
kind = name == "initialize" ? Constant::SymbolKind::CONSTRUCTOR : Constant::SymbolKind::METHOD
end

symbol = create_document_symbol(
Expand All @@ -184,21 +167,6 @@ def on_def(node)
@stack << symbol
end

sig { params(node: YARP::DefNode).void }
def after_def(node)
@stack.pop
end

sig { params(node: YARP::ModuleNode).void }
def on_module(node)
@stack << create_document_symbol(
name: node.constant_path.location.slice,
kind: :module,
range_node: node,
selection_range_node: node.constant_path,
)
end

sig { params(node: YARP::ModuleNode).void }
def after_module(node)
@stack.pop
Expand All @@ -208,7 +176,7 @@ def after_module(node)
def on_instance_variable_write(node)
create_document_symbol(
name: node.name.to_s,
kind: :variable,
kind: Constant::SymbolKind::VARIABLE,
range_node: node,
selection_range_node: node.name_loc,
)
Expand All @@ -218,7 +186,7 @@ def on_instance_variable_write(node)
def on_class_variable_write(node)
create_document_symbol(
name: node.name.to_s,
kind: :variable,
kind: Constant::SymbolKind::VARIABLE,
range_node: node,
selection_range_node: node.name_loc,
)
Expand All @@ -229,7 +197,7 @@ def on_class_variable_write(node)
sig do
params(
name: String,
kind: Symbol,
kind: Integer,
range_node: YARP::Node,
selection_range_node: T.any(YARP::Node, YARP::Location),
).returns(Interface::DocumentSymbol)
Expand All @@ -243,7 +211,7 @@ def create_document_symbol(name:, kind:, range_node:, selection_range_node:)

symbol = Interface::DocumentSymbol.new(
name: name,
kind: SYMBOL_KIND[kind],
kind: kind,
range: range_from_syntax_tree_node(range_node),
selection_range: selection_range,
children: [],
Expand Down
2 changes: 1 addition & 1 deletion test/integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def test_document_symbol
response = make_request("textDocument/documentSymbol", { textDocument: { uri: @uri } })
symbol = response[:result].first
assert_equal("Foo", symbol[:name])
assert_equal(RubyLsp::Requests::DocumentSymbol::SYMBOL_KIND[:class], symbol[:kind])
assert_equal(LanguageServer::Protocol::Constant::SymbolKind::CLASS, symbol[:kind])
end

def test_document_highlight
Expand Down

0 comments on commit 339aa3a

Please sign in to comment.