Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor yard definition generator about location #67

Merged
merged 2 commits into from
Sep 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/rucoa/position.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ def initialize(
@line = line
end

# @param other [Rucoa::Position]
# @return [Boolean]
def ==(other)
column == other.column && line == other.line
end

# @param text [String]
# @return [Integer]
def to_index_of(text)
Expand Down
6 changes: 6 additions & 0 deletions lib/rucoa/range.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ def initialize(
@including_ending = including_ending
end

# @param other [Rucoa::Range]
# @return [Boolean]
def ==(other)
beginning == other.beginning && ending == other.ending
end

# @param range [Rucoa::Range]
# @return [Boolean]
# @example returns true when the range is contained in self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def call
Definitions::MethodDefinition.new(
description: description,
kind: :instance,
location: Location.from_rucoa_node(@node),
location: location,
method_name: argument.value.to_s,
namespace: @node.namespace,
types: return_types.map do |type|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def call
Definitions::MethodDefinition.new(
description: description,
kind: :instance,
location: Location.from_rucoa_node(@node),
location: location,
method_name: "#{argument.value}=",
namespace: @node.namespace,
types: return_types.map do |type|
Expand Down
28 changes: 28 additions & 0 deletions lib/rucoa/yard/definition_generators/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,34 @@ def docstring_parser
end
end

# @return [Rucoa::Location]
# @example returns source uri and entire range of target node
# definitions = Rucoa::Source.new(
# content: <<~RUBY,
# class Foo
# end
# RUBY
# uri: '/path/to/foo.rb'
# ).definitions
# expect(definitions[0].location).to eq(
# Rucoa::Location.new(
# range: Rucoa::Range.new(
# Rucoa::Position.new(
# column: 0,
# line: 1
# ),
# Rucoa::Position.new(
# column: 3,
# line: 2
# )
# ),
# uri: '/path/to/foo.rb'
# )
# )
def location
Location.from_rucoa_node(@node)
end

# @return [Array<String>]
# @example returns annotated return types if return tag is provided
# definitions = Rucoa::Source.new(
Expand Down
29 changes: 17 additions & 12 deletions lib/rucoa/yard/definition_generators/class_definition_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,33 @@ class ClassDefinitionGenerator < Base
def call
return [] unless @node.is_a?(Nodes::ClassNode)

included_module_chained_names = @node.body_children.flat_map do |child|
next [] unless child.is_a?(Nodes::SendNode)
next [] unless child.name == 'include'

child.arguments.filter_map do |includee|
next unless includee.is_a?(Nodes::ConstNode)

includee.chained_name
end
end

[
Definitions::ClassDefinition.new(
description: description,
fully_qualified_name: @node.fully_qualified_name,
included_module_chained_names: included_module_chained_names,
location: Location.from_rucoa_node(@node),
location: location,
module_nesting: @node.module_nesting,
super_class_chained_name: @node.super_class_chained_name
)
]
end

private

# @return [Array<String>]
def included_module_chained_names
@node.body_children.flat_map do |child|
next [] unless child.is_a?(Nodes::SendNode)
next [] unless child.name == 'include'

child.arguments.filter_map do |includee|
next unless includee.is_a?(Nodes::ConstNode)

includee.chained_name
end
end
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def call
Definitions::ConstantDefinition.new(
description: description,
fully_qualified_name: @node.fully_qualified_name,
location: Location.from_rucoa_node(@node)
location: location
)
]
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def call
Definitions::MethodDefinition.new(
description: description,
kind: @node.singleton? ? :singleton : :instance,
location: Location.from_rucoa_node(@node),
location: location,
method_name: @node.name,
namespace: @node.namespace,
types: return_types.map do |type|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def call
Definitions::ModuleDefinition.new(
description: description,
fully_qualified_name: @node.fully_qualified_name,
location: Location.from_rucoa_node(@node)
location: location
)
]
end
Expand Down