rucoa 0.10.0 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +8 -2
  3. data/Gemfile.lock +3 -3
  4. data/README.md +4 -1
  5. data/data/definitions_ruby_3_1 +6594 -6594
  6. data/lib/rucoa/configuration.rb +22 -12
  7. data/lib/rucoa/definition_store.rb +270 -158
  8. data/lib/rucoa/definitions/method_definition.rb +11 -11
  9. data/lib/rucoa/definitions/module_definition.rb +14 -0
  10. data/lib/rucoa/handler_concerns/configuration_requestable.rb +7 -7
  11. data/lib/rucoa/handler_concerns/diagnostics_publishable.rb +11 -11
  12. data/lib/rucoa/handler_concerns/text_document_position_parameters.rb +29 -0
  13. data/lib/rucoa/handler_concerns/text_document_uri_parameters.rb +21 -0
  14. data/lib/rucoa/handler_concerns.rb +2 -0
  15. data/lib/rucoa/handlers/base.rb +9 -9
  16. data/lib/rucoa/handlers/initialize_handler.rb +1 -0
  17. data/lib/rucoa/handlers/initialized_handler.rb +16 -16
  18. data/lib/rucoa/handlers/text_document_code_action_handler.rb +12 -12
  19. data/lib/rucoa/handlers/text_document_completion_handler.rb +85 -99
  20. data/lib/rucoa/handlers/text_document_definition_handler.rb +11 -30
  21. data/lib/rucoa/handlers/text_document_did_close_handler.rb +2 -8
  22. data/lib/rucoa/handlers/text_document_did_open_handler.rb +3 -7
  23. data/lib/rucoa/handlers/text_document_document_highlight_handler.rb +256 -0
  24. data/lib/rucoa/handlers/text_document_document_symbol_handler.rb +47 -76
  25. data/lib/rucoa/handlers/text_document_formatting_handler.rb +15 -23
  26. data/lib/rucoa/handlers/text_document_hover_handler.rb +24 -43
  27. data/lib/rucoa/handlers/text_document_range_formatting_handler.rb +17 -25
  28. data/lib/rucoa/handlers/text_document_selection_range_handler.rb +11 -19
  29. data/lib/rucoa/handlers/text_document_signature_help_handler.rb +17 -36
  30. data/lib/rucoa/handlers.rb +1 -0
  31. data/lib/rucoa/node_concerns/body.rb +1 -1
  32. data/lib/rucoa/node_concerns/qualified_name.rb +5 -5
  33. data/lib/rucoa/node_concerns/rescue.rb +21 -0
  34. data/lib/rucoa/node_concerns.rb +1 -0
  35. data/lib/rucoa/node_inspector.rb +17 -6
  36. data/lib/rucoa/nodes/base.rb +48 -48
  37. data/lib/rucoa/nodes/begin_node.rb +2 -0
  38. data/lib/rucoa/nodes/block_node.rb +24 -0
  39. data/lib/rucoa/nodes/case_node.rb +24 -0
  40. data/lib/rucoa/nodes/const_node.rb +26 -26
  41. data/lib/rucoa/nodes/def_node.rb +14 -13
  42. data/lib/rucoa/nodes/ensure_node.rb +19 -0
  43. data/lib/rucoa/nodes/for_node.rb +8 -0
  44. data/lib/rucoa/nodes/if_node.rb +32 -0
  45. data/lib/rucoa/nodes/resbody_node.rb +8 -0
  46. data/lib/rucoa/nodes/rescue_node.rb +17 -0
  47. data/lib/rucoa/nodes/send_node.rb +40 -0
  48. data/lib/rucoa/nodes/until_node.rb +8 -0
  49. data/lib/rucoa/nodes/when_node.rb +8 -0
  50. data/lib/rucoa/nodes/while_node.rb +8 -0
  51. data/lib/rucoa/nodes.rb +10 -1
  52. data/lib/rucoa/parser_builder.rb +16 -2
  53. data/lib/rucoa/range.rb +9 -3
  54. data/lib/rucoa/rbs/class_definition_mapper.rb +1 -0
  55. data/lib/rucoa/rbs/constant_definition_mapper.rb +5 -5
  56. data/lib/rucoa/rbs/method_definition_mapper.rb +18 -18
  57. data/lib/rucoa/rbs/module_definition_mapper.rb +17 -12
  58. data/lib/rucoa/rbs/ruby_definitions_loader.rb +5 -5
  59. data/lib/rucoa/rubocop/configuration_checker.rb +7 -7
  60. data/lib/rucoa/server.rb +25 -24
  61. data/lib/rucoa/source.rb +5 -5
  62. data/lib/rucoa/source_store.rb +8 -8
  63. data/lib/rucoa/version.rb +1 -1
  64. data/lib/rucoa/yard/definition_generators/attribute_reader_definition_generator.rb +17 -1
  65. data/lib/rucoa/yard/definition_generators/attribute_writer_definition_generator.rb +17 -1
  66. data/lib/rucoa/yard/definition_generators/class_definition_generator.rb +1 -0
  67. data/lib/rucoa/yard/definition_generators/method_definition_generator.rb +14 -1
  68. data/lib/rucoa/yard/definition_generators/module_definition_generator.rb +6 -0
  69. metadata +16 -3
  70. data/lib/rucoa/nodes/defs_node.rb +0 -33
@@ -5,17 +5,6 @@ module Rucoa
5
5
  module DiagnosticsPublishable
6
6
  private
7
7
 
8
- # @param uri [String]
9
- # @return [Array<Hash>]
10
- def diagnostics_on(uri)
11
- return [] unless configuration.enables_diagnostics?
12
-
13
- DiagnosticProvider.call(
14
- source: source_store.get(uri),
15
- uri: uri
16
- )
17
- end
18
-
19
8
  # @param uri [String]
20
9
  # @return [void]
21
10
  def clear_diagnostics_on(uri)
@@ -28,6 +17,17 @@ module Rucoa
28
17
  )
29
18
  end
30
19
 
20
+ # @param uri [String]
21
+ # @return [Array<Hash>]
22
+ def diagnostics_on(uri)
23
+ return [] unless configuration.enables_diagnostics?
24
+
25
+ DiagnosticProvider.call(
26
+ source: source_store.get(uri),
27
+ uri: uri
28
+ )
29
+ end
30
+
31
31
  # @param uri [String]
32
32
  # @return [void]
33
33
  def publish_diagnostics_on(uri)
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rucoa
4
+ module HandlerConcerns
5
+ module TextDocumentPositionParameters
6
+ private
7
+
8
+ # @return [Rucoa::Nodes::Base, nil]
9
+ def node
10
+ return unless position
11
+ return unless source
12
+
13
+ @node ||= source.node_at(position)
14
+ end
15
+
16
+ # @return [String, nil]
17
+ def parameter_position
18
+ @parameter_position ||= request.dig('params', 'position')
19
+ end
20
+
21
+ # @return [Rucoa::Position, nil]
22
+ def position
23
+ return unless parameter_position
24
+
25
+ @position ||= Position.from_vscode_position(parameter_position)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rucoa
4
+ module HandlerConcerns
5
+ module TextDocumentUriParameters
6
+ private
7
+
8
+ # @return [String, nil]
9
+ def parameter_uri
10
+ @parameter_uri ||= request.dig('params', 'textDocument', 'uri')
11
+ end
12
+
13
+ # @return [Rucoa::Source, nil]
14
+ def source
15
+ return unless parameter_uri
16
+
17
+ @source ||= source_store.get(parameter_uri)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -4,5 +4,7 @@ module Rucoa
4
4
  module HandlerConcerns
5
5
  autoload :ConfigurationRequestable, 'rucoa/handler_concerns/configuration_requestable'
6
6
  autoload :DiagnosticsPublishable, 'rucoa/handler_concerns/diagnostics_publishable'
7
+ autoload :TextDocumentPositionParameters, 'rucoa/handler_concerns/text_document_position_parameters'
8
+ autoload :TextDocumentUriParameters, 'rucoa/handler_concerns/text_document_uri_parameters'
7
9
  end
8
10
  end
@@ -41,15 +41,6 @@ module Rucoa
41
41
  # @return [Rucoa::Server]
42
42
  attr_reader :server
43
43
 
44
- # @param message [Hash]
45
- # @return [void]
46
- def respond(message)
47
- write(
48
- id: request['id'],
49
- result: message
50
- )
51
- end
52
-
53
44
  # @return [Rucoa::Configuration]
54
45
  def configuration
55
46
  @server.configuration
@@ -60,6 +51,15 @@ module Rucoa
60
51
  @server.definition_store
61
52
  end
62
53
 
54
+ # @param message [Hash]
55
+ # @return [void]
56
+ def respond(message)
57
+ write(
58
+ id: request['id'],
59
+ result: message
60
+ )
61
+ end
62
+
63
63
  # @return [Rucoa::SourceStore]
64
64
  def source_store
65
65
  @server.source_store
@@ -15,6 +15,7 @@ module Rucoa
15
15
  },
16
16
  definitionProvider: true,
17
17
  documentFormattingProvider: true,
18
+ documentHighlightProvider: true,
18
19
  documentRangeFormattingProvider: true,
19
20
  documentSymbolProvider: true,
20
21
  hoverProvider: true,
@@ -13,18 +13,14 @@ module Rucoa
13
13
 
14
14
  private
15
15
 
16
- # @return [void]
17
- def update_definition_store
18
- sources.each do |source|
19
- definition_store.update_from(source)
20
- end
16
+ # @return [String]
17
+ def glob
18
+ ::File.expand_path('{app,lib}/**/*.rb')
21
19
  end
22
20
 
23
- # @return [void]
24
- def update_source_store
25
- sources.each do |source|
26
- source_store.update(source)
27
- end
21
+ # @return [Array<Pathname>]
22
+ def pathnames
23
+ ::Pathname.glob(glob)
28
24
  end
29
25
 
30
26
  # @return [Array<Rucoa::Source>]
@@ -37,14 +33,18 @@ module Rucoa
37
33
  end
38
34
  end
39
35
 
40
- # @return [Array<Pathname>]
41
- def pathnames
42
- ::Pathname.glob(glob)
36
+ # @return [void]
37
+ def update_definition_store
38
+ sources.each do |source|
39
+ definition_store.update_from(source)
40
+ end
43
41
  end
44
42
 
45
- # @return [String]
46
- def glob
47
- ::File.expand_path('{app,lib}/**/*.rb')
43
+ # @return [void]
44
+ def update_source_store
45
+ sources.each do |source|
46
+ source_store.update(source)
47
+ end
48
48
  end
49
49
  end
50
50
  end
@@ -59,6 +59,16 @@ module Rucoa
59
59
 
60
60
  private
61
61
 
62
+ # @return [String]
63
+ def cop_name
64
+ @diagnostic.dig('data', 'cop_name')
65
+ end
66
+
67
+ # @return [Array]
68
+ def diagnostics
69
+ [@diagnostic]
70
+ end
71
+
62
72
  # @return [Hash]
63
73
  def edit
64
74
  {
@@ -75,13 +85,8 @@ module Rucoa
75
85
  end
76
86
 
77
87
  # @return [String]
78
- def cop_name
79
- @diagnostic.dig('data', 'cop_name')
80
- end
81
-
82
- # @return [Array]
83
- def diagnostics
84
- [@diagnostic]
88
+ def kind
89
+ 'quickfix'
85
90
  end
86
91
 
87
92
  # @return [Boolean]
@@ -89,11 +94,6 @@ module Rucoa
89
94
  true
90
95
  end
91
96
 
92
- # @return [String]
93
- def kind
94
- 'quickfix'
95
- end
96
-
97
97
  # @return [String]
98
98
  def title
99
99
  "Autocorrect #{cop_name}"
@@ -3,6 +3,9 @@
3
3
  module Rucoa
4
4
  module Handlers
5
5
  class TextDocumentCompletionHandler < Base
6
+ include HandlerConcerns::TextDocumentPositionParameters
7
+ include HandlerConcerns::TextDocumentUriParameters
8
+
6
9
  COMPLETION_ITEM_KIND_FOR_TEXT = 1
7
10
  COMPLETION_ITEM_KIND_FOR_METHOD = 2
8
11
  COMPLETION_ITEM_KIND_FOR_FUNCTION = 3
@@ -38,6 +41,42 @@ module Rucoa
38
41
 
39
42
  private
40
43
 
44
+ # @return [Array<String>]
45
+ def callable_method_definitions
46
+ receiver_types.flat_map do |type|
47
+ definition_store.instance_method_definitions_of(type)
48
+ end
49
+ end
50
+
51
+ # @return [Array<String>]
52
+ def callable_method_names
53
+ callable_method_definitions.map(&:method_name).uniq
54
+ end
55
+
56
+ # @return [Array<String>]
57
+ def completable_constant_names
58
+ referrable_constant_names.select do |constant_name|
59
+ constant_name.start_with?(completion_head)
60
+ end.sort
61
+ end
62
+
63
+ # @return [Array<String>]
64
+ def completable_method_names
65
+ callable_method_names.select do |method_name|
66
+ method_name.start_with?(completion_head)
67
+ end.sort
68
+ end
69
+
70
+ # @return [String] e.g. "SE" to `File::SE|`, "ba" to `foo.ba|`
71
+ def completion_head
72
+ @completion_head ||=
73
+ if @repaired
74
+ ''
75
+ else
76
+ node.name
77
+ end
78
+ end
79
+
41
80
  # @return [Array<Hash>, nil]
42
81
  def completion_items
43
82
  return unless responsible?
@@ -56,37 +95,14 @@ module Rucoa
56
95
  end
57
96
  end
58
97
 
59
- # @return [Boolean]
60
- def responsible?
61
- configuration.enables_completion? &&
62
- !source.nil?
63
- end
64
-
65
- # @return [Rucoa::Source, nil]
66
- def source
67
- @source ||= source_store.get(uri)
68
- end
69
-
70
- # @return [Rucoa::Position]
71
- def position
72
- @position ||= Position.from_vscode_position(
73
- request.dig('params', 'position')
74
- )
75
- end
76
-
77
- # @return [String]
78
- def uri
79
- request.dig('params', 'textDocument', 'uri')
80
- end
81
-
82
98
  # @return [Array<Hash>]
83
- def completion_items_for_method
84
- completable_method_names.map do |method_name|
99
+ def completion_items_for_constant
100
+ completable_constant_names.map do |constant_name|
85
101
  {
86
- kind: COMPLETION_ITEM_KIND_FOR_METHOD,
87
- label: method_name,
102
+ kind: COMPLETION_ITEM_KIND_FOR_CONSTANT,
103
+ label: constant_name,
88
104
  textEdit: {
89
- newText: method_name,
105
+ newText: constant_name,
90
106
  range: range.to_vscode_range
91
107
  }
92
108
  }
@@ -94,72 +110,24 @@ module Rucoa
94
110
  end
95
111
 
96
112
  # @return [Array<Hash>]
97
- def completion_items_for_constant
98
- completable_constant_names.map do |constant_name|
113
+ def completion_items_for_method
114
+ completable_method_names.map do |method_name|
99
115
  {
100
- kind: COMPLETION_ITEM_KIND_FOR_CONSTANT,
101
- label: constant_name,
116
+ kind: COMPLETION_ITEM_KIND_FOR_METHOD,
117
+ label: method_name,
102
118
  textEdit: {
103
- newText: constant_name,
119
+ newText: method_name,
104
120
  range: range.to_vscode_range
105
121
  }
106
122
  }
107
123
  end
108
124
  end
109
125
 
110
- # @return [Array<String>]
111
- def completable_constant_names
112
- referrable_constant_names.select do |constant_name|
113
- constant_name.start_with?(completion_head)
114
- end.sort
115
- end
116
-
117
- # @return [String] e.g. "SE" to `File::SE|`, "ba" to `foo.ba|`
118
- def completion_head
119
- @completion_head ||=
120
- if @repaired
121
- ''
122
- else
123
- node.name
124
- end
125
- end
126
-
127
- def referrable_constant_names
128
- definition_store.constant_definitions_under(constant_namespace).map(&:name).uniq
129
- end
130
-
131
126
  # @return [String] e.g. "Foo::Bar" to `Foo::Bar.baz|`.
132
127
  def constant_namespace
133
128
  node.each_child_node(:const).map(&:name).reverse.join('::')
134
129
  end
135
130
 
136
- # @return [Array<String>]
137
- def completable_method_names
138
- callable_method_names.select do |method_name|
139
- method_name.start_with?(completion_head)
140
- end.sort
141
- end
142
-
143
- # @return [Array<String>]
144
- def callable_method_names
145
- callable_method_definitions.map(&:method_name).uniq
146
- end
147
-
148
- # @return [Array<String>]
149
- def callable_method_definitions
150
- receiver_types.flat_map do |type|
151
- definition_store.instance_method_definitions_of(type)
152
- end
153
- end
154
-
155
- # @return [Array<String>]
156
- def receiver_types
157
- NodeInspector.new(
158
- definition_store: definition_store,
159
- node: node
160
- ).method_receiver_types
161
- end
162
-
163
131
  # @return [Rucoa::Node, nil]
164
132
  def node
165
133
  @node ||=
@@ -176,9 +144,33 @@ module Rucoa
176
144
  source.node_at(position)
177
145
  end
178
146
 
179
- # @return [Rucoa::Node, nil]
180
- def repaired_node
181
- repaired_source.node_at(position)
147
+ # @return [Rucoa::Range]
148
+ def range
149
+ @range ||=
150
+ if @repaired
151
+ position.to_range
152
+ else
153
+ Range.from_parser_range(
154
+ case node
155
+ when Nodes::SendNode
156
+ node.location.selector
157
+ else
158
+ node.location.expression
159
+ end
160
+ )
161
+ end
162
+ end
163
+
164
+ # @return [Array<String>]
165
+ def receiver_types
166
+ NodeInspector.new(
167
+ definition_store: definition_store,
168
+ node: node
169
+ ).method_receiver_types
170
+ end
171
+
172
+ def referrable_constant_names
173
+ definition_store.constant_definitions_under(constant_namespace).map(&:name).uniq
182
174
  end
183
175
 
184
176
  # @return [void]
@@ -194,6 +186,11 @@ module Rucoa
194
186
  )
195
187
  end
196
188
 
189
+ # @return [Rucoa::Node, nil]
190
+ def repaired_node
191
+ repaired_source.node_at(position)
192
+ end
193
+
197
194
  # @return [Rucoa::Source]
198
195
  def repaired_source
199
196
  Source.new(
@@ -202,21 +199,10 @@ module Rucoa
202
199
  )
203
200
  end
204
201
 
205
- # @return [Rucoa::Range]
206
- def range
207
- @range ||=
208
- if @repaired
209
- position.to_range
210
- else
211
- Range.from_parser_range(
212
- case node
213
- when Nodes::SendNode
214
- node.location.selector
215
- else
216
- node.location.expression
217
- end
218
- )
219
- end
202
+ # @return [Boolean]
203
+ def responsible?
204
+ configuration.enables_completion? &&
205
+ !source.nil?
220
206
  end
221
207
  end
222
208
  end
@@ -3,12 +3,23 @@
3
3
  module Rucoa
4
4
  module Handlers
5
5
  class TextDocumentDefinitionHandler < Base
6
+ include HandlerConcerns::TextDocumentPositionParameters
7
+ include HandlerConcerns::TextDocumentUriParameters
8
+
6
9
  def call
7
10
  respond(location)
8
11
  end
9
12
 
10
13
  private
11
14
 
15
+ # @return [Rucoa::Definitions::Base, nil]
16
+ def definition
17
+ @definition ||= NodeInspector.new(
18
+ definition_store: definition_store,
19
+ node: node
20
+ ).definitions.first
21
+ end
22
+
12
23
  # @return [Hash, nil]
13
24
  def location
14
25
  return unless reponsible?
@@ -24,36 +35,6 @@ module Rucoa
24
35
  configuration.enables_definition? &&
25
36
  !definition&.location.nil?
26
37
  end
27
-
28
- # @return [Rucoa::Position]
29
- def position
30
- Position.from_vscode_position(
31
- request.dig('params', 'position')
32
- )
33
- end
34
-
35
- # @return [String]
36
- def uri
37
- request.dig('params', 'textDocument', 'uri')
38
- end
39
-
40
- # @return [Rucoa::Source, nil]
41
- def source
42
- source_store.get(uri)
43
- end
44
-
45
- # @return [Rucoa::Nodes::Base]
46
- def node
47
- source&.node_at(position)
48
- end
49
-
50
- # @return [Rucoa::Definitions::Base, nil]
51
- def definition
52
- @definition ||= NodeInspector.new(
53
- definition_store: definition_store,
54
- node: node
55
- ).definitions.first
56
- end
57
38
  end
58
39
  end
59
40
  end
@@ -4,16 +4,10 @@ module Rucoa
4
4
  module Handlers
5
5
  class TextDocumentDidCloseHandler < Base
6
6
  include HandlerConcerns::DiagnosticsPublishable
7
+ include HandlerConcerns::TextDocumentUriParameters
7
8
 
8
9
  def call
9
- clear_diagnostics_on(uri)
10
- end
11
-
12
- private
13
-
14
- # @return [String]
15
- def uri
16
- request.dig('params', 'textDocument', 'uri')
10
+ clear_diagnostics_on(parameter_uri)
17
11
  end
18
12
  end
19
13
  end
@@ -4,11 +4,12 @@ module Rucoa
4
4
  module Handlers
5
5
  class TextDocumentDidOpenHandler < Base
6
6
  include HandlerConcerns::DiagnosticsPublishable
7
+ include HandlerConcerns::TextDocumentUriParameters
7
8
 
8
9
  def call
9
10
  source_store.update(source)
10
11
  definition_store.update_from(source)
11
- publish_diagnostics_on(uri)
12
+ publish_diagnostics_on(parameter_uri)
12
13
  end
13
14
 
14
15
  private
@@ -17,7 +18,7 @@ module Rucoa
17
18
  def source
18
19
  @source ||= Source.new(
19
20
  content: text,
20
- uri: uri
21
+ uri: parameter_uri
21
22
  )
22
23
  end
23
24
 
@@ -25,11 +26,6 @@ module Rucoa
25
26
  def text
26
27
  request.dig('params', 'textDocument', 'text')
27
28
  end
28
-
29
- # @return [String]
30
- def uri
31
- @uri ||= request.dig('params', 'textDocument', 'uri')
32
- end
33
29
  end
34
30
  end
35
31
  end