Skip to content

Commit

Permalink
Move provider definition to individual requests
Browse files Browse the repository at this point in the history
Some provider definitions require the request's data like constants. Instead
of exposing all those to the executor, it's better to define the providers
inside the request classes.
  • Loading branch information
st0012 committed Jan 4, 2024
1 parent 429265e commit 5c9d9bd
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 75 deletions.
87 changes: 12 additions & 75 deletions lib/ruby_lsp/executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -663,81 +663,18 @@ def initialize_request(options)
Hash.new(true)
end

document_symbol_provider = if enabled_features["documentSymbols"]
Interface::DocumentSymbolClientCapabilities.new(
hierarchical_document_symbol_support: true,
symbol_kind: {
value_set: (Constant::SymbolKind::FILE..Constant::SymbolKind::TYPE_PARAMETER).to_a,
},
)
end

document_link_provider = if enabled_features["documentLink"]
Interface::DocumentLinkOptions.new(resolve_provider: false)
end

code_lens_provider = if enabled_features["codeLens"]
Interface::CodeLensOptions.new(resolve_provider: false)
end

hover_provider = if enabled_features["hover"]
Interface::HoverClientCapabilities.new(dynamic_registration: false)
end

folding_ranges_provider = if enabled_features["foldingRanges"]
Interface::FoldingRangeClientCapabilities.new(line_folding_only: true)
end

semantic_tokens_provider = if enabled_features["semanticHighlighting"]
Interface::SemanticTokensRegistrationOptions.new(
document_selector: { scheme: "file", language: "ruby" },
legend: Interface::SemanticTokensLegend.new(
token_types: Requests::SemanticHighlighting::TOKEN_TYPES.keys,
token_modifiers: Requests::SemanticHighlighting::TOKEN_MODIFIERS.keys,
),
range: true,
full: { delta: false },
)
end

diagnostics_provider = if enabled_features["diagnostics"]
{
interFileDependencies: false,
workspaceDiagnostics: false,
}
end

on_type_formatting_provider = if enabled_features["onTypeFormatting"]
Interface::DocumentOnTypeFormattingOptions.new(
first_trigger_character: "{",
more_trigger_character: ["\n", "|", "d"],
)
end

code_action_provider = if enabled_features["codeActions"]
Interface::CodeActionOptions.new(resolve_provider: true)
end

inlay_hint_provider = if enabled_features["inlayHint"]
Interface::InlayHintOptions.new(resolve_provider: false)
end

completion_provider = if enabled_features["completion"]
Interface::CompletionOptions.new(
resolve_provider: false,
trigger_characters: ["/"],
completion_item: {
labelDetailsSupport: true,
},
)
end

signature_help_provider = if enabled_features["signatureHelp"]
# Identifier characters are automatically included, such as A-Z, a-z, 0-9, _, * or :
Interface::SignatureHelpOptions.new(
trigger_characters: ["(", " ", ","],
)
end
document_symbol_provider = Requests::DocumentSymbol.provider if enabled_features["documentSymbols"]
document_link_provider = Requests::DocumentLink.provider if enabled_features["documentLink"]
code_lens_provider = Requests::CodeLens.provider if enabled_features["codeLens"]
hover_provider = Requests::Hover.provider if enabled_features["hover"]
folding_ranges_provider = Requests::FoldingRanges.provider if enabled_features["foldingRanges"]
semantic_tokens_provider = Requests::SemanticHighlighting.provider if enabled_features["semanticHighlighting"]
diagnostics_provider = Requests::Diagnostics.provider if enabled_features["diagnostics"]
on_type_formatting_provider = Requests::OnTypeFormatting.provider if enabled_features["onTypeFormatting"]
code_action_provider = Requests::CodeActions.provider if enabled_features["codeActions"]
inlay_hint_provider = Requests::InlayHints.provider if enabled_features["inlayHint"]
completion_provider = Requests::Completion.provider if enabled_features["completion"]
signature_help_provider = Requests::SignatureHelp.provider if enabled_features["signatureHelp"]

# Dynamically registered capabilities
file_watching_caps = options.dig(:capabilities, :workspace, :didChangeWatchedFiles)
Expand Down
9 changes: 9 additions & 0 deletions lib/ruby_lsp/requests/code_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ module Requests
class CodeActions
extend T::Sig

class << self
extend T::Sig

sig { returns(Interface::CodeActionOptions) }
def provider
Interface::CodeActionOptions.new(resolve_provider: true)
end
end

sig do
params(
document: Document,
Expand Down
9 changes: 9 additions & 0 deletions lib/ruby_lsp/requests/code_lens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ class CodeLens < ExtensibleListener
extend T::Sig
extend T::Generic

class << self
extend T::Sig

sig { returns(Interface::CodeLensOptions) }
def provider
Interface::CodeLensOptions.new(resolve_provider: false)
end
end

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

BASE_COMMAND = T.let(
Expand Down
15 changes: 15 additions & 0 deletions lib/ruby_lsp/requests/completion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ class Completion < Listener
extend T::Sig
extend T::Generic

class << self
extend T::Sig

sig { returns(Interface::CompletionOptions) }
def provider
Interface::CompletionOptions.new(
resolve_provider: false,
trigger_characters: ["/"],
completion_item: {
labelDetailsSupport: true,
},
)
end
end

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

sig { override.returns(ResponseType) }
Expand Down
12 changes: 12 additions & 0 deletions lib/ruby_lsp/requests/diagnostics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ module Requests
class Diagnostics
extend T::Sig

class << self
extend T::Sig

sig { returns(T::Hash[Symbol, T::Boolean]) }
def provider
{
interFileDependencies: false,
workspaceDiagnostics: false,
}
end
end

sig { params(document: Document).void }
def initialize(document)
@document = document
Expand Down
9 changes: 9 additions & 0 deletions lib/ruby_lsp/requests/document_link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ class DocumentLink < Listener
extend T::Sig
extend T::Generic

class << self
extend T::Sig

sig { returns(Interface::DocumentLinkOptions) }
def provider
Interface::DocumentLinkOptions.new(resolve_provider: false)
end
end

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

GEM_TO_VERSION_MAP = T.let(
Expand Down
14 changes: 14 additions & 0 deletions lib/ruby_lsp/requests/document_symbol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ class DocumentSymbol < ExtensibleListener
extend T::Sig
extend T::Generic

class << self
extend T::Sig

sig { returns(Interface::DocumentSymbolClientCapabilities) }
def provider
Interface::DocumentSymbolClientCapabilities.new(
hierarchical_document_symbol_support: true,
symbol_kind: {
value_set: (Constant::SymbolKind::FILE..Constant::SymbolKind::TYPE_PARAMETER).to_a,
},
)
end
end

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

ATTR_ACCESSORS = T.let([:attr_reader, :attr_writer, :attr_accessor].freeze, T::Array[Symbol])
Expand Down
9 changes: 9 additions & 0 deletions lib/ruby_lsp/requests/folding_ranges.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ class FoldingRanges < Listener
extend T::Sig
extend T::Generic

class << self
extend T::Sig

sig { returns(Interface::FoldingRangeClientCapabilities) }
def provider
Interface::FoldingRangeClientCapabilities.new(line_folding_only: true)
end
end

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

sig { params(comments: T::Array[Prism::Comment], dispatcher: Prism::Dispatcher).void }
Expand Down
9 changes: 9 additions & 0 deletions lib/ruby_lsp/requests/hover.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ class Hover < ExtensibleListener
extend T::Sig
extend T::Generic

class << self
extend T::Sig

sig { returns(Interface::HoverClientCapabilities) }
def provider
Interface::HoverClientCapabilities.new(dynamic_registration: false)
end
end

ResponseType = type_member { { fixed: T.nilable(Interface::Hover) } }

ALLOWED_TARGETS = T.let(
Expand Down
9 changes: 9 additions & 0 deletions lib/ruby_lsp/requests/inlay_hints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ class InlayHints < Listener
extend T::Sig
extend T::Generic

class << self
extend T::Sig

sig { returns(Interface::InlayHintOptions) }
def provider
Interface::InlayHintOptions.new(resolve_provider: false)
end
end

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

RESCUE_STRING_LENGTH = T.let("rescue".length, Integer)
Expand Down
12 changes: 12 additions & 0 deletions lib/ruby_lsp/requests/on_type_formatting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ module Requests
class OnTypeFormatting
extend T::Sig

class << self
extend T::Sig

sig { returns(Interface::DocumentOnTypeFormattingOptions) }
def provider
Interface::DocumentOnTypeFormattingOptions.new(
first_trigger_character: "{",
more_trigger_character: ["\n", "|", "d"],
)
end
end

END_REGEXES = T.let(
[
/\b(if|unless|for|while|class|module|until|def|case)\b.*/,
Expand Down
17 changes: 17 additions & 0 deletions lib/ruby_lsp/requests/semantic_highlighting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@ class SemanticHighlighting < Listener
extend T::Sig
extend T::Generic

class << self
extend T::Sig

sig { returns(Interface::SemanticTokensRegistrationOptions) }
def provider
Interface::SemanticTokensRegistrationOptions.new(
document_selector: { scheme: "file", language: "ruby" },
legend: Interface::SemanticTokensLegend.new(
token_types: Requests::SemanticHighlighting::TOKEN_TYPES.keys,
token_modifiers: Requests::SemanticHighlighting::TOKEN_MODIFIERS.keys,
),
range: true,
full: { delta: false },
)
end
end

ResponseType = type_member { { fixed: T::Array[SemanticToken] } }

TOKEN_TYPES = T.let(
Expand Down
12 changes: 12 additions & 0 deletions lib/ruby_lsp/requests/signature_help.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ class SignatureHelp < Listener
extend T::Sig
extend T::Generic

class << self
extend T::Sig

sig { returns(Interface::SignatureHelpOptions) }
def provider
# Identifier characters are automatically included, such as A-Z, a-z, 0-9, _, * or :
Interface::SignatureHelpOptions.new(
trigger_characters: ["(", " ", ","],
)
end
end

ResponseType = type_member { { fixed: T.nilable(T.any(Interface::SignatureHelp, T::Hash[Symbol, T.untyped])) } }

sig { override.returns(ResponseType) }
Expand Down

0 comments on commit 5c9d9bd

Please sign in to comment.