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 Aug 29, 2023
1 parent e114f09 commit db36338
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 45 deletions.
2 changes: 1 addition & 1 deletion lib/ruby_lsp/executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 11 additions & 43 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 @@ -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,
)
Expand All @@ -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,
)
Expand All @@ -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,
)
Expand All @@ -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(
Expand All @@ -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,
)
Expand All @@ -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,
)
Expand All @@ -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
Expand All @@ -225,15 +193,15 @@ 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)
end
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: [],
Expand Down
2 changes: 1 addition & 1 deletion test/integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit db36338

Please sign in to comment.