From db363381a180bdb23543745e2a8d3b832bae002c Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Tue, 29 Aug 2023 18:36:05 +0100 Subject: [PATCH] Use language_server-protocol gem's SymbolKind constants (#947) 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. --- lib/ruby_lsp/executor.rb | 2 +- lib/ruby_lsp/requests/document_symbol.rb | 54 +++++------------------- test/integration_test.rb | 2 +- 3 files changed, 13 insertions(+), 45 deletions(-) diff --git a/lib/ruby_lsp/executor.rb b/lib/ruby_lsp/executor.rb index 2864352fe..a388788cf 100644 --- a/lib/ruby_lsp/executor.rb +++ b/lib/ruby_lsp/executor.rb @@ -579,7 +579,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 diff --git a/lib/ruby_lsp/requests/document_symbol.rb b/lib/ruby_lsp/requests/document_symbol.rb index 876147c0b..b20b3f215 100644 --- a/lib/ruby_lsp/requests/document_symbol.rb +++ b/lib/ruby_lsp/requests/document_symbol.rb @@ -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 @@ -111,7 +79,7 @@ def initialize(emitter, message_queue) def on_class(node) @stack << create_document_symbol( name: full_constant_name(node.constant), - kind: :class, + kind: Constant::SymbolKind::CLASS, range_node: node, selection_range_node: node.constant, ) @@ -131,7 +99,7 @@ def on_command(node) create_document_symbol( name: argument.value.value, - kind: :field, + kind: Constant::SymbolKind::FIELD, range_node: argument, selection_range_node: argument.value, ) @@ -142,7 +110,7 @@ def on_command(node) def on_const_path_field(node) create_document_symbol( name: node.constant.value, - kind: :constant, + kind: Constant::SymbolKind::CONSTANT, range_node: node, selection_range_node: node.constant, ) @@ -154,10 +122,10 @@ def on_def(node) if target.is_a?(SyntaxTree::VarRef) && target.value.is_a?(SyntaxTree::Kw) && target.value.value == "self" name = "self.#{node.name.value}" - kind = :method + kind = Constant::SymbolKind::METHOD else name = node.name.value - kind = name == "initialize" ? :constructor : :method + kind = name == "initialize" ? Constant::SymbolKind::CONSTRUCTOR : Constant::SymbolKind::METHOD end symbol = create_document_symbol( @@ -179,7 +147,7 @@ def after_def(node) def on_module(node) @stack << create_document_symbol( name: full_constant_name(node.constant), - kind: :module, + kind: Constant::SymbolKind::MODULE, range_node: node, selection_range_node: node.constant, ) @@ -194,7 +162,7 @@ def after_module(node) def on_top_const_field(node) create_document_symbol( name: node.constant.value, - kind: :constant, + kind: Constant::SymbolKind::CONSTANT, range_node: node, selection_range_node: node.constant, ) @@ -205,9 +173,9 @@ def on_var_field(node) value = node.value kind = case value when SyntaxTree::Const - :constant + Constant::SymbolKind::CONSTANT when SyntaxTree::CVar, SyntaxTree::IVar - :variable + Constant::SymbolKind::VARIABLE else return end @@ -225,7 +193,7 @@ def on_var_field(node) sig do params( name: String, - kind: Symbol, + kind: Integer, range_node: SyntaxTree::Node, selection_range_node: SyntaxTree::Node, ).returns(Interface::DocumentSymbol) @@ -233,7 +201,7 @@ def on_var_field(node) 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: range_from_syntax_tree_node(selection_range_node), children: [], diff --git a/test/integration_test.rb b/test/integration_test.rb index d314bc031..755eaa3ea 100644 --- a/test/integration_test.rb +++ b/test/integration_test.rb @@ -68,7 +68,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