ruby-lsp 0.23.10 → 0.23.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -2
  3. data/VERSION +1 -1
  4. data/exe/ruby-lsp-launcher +12 -11
  5. data/lib/rubocop/cop/ruby_lsp/use_language_server_aliases.rb +1 -1
  6. data/lib/rubocop/cop/ruby_lsp/use_register_with_handler_method.rb +3 -5
  7. data/lib/ruby_indexer/lib/ruby_indexer/configuration.rb +81 -115
  8. data/lib/ruby_indexer/lib/ruby_indexer/declaration_listener.rb +117 -166
  9. data/lib/ruby_indexer/lib/ruby_indexer/enhancement.rb +9 -7
  10. data/lib/ruby_indexer/lib/ruby_indexer/entry.rb +89 -201
  11. data/lib/ruby_indexer/lib/ruby_indexer/index.rb +63 -192
  12. data/lib/ruby_indexer/lib/ruby_indexer/location.rb +4 -27
  13. data/lib/ruby_indexer/lib/ruby_indexer/prefix_tree.rb +14 -16
  14. data/lib/ruby_indexer/lib/ruby_indexer/rbs_indexer.rb +22 -45
  15. data/lib/ruby_indexer/lib/ruby_indexer/reference_finder.rb +42 -60
  16. data/lib/ruby_indexer/lib/ruby_indexer/uri.rb +9 -16
  17. data/lib/ruby_indexer/lib/ruby_indexer/visibility_scope.rb +5 -9
  18. data/lib/ruby_indexer/test/classes_and_modules_test.rb +75 -0
  19. data/lib/ruby_indexer/test/configuration_test.rb +42 -3
  20. data/lib/ruby_indexer/test/index_test.rb +21 -0
  21. data/lib/ruby_indexer/test/method_test.rb +28 -2
  22. data/lib/ruby_indexer/test/rbs_indexer_test.rb +1 -1
  23. data/lib/ruby_lsp/addon.rb +44 -71
  24. data/lib/ruby_lsp/base_server.rb +31 -33
  25. data/lib/ruby_lsp/client_capabilities.rb +10 -12
  26. data/lib/ruby_lsp/document.rb +34 -45
  27. data/lib/ruby_lsp/erb_document.rb +24 -36
  28. data/lib/ruby_lsp/global_state.rb +51 -56
  29. data/lib/ruby_lsp/internal.rb +6 -0
  30. data/lib/ruby_lsp/listeners/code_lens.rb +81 -88
  31. data/lib/ruby_lsp/listeners/completion.rb +36 -55
  32. data/lib/ruby_lsp/listeners/definition.rb +37 -51
  33. data/lib/ruby_lsp/listeners/document_highlight.rb +123 -150
  34. data/lib/ruby_lsp/listeners/document_link.rb +43 -62
  35. data/lib/ruby_lsp/listeners/document_symbol.rb +35 -49
  36. data/lib/ruby_lsp/listeners/folding_ranges.rb +32 -39
  37. data/lib/ruby_lsp/listeners/hover.rb +81 -100
  38. data/lib/ruby_lsp/listeners/inlay_hints.rb +4 -11
  39. data/lib/ruby_lsp/listeners/semantic_highlighting.rb +42 -51
  40. data/lib/ruby_lsp/listeners/signature_help.rb +6 -25
  41. data/lib/ruby_lsp/listeners/spec_style.rb +155 -0
  42. data/lib/ruby_lsp/listeners/test_discovery.rb +89 -0
  43. data/lib/ruby_lsp/listeners/test_style.rb +236 -0
  44. data/lib/ruby_lsp/node_context.rb +12 -39
  45. data/lib/ruby_lsp/rbs_document.rb +8 -6
  46. data/lib/ruby_lsp/requests/code_action_resolve.rb +10 -10
  47. data/lib/ruby_lsp/requests/code_actions.rb +14 -26
  48. data/lib/ruby_lsp/requests/code_lens.rb +6 -17
  49. data/lib/ruby_lsp/requests/completion.rb +7 -20
  50. data/lib/ruby_lsp/requests/completion_resolve.rb +5 -5
  51. data/lib/ruby_lsp/requests/definition.rb +8 -17
  52. data/lib/ruby_lsp/requests/diagnostics.rb +8 -11
  53. data/lib/ruby_lsp/requests/discover_tests.rb +75 -0
  54. data/lib/ruby_lsp/requests/document_highlight.rb +5 -15
  55. data/lib/ruby_lsp/requests/document_link.rb +6 -17
  56. data/lib/ruby_lsp/requests/document_symbol.rb +5 -8
  57. data/lib/ruby_lsp/requests/folding_ranges.rb +7 -15
  58. data/lib/ruby_lsp/requests/formatting.rb +6 -9
  59. data/lib/ruby_lsp/requests/go_to_relevant_file.rb +87 -0
  60. data/lib/ruby_lsp/requests/hover.rb +8 -18
  61. data/lib/ruby_lsp/requests/inlay_hints.rb +6 -17
  62. data/lib/ruby_lsp/requests/on_type_formatting.rb +28 -38
  63. data/lib/ruby_lsp/requests/prepare_rename.rb +4 -9
  64. data/lib/ruby_lsp/requests/prepare_type_hierarchy.rb +4 -13
  65. data/lib/ruby_lsp/requests/range_formatting.rb +5 -6
  66. data/lib/ruby_lsp/requests/references.rb +6 -36
  67. data/lib/ruby_lsp/requests/rename.rb +11 -37
  68. data/lib/ruby_lsp/requests/request.rb +7 -19
  69. data/lib/ruby_lsp/requests/selection_ranges.rb +5 -5
  70. data/lib/ruby_lsp/requests/semantic_highlighting.rb +12 -31
  71. data/lib/ruby_lsp/requests/show_syntax_tree.rb +5 -6
  72. data/lib/ruby_lsp/requests/signature_help.rb +8 -26
  73. data/lib/ruby_lsp/requests/support/annotation.rb +4 -10
  74. data/lib/ruby_lsp/requests/support/common.rb +13 -48
  75. data/lib/ruby_lsp/requests/support/rubocop_diagnostic.rb +27 -35
  76. data/lib/ruby_lsp/requests/support/rubocop_formatter.rb +9 -12
  77. data/lib/ruby_lsp/requests/support/rubocop_runner.rb +22 -34
  78. data/lib/ruby_lsp/requests/support/selection_range.rb +1 -3
  79. data/lib/ruby_lsp/requests/support/sorbet.rb +29 -38
  80. data/lib/ruby_lsp/requests/support/source_uri.rb +16 -30
  81. data/lib/ruby_lsp/requests/support/syntax_tree_formatter.rb +12 -19
  82. data/lib/ruby_lsp/requests/support/test_item.rb +55 -0
  83. data/lib/ruby_lsp/requests/type_hierarchy_supertypes.rb +5 -6
  84. data/lib/ruby_lsp/requests/workspace_symbol.rb +4 -4
  85. data/lib/ruby_lsp/response_builders/collection_response_builder.rb +5 -5
  86. data/lib/ruby_lsp/response_builders/document_symbol.rb +10 -16
  87. data/lib/ruby_lsp/response_builders/hover.rb +10 -13
  88. data/lib/ruby_lsp/response_builders/response_builder.rb +1 -1
  89. data/lib/ruby_lsp/response_builders/semantic_highlighting.rb +59 -87
  90. data/lib/ruby_lsp/response_builders/signature_help.rb +5 -6
  91. data/lib/ruby_lsp/response_builders/test_collection.rb +34 -0
  92. data/lib/ruby_lsp/ruby_document.rb +22 -60
  93. data/lib/ruby_lsp/ruby_lsp_reporter_plugin.rb +109 -0
  94. data/lib/ruby_lsp/scope.rb +7 -11
  95. data/lib/ruby_lsp/server.rb +177 -72
  96. data/lib/ruby_lsp/setup_bundler.rb +61 -59
  97. data/lib/ruby_lsp/static_docs.rb +4 -7
  98. data/lib/ruby_lsp/store.rb +21 -40
  99. data/lib/ruby_lsp/test_helper.rb +3 -12
  100. data/lib/ruby_lsp/test_reporter.rb +207 -0
  101. data/lib/ruby_lsp/test_unit_test_runner.rb +98 -0
  102. data/lib/ruby_lsp/type_inferrer.rb +9 -13
  103. data/lib/ruby_lsp/utils.rb +37 -81
  104. metadata +13 -3
@@ -9,24 +9,14 @@ module RubyLsp
9
9
  # request](https://microsoft.github.io/language-server-protocol/specification#textDocument_definition) jumps to the
10
10
  # definition of the symbol under the cursor.
11
11
  class Definition < Request
12
- extend T::Sig
13
12
  extend T::Generic
14
13
 
15
- sig do
16
- params(
17
- document: T.any(RubyDocument, ERBDocument),
18
- global_state: GlobalState,
19
- position: T::Hash[Symbol, T.untyped],
20
- dispatcher: Prism::Dispatcher,
21
- sorbet_level: RubyDocument::SorbetLevel,
22
- ).void
23
- end
14
+ #: ((RubyDocument | ERBDocument) document, GlobalState global_state, Hash[Symbol, untyped] position, Prism::Dispatcher dispatcher, RubyDocument::SorbetLevel sorbet_level) -> void
24
15
  def initialize(document, global_state, position, dispatcher, sorbet_level)
25
16
  super()
26
- @response_builder = T.let(
27
- ResponseBuilders::CollectionResponseBuilder[T.any(Interface::Location, Interface::LocationLink)].new,
28
- ResponseBuilders::CollectionResponseBuilder[T.any(Interface::Location, Interface::LocationLink)],
29
- )
17
+ @response_builder = ResponseBuilders::CollectionResponseBuilder[
18
+ T.any(Interface::Location, Interface::LocationLink)
19
+ ].new #: ResponseBuilders::CollectionResponseBuilder[(Interface::Location | Interface::LocationLink)]
30
20
  @dispatcher = dispatcher
31
21
 
32
22
  char_position, _ = document.find_index_by_position(position)
@@ -100,10 +90,11 @@ module RubyLsp
100
90
  end
101
91
  end
102
92
 
103
- @target = T.let(target, T.nilable(Prism::Node))
93
+ @target = target #: Prism::Node?
104
94
  end
105
95
 
106
- sig { override.returns(T::Array[T.any(Interface::Location, Interface::LocationLink)]) }
96
+ # @override
97
+ #: -> Array[(Interface::Location | Interface::LocationLink)]
107
98
  def perform
108
99
  @dispatcher.dispatch_once(@target) if @target
109
100
  @response_builder.response
@@ -111,7 +102,7 @@ module RubyLsp
111
102
 
112
103
  private
113
104
 
114
- sig { params(position: T::Hash[Symbol, T.untyped], target: T.nilable(Prism::Node)).returns(T::Boolean) }
105
+ #: (Hash[Symbol, untyped] position, Prism::Node? target) -> bool
115
106
  def position_outside_target?(position, target)
116
107
  case target
117
108
  when Prism::GlobalVariableAndWriteNode,
@@ -7,12 +7,8 @@ module RubyLsp
7
7
  # [diagnostics](https://microsoft.github.io/language-server-protocol/specification#textDocument_publishDiagnostics)
8
8
  # request informs the editor of RuboCop offenses for a given file.
9
9
  class Diagnostics < Request
10
- extend T::Sig
11
-
12
10
  class << self
13
- extend T::Sig
14
-
15
- sig { returns(Interface::DiagnosticRegistrationOptions) }
11
+ #: -> Interface::DiagnosticRegistrationOptions
16
12
  def provider
17
13
  Interface::DiagnosticRegistrationOptions.new(
18
14
  document_selector: nil,
@@ -22,15 +18,16 @@ module RubyLsp
22
18
  end
23
19
  end
24
20
 
25
- sig { params(global_state: GlobalState, document: RubyDocument).void }
21
+ #: (GlobalState global_state, RubyDocument document) -> void
26
22
  def initialize(global_state, document)
27
23
  super()
28
- @active_linters = T.let(global_state.active_linters, T::Array[Support::Formatter])
24
+ @active_linters = global_state.active_linters #: Array[Support::Formatter]
29
25
  @document = document
30
- @uri = T.let(document.uri, URI::Generic)
26
+ @uri = document.uri #: URI::Generic
31
27
  end
32
28
 
33
- sig { override.returns(T.nilable(T.all(T::Array[Interface::Diagnostic], Object))) }
29
+ # @override
30
+ #: -> (Array[Interface::Diagnostic] & Object)?
34
31
  def perform
35
32
  diagnostics = []
36
33
  diagnostics.concat(syntax_error_diagnostics, syntax_warning_diagnostics)
@@ -50,7 +47,7 @@ module RubyLsp
50
47
 
51
48
  private
52
49
 
53
- sig { returns(T::Array[Interface::Diagnostic]) }
50
+ #: -> Array[Interface::Diagnostic]
54
51
  def syntax_warning_diagnostics
55
52
  @document.parse_result.warnings.map do |warning|
56
53
  location = warning.location
@@ -73,7 +70,7 @@ module RubyLsp
73
70
  end
74
71
  end
75
72
 
76
- sig { returns(T::Array[Interface::Diagnostic]) }
73
+ #: -> Array[Interface::Diagnostic]
77
74
  def syntax_error_diagnostics
78
75
  @document.parse_result.errors.map do |error|
79
76
  location = error.location
@@ -0,0 +1,75 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require "ruby_lsp/listeners/test_discovery"
5
+ require "ruby_lsp/listeners/test_style"
6
+ require "ruby_lsp/listeners/spec_style"
7
+
8
+ module RubyLsp
9
+ module Requests
10
+ # This is a custom request to ask the server to parse a test file and discover all available examples in it. Add-ons
11
+ # can augment the behavior through listeners, allowing them to handle discovery for different frameworks
12
+ class DiscoverTests < Request
13
+ include Support::Common
14
+
15
+ #: (GlobalState global_state, RubyDocument document, Prism::Dispatcher dispatcher) -> void
16
+ def initialize(global_state, document, dispatcher)
17
+ super()
18
+ @global_state = global_state
19
+ @document = document
20
+ @dispatcher = dispatcher
21
+ @response_builder = ResponseBuilders::TestCollection.new #: ResponseBuilders::TestCollection
22
+ @index = global_state.index #: RubyIndexer::Index
23
+ end
24
+
25
+ # @override
26
+ #: -> Array[Support::TestItem]
27
+ def perform
28
+ uri = @document.uri
29
+
30
+ # We normally only index test files once they are opened in the editor to save memory and avoid doing
31
+ # unnecessary work. If the file is already opened and we already indexed it, then we can just discover the tests
32
+ # straight away.
33
+ #
34
+ # However, if the user navigates to a specific test file from the explorer with nothing opened in the UI, then
35
+ # we will not have indexed the test file yet and trying to linearize the ancestor of the class will fail. In
36
+ # this case, we have to instantiate the indexer listener first, so that we insert classes, modules and methods
37
+ # in the index first and then discover the tests, all in the same traversal.
38
+ if @index.entries_for(uri.to_s)
39
+ Listeners::TestStyle.new(@response_builder, @global_state, @dispatcher, @document.uri)
40
+ Listeners::SpecStyle.new(@response_builder, @global_state, @dispatcher, @document.uri)
41
+
42
+ Addon.addons.each do |addon|
43
+ addon.create_discover_tests_listener(@response_builder, @dispatcher, @document.uri)
44
+ end
45
+
46
+ @dispatcher.visit(@document.parse_result.value)
47
+ else
48
+ @global_state.synchronize do
49
+ RubyIndexer::DeclarationListener.new(
50
+ @index,
51
+ @dispatcher,
52
+ @document.parse_result,
53
+ uri,
54
+ collect_comments: true,
55
+ )
56
+
57
+ Listeners::TestStyle.new(@response_builder, @global_state, @dispatcher, @document.uri)
58
+ Listeners::SpecStyle.new(@response_builder, @global_state, @dispatcher, @document.uri)
59
+
60
+ Addon.addons.each do |addon|
61
+ addon.create_discover_tests_listener(@response_builder, @dispatcher, @document.uri)
62
+ end
63
+
64
+ # Dispatch the events both for indexing the test file and discovering the tests. The order here is
65
+ # important because we need the index to be aware of the existing classes/modules/methods before the test
66
+ # listeners can do their work
67
+ @dispatcher.visit(@document.parse_result.value)
68
+ end
69
+ end
70
+
71
+ @response_builder.response
72
+ end
73
+ end
74
+ end
75
+ end
@@ -13,16 +13,7 @@ module RubyLsp
13
13
  # For writable elements like constants or variables, their read/write occurrences should be highlighted differently.
14
14
  # This is achieved by sending different "kind" attributes to the editor (2 for read and 3 for write).
15
15
  class DocumentHighlight < Request
16
- extend T::Sig
17
-
18
- sig do
19
- params(
20
- global_state: GlobalState,
21
- document: T.any(RubyDocument, ERBDocument),
22
- position: T::Hash[Symbol, T.untyped],
23
- dispatcher: Prism::Dispatcher,
24
- ).void
25
- end
16
+ #: (GlobalState global_state, (RubyDocument | ERBDocument) document, Hash[Symbol, untyped] position, Prism::Dispatcher dispatcher) -> void
26
17
  def initialize(global_state, document, position, dispatcher)
27
18
  super()
28
19
  char_position, _ = document.find_index_by_position(position)
@@ -34,10 +25,8 @@ module RubyLsp
34
25
  code_units_cache: document.code_units_cache,
35
26
  )
36
27
 
37
- @response_builder = T.let(
38
- ResponseBuilders::CollectionResponseBuilder[Interface::DocumentHighlight].new,
39
- ResponseBuilders::CollectionResponseBuilder[Interface::DocumentHighlight],
40
- )
28
+ @response_builder = ResponseBuilders::CollectionResponseBuilder[Interface::DocumentHighlight]
29
+ .new #: ResponseBuilders::CollectionResponseBuilder[Interface::DocumentHighlight]
41
30
  Listeners::DocumentHighlight.new(
42
31
  @response_builder,
43
32
  node_context.node,
@@ -47,7 +36,8 @@ module RubyLsp
47
36
  )
48
37
  end
49
38
 
50
- sig { override.returns(T::Array[Interface::DocumentHighlight]) }
39
+ # @override
40
+ #: -> Array[Interface::DocumentHighlight]
51
41
  def perform
52
42
  @response_builder.response
53
43
  end
@@ -9,34 +9,23 @@ module RubyLsp
9
9
  # makes `# source://PATH_TO_FILE#line` comments in a Ruby/RBI file clickable if the file exists.
10
10
  # When the user clicks the link, it'll open that location.
11
11
  class DocumentLink < Request
12
- extend T::Sig
13
-
14
12
  class << self
15
- extend T::Sig
16
-
17
- sig { returns(Interface::DocumentLinkOptions) }
13
+ #: -> Interface::DocumentLinkOptions
18
14
  def provider
19
15
  Interface::DocumentLinkOptions.new(resolve_provider: false)
20
16
  end
21
17
  end
22
18
 
23
- sig do
24
- params(
25
- uri: URI::Generic,
26
- comments: T::Array[Prism::Comment],
27
- dispatcher: Prism::Dispatcher,
28
- ).void
29
- end
19
+ #: (URI::Generic uri, Array[Prism::Comment] comments, Prism::Dispatcher dispatcher) -> void
30
20
  def initialize(uri, comments, dispatcher)
31
21
  super()
32
- @response_builder = T.let(
33
- ResponseBuilders::CollectionResponseBuilder[Interface::DocumentLink].new,
34
- ResponseBuilders::CollectionResponseBuilder[Interface::DocumentLink],
35
- )
22
+ @response_builder = ResponseBuilders::CollectionResponseBuilder[Interface::DocumentLink]
23
+ .new #: ResponseBuilders::CollectionResponseBuilder[Interface::DocumentLink]
36
24
  Listeners::DocumentLink.new(@response_builder, uri, comments, dispatcher)
37
25
  end
38
26
 
39
- sig { override.returns(T::Array[Interface::DocumentLink]) }
27
+ # @override
28
+ #: -> Array[Interface::DocumentLink]
40
29
  def perform
41
30
  @response_builder.response
42
31
  end
@@ -13,21 +13,17 @@ module RubyLsp
13
13
  # In VS Code, symbol search known as 'Go To Symbol in Editor' and can be accessed with Ctrl/Cmd-Shift-O,
14
14
  # or by opening the command palette and inserting an `@` symbol.
15
15
  class DocumentSymbol < Request
16
- extend T::Sig
17
-
18
16
  class << self
19
- extend T::Sig
20
-
21
- sig { returns(Interface::DocumentSymbolOptions) }
17
+ #: -> Interface::DocumentSymbolOptions
22
18
  def provider
23
19
  Interface::DocumentSymbolOptions.new
24
20
  end
25
21
  end
26
22
 
27
- sig { params(uri: URI::Generic, dispatcher: Prism::Dispatcher).void }
23
+ #: (URI::Generic uri, Prism::Dispatcher dispatcher) -> void
28
24
  def initialize(uri, dispatcher)
29
25
  super()
30
- @response_builder = T.let(ResponseBuilders::DocumentSymbol.new, ResponseBuilders::DocumentSymbol)
26
+ @response_builder = ResponseBuilders::DocumentSymbol.new #: ResponseBuilders::DocumentSymbol
31
27
  Listeners::DocumentSymbol.new(@response_builder, uri, dispatcher)
32
28
 
33
29
  Addon.addons.each do |addon|
@@ -35,7 +31,8 @@ module RubyLsp
35
31
  end
36
32
  end
37
33
 
38
- sig { override.returns(T::Array[Interface::DocumentSymbol]) }
34
+ # @override
35
+ #: -> Array[Interface::DocumentSymbol]
39
36
  def perform
40
37
  @response_builder.response
41
38
  end
@@ -8,31 +8,23 @@ module RubyLsp
8
8
  # The [folding ranges](https://microsoft.github.io/language-server-protocol/specification#textDocument_foldingRange)
9
9
  # request informs the editor of the ranges where and how code can be folded.
10
10
  class FoldingRanges < Request
11
- extend T::Sig
12
-
13
11
  class << self
14
- extend T::Sig
15
-
16
- sig { returns(TrueClass) }
12
+ #: -> TrueClass
17
13
  def provider
18
14
  true
19
15
  end
20
16
  end
21
17
 
22
- sig { params(comments: T::Array[Prism::Comment], dispatcher: Prism::Dispatcher).void }
18
+ #: (Array[Prism::Comment] comments, Prism::Dispatcher dispatcher) -> void
23
19
  def initialize(comments, dispatcher)
24
20
  super()
25
- @response_builder = T.let(
26
- ResponseBuilders::CollectionResponseBuilder[Interface::FoldingRange].new,
27
- ResponseBuilders::CollectionResponseBuilder[Interface::FoldingRange],
28
- )
29
- @listener = T.let(
30
- Listeners::FoldingRanges.new(@response_builder, comments, dispatcher),
31
- Listeners::FoldingRanges,
32
- )
21
+ @response_builder = ResponseBuilders::CollectionResponseBuilder[Interface::FoldingRange]
22
+ .new #: ResponseBuilders::CollectionResponseBuilder[Interface::FoldingRange]
23
+ @listener = Listeners::FoldingRanges.new(@response_builder, comments, dispatcher) #: Listeners::FoldingRanges
33
24
  end
34
25
 
35
- sig { override.returns(T::Array[Interface::FoldingRange]) }
26
+ # @override
27
+ #: -> Array[Interface::FoldingRange]
36
28
  def perform
37
29
  @listener.finalize_response!
38
30
  @response_builder.response
@@ -7,28 +7,25 @@ module RubyLsp
7
7
  # request uses RuboCop to fix auto-correctable offenses in the document. This requires enabling format on save and
8
8
  # registering the ruby-lsp as the Ruby formatter.
9
9
  class Formatting < Request
10
- extend T::Sig
11
-
12
10
  class Error < StandardError; end
13
11
 
14
12
  class << self
15
- extend T::Sig
16
-
17
- sig { returns(TrueClass) }
13
+ #: -> TrueClass
18
14
  def provider
19
15
  true
20
16
  end
21
17
  end
22
18
 
23
- sig { params(global_state: GlobalState, document: RubyDocument).void }
19
+ #: (GlobalState global_state, RubyDocument document) -> void
24
20
  def initialize(global_state, document)
25
21
  super()
26
22
  @document = document
27
- @active_formatter = T.let(global_state.active_formatter, T.nilable(Support::Formatter))
28
- @uri = T.let(document.uri, URI::Generic)
23
+ @active_formatter = global_state.active_formatter #: Support::Formatter?
24
+ @uri = document.uri #: URI::Generic
29
25
  end
30
26
 
31
- sig { override.returns(T.nilable(T.all(T::Array[Interface::TextEdit], Object))) }
27
+ # @override
28
+ #: -> (Array[Interface::TextEdit] & Object)?
32
29
  def perform
33
30
  return unless @active_formatter
34
31
  return if @document.syntax_error?
@@ -0,0 +1,87 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module RubyLsp
5
+ module Requests
6
+ # GoTo Relevant File is a custom [LSP
7
+ # request](https://microsoft.github.io/language-server-protocol/specification#requestMessage)
8
+ # that navigates to the relevant file for the current document.
9
+ # Currently, it supports source code file <> test file navigation.
10
+ class GoToRelevantFile < Request
11
+ extend T::Sig
12
+
13
+ TEST_KEYWORDS = ["test", "spec", "integration_test"]
14
+
15
+ TEST_PREFIX_PATTERN = /^(#{TEST_KEYWORDS.join("_|")}_)/
16
+ TEST_SUFFIX_PATTERN = /(_#{TEST_KEYWORDS.join("|_")})$/
17
+ TEST_PATTERN = /#{TEST_PREFIX_PATTERN}|#{TEST_SUFFIX_PATTERN}/
18
+
19
+ TEST_PREFIX_GLOB = "#{TEST_KEYWORDS.join("_,")}_" #: String
20
+ TEST_SUFFIX_GLOB = "_#{TEST_KEYWORDS.join(",_")}" #: String
21
+
22
+ #: (String path, String workspace_path) -> void
23
+ def initialize(path, workspace_path)
24
+ super()
25
+
26
+ @workspace_path = workspace_path
27
+ @path = path.delete_prefix(workspace_path) #: String
28
+ end
29
+
30
+ # @override
31
+ #: -> Array[String]
32
+ def perform
33
+ find_relevant_paths
34
+ end
35
+
36
+ private
37
+
38
+ #: -> Array[String]
39
+ def find_relevant_paths
40
+ candidate_paths = Dir.glob(File.join("**", relevant_filename_pattern))
41
+ return [] if candidate_paths.empty?
42
+
43
+ find_most_similar_with_jaccard(candidate_paths).map { File.join(@workspace_path, _1) }
44
+ end
45
+
46
+ #: -> String
47
+ def relevant_filename_pattern
48
+ input_basename = File.basename(@path, File.extname(@path))
49
+
50
+ relevant_basename_pattern =
51
+ if input_basename.match?(TEST_PATTERN)
52
+ input_basename.gsub(TEST_PATTERN, "")
53
+ else
54
+ "{{#{TEST_PREFIX_GLOB}}#{input_basename},#{input_basename}{#{TEST_SUFFIX_GLOB}}}"
55
+ end
56
+
57
+ "#{relevant_basename_pattern}#{File.extname(@path)}"
58
+ end
59
+
60
+ # Using the Jaccard algorithm to determine the similarity between the
61
+ # input path and the candidate relevant file paths.
62
+ # Ref: https://en.wikipedia.org/wiki/Jaccard_index
63
+ # The main idea of this algorithm is to take the size of interaction and divide
64
+ # it by the size of union between two sets (in our case the elements in each set
65
+ # would be the parts of the path separated by path divider.)
66
+ #: (Array[String] candidates) -> Array[String]
67
+ def find_most_similar_with_jaccard(candidates)
68
+ dirs = get_dir_parts(@path)
69
+
70
+ _, results = candidates
71
+ .group_by do |other_path|
72
+ other_dirs = get_dir_parts(other_path)
73
+ # Similarity score between the two directories
74
+ (dirs & other_dirs).size.to_f / (dirs | other_dirs).size
75
+ end
76
+ .max_by(&:first)
77
+
78
+ results || []
79
+ end
80
+
81
+ #: (String path) -> Set[String]
82
+ def get_dir_parts(path)
83
+ Set.new(File.dirname(path).split(File::SEPARATOR))
84
+ end
85
+ end
86
+ end
87
+ end
@@ -8,13 +8,10 @@ module RubyLsp
8
8
  # The [hover request](https://microsoft.github.io/language-server-protocol/specification#textDocument_hover)
9
9
  # displays the documentation for the symbol currently under the cursor.
10
10
  class Hover < Request
11
- extend T::Sig
12
11
  extend T::Generic
13
12
 
14
13
  class << self
15
- extend T::Sig
16
-
17
- sig { returns(Interface::HoverOptions) }
14
+ #: -> Interface::HoverOptions
18
15
  def provider
19
16
  Interface::HoverOptions.new
20
17
  end
@@ -22,15 +19,7 @@ module RubyLsp
22
19
 
23
20
  ResponseType = type_member { { fixed: T.nilable(Interface::Hover) } }
24
21
 
25
- sig do
26
- params(
27
- document: T.any(RubyDocument, ERBDocument),
28
- global_state: GlobalState,
29
- position: T::Hash[Symbol, T.untyped],
30
- dispatcher: Prism::Dispatcher,
31
- sorbet_level: RubyDocument::SorbetLevel,
32
- ).void
33
- end
22
+ #: ((RubyDocument | ERBDocument) document, GlobalState global_state, Hash[Symbol, untyped] position, Prism::Dispatcher dispatcher, RubyDocument::SorbetLevel sorbet_level) -> void
34
23
  def initialize(document, global_state, position, dispatcher, sorbet_level)
35
24
  super()
36
25
 
@@ -59,9 +48,9 @@ module RubyLsp
59
48
  # Don't need to instantiate any listeners if there's no target
60
49
  return unless target
61
50
 
62
- @target = T.let(target, T.nilable(Prism::Node))
51
+ @target = target #: Prism::Node?
63
52
  uri = document.uri
64
- @response_builder = T.let(ResponseBuilders::Hover.new, ResponseBuilders::Hover)
53
+ @response_builder = ResponseBuilders::Hover.new #: ResponseBuilders::Hover
65
54
  Listeners::Hover.new(@response_builder, global_state, uri, node_context, dispatcher, sorbet_level)
66
55
  Addon.addons.each do |addon|
67
56
  addon.create_hover_listener(@response_builder, node_context, dispatcher)
@@ -70,7 +59,8 @@ module RubyLsp
70
59
  @dispatcher = dispatcher
71
60
  end
72
61
 
73
- sig { override.returns(ResponseType) }
62
+ # @override
63
+ #: -> ResponseType
74
64
  def perform
75
65
  return unless @target
76
66
 
@@ -88,14 +78,14 @@ module RubyLsp
88
78
 
89
79
  private
90
80
 
91
- sig { params(parent: T.nilable(Prism::Node), target: T.nilable(Prism::Node)).returns(T::Boolean) }
81
+ #: (Prism::Node? parent, Prism::Node? target) -> bool
92
82
  def should_refine_target?(parent, target)
93
83
  (Listeners::Hover::ALLOWED_TARGETS.include?(parent.class) &&
94
84
  !Listeners::Hover::ALLOWED_TARGETS.include?(target.class)) ||
95
85
  (parent.is_a?(Prism::ConstantPathNode) && target.is_a?(Prism::ConstantReadNode))
96
86
  end
97
87
 
98
- sig { params(position: T::Hash[Symbol, T.untyped], target: T.nilable(Prism::Node)).returns(T::Boolean) }
88
+ #: (Hash[Symbol, untyped] position, Prism::Node? target) -> bool
99
89
  def position_outside_target?(position, target)
100
90
  case target
101
91
  when Prism::GlobalVariableAndWriteNode,
@@ -9,35 +9,24 @@ module RubyLsp
9
9
  # are labels added directly in the code that explicitly show the user something that might
10
10
  # otherwise just be implied.
11
11
  class InlayHints < Request
12
- extend T::Sig
13
-
14
12
  class << self
15
- extend T::Sig
16
-
17
- sig { returns(Interface::InlayHintOptions) }
13
+ #: -> Interface::InlayHintOptions
18
14
  def provider
19
15
  Interface::InlayHintOptions.new(resolve_provider: false)
20
16
  end
21
17
  end
22
18
 
23
- sig do
24
- params(
25
- document: T.any(RubyDocument, ERBDocument),
26
- hints_configuration: RequestConfig,
27
- dispatcher: Prism::Dispatcher,
28
- ).void
29
- end
19
+ #: ((RubyDocument | ERBDocument) document, RequestConfig hints_configuration, Prism::Dispatcher dispatcher) -> void
30
20
  def initialize(document, hints_configuration, dispatcher)
31
21
  super()
32
22
 
33
- @response_builder = T.let(
34
- ResponseBuilders::CollectionResponseBuilder[Interface::InlayHint].new,
35
- ResponseBuilders::CollectionResponseBuilder[Interface::InlayHint],
36
- )
23
+ @response_builder = ResponseBuilders::CollectionResponseBuilder[Interface::InlayHint]
24
+ .new #: ResponseBuilders::CollectionResponseBuilder[Interface::InlayHint]
37
25
  Listeners::InlayHints.new(@response_builder, hints_configuration, dispatcher)
38
26
  end
39
27
 
40
- sig { override.returns(T::Array[Interface::InlayHint]) }
28
+ # @override
29
+ #: -> Array[Interface::InlayHint]
41
30
  def perform
42
31
  @response_builder.response
43
32
  end