rucoa 0.9.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +6 -0
  3. data/Gemfile.lock +8 -8
  4. data/data/definitions_ruby_3_1 +0 -0
  5. data/lib/rucoa/configuration.rb +4 -1
  6. data/lib/rucoa/definition_store.rb +216 -83
  7. data/lib/rucoa/definitions/base.rb +14 -3
  8. data/lib/rucoa/definitions/class_definition.rb +14 -64
  9. data/lib/rucoa/definitions/constant_definition.rb +31 -19
  10. data/lib/rucoa/definitions/method_definition.rb +44 -52
  11. data/lib/rucoa/definitions/method_parameter_definition.rb +4 -1
  12. data/lib/rucoa/definitions/module_definition.rb +39 -0
  13. data/lib/rucoa/handler_concerns/diagnostics_publishable.rb +14 -3
  14. data/lib/rucoa/handlers/base.rb +12 -3
  15. data/lib/rucoa/handlers/text_document_definition_handler.rb +3 -99
  16. data/lib/rucoa/handlers/text_document_hover_handler.rb +21 -13
  17. data/lib/rucoa/handlers/text_document_selection_range_handler.rb +8 -2
  18. data/lib/rucoa/location.rb +37 -0
  19. data/lib/rucoa/node_concerns/body.rb +24 -0
  20. data/lib/rucoa/node_concerns/{name_fully_qualifiable.rb → qualified_name.rb} +2 -2
  21. data/lib/rucoa/node_concerns.rb +2 -1
  22. data/lib/rucoa/node_inspector.rb +17 -22
  23. data/lib/rucoa/nodes/base.rb +22 -6
  24. data/lib/rucoa/nodes/begin_node.rb +8 -0
  25. data/lib/rucoa/nodes/casgn_node.rb +1 -1
  26. data/lib/rucoa/nodes/class_node.rb +2 -1
  27. data/lib/rucoa/nodes/const_node.rb +33 -15
  28. data/lib/rucoa/nodes/def_node.rb +2 -2
  29. data/lib/rucoa/nodes/defs_node.rb +1 -1
  30. data/lib/rucoa/nodes/module_node.rb +2 -1
  31. data/lib/rucoa/nodes.rb +2 -1
  32. data/lib/rucoa/parser.rb +14 -14
  33. data/lib/rucoa/parser_builder.rb +6 -1
  34. data/lib/rucoa/position.rb +10 -1
  35. data/lib/rucoa/range.rb +11 -1
  36. data/lib/rucoa/rbs/class_definition_mapper.rb +13 -28
  37. data/lib/rucoa/rbs/constant_definition_mapper.rb +12 -6
  38. data/lib/rucoa/rbs/method_definition_mapper.rb +12 -6
  39. data/lib/rucoa/rbs/module_definition_mapper.rb +34 -6
  40. data/lib/rucoa/rubocop/investigator.rb +4 -1
  41. data/lib/rucoa/server.rb +8 -2
  42. data/lib/rucoa/source.rb +19 -4
  43. data/lib/rucoa/unqualified_name.rb +9 -0
  44. data/lib/rucoa/version.rb +1 -1
  45. data/lib/rucoa/yard/definition_generators/attribute_reader_definition_generator.rb +60 -0
  46. data/lib/rucoa/yard/definition_generators/attribute_writer_definition_generator.rb +60 -0
  47. data/lib/rucoa/yard/definition_generators/base.rb +117 -0
  48. data/lib/rucoa/yard/definition_generators/class_definition_generator.rb +66 -0
  49. data/lib/rucoa/yard/definition_generators/constant_assignment_definition_generator.rb +29 -0
  50. data/lib/rucoa/yard/definition_generators/method_definition_generator.rb +47 -0
  51. data/lib/rucoa/yard/definition_generators/module_definition_generator.rb +62 -0
  52. data/lib/rucoa/yard/definition_generators.rb +15 -0
  53. data/lib/rucoa/yard/definitions_loader.rb +1 -271
  54. data/lib/rucoa/yard.rb +1 -0
  55. data/lib/rucoa.rb +4 -2
  56. metadata +15 -3
data/lib/rucoa/source.rb CHANGED
@@ -12,7 +12,10 @@ module Rucoa
12
12
 
13
13
  # @param content [String]
14
14
  # @param uri [String]
15
- def initialize(content:, uri:)
15
+ def initialize(
16
+ content:,
17
+ uri:
18
+ )
16
19
  @content = content
17
20
  @uri = uri
18
21
  end
@@ -35,9 +38,21 @@ module Rucoa
35
38
  # a_kind_of(Rucoa::Definitions::MethodDefinition)
36
39
  # ]
37
40
  # )
41
+ # @example returns empty array when failed to parse
42
+ # source = Rucoa::Source.new(
43
+ # content: 'class Foo',
44
+ # uri: 'file:///path/to/foo.rb'
45
+ # )
46
+ # expect(source.definitions).to eq([])
47
+ # @example returns empty array when no node is found from content
48
+ # source = Rucoa::Source.new(
49
+ # content: '',
50
+ # uri: 'file:///path/to/foo.rb'
51
+ # )
52
+ # expect(source.definitions).to eq([])
38
53
  def definitions
39
54
  @definitions ||=
40
- if parse_result.failed?
55
+ if parse_result.failed? || root_node.nil?
41
56
  []
42
57
  else
43
58
  Yard::DefinitionsLoader.call(
@@ -98,8 +113,8 @@ module Rucoa
98
113
  return @parse_result if instance_variable_defined?(:@parse_result)
99
114
 
100
115
  @parse_result = Parser.call(
101
- path: name,
102
- text: @content
116
+ text: @content,
117
+ uri: @uri
103
118
  )
104
119
  end
105
120
 
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rucoa
4
+ UnqualifiedName = ::Struct.new(
5
+ :chained_name,
6
+ :module_nesting,
7
+ keyword_init: true
8
+ )
9
+ end
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.9.0'
4
+ VERSION = '0.10.0'
5
5
  end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rucoa
4
+ module Yard
5
+ module DefinitionGenerators
6
+ class AttributeReaderDefinitionGenerator < Base
7
+ READER_METHOD_NAMES = ::Set[
8
+ 'attr_accessor',
9
+ 'attr_reader'
10
+ ].freeze
11
+
12
+ # @example returns method definition for attr_reader node
13
+ # definitions = Rucoa::Source.new(
14
+ # content: <<~RUBY,
15
+ # class Foo
16
+ # attr_reader :bar
17
+ # end
18
+ # RUBY
19
+ # uri: '/path/to/foo.rb'
20
+ # ).definitions
21
+ # expect(definitions[1]).to be_a(Rucoa::Definitions::MethodDefinition)
22
+ # @example returns method definition for attr_accessor node
23
+ # definitions = Rucoa::Source.new(
24
+ # content: <<~RUBY,
25
+ # class Foo
26
+ # attr_accessor :bar
27
+ # end
28
+ # RUBY
29
+ # uri: '/path/to/foo.rb'
30
+ # ).definitions
31
+ # expect(definitions.map(&:qualified_name)).to eq(
32
+ # %w[
33
+ # Foo
34
+ # Foo#bar
35
+ # Foo#bar=
36
+ # ]
37
+ # )
38
+ def call
39
+ return [] unless @node.is_a?(Nodes::SendNode) && READER_METHOD_NAMES.include?(@node.name)
40
+
41
+ @node.arguments.map do |argument|
42
+ Definitions::MethodDefinition.new(
43
+ description: description,
44
+ kind: :instance,
45
+ location: location,
46
+ method_name: argument.value.to_s,
47
+ namespace: @node.namespace,
48
+ types: return_types.map do |type|
49
+ Types::MethodType.new(
50
+ parameters_string: '', # TODO
51
+ return_type: type
52
+ )
53
+ end
54
+ )
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rucoa
4
+ module Yard
5
+ module DefinitionGenerators
6
+ class AttributeWriterDefinitionGenerator < Base
7
+ WRITER_METHOD_NAMES = ::Set[
8
+ 'attr_accessor',
9
+ 'attr_writer'
10
+ ].freeze
11
+
12
+ # @example returns method definition for attr_writer node
13
+ # definitions = Rucoa::Source.new(
14
+ # content: <<~RUBY,
15
+ # class Foo
16
+ # attr_writer :bar
17
+ # end
18
+ # RUBY
19
+ # uri: '/path/to/foo.rb'
20
+ # ).definitions
21
+ # expect(definitions[1]).to be_a(Rucoa::Definitions::MethodDefinition)
22
+ # @example returns method definition for attr_accessor node
23
+ # definitions = Rucoa::Source.new(
24
+ # content: <<~RUBY,
25
+ # class Foo
26
+ # attr_accessor :bar
27
+ # end
28
+ # RUBY
29
+ # uri: '/path/to/foo.rb'
30
+ # ).definitions
31
+ # expect(definitions.map(&:qualified_name)).to eq(
32
+ # %w[
33
+ # Foo
34
+ # Foo#bar
35
+ # Foo#bar=
36
+ # ]
37
+ # )
38
+ def call
39
+ return [] unless @node.is_a?(Nodes::SendNode) && WRITER_METHOD_NAMES.include?(@node.name)
40
+
41
+ @node.arguments.map do |argument|
42
+ Definitions::MethodDefinition.new(
43
+ description: description,
44
+ kind: :instance,
45
+ location: location,
46
+ method_name: "#{argument.value}=",
47
+ namespace: @node.namespace,
48
+ types: return_types.map do |type|
49
+ Types::MethodType.new(
50
+ parameters_string: 'value', # TODO
51
+ return_type: type
52
+ )
53
+ end
54
+ )
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,117 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rucoa
4
+ module Yard
5
+ module DefinitionGenerators
6
+ class Base
7
+ class << self
8
+ # @param comment [String]
9
+ # @param node [Rucoa::Nodes::Base]
10
+ # @return [Array<Rucoa::Definitions::Base>]
11
+ def call(
12
+ comment:,
13
+ node:
14
+ )
15
+ new(
16
+ comment: comment,
17
+ node: node
18
+ ).call
19
+ end
20
+ end
21
+
22
+ # @param comment [String]
23
+ # @param node [Rucoa::Nodes::Base]
24
+ def initialize(
25
+ comment:,
26
+ node:
27
+ )
28
+ @comment = comment
29
+ @node = node
30
+ end
31
+
32
+ # @return [Array<Rucoa::Definitions::Base>]
33
+ def call
34
+ raise ::NotImplementedError
35
+ end
36
+
37
+ private
38
+
39
+ # @return [String]
40
+ def description
41
+ docstring_parser.to_docstring.to_s
42
+ end
43
+
44
+ # @return [YARD::DocstringParser]
45
+ def docstring_parser
46
+ @docstring_parser ||= ::YARD::Logger.instance.enter_level(::Logger::FATAL) do
47
+ ::YARD::Docstring.parser.parse(
48
+ @comment,
49
+ ::YARD::CodeObjects::Base.new(:root, 'stub')
50
+ )
51
+ end
52
+ end
53
+
54
+ # @return [Rucoa::Location]
55
+ # @example returns source uri and entire range of target node
56
+ # definitions = Rucoa::Source.new(
57
+ # content: <<~RUBY,
58
+ # class Foo
59
+ # end
60
+ # RUBY
61
+ # uri: '/path/to/foo.rb'
62
+ # ).definitions
63
+ # expect(definitions[0].location).to eq(
64
+ # Rucoa::Location.new(
65
+ # range: Rucoa::Range.new(
66
+ # Rucoa::Position.new(
67
+ # column: 0,
68
+ # line: 1
69
+ # ),
70
+ # Rucoa::Position.new(
71
+ # column: 3,
72
+ # line: 2
73
+ # )
74
+ # ),
75
+ # uri: '/path/to/foo.rb'
76
+ # )
77
+ # )
78
+ def location
79
+ Location.from_rucoa_node(@node)
80
+ end
81
+
82
+ # @return [Array<String>]
83
+ # @example returns annotated return types if return tag is provided
84
+ # definitions = Rucoa::Source.new(
85
+ # content: <<~RUBY,
86
+ # # @return [String]
87
+ # def foo
88
+ # end
89
+ # RUBY
90
+ # uri: '/path/to/foo.rb'
91
+ # ).definitions
92
+ # expect(definitions[0].return_types).to eq(%w[String])
93
+ # @example returns Object if no return tag is provided
94
+ # definitions = Rucoa::Source.new(
95
+ # content: <<~RUBY,
96
+ # def foo
97
+ # end
98
+ # RUBY
99
+ # uri: '/path/to/foo.rb'
100
+ # ).definitions
101
+ # expect(definitions[0].return_types).to eq(%w[Object])
102
+ def return_types
103
+ types = docstring_parser.tags.select do |tag|
104
+ tag.tag_name == 'return'
105
+ end.flat_map(&:types).compact.map do |yard_type|
106
+ Type.new(yard_type).to_rucoa_type
107
+ end
108
+ if types.empty?
109
+ %w[Object]
110
+ else
111
+ types
112
+ end
113
+ end
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rucoa
4
+ module Yard
5
+ module DefinitionGenerators
6
+ class ClassDefinitionGenerator < ModuleDefinitionGenerator
7
+ # @example returns class definition for class node
8
+ # definitions = Rucoa::Source.new(
9
+ # content: <<~RUBY,
10
+ # class Foo
11
+ # end
12
+ # RUBY
13
+ # uri: '/path/to/foo.rb'
14
+ # ).definitions
15
+ # expect(definitions[0]).to be_a(Rucoa::Definitions::ClassDefinition)
16
+ # @example detects single-argument include
17
+ # definitions = Rucoa::Source.new(
18
+ # content: <<~RUBY,
19
+ # class Foo
20
+ # include Bar
21
+ # end
22
+ # RUBY
23
+ # uri: '/path/to/foo.rb'
24
+ # ).definitions
25
+ # expect(definitions[0].included_module_unqualified_names.map(&:chained_name)).to eq(%w[Bar])
26
+ # @example detects multi-arguments include
27
+ # definitions = Rucoa::Source.new(
28
+ # content: <<~RUBY,
29
+ # class Foo
30
+ # include Bar, Baz
31
+ # end
32
+ # RUBY
33
+ # uri: '/path/to/foo.rb'
34
+ # ).definitions
35
+ # expect(definitions[0].included_module_unqualified_names.map(&:chained_name)).to eq(%w[Bar Baz])
36
+ # @example ignores non-simple include
37
+ # definitions = Rucoa::Source.new(
38
+ # content: <<~RUBY,
39
+ # class Foo
40
+ # include foo
41
+ # end
42
+ # RUBY
43
+ # uri: '/path/to/foo.rb'
44
+ # ).definitions
45
+ # expect(definitions[0].included_module_unqualified_names.map(&:chained_name)).to eq([])
46
+ def call
47
+ return [] unless @node.is_a?(Nodes::ClassNode)
48
+
49
+ [
50
+ Definitions::ClassDefinition.new(
51
+ description: description,
52
+ included_module_unqualified_names: included_module_unqualified_names,
53
+ location: location,
54
+ prepended_module_unqualified_names: prepended_module_unqualified_names,
55
+ qualified_name: @node.qualified_name,
56
+ super_class_unqualified_name: UnqualifiedName.new(
57
+ chained_name: @node.super_class_chained_name,
58
+ module_nesting: @node.module_nesting
59
+ )
60
+ )
61
+ ]
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rucoa
4
+ module Yard
5
+ module DefinitionGenerators
6
+ class ConstantAssignmentDefinitionGenerator < Base
7
+ # @example returns constant definition for constant assignment node
8
+ # definitions = Rucoa::Source.new(
9
+ # content: <<~RUBY,
10
+ # Foo = 'foo'
11
+ # RUBY
12
+ # uri: '/path/to/foo.rb'
13
+ # ).definitions
14
+ # expect(definitions[0]).to be_a(Rucoa::Definitions::ConstantDefinition)
15
+ def call
16
+ return [] unless @node.is_a?(Nodes::CasgnNode)
17
+
18
+ [
19
+ Definitions::ConstantDefinition.new(
20
+ description: description,
21
+ location: location,
22
+ qualified_name: @node.qualified_name
23
+ )
24
+ ]
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rucoa
4
+ module Yard
5
+ module DefinitionGenerators
6
+ class MethodDefinitionGenerator < Base
7
+ # @example returns method definition for def node
8
+ # definitions = Rucoa::Source.new(
9
+ # content: <<~RUBY,
10
+ # def foo
11
+ # end
12
+ # RUBY
13
+ # uri: '/path/to/foo.rb'
14
+ # ).definitions
15
+ # expect(definitions[0]).to be_a(Rucoa::Definitions::MethodDefinition)
16
+ # @example returns method definition for defs node
17
+ # definitions = Rucoa::Source.new(
18
+ # content: <<~RUBY,
19
+ # def self.foo
20
+ # end
21
+ # RUBY
22
+ # uri: '/path/to/foo.rb'
23
+ # ).definitions
24
+ # expect(definitions[0]).to be_a(Rucoa::Definitions::MethodDefinition)
25
+ def call
26
+ return [] unless @node.is_a?(Nodes::DefNode) || @node.is_a?(Nodes::DefsNode)
27
+
28
+ [
29
+ Definitions::MethodDefinition.new(
30
+ description: description,
31
+ kind: @node.singleton? ? :singleton : :instance,
32
+ location: location,
33
+ method_name: @node.name,
34
+ namespace: @node.namespace,
35
+ types: return_types.map do |type|
36
+ Types::MethodType.new(
37
+ parameters_string: '', # TODO
38
+ return_type: type
39
+ )
40
+ end
41
+ )
42
+ ]
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rucoa
4
+ module Yard
5
+ module DefinitionGenerators
6
+ class ModuleDefinitionGenerator < Base
7
+ # @example returns module definition for module node
8
+ # definitions = Rucoa::Source.new(
9
+ # content: <<~RUBY,
10
+ # module Foo
11
+ # end
12
+ # RUBY
13
+ # uri: '/path/to/foo.rb'
14
+ # ).definitions
15
+ # expect(definitions[0]).to be_a(Rucoa::Definitions::ModuleDefinition)
16
+ def call
17
+ return [] unless @node.is_a?(Nodes::ModuleNode)
18
+
19
+ [
20
+ Definitions::ModuleDefinition.new(
21
+ description: description,
22
+ included_module_unqualified_names: included_module_unqualified_names,
23
+ location: location,
24
+ prepended_module_unqualified_names: prepended_module_unqualified_names,
25
+ qualified_name: @node.qualified_name
26
+ )
27
+ ]
28
+ end
29
+
30
+ private
31
+
32
+ # @return [Array<Rucoa::UnqualifiedName>]
33
+ def included_module_unqualified_names
34
+ unqualified_names_for('include')
35
+ end
36
+
37
+ # @return [Array<Rucoa::UnqualifiedName>]
38
+ def prepended_module_unqualified_names
39
+ unqualified_names_for('prepend')
40
+ end
41
+
42
+ # @param method_name [String]
43
+ # @return [Array<Rucoa::UnqualifiedName>]
44
+ def unqualified_names_for(method_name)
45
+ @node.body_children.flat_map do |child|
46
+ next [] unless child.is_a?(Nodes::SendNode)
47
+ next [] unless child.name == method_name
48
+
49
+ child.arguments.filter_map do |argument|
50
+ next unless argument.is_a?(Nodes::ConstNode)
51
+
52
+ UnqualifiedName.new(
53
+ chained_name: argument.chained_name,
54
+ module_nesting: @node.module_nesting
55
+ )
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rucoa
4
+ module Yard
5
+ module DefinitionGenerators
6
+ autoload :AttributeReaderDefinitionGenerator, 'rucoa/yard/definition_generators/attribute_reader_definition_generator'
7
+ autoload :AttributeWriterDefinitionGenerator, 'rucoa/yard/definition_generators/attribute_writer_definition_generator'
8
+ autoload :Base, 'rucoa/yard/definition_generators/base'
9
+ autoload :ClassDefinitionGenerator, 'rucoa/yard/definition_generators/class_definition_generator'
10
+ autoload :ConstantAssignmentDefinitionGenerator, 'rucoa/yard/definition_generators/constant_assignment_definition_generator'
11
+ autoload :MethodDefinitionGenerator, 'rucoa/yard/definition_generators/method_definition_generator'
12
+ autoload :ModuleDefinitionGenerator, 'rucoa/yard/definition_generators/module_definition_generator'
13
+ end
14
+ end
15
+ end