rucoa 0.2.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +6 -0
  3. data/Gemfile +1 -0
  4. data/Gemfile.lock +11 -2
  5. data/README.md +39 -6
  6. data/data/definitions_ruby_3_1 +0 -0
  7. data/images/diagnostics.gif +0 -0
  8. data/images/document-formatting.gif +0 -0
  9. data/images/document-symbol.gif +0 -0
  10. data/images/selection-ranges.gif +0 -0
  11. data/lib/rucoa/cli.rb +2 -2
  12. data/lib/rucoa/configuration.rb +97 -0
  13. data/lib/rucoa/definition_archiver.rb +29 -0
  14. data/lib/rucoa/definition_builders/rbs_constant_definition_builder.rb +44 -0
  15. data/lib/rucoa/definition_builders/rbs_method_definition_builder.rb +106 -0
  16. data/lib/rucoa/definition_builders/yard_method_definition_builder.rb +191 -0
  17. data/lib/rucoa/definition_builders.rb +9 -0
  18. data/lib/rucoa/definition_store.rb +63 -0
  19. data/lib/rucoa/definitions/base.rb +12 -0
  20. data/lib/rucoa/definitions/constant_definition.rb +51 -0
  21. data/lib/rucoa/definitions/method_definition.rb +126 -0
  22. data/lib/rucoa/definitions/method_parameter_definition.rb +30 -0
  23. data/lib/rucoa/definitions.rb +9 -0
  24. data/lib/rucoa/handler_concerns/configuration_requestable.rb +35 -0
  25. data/lib/rucoa/handler_concerns/diagnostics_publishable.rb +172 -0
  26. data/lib/rucoa/handler_concerns.rb +8 -0
  27. data/lib/rucoa/handlers/base.rb +69 -0
  28. data/lib/rucoa/handlers/exit_handler.rb +11 -0
  29. data/lib/rucoa/handlers/initialize_handler.rb +29 -0
  30. data/lib/rucoa/handlers/initialized_handler.rb +23 -0
  31. data/lib/rucoa/handlers/shutdown_handler.rb +12 -0
  32. data/lib/rucoa/handlers/text_document_code_action_handler.rb +104 -0
  33. data/lib/rucoa/handlers/text_document_did_change_handler.rb +12 -0
  34. data/lib/rucoa/handlers/text_document_did_open_handler.rb +38 -0
  35. data/lib/rucoa/handlers/text_document_document_symbol_handler.rb +241 -0
  36. data/lib/rucoa/handlers/text_document_formatting_handler.rb +64 -0
  37. data/lib/rucoa/handlers/text_document_range_formatting_handler.rb +76 -0
  38. data/lib/rucoa/handlers/text_document_selection_range_handler.rb +141 -0
  39. data/lib/rucoa/handlers/text_document_signature_help_handler.rb +68 -0
  40. data/lib/rucoa/handlers/workspace_did_change_configuration_handler.rb +13 -0
  41. data/lib/rucoa/handlers.rb +20 -0
  42. data/lib/rucoa/message_reader.rb +3 -1
  43. data/lib/rucoa/message_writer.rb +3 -0
  44. data/lib/rucoa/node_concerns/name_full_qualifiable.rb +20 -0
  45. data/lib/rucoa/node_concerns.rb +7 -0
  46. data/lib/rucoa/node_inspector.rb +109 -0
  47. data/lib/rucoa/nodes/base.rb +43 -0
  48. data/lib/rucoa/nodes/casgn_node.rb +14 -0
  49. data/lib/rucoa/nodes/class_node.rb +8 -0
  50. data/lib/rucoa/nodes/const_node.rb +12 -0
  51. data/lib/rucoa/nodes/def_node.rb +71 -0
  52. data/lib/rucoa/nodes/defs_node.rb +12 -0
  53. data/lib/rucoa/nodes/lvar_node.rb +25 -0
  54. data/lib/rucoa/nodes/module_node.rb +21 -0
  55. data/lib/rucoa/nodes/sclass_node.rb +8 -0
  56. data/lib/rucoa/nodes/send_node.rb +60 -0
  57. data/lib/rucoa/nodes/str_node.rb +4 -0
  58. data/lib/rucoa/nodes/sym_node.rb +12 -0
  59. data/lib/rucoa/nodes.rb +10 -0
  60. data/lib/rucoa/parser_builder.rb +20 -10
  61. data/lib/rucoa/range.rb +89 -13
  62. data/lib/rucoa/rbs_document_loader.rb +43 -0
  63. data/lib/rucoa/rubocop_autocorrector.rb +1 -1
  64. data/lib/rucoa/rubocop_configuration_checker.rb +42 -0
  65. data/lib/rucoa/rubocop_investigator.rb +1 -1
  66. data/lib/rucoa/server.rb +96 -122
  67. data/lib/rucoa/source.rb +56 -6
  68. data/lib/rucoa/source_store.rb +11 -9
  69. data/lib/rucoa/types/method_type.rb +23 -0
  70. data/lib/rucoa/types.rb +7 -0
  71. data/lib/rucoa/version.rb +1 -1
  72. data/lib/rucoa/yard_glob_document_loader.rb +47 -0
  73. data/lib/rucoa/yard_string_document_loader.rb +70 -0
  74. data/lib/rucoa.rb +14 -4
  75. data/rucoa.gemspec +1 -0
  76. metadata +70 -6
  77. data/lib/rucoa/code_action_provider.rb +0 -102
  78. data/lib/rucoa/diagnostic_provider.rb +0 -159
  79. data/lib/rucoa/formatting_provider.rb +0 -52
  80. data/lib/rucoa/selection_range_provider.rb +0 -97
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'logger'
4
+ require 'yard'
5
+
6
+ module Rucoa
7
+ class YardGlobDocumentLoader
8
+ class << self
9
+ # @param globs [Array<String>]
10
+ # @return [Array<Rucoa::Definitions::Base>]
11
+ def call(globs:)
12
+ new(
13
+ globs: globs
14
+ ).call
15
+ end
16
+ end
17
+
18
+ # @param globs [String]
19
+ def initialize(globs:)
20
+ @globs = globs
21
+ end
22
+
23
+ # @return [Array<Rucoa::Definitions::Base>]
24
+ def call
25
+ code_objects.filter_map do |code_object|
26
+ case code_object
27
+ when ::YARD::CodeObjects::MethodObject
28
+ DefinitionBuilders::YardMethodDefinitionBuilder.call(
29
+ code_object: code_object,
30
+ path: code_object.file
31
+ )
32
+ end
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ # @return [Array<YARD::CodeObjects::Base>]
39
+ def code_objects
40
+ ::YARD::Logger.instance.enter_level(::Logger::FATAL) do
41
+ ::YARD::Registry.clear
42
+ ::YARD.parse(@globs)
43
+ ::YARD::Registry.all
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'logger'
4
+ require 'yard'
5
+
6
+ module Rucoa
7
+ class YardStringDocumentLoader
8
+ class << self
9
+ # @param content [String]
10
+ # @return [Array<Rucoa::Definitions::Base>]
11
+ # @example returns method definitions from Ruby source code
12
+ # content = <<~RUBY
13
+ # class Foo
14
+ # # Return given argument as an Integer.
15
+ # # @param bar [String]
16
+ # # @return [Integer]
17
+ # def foo(bar)
18
+ # bar.to_i
19
+ # end
20
+ # end
21
+ # RUBY
22
+ # definitions = Rucoa::YardStringDocumentLoader.call(
23
+ # content: content,
24
+ # path: '/path/to/foo.rb'
25
+ # )
26
+ # expect(definitions.size).to eq(1)
27
+ # expect(definitions.first.full_qualified_name).to eq('Foo#foo')
28
+ # expect(definitions.first.source_path).to eq('/path/to/foo.rb')
29
+ # expect(definitions.first.description).to eq('Return given argument as an Integer.')
30
+ # expect(definitions.first.return_types).to eq(%w[Integer])
31
+ def call(content:, path:)
32
+ new(
33
+ content: content,
34
+ path: path
35
+ ).call
36
+ end
37
+ end
38
+
39
+ # @param content [String]
40
+ # @param path [String]
41
+ def initialize(content:, path:)
42
+ @content = content
43
+ @path = path
44
+ end
45
+
46
+ # @return [Array<Rucoa::Definitions::Base>]
47
+ def call
48
+ code_objects.filter_map do |code_object|
49
+ case code_object
50
+ when ::YARD::CodeObjects::MethodObject
51
+ DefinitionBuilders::YardMethodDefinitionBuilder.call(
52
+ code_object: code_object,
53
+ path: @path
54
+ )
55
+ end
56
+ end
57
+ end
58
+
59
+ private
60
+
61
+ # @return [Array<YARD::CodeObjects::Base>]
62
+ def code_objects
63
+ ::YARD::Logger.instance.enter_level(::Logger::FATAL) do
64
+ ::YARD::Registry.clear
65
+ ::YARD.parse_string(@content)
66
+ ::YARD::Registry.all
67
+ end
68
+ end
69
+ end
70
+ end
data/lib/rucoa.rb CHANGED
@@ -4,21 +4,31 @@ require_relative 'rucoa/version'
4
4
 
5
5
  module Rucoa
6
6
  autoload :Cli, 'rucoa/cli'
7
- autoload :CodeActionProvider, 'rucoa/code_action_provider'
8
- autoload :DiagnosticProvider, 'rucoa/diagnostic_provider'
7
+ autoload :Configuration, 'rucoa/configuration'
8
+ autoload :DefinitionArchiver, 'rucoa/definition_archiver'
9
+ autoload :DefinitionBuilders, 'rucoa/definition_builders'
10
+ autoload :Definitions, 'rucoa/definitions'
11
+ autoload :DefinitionStore, 'rucoa/definition_store'
9
12
  autoload :Errors, 'rucoa/errors'
10
- autoload :FormattingProvider, 'rucoa/formatting_provider'
13
+ autoload :HandlerConcerns, 'rucoa/handler_concerns'
14
+ autoload :Handlers, 'rucoa/handlers'
11
15
  autoload :MessageReader, 'rucoa/message_reader'
12
16
  autoload :MessageWriter, 'rucoa/message_writer'
17
+ autoload :NodeConcerns, 'rucoa/node_concerns'
18
+ autoload :NodeInspector, 'rucoa/node_inspector'
13
19
  autoload :Nodes, 'rucoa/nodes'
14
20
  autoload :Parser, 'rucoa/parser'
15
21
  autoload :ParserBuilder, 'rucoa/parser_builder'
16
22
  autoload :Position, 'rucoa/position'
17
23
  autoload :Range, 'rucoa/range'
24
+ autoload :RbsDocumentLoader, 'rucoa/rbs_document_loader'
18
25
  autoload :RubocopAutocorrector, 'rucoa/rubocop_autocorrector'
26
+ autoload :RubocopConfigurationChecker, 'rucoa/rubocop_configuration_checker'
19
27
  autoload :RubocopInvestigator, 'rucoa/rubocop_investigator'
20
- autoload :SelectionRangeProvider, 'rucoa/selection_range_provider'
21
28
  autoload :Server, 'rucoa/server'
22
29
  autoload :Source, 'rucoa/source'
23
30
  autoload :SourceStore, 'rucoa/source_store'
31
+ autoload :Types, 'rucoa/types'
32
+ autoload :YardGlobDocumentLoader, 'rucoa/yard_glob_document_loader'
33
+ autoload :YardStringDocumentLoader, 'rucoa/yard_string_document_loader'
24
34
  end
data/rucoa.gemspec CHANGED
@@ -28,5 +28,6 @@ Gem::Specification.new do |spec|
28
28
  spec.require_paths = ['lib']
29
29
 
30
30
  spec.add_dependency 'parser'
31
+ spec.add_dependency 'rbs'
31
32
  spec.add_dependency 'rubocop'
32
33
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rucoa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-09-04 00:00:00.000000000 Z
11
+ date: 2022-09-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parser
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rbs
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rubocop
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -54,29 +68,79 @@ files:
54
68
  - LICENSE.txt
55
69
  - README.md
56
70
  - Rakefile
71
+ - data/definitions_ruby_3_1
57
72
  - exe/rucoa
73
+ - images/diagnostics.gif
74
+ - images/document-formatting.gif
75
+ - images/document-symbol.gif
76
+ - images/selection-ranges.gif
58
77
  - lib/rucoa.rb
59
78
  - lib/rucoa/cli.rb
60
- - lib/rucoa/code_action_provider.rb
61
- - lib/rucoa/diagnostic_provider.rb
79
+ - lib/rucoa/configuration.rb
80
+ - lib/rucoa/definition_archiver.rb
81
+ - lib/rucoa/definition_builders.rb
82
+ - lib/rucoa/definition_builders/rbs_constant_definition_builder.rb
83
+ - lib/rucoa/definition_builders/rbs_method_definition_builder.rb
84
+ - lib/rucoa/definition_builders/yard_method_definition_builder.rb
85
+ - lib/rucoa/definition_store.rb
86
+ - lib/rucoa/definitions.rb
87
+ - lib/rucoa/definitions/base.rb
88
+ - lib/rucoa/definitions/constant_definition.rb
89
+ - lib/rucoa/definitions/method_definition.rb
90
+ - lib/rucoa/definitions/method_parameter_definition.rb
62
91
  - lib/rucoa/errors.rb
63
- - lib/rucoa/formatting_provider.rb
92
+ - lib/rucoa/handler_concerns.rb
93
+ - lib/rucoa/handler_concerns/configuration_requestable.rb
94
+ - lib/rucoa/handler_concerns/diagnostics_publishable.rb
95
+ - lib/rucoa/handlers.rb
96
+ - lib/rucoa/handlers/base.rb
97
+ - lib/rucoa/handlers/exit_handler.rb
98
+ - lib/rucoa/handlers/initialize_handler.rb
99
+ - lib/rucoa/handlers/initialized_handler.rb
100
+ - lib/rucoa/handlers/shutdown_handler.rb
101
+ - lib/rucoa/handlers/text_document_code_action_handler.rb
102
+ - lib/rucoa/handlers/text_document_did_change_handler.rb
103
+ - lib/rucoa/handlers/text_document_did_open_handler.rb
104
+ - lib/rucoa/handlers/text_document_document_symbol_handler.rb
105
+ - lib/rucoa/handlers/text_document_formatting_handler.rb
106
+ - lib/rucoa/handlers/text_document_range_formatting_handler.rb
107
+ - lib/rucoa/handlers/text_document_selection_range_handler.rb
108
+ - lib/rucoa/handlers/text_document_signature_help_handler.rb
109
+ - lib/rucoa/handlers/workspace_did_change_configuration_handler.rb
64
110
  - lib/rucoa/message_reader.rb
65
111
  - lib/rucoa/message_writer.rb
112
+ - lib/rucoa/node_concerns.rb
113
+ - lib/rucoa/node_concerns/name_full_qualifiable.rb
114
+ - lib/rucoa/node_inspector.rb
66
115
  - lib/rucoa/nodes.rb
67
116
  - lib/rucoa/nodes/base.rb
117
+ - lib/rucoa/nodes/casgn_node.rb
118
+ - lib/rucoa/nodes/class_node.rb
119
+ - lib/rucoa/nodes/const_node.rb
120
+ - lib/rucoa/nodes/def_node.rb
121
+ - lib/rucoa/nodes/defs_node.rb
122
+ - lib/rucoa/nodes/lvar_node.rb
123
+ - lib/rucoa/nodes/module_node.rb
124
+ - lib/rucoa/nodes/sclass_node.rb
125
+ - lib/rucoa/nodes/send_node.rb
68
126
  - lib/rucoa/nodes/str_node.rb
127
+ - lib/rucoa/nodes/sym_node.rb
69
128
  - lib/rucoa/parser.rb
70
129
  - lib/rucoa/parser_builder.rb
71
130
  - lib/rucoa/position.rb
72
131
  - lib/rucoa/range.rb
132
+ - lib/rucoa/rbs_document_loader.rb
73
133
  - lib/rucoa/rubocop_autocorrector.rb
134
+ - lib/rucoa/rubocop_configuration_checker.rb
74
135
  - lib/rucoa/rubocop_investigator.rb
75
- - lib/rucoa/selection_range_provider.rb
76
136
  - lib/rucoa/server.rb
77
137
  - lib/rucoa/source.rb
78
138
  - lib/rucoa/source_store.rb
139
+ - lib/rucoa/types.rb
140
+ - lib/rucoa/types/method_type.rb
79
141
  - lib/rucoa/version.rb
142
+ - lib/rucoa/yard_glob_document_loader.rb
143
+ - lib/rucoa/yard_string_document_loader.rb
80
144
  - rucoa.gemspec
81
145
  homepage: https://github.com/r7kamura/rucoa
82
146
  licenses:
@@ -1,102 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Rucoa
4
- class CodeActionProvider
5
- class << self
6
- # @param diagnostics [Array<Hash>]
7
- # @return [Array<Hash>]
8
- def call(diagnostics:)
9
- new(diagnostics: diagnostics).call
10
- end
11
- end
12
-
13
- # @param diagnostics [Array<Hash>]
14
- def initialize(diagnostics:)
15
- @diagnostics = diagnostics
16
- end
17
-
18
- # @return [Array<Hash>]
19
- def call
20
- correctable_diagnostics.map do |diagnostic|
21
- DiagnosticToCodeActionMapper.call(diagnostic)
22
- end
23
- end
24
-
25
- private
26
-
27
- # @return [Array<Hash>]
28
- def correctable_diagnostics
29
- @diagnostics.select do |diagnostic|
30
- diagnostic.dig('data', 'edits')
31
- end
32
- end
33
-
34
- class DiagnosticToCodeActionMapper
35
- class << self
36
- # @param diagnostic [Hash]
37
- # @return [Hash]
38
- def call(diagnostic)
39
- new(diagnostic).call
40
- end
41
- end
42
-
43
- # @param diagnostic [Hash]
44
- def initialize(diagnostic)
45
- @diagnostic = diagnostic
46
- end
47
-
48
- # @return [Hash]
49
- def call
50
- {
51
- diagnostics: diagnostics,
52
- edit: edit,
53
- isPreferred: preferred?,
54
- kind: kind,
55
- title: title
56
- }
57
- end
58
-
59
- private
60
-
61
- # @return [Hash]
62
- def edit
63
- {
64
- documentChanges: [
65
- {
66
- edits: @diagnostic.dig('data', 'edits'),
67
- textDocument: {
68
- uri: @diagnostic.dig('data', 'uri'),
69
- version: nil
70
- }
71
- }
72
- ]
73
- }
74
- end
75
-
76
- # @return [String]
77
- def cop_name
78
- @diagnostic.dig('data', 'cop_name')
79
- end
80
-
81
- # @return [Array]
82
- def diagnostics
83
- [@diagnostic]
84
- end
85
-
86
- # @return [Boolean]
87
- def preferred?
88
- true
89
- end
90
-
91
- # @return [String]
92
- def kind
93
- 'quickfix'
94
- end
95
-
96
- # @return [String]
97
- def title
98
- "Autocorrect #{cop_name}"
99
- end
100
- end
101
- end
102
- end
@@ -1,159 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'pathname'
4
-
5
- module Rucoa
6
- class DiagnosticProvider
7
- # @param source [Rucoa::Source]
8
- # @param uri [String]
9
- # @return [Array<Hash>]
10
- def self.call(source:, uri:)
11
- new(
12
- source: source,
13
- uri: uri
14
- ).call
15
- end
16
-
17
- # @param source [Rucoa::Source]
18
- # @param uri [String]
19
- def initialize(source:, uri:)
20
- @source = source
21
- @uri = uri
22
- end
23
-
24
- # @return [Array<Hash>]
25
- def call
26
- return [] unless rubocop_configured?
27
-
28
- offenses.map do |offense|
29
- OffenseToDiagnosticMapper.call(
30
- offense,
31
- uri: @uri
32
- )
33
- end
34
- end
35
-
36
- private
37
-
38
- # @return [Array<RuboCop::Cop::Offense>]
39
- def offenses
40
- RubocopInvestigator.call(source: @source)
41
- end
42
-
43
- # @return [Boolean]
44
- def rubocop_configured?
45
- each_ancestor_pathname.any? do |pathname|
46
- pathname.join('.rubocop.yml').exist?
47
- end
48
- end
49
-
50
- # @return [Enumerable<Pathname>]
51
- def each_ancestor_pathname
52
- return to_enum(__method__) unless block_given?
53
-
54
- pathname = ::Pathname.new(@source.path)
55
- loop do
56
- pathname = pathname.parent
57
- yield pathname
58
- break if pathname.root?
59
- end
60
- self
61
- end
62
-
63
- class OffenseToDiagnosticMapper
64
- LSP_SEVERITY_NAME_TO_VALUE_MAP = {
65
- error: 1,
66
- hint: 4,
67
- information: 3,
68
- warning: 2
69
- }.freeze
70
- private_constant :LSP_SEVERITY_NAME_TO_VALUE_MAP
71
-
72
- RUBOCOP_SEVERITY_NAME_TO_LSP_SEVERITY_MAP = {
73
- convention: LSP_SEVERITY_NAME_TO_VALUE_MAP[:information],
74
- error: LSP_SEVERITY_NAME_TO_VALUE_MAP[:error],
75
- fatal: LSP_SEVERITY_NAME_TO_VALUE_MAP[:error],
76
- info: LSP_SEVERITY_NAME_TO_VALUE_MAP[:information],
77
- refactor: LSP_SEVERITY_NAME_TO_VALUE_MAP[:hint],
78
- warning: LSP_SEVERITY_NAME_TO_VALUE_MAP[:warning]
79
- }.freeze
80
- private_constant :RUBOCOP_SEVERITY_NAME_TO_LSP_SEVERITY_MAP
81
-
82
- class << self
83
- # @param offense [RuboCop::Cop::Offense]
84
- # @param uri [String]
85
- # @return [Hash]
86
- def call(offense, uri:)
87
- new(offense, uri: uri).call
88
- end
89
- end
90
-
91
- # @param offense [RuboCop::Cop::Offense]
92
- # @param uri [String]
93
- def initialize(offense, uri:)
94
- @offense = offense
95
- @uri = uri
96
- end
97
-
98
- # @return [Hash]
99
- def call
100
- {
101
- code: code,
102
- data: data,
103
- message: message,
104
- range: range,
105
- severity: severity,
106
- source: source
107
- }
108
- end
109
-
110
- private
111
-
112
- # @return [String]
113
- def code
114
- @offense.cop_name
115
- end
116
-
117
- # @return [Hash]
118
- def data
119
- {
120
- cop_name: @offense.cop_name,
121
- edits: edits,
122
- path: @offense.location.source_buffer.name,
123
- range: range,
124
- uri: @uri
125
- }
126
- end
127
-
128
- # @return [Array<Hash>, nil]
129
- def edits
130
- @offense.corrector&.as_replacements&.map do |range, replacement|
131
- {
132
- newText: replacement,
133
- range: Range.from_parser_range(range).to_vscode_range
134
- }
135
- end
136
- end
137
-
138
- # @return [String]
139
- def message
140
- @offense.message.delete_prefix("#{@offense.cop_name}: ")
141
- end
142
-
143
- # @return [Hash]
144
- def range
145
- Range.from_parser_range(@offense.location).to_vscode_range
146
- end
147
-
148
- # @return [Integer]
149
- def severity
150
- RUBOCOP_SEVERITY_NAME_TO_LSP_SEVERITY_MAP.fetch(@offense.severity.name, 1)
151
- end
152
-
153
- # @return [String]
154
- def source
155
- 'RuboCop'
156
- end
157
- end
158
- end
159
- end
@@ -1,52 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Rucoa
4
- class FormattingProvider
5
- class << self
6
- # @param source [Rucoa::Source]
7
- # @return [Array<Hash>]
8
- def call(source:)
9
- new(source: source).call
10
- end
11
- end
12
-
13
- # @param source [Rucoa::Source]
14
- def initialize(source:)
15
- @source = source
16
- end
17
-
18
- # @return [Array<Hash>]
19
- def call
20
- [text_edit]
21
- end
22
-
23
- private
24
-
25
- # @return [Hash]
26
- def text_edit
27
- {
28
- newText: new_text,
29
- range: range
30
- }
31
- end
32
-
33
- # @return [String]
34
- def new_text
35
- RubocopAutocorrector.call(source: @source)
36
- end
37
-
38
- # @return [Hash]
39
- def range
40
- Range.new(
41
- Position.new(
42
- column: 0,
43
- line: 1
44
- ),
45
- Position.new(
46
- column: @source.content.lines.last.length,
47
- line: @source.content.lines.count + 1
48
- )
49
- ).to_vscode_range
50
- end
51
- end
52
- end
@@ -1,97 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Rucoa
4
- class SelectionRangeProvider
5
- class << self
6
- # @param source [Rucoa::Source]
7
- # @param position [Rucoa::Position]
8
- # @return [Hash, nil]
9
- def call(position:, source:)
10
- new(
11
- position: position,
12
- source: source
13
- ).call
14
- end
15
- end
16
-
17
- # @param position [Rucoa::Position]
18
- # @param source [Rucoa::Source]
19
- def initialize(position:, source:)
20
- @position = position
21
- @source = source
22
- end
23
-
24
- # @return [Hash, nil]
25
- def call
26
- ranges.reverse.reduce(nil) do |result, range|
27
- {
28
- parent: result,
29
- range: range.to_vscode_range
30
- }
31
- end
32
- end
33
-
34
- private
35
-
36
- # @return [Rucoa::Nodes::Base, nil]
37
- def node_at_position
38
- if instance_variable_defined?(:@node_at_position)
39
- @node_at_position
40
- else
41
- @node_at_position = @source.node_at(@position)
42
- end
43
- end
44
-
45
- # @return [Array<Rucoa::Range>]
46
- def ranges
47
- return [] unless node_at_position
48
-
49
- [node_at_position, *node_at_position.ancestors].flat_map do |node|
50
- NodeToRangesMapper.call(node)
51
- end
52
- end
53
-
54
- class NodeToRangesMapper
55
- class << self
56
- # @param node [Rucoa::Nodes::Base]
57
- # @return [Array<Rucoa::Range>]
58
- def call(node)
59
- new(node).call
60
- end
61
- end
62
-
63
- # @param node [Rucoa::Nodes::Base]
64
- def initialize(node)
65
- @node = node
66
- end
67
-
68
- # @return [Array<Rucoa::Range>]
69
- def call
70
- case @node
71
- when Nodes::StrNode
72
- [
73
- inner_range,
74
- expression_range
75
- ]
76
- else
77
- []
78
- end
79
- end
80
-
81
- private
82
-
83
- # @return [Rucoa::Range]
84
- def inner_range
85
- Range.new(
86
- Position.from_parser_range_ending(@node.location.begin),
87
- Position.from_parser_range_beginning(@node.location.end)
88
- )
89
- end
90
-
91
- # @return [Rucoa::Range]
92
- def expression_range
93
- Range.from_parser_range(@node.location.expression)
94
- end
95
- end
96
- end
97
- end