rucoa 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +1 -1
  3. data/data/definitions_ruby_3_1 +0 -0
  4. data/lib/rucoa/handler_concerns/diagnostics_publishable.rb +14 -2
  5. data/lib/rucoa/handlers/initialized_handler.rb +1 -1
  6. data/lib/rucoa/handlers/text_document_completion_handler.rb +8 -1
  7. data/lib/rucoa/handlers/text_document_did_close_handler.rb +20 -0
  8. data/lib/rucoa/handlers/text_document_formatting_handler.rb +2 -2
  9. data/lib/rucoa/handlers/text_document_range_formatting_handler.rb +2 -2
  10. data/lib/rucoa/handlers.rb +1 -0
  11. data/lib/rucoa/node_inspector.rb +13 -1
  12. data/lib/rucoa/{definition_builders/rbs_constant_definition_builder.rb → rbs/constant_definition_mapper.rb} +4 -8
  13. data/lib/rucoa/{definition_builders/rbs_method_definition_builder.rb → rbs/method_definition_mapper.rb} +4 -7
  14. data/lib/rucoa/rbs/ruby_definitions_loader.rb +78 -0
  15. data/lib/rucoa/rbs.rb +9 -0
  16. data/lib/rucoa/rubocop/autocorrector.rb +40 -0
  17. data/lib/rucoa/rubocop/configuration_checker.rb +44 -0
  18. data/lib/rucoa/rubocop/investigator.rb +50 -0
  19. data/lib/rucoa/rubocop.rb +9 -0
  20. data/lib/rucoa/server.rb +1 -0
  21. data/lib/rucoa/source.rb +1 -1
  22. data/lib/rucoa/version.rb +1 -1
  23. data/lib/rucoa/{definition_builders/yard_method_definition_builder.rb → yard/definition_mapper.rb} +20 -21
  24. data/lib/rucoa/yard/definitions_loader.rb +62 -0
  25. data/lib/rucoa/yard.rb +8 -0
  26. data/lib/rucoa.rb +3 -7
  27. metadata +14 -12
  28. data/lib/rucoa/definition_builders.rb +0 -9
  29. data/lib/rucoa/rbs_document_loader.rb +0 -43
  30. data/lib/rucoa/rubocop_autocorrector.rb +0 -38
  31. data/lib/rucoa/rubocop_configuration_checker.rb +0 -42
  32. data/lib/rucoa/rubocop_investigator.rb +0 -48
  33. data/lib/rucoa/yard_glob_document_loader.rb +0 -47
  34. data/lib/rucoa/yard_string_document_loader.rb +0 -70
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f31a0a81d1013267e3bfc0358911e1306e22f0b34f40249604050914da18d8f4
4
- data.tar.gz: b999e9fe78ee1dbb0b11662756c84399b474fb8522476429c90becb454fb0fd1
3
+ metadata.gz: 48f65475f89a895b8fa3429ddcb5a10738c78e2181a0a0f41a8fe14ec8a2fd17
4
+ data.tar.gz: b94bffab0ae35cf83ee0ae5146f41b583abcea4b4192eabcdc8acbcedf3418b4
5
5
  SHA512:
6
- metadata.gz: 1af0f2fed0d7d7930f69768234263e77707f1f22d80e93392ae126c0cfa72d11b3356de6ba5c6f3476ff7b3f27239c1667ac1c2442e9e12301bdadb0a25fec47
7
- data.tar.gz: bdd44ba9cb0284a16589798ec20ee7d9e9bf01989e054108859f3f8286a3f6382a45d84be056dd7a755de8b14d4cf9e28610652489e8e57f9c009b1f81c7f432
6
+ metadata.gz: 1693536178207fc2740ede4806a8803f140477b312c143e7ad0284875c1685244a09fa33dcd6208e993f4d0acb87284391a605a27e09d8479c71f556cf951d7f
7
+ data.tar.gz: d4458ea8223b032e5df2680bfa4f8ed2f939f2971a19db327306a3c6fb6a9a6b3810ffd8ed56346b6e7defb36b916e9054bcb71d031f2494c30b0d6f216995eb
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rucoa (0.6.0)
4
+ rucoa (0.7.0)
5
5
  parser
6
6
  rbs
7
7
  rubocop
Binary file
@@ -16,6 +16,18 @@ module Rucoa
16
16
  )
17
17
  end
18
18
 
19
+ # @param uri [String]
20
+ # @return [void]
21
+ def clear_diagnostics_on(uri)
22
+ write(
23
+ method: 'textDocument/publishDiagnostics',
24
+ params: {
25
+ diagnostics: [],
26
+ uri: uri
27
+ }
28
+ )
29
+ end
30
+
19
31
  # @param uri [String]
20
32
  # @return [void]
21
33
  def publish_diagnostics_on(uri)
@@ -48,7 +60,7 @@ module Rucoa
48
60
 
49
61
  # @return [Array<Hash>]
50
62
  def call
51
- return [] unless RubocopConfigurationChecker.call
63
+ return [] unless Rubocop::ConfigurationChecker.call
52
64
 
53
65
  offenses.map do |offense|
54
66
  OffenseToDiagnosticMapper.call(
@@ -63,7 +75,7 @@ module Rucoa
63
75
 
64
76
  # @return [Array<RuboCop::Cop::Offense>]
65
77
  def offenses
66
- RubocopInvestigator.call(source: @source)
78
+ Rubocop::Investigator.call(source: @source)
67
79
  end
68
80
 
69
81
  class OffenseToDiagnosticMapper
@@ -14,7 +14,7 @@ module Rucoa
14
14
 
15
15
  # @return [void]
16
16
  def load_definitions
17
- YardGlobDocumentLoader.call(
17
+ Yard::DefinitionsLoader.load_globs(
18
18
  globs: ::YARD::Parser::SourceParser::DEFAULT_PATH_GLOB
19
19
  )
20
20
  end
@@ -208,7 +208,14 @@ module Rucoa
208
208
  if @repaired
209
209
  position.to_range
210
210
  else
211
- Range.from_parser_range(node.location.expression)
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
+ )
212
219
  end
213
220
  end
214
221
  end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rucoa
4
+ module Handlers
5
+ class TextDocumentDidCloseHandler < Base
6
+ include HandlerConcerns::DiagnosticsPublishable
7
+
8
+ 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')
17
+ end
18
+ end
19
+ end
20
+ end
@@ -13,7 +13,7 @@ module Rucoa
13
13
  def formattable?
14
14
  configuration.enables_formatting? &&
15
15
  source &&
16
- RubocopConfigurationChecker.call
16
+ Rubocop::ConfigurationChecker.call
17
17
  end
18
18
 
19
19
  # @return [Array<Hash>]
@@ -43,7 +43,7 @@ module Rucoa
43
43
 
44
44
  # @return [String]
45
45
  def new_text
46
- RubocopAutocorrector.call(source: source)
46
+ Rubocop::Autocorrector.call(source: source)
47
47
  end
48
48
 
49
49
  # @return [Hash]
@@ -25,7 +25,7 @@ module Rucoa
25
25
  def formattable?
26
26
  configuration.enables_formatting? &&
27
27
  source &&
28
- RubocopConfigurationChecker.call
28
+ Rubocop::ConfigurationChecker.call
29
29
  end
30
30
 
31
31
  # @return [Rucoa::Source]
@@ -59,7 +59,7 @@ module Rucoa
59
59
 
60
60
  # @return [Array<RuboCop::Cop::Offense>]
61
61
  def offenses
62
- RubocopInvestigator.call(source: source)
62
+ Rubocop::Investigator.call(source: source)
63
63
  end
64
64
 
65
65
  # @return [Array(Rucoa::Range, String)]
@@ -10,6 +10,7 @@ module Rucoa
10
10
  autoload :TextDocumentCodeActionHandler, 'rucoa/handlers/text_document_code_action_handler'
11
11
  autoload :TextDocumentCompletionHandler, 'rucoa/handlers/text_document_completion_handler'
12
12
  autoload :TextDocumentDidChangeHandler, 'rucoa/handlers/text_document_did_change_handler'
13
+ autoload :TextDocumentDidCloseHandler, 'rucoa/handlers/text_document_did_close_handler'
13
14
  autoload :TextDocumentDidOpenHandler, 'rucoa/handlers/text_document_did_open_handler'
14
15
  autoload :TextDocumentDocumentSymbolHandler, 'rucoa/handlers/text_document_document_symbol_handler'
15
16
  autoload :TextDocumentFormattingHandler, 'rucoa/handlers/text_document_formatting_handler'
@@ -80,7 +80,19 @@ module Rucoa
80
80
  [
81
81
  type,
82
82
  @node.name
83
- ].join('#')
83
+ ].join(method_kind_symbol)
84
+ end
85
+ end
86
+
87
+ # @return [String, nil]
88
+ def method_kind_symbol
89
+ return unless @node.is_a?(Nodes::SendNode)
90
+
91
+ case @node.receiver
92
+ when Nodes::ConstNode
93
+ '.'
94
+ else
95
+ '#'
84
96
  end
85
97
  end
86
98
 
@@ -1,28 +1,24 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Rucoa
4
- module DefinitionBuilders
5
- # Build Rucoa contant definition from RBS constant declaration.
6
- class RbsConstantDefinitionBuilder
4
+ module Rbs
5
+ class ConstantDefinitionMapper
7
6
  class << self
8
7
  # @param declaration [RBS::AST::Declarations::Constant]
9
8
  # @return [Rucoa::Definitions::ConstantDefinition]
10
9
  def call(declaration:)
11
- new(
12
- declaration: declaration
13
- ).call
10
+ new(declaration: declaration).call
14
11
  end
15
12
  end
16
13
 
17
14
  # @param declaration [RBS::AST::Declarations::Constant]
18
- # @return [Rucoa::Definitions::ConstantDefinition]
19
15
  def initialize(declaration:)
20
16
  @declaration = declaration
21
17
  end
22
18
 
23
19
  # @return [Rucoa::Definitions::ConstantDefinition]
24
20
  def call
25
- ::Rucoa::Definitions::ConstantDefinition.new(
21
+ Definitions::ConstantDefinition.new(
26
22
  full_qualified_name: full_qualified_name,
27
23
  source_path: source_path
28
24
  )
@@ -1,11 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rbs'
4
-
5
3
  module Rucoa
6
- module DefinitionBuilders
7
- # Build Rucoa method definition from RBS method definition.
8
- class RbsMethodDefinitionBuilder
4
+ module Rbs
5
+ class MethodDefinitionMapper
9
6
  class << self
10
7
  # @param declaration [RBS::AST::Declarations::Class, RBS::AST::Declarations::Module]
11
8
  # @param method_definition [RBS::AST::Members::MethodDefinition]
@@ -42,7 +39,7 @@ module Rucoa
42
39
  # @return [Array<Rucoa::Types::MethodType>]
43
40
  def types
44
41
  @method_definition.types.map do |method_type|
45
- RbsMethodTypeToRucoaMethodTypeMapper.call(
42
+ MethodTypeMapper.call(
46
43
  method_type: method_type
47
44
  )
48
45
  end
@@ -73,7 +70,7 @@ module Rucoa
73
70
  @declaration.location.name
74
71
  end
75
72
 
76
- class RbsMethodTypeToRucoaMethodTypeMapper
73
+ class MethodTypeMapper
77
74
  class << self
78
75
  # @param method_type [RBS::Types::MethodType]
79
76
  # @return [Rucoa::Types::MethodType]
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rbs'
4
+
5
+ module Rucoa
6
+ module Rbs
7
+ # Load definitions for Ruby core and standard libraries.
8
+ class RubyDefinitionsLoader
9
+ class << self
10
+ # @return [Array<Rucoa::Definitions::Base>]
11
+ def call
12
+ new.call
13
+ end
14
+ end
15
+
16
+ # @return [Array<Rucoa::Definitions::Base>]
17
+ def call
18
+ declarations.flat_map do |declaration|
19
+ case declaration
20
+ when ::RBS::AST::Declarations::Constant
21
+ [
22
+ ConstantDefinitionMapper.call(declaration: declaration)
23
+ ]
24
+ when ::RBS::AST::Declarations::Class, ::RBS::AST::Declarations::Module
25
+ [
26
+ ConstantDefinitionMapper.call(declaration: declaration)
27
+ ] +
28
+ declaration.members.grep(::RBS::AST::Members::MethodDefinition).map do |method_definition|
29
+ MethodDefinitionMapper.call(
30
+ declaration: declaration,
31
+ method_definition: method_definition
32
+ )
33
+ end
34
+ else
35
+ []
36
+ end
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ # @return [Array<RBS::AST::Declarations::Base>]
43
+ def declarations
44
+ class_declarations + constant_declarations
45
+ end
46
+
47
+ # @return [Array<RBS::AST::Declarations::Class>]
48
+ def class_declarations
49
+ environment.class_decls.values.flat_map do |multi_entry|
50
+ multi_entry.decls.map(&:decl)
51
+ end
52
+ end
53
+
54
+ # @return [Array<RBS::AST::Declarations::Constant>]
55
+ def constant_declarations
56
+ environment.constant_decls.values.map(&:decl)
57
+ end
58
+
59
+ # @return [RBS::Environment]
60
+ def environment
61
+ @environment ||= ::RBS::Environment.from_loader(
62
+ environment_loader
63
+ ).resolve_type_names
64
+ end
65
+
66
+ # @return [RBS::EnvironmentLoader]
67
+ def environment_loader
68
+ loader = ::RBS::EnvironmentLoader.new
69
+ ::RBS::Repository::DEFAULT_STDLIB_ROOT.children.sort.each do |pathname|
70
+ loader.add(
71
+ library: pathname.basename.to_s
72
+ )
73
+ end
74
+ loader
75
+ end
76
+ end
77
+ end
78
+ end
data/lib/rucoa/rbs.rb ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rucoa
4
+ module Rbs
5
+ autoload :ConstantDefinitionMapper, 'rucoa/rbs/constant_definition_mapper'
6
+ autoload :MethodDefinitionMapper, 'rucoa/rbs/method_definition_mapper'
7
+ autoload :RubyDefinitionsLoader, 'rucoa/rbs/ruby_definitions_loader'
8
+ end
9
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubocop'
4
+
5
+ module Rucoa
6
+ module Rubocop
7
+ class Autocorrector < ::RuboCop::Runner
8
+ class << self
9
+ # @param source [Rucoa::Source]
10
+ # @return [String]
11
+ def call(source:)
12
+ new(source: source).call
13
+ end
14
+ end
15
+
16
+ # @param source [Rucoa::Source]
17
+ def initialize(source:)
18
+ @source = source
19
+ super(
20
+ ::RuboCop::Options.new.parse(
21
+ %w[
22
+ --stderr
23
+ --force-exclusion
24
+ --format RuboCop::Formatter::BaseFormatter
25
+ -A
26
+ ]
27
+ ).first,
28
+ ::RuboCop::ConfigStore.new
29
+ )
30
+ end
31
+
32
+ # @return [String]
33
+ def call
34
+ @options[:stdin] = @source.content
35
+ run([@source.path || 'untitled'])
36
+ @options[:stdin]
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pathname'
4
+
5
+ module Rucoa
6
+ module Rubocop
7
+ class ConfigurationChecker
8
+ class << self
9
+ # @return [Boolean]
10
+ def call
11
+ new.call
12
+ end
13
+ end
14
+
15
+ # @return [Boolean]
16
+ def call
17
+ rubocop_configured_for_current_directory?
18
+ end
19
+
20
+ private
21
+
22
+ # @return [Boolean]
23
+ def rubocop_configured_for_current_directory?
24
+ each_current_and_ancestor_pathname.any? do |pathname|
25
+ pathname.join('.rubocop.yml').exist?
26
+ end
27
+ end
28
+
29
+ # @return [Enumerable<Pathname>]
30
+ def each_current_and_ancestor_pathname
31
+ return to_enum(__method__) unless block_given?
32
+
33
+ pathname = ::Pathname.pwd
34
+ loop do
35
+ yield pathname
36
+ break if pathname.root?
37
+
38
+ pathname = pathname.parent
39
+ end
40
+ self
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubocop'
4
+
5
+ module Rucoa
6
+ module Rubocop
7
+ class Investigator < ::RuboCop::Runner
8
+ class << self
9
+ # @param source [Rucoa::Source]
10
+ # @return [Array<RuboCop::Cop::Offense>]
11
+ def call(source:)
12
+ new(source: source).call
13
+ end
14
+ end
15
+
16
+ # @param source [Rucoa::Source]
17
+ def initialize(source:)
18
+ @source = source
19
+ @offenses = []
20
+ super(
21
+ ::RuboCop::Options.new.parse(
22
+ %w[
23
+ --stderr
24
+ --force-exclusion
25
+ --format RuboCop::Formatter::BaseFormatter
26
+ ]
27
+ ).first,
28
+ ::RuboCop::ConfigStore.new
29
+ )
30
+ end
31
+
32
+ # @return [Array<RuboCop::Cop::Offense>]
33
+ def call
34
+ @options[:stdin] = @source.content
35
+ run([@source.path || 'untitled'])
36
+ @offenses
37
+ end
38
+
39
+ private
40
+
41
+ # @param file [String]
42
+ # @param offenses [Array<RuboCop::Cop::Offense>]
43
+ # @return [void]
44
+ def file_finished(file, offenses)
45
+ @offenses = offenses
46
+ super
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rucoa
4
+ module Rubocop
5
+ autoload :Autocorrector, 'rucoa/rubocop/autocorrector'
6
+ autoload :ConfigurationChecker, 'rucoa/rubocop/configuration_checker'
7
+ autoload :Investigator, 'rucoa/rubocop/investigator'
8
+ end
9
+ end
data/lib/rucoa/server.rb CHANGED
@@ -14,6 +14,7 @@ module Rucoa
14
14
  'textDocument/codeAction' => Handlers::TextDocumentCodeActionHandler,
15
15
  'textDocument/completion' => Handlers::TextDocumentCompletionHandler,
16
16
  'textDocument/didChange' => Handlers::TextDocumentDidChangeHandler,
17
+ 'textDocument/didClose' => Handlers::TextDocumentDidCloseHandler,
17
18
  'textDocument/didOpen' => Handlers::TextDocumentDidOpenHandler,
18
19
  'textDocument/documentSymbol' => Handlers::TextDocumentDocumentSymbolHandler,
19
20
  'textDocument/formatting' => Handlers::TextDocumentFormattingHandler,
data/lib/rucoa/source.rb CHANGED
@@ -33,7 +33,7 @@ module Rucoa
33
33
  # ]
34
34
  # )
35
35
  def definitions
36
- @definitions ||= YardStringDocumentLoader.call(
36
+ @definitions ||= Yard::DefinitionsLoader.load_string(
37
37
  content: @content,
38
38
  path: path
39
39
  )
data/lib/rucoa/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Rucoa
4
- VERSION = '0.6.0'
4
+ VERSION = '0.7.0'
5
5
  end
@@ -3,30 +3,29 @@
3
3
  require 'yard'
4
4
 
5
5
  module Rucoa
6
- module DefinitionBuilders
7
- class YardMethodDefinitionBuilder
6
+ module Yard
7
+ class DefinitionMapper
8
8
  class << self
9
- # @param code_object [YARD::CodeObjects::MethodObject]
10
- # @param path [String]
11
- # @return [Rucoa::Definitions::Base]
12
- def call(code_object:, path:)
13
- new(
14
- code_object: code_object,
15
- path: path
16
- ).call
9
+ # @param code_object [YARD::CodeObjects::Base]
10
+ # @param path [String] This must be passed if the path is not available from code object.
11
+ # @return [Rucoa::Definitions::Base, nil]
12
+ def call(code_object, path: code_object.file)
13
+ new(code_object, path: path).call
17
14
  end
18
15
  end
19
16
 
20
17
  # @param code_object [YARD::CodeObjects::Base]
21
18
  # @param path [String]
22
- def initialize(code_object:, path:)
19
+ def initialize(code_object, path:)
23
20
  @code_object = code_object
24
21
  @path = path
25
22
  end
26
23
 
27
- # @return [Rucoa::Definitions::Base]
24
+ # @return [Rucoa::Definitions::Base, nil]
28
25
  def call
29
- ::Rucoa::Definitions::MethodDefinition.new(
26
+ return unless @code_object.is_a?(::YARD::CodeObjects::MethodObject)
27
+
28
+ Definitions::MethodDefinition.new(
30
29
  description: description,
31
30
  kind: kind,
32
31
  method_name: method_name,
@@ -74,7 +73,7 @@ module Rucoa
74
73
 
75
74
  # @return [String]
76
75
  # @example
77
- # definitions = Rucoa::YardStringDocumentLoader.call(
76
+ # definitions = Rucoa::Yard::DefinitionsLoader.load_string(
78
77
  # content: <<~RUBY,
79
78
  # class Foo
80
79
  # def bar(
@@ -133,7 +132,7 @@ module Rucoa
133
132
  # @return [Array<String>]
134
133
  # @return [String]
135
134
  # @example returns return type annotated by YARD @return tags
136
- # definitions = Rucoa::YardStringDocumentLoader.call(
135
+ # definitions = Rucoa::Yard::DefinitionsLoader.load_string(
137
136
  # content: <<~RUBY,
138
137
  # # @return [String]
139
138
  # def foo
@@ -148,7 +147,7 @@ module Rucoa
148
147
  # ]
149
148
  # )
150
149
  # @example ignores empty @return tags
151
- # definitions = Rucoa::YardStringDocumentLoader.call(
150
+ # definitions = Rucoa::Yard::DefinitionsLoader.load_string(
152
151
  # content: <<~RUBY,
153
152
  # # @return []
154
153
  # def foo
@@ -181,27 +180,27 @@ module Rucoa
181
180
 
182
181
  # @return [String]
183
182
  # @example scrubs "Array<String>" to "Array"
184
- # yard_type = Rucoa::DefinitionBuilders::YardMethodDefinitionBuilder::YardType.new(
183
+ # yard_type = Rucoa::Yard::DefinitionMapper::YardType.new(
185
184
  # 'Array<String>'
186
185
  # )
187
186
  # expect(yard_type.to_rucoa_type).to eq('Array')
188
187
  # @example scrubs "Array(String, Integer)" to "Array"
189
- # yard_type = Rucoa::DefinitionBuilders::YardMethodDefinitionBuilder::YardType.new(
188
+ # yard_type = Rucoa::Yard::DefinitionMapper::YardType.new(
190
189
  # 'Array(String, Integer)'
191
190
  # )
192
191
  # expect(yard_type.to_rucoa_type).to eq('Array')
193
192
  # @example scrubs "::Array" to "Array"
194
- # yard_type = Rucoa::DefinitionBuilders::YardMethodDefinitionBuilder::YardType.new(
193
+ # yard_type = Rucoa::Yard::DefinitionMapper::YardType.new(
195
194
  # '::Array'
196
195
  # )
197
196
  # expect(yard_type.to_rucoa_type).to eq('Array')
198
197
  # @example scrubs "Hash{Symbol => Object}" to "Hash"
199
- # yard_type = Rucoa::DefinitionBuilders::YardMethodDefinitionBuilder::YardType.new(
198
+ # yard_type = Rucoa::Yard::DefinitionMapper::YardType.new(
200
199
  # 'Hash{Symbol => Object}'
201
200
  # )
202
201
  # expect(yard_type.to_rucoa_type).to eq('Hash')
203
202
  # @example scrubs "Array<Array<Integer>>" to "Array"
204
- # yard_type = Rucoa::DefinitionBuilders::YardMethodDefinitionBuilder::YardType.new(
203
+ # yard_type = Rucoa::Yard::DefinitionMapper::YardType.new(
205
204
  # 'Array<Array<Integer>>'
206
205
  # )
207
206
  # expect(yard_type.to_rucoa_type).to eq('Array')
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'logger'
4
+ require 'yard'
5
+
6
+ module Rucoa
7
+ module Yard
8
+ class DefinitionsLoader
9
+ class << self
10
+ # @param content [String]
11
+ # @return [Array<Rucoa::Definitions::Base>]
12
+ # @example returns method definitions from Ruby source code
13
+ # content = <<~RUBY
14
+ # class Foo
15
+ # # Return given argument as an Integer.
16
+ # # @param bar [String]
17
+ # # @return [Integer]
18
+ # def foo(bar)
19
+ # bar.to_i
20
+ # end
21
+ # end
22
+ # RUBY
23
+ # definitions = Rucoa::Yard::DefinitionsLoader.load_string(
24
+ # content: content,
25
+ # path: '/path/to/foo.rb'
26
+ # )
27
+ # expect(definitions.size).to eq(1)
28
+ # expect(definitions.first.full_qualified_name).to eq('Foo#foo')
29
+ # expect(definitions.first.source_path).to eq('/path/to/foo.rb')
30
+ # expect(definitions.first.description).to eq('Return given argument as an Integer.')
31
+ # expect(definitions.first.return_types).to eq(%w[Integer])
32
+ def load_string(content:, path:)
33
+ ::YARD::Registry.clear
34
+ quietly do
35
+ ::YARD.parse_string(content)
36
+ end
37
+ ::YARD::Registry.all.filter_map do |code_object|
38
+ DefinitionMapper.call(code_object, path: path)
39
+ end
40
+ end
41
+
42
+ # @param globs [String]
43
+ # @return [Array<Rucoa::Definitions::Base>]
44
+ def load_globs(globs:)
45
+ ::YARD::Registry.clear
46
+ quietly do
47
+ ::YARD.parse(globs)
48
+ end
49
+ ::YARD::Registry.all.filter_map do |code_object|
50
+ DefinitionMapper.call(code_object)
51
+ end
52
+ end
53
+
54
+ private
55
+
56
+ def quietly(&block)
57
+ ::YARD::Logger.instance.enter_level(::Logger::FATAL, &block)
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
data/lib/rucoa/yard.rb ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rucoa
4
+ module Yard
5
+ autoload :DefinitionMapper, 'rucoa/yard/definition_mapper'
6
+ autoload :DefinitionsLoader, 'rucoa/yard/definitions_loader'
7
+ end
8
+ end
data/lib/rucoa.rb CHANGED
@@ -6,7 +6,6 @@ module Rucoa
6
6
  autoload :Cli, 'rucoa/cli'
7
7
  autoload :Configuration, 'rucoa/configuration'
8
8
  autoload :DefinitionArchiver, 'rucoa/definition_archiver'
9
- autoload :DefinitionBuilders, 'rucoa/definition_builders'
10
9
  autoload :Definitions, 'rucoa/definitions'
11
10
  autoload :DefinitionStore, 'rucoa/definition_store'
12
11
  autoload :Errors, 'rucoa/errors'
@@ -21,14 +20,11 @@ module Rucoa
21
20
  autoload :ParserBuilder, 'rucoa/parser_builder'
22
21
  autoload :Position, 'rucoa/position'
23
22
  autoload :Range, 'rucoa/range'
24
- autoload :RbsDocumentLoader, 'rucoa/rbs_document_loader'
25
- autoload :RubocopAutocorrector, 'rucoa/rubocop_autocorrector'
26
- autoload :RubocopConfigurationChecker, 'rucoa/rubocop_configuration_checker'
27
- autoload :RubocopInvestigator, 'rucoa/rubocop_investigator'
23
+ autoload :Rbs, 'rucoa/rbs'
24
+ autoload :Rubocop, 'rucoa/rubocop'
28
25
  autoload :Server, 'rucoa/server'
29
26
  autoload :Source, 'rucoa/source'
30
27
  autoload :SourceStore, 'rucoa/source_store'
31
28
  autoload :Types, 'rucoa/types'
32
- autoload :YardGlobDocumentLoader, 'rucoa/yard_glob_document_loader'
33
- autoload :YardStringDocumentLoader, 'rucoa/yard_string_document_loader'
29
+ autoload :Yard, 'rucoa/yard'
34
30
  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.6.0
4
+ version: 0.7.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-13 00:00:00.000000000 Z
11
+ date: 2022-09-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parser
@@ -92,10 +92,6 @@ files:
92
92
  - lib/rucoa/cli.rb
93
93
  - lib/rucoa/configuration.rb
94
94
  - lib/rucoa/definition_archiver.rb
95
- - lib/rucoa/definition_builders.rb
96
- - lib/rucoa/definition_builders/rbs_constant_definition_builder.rb
97
- - lib/rucoa/definition_builders/rbs_method_definition_builder.rb
98
- - lib/rucoa/definition_builders/yard_method_definition_builder.rb
99
95
  - lib/rucoa/definition_store.rb
100
96
  - lib/rucoa/definitions.rb
101
97
  - lib/rucoa/definitions/base.rb
@@ -115,6 +111,7 @@ files:
115
111
  - lib/rucoa/handlers/text_document_code_action_handler.rb
116
112
  - lib/rucoa/handlers/text_document_completion_handler.rb
117
113
  - lib/rucoa/handlers/text_document_did_change_handler.rb
114
+ - lib/rucoa/handlers/text_document_did_close_handler.rb
118
115
  - lib/rucoa/handlers/text_document_did_open_handler.rb
119
116
  - lib/rucoa/handlers/text_document_document_symbol_handler.rb
120
117
  - lib/rucoa/handlers/text_document_formatting_handler.rb
@@ -145,18 +142,23 @@ files:
145
142
  - lib/rucoa/parser_builder.rb
146
143
  - lib/rucoa/position.rb
147
144
  - lib/rucoa/range.rb
148
- - lib/rucoa/rbs_document_loader.rb
149
- - lib/rucoa/rubocop_autocorrector.rb
150
- - lib/rucoa/rubocop_configuration_checker.rb
151
- - lib/rucoa/rubocop_investigator.rb
145
+ - lib/rucoa/rbs.rb
146
+ - lib/rucoa/rbs/constant_definition_mapper.rb
147
+ - lib/rucoa/rbs/method_definition_mapper.rb
148
+ - lib/rucoa/rbs/ruby_definitions_loader.rb
149
+ - lib/rucoa/rubocop.rb
150
+ - lib/rucoa/rubocop/autocorrector.rb
151
+ - lib/rucoa/rubocop/configuration_checker.rb
152
+ - lib/rucoa/rubocop/investigator.rb
152
153
  - lib/rucoa/server.rb
153
154
  - lib/rucoa/source.rb
154
155
  - lib/rucoa/source_store.rb
155
156
  - lib/rucoa/types.rb
156
157
  - lib/rucoa/types/method_type.rb
157
158
  - lib/rucoa/version.rb
158
- - lib/rucoa/yard_glob_document_loader.rb
159
- - lib/rucoa/yard_string_document_loader.rb
159
+ - lib/rucoa/yard.rb
160
+ - lib/rucoa/yard/definition_mapper.rb
161
+ - lib/rucoa/yard/definitions_loader.rb
160
162
  - rucoa.gemspec
161
163
  homepage: https://github.com/r7kamura/rucoa
162
164
  licenses:
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Rucoa
4
- module DefinitionBuilders
5
- autoload :RbsConstantDefinitionBuilder, 'rucoa/definition_builders/rbs_constant_definition_builder'
6
- autoload :RbsMethodDefinitionBuilder, 'rucoa/definition_builders/rbs_method_definition_builder'
7
- autoload :YardMethodDefinitionBuilder, 'rucoa/definition_builders/yard_method_definition_builder'
8
- end
9
- end
@@ -1,43 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rbs'
4
-
5
- module Rucoa
6
- class RbsDocumentLoader
7
- class << self
8
- def call
9
- new.call
10
- end
11
- end
12
-
13
- # @return [Array<Rucoa::Definitions::Base>]
14
- def call
15
- declarations.flat_map do |declaration|
16
- case declaration
17
- when ::RBS::AST::Declarations::Constant
18
- [
19
- DefinitionBuilders::RbsConstantDefinitionBuilder.call(declaration: declaration)
20
- ]
21
- when ::RBS::AST::Declarations::Class, ::RBS::AST::Declarations::Module
22
- declaration.members.grep(::RBS::AST::Members::MethodDefinition).map do |method_definition|
23
- DefinitionBuilders::RbsMethodDefinitionBuilder.call(
24
- declaration: declaration,
25
- method_definition: method_definition
26
- )
27
- end
28
- else
29
- []
30
- end
31
- end
32
- end
33
-
34
- private
35
-
36
- # @return [Array<RBS::AST::Declarations::Base>]
37
- def declarations
38
- ::RBS::Environment.from_loader(
39
- ::RBS::EnvironmentLoader.new
40
- ).resolve_type_names.declarations
41
- end
42
- end
43
- end
@@ -1,38 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rubocop'
4
-
5
- module Rucoa
6
- class RubocopAutocorrector < ::RuboCop::Runner
7
- class << self
8
- # @param source [Rucoa::Source]
9
- # @return [String]
10
- def call(source:)
11
- new(source: source).call
12
- end
13
- end
14
-
15
- # @param source [Rucoa::Source]
16
- def initialize(source:)
17
- @source = source
18
- super(
19
- ::RuboCop::Options.new.parse(
20
- %w[
21
- --stderr
22
- --force-exclusion
23
- --format RuboCop::Formatter::BaseFormatter
24
- -A
25
- ]
26
- ).first,
27
- ::RuboCop::ConfigStore.new
28
- )
29
- end
30
-
31
- # @return [String]
32
- def call
33
- @options[:stdin] = @source.content
34
- run([@source.path || 'untitled'])
35
- @options[:stdin]
36
- end
37
- end
38
- end
@@ -1,42 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'pathname'
4
-
5
- module Rucoa
6
- class RubocopConfigurationChecker
7
- class << self
8
- # @return [Boolean]
9
- def call
10
- new.call
11
- end
12
- end
13
-
14
- # @return [Boolean]
15
- def call
16
- rubocop_configured_for_current_directory?
17
- end
18
-
19
- private
20
-
21
- # @return [Boolean]
22
- def rubocop_configured_for_current_directory?
23
- each_current_and_ancestor_pathname.any? do |pathname|
24
- pathname.join('.rubocop.yml').exist?
25
- end
26
- end
27
-
28
- # @return [Enumerable<Pathname>]
29
- def each_current_and_ancestor_pathname
30
- return to_enum(__method__) unless block_given?
31
-
32
- pathname = ::Pathname.pwd
33
- loop do
34
- yield pathname
35
- break if pathname.root?
36
-
37
- pathname = pathname.parent
38
- end
39
- self
40
- end
41
- end
42
- end
@@ -1,48 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rubocop'
4
-
5
- module Rucoa
6
- class RubocopInvestigator < ::RuboCop::Runner
7
- class << self
8
- # @param source [Rucoa::Source]
9
- # @return [Array<RuboCop::Cop::Offense>]
10
- def call(source:)
11
- new(source: source).call
12
- end
13
- end
14
-
15
- # @param source [Rucoa::Source]
16
- def initialize(source:)
17
- @source = source
18
- @offenses = []
19
- super(
20
- ::RuboCop::Options.new.parse(
21
- %w[
22
- --stderr
23
- --force-exclusion
24
- --format RuboCop::Formatter::BaseFormatter
25
- ]
26
- ).first,
27
- ::RuboCop::ConfigStore.new
28
- )
29
- end
30
-
31
- # @return [Array<RuboCop::Cop::Offense>]
32
- def call
33
- @options[:stdin] = @source.content
34
- run([@source.path || 'untitled'])
35
- @offenses
36
- end
37
-
38
- private
39
-
40
- # @param file [String]
41
- # @param offenses [Array<RuboCop::Cop::Offense>]
42
- # @return [void]
43
- def file_finished(file, offenses)
44
- @offenses = offenses
45
- super
46
- end
47
- end
48
- end
@@ -1,47 +0,0 @@
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
@@ -1,70 +0,0 @@
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