-
-
Notifications
You must be signed in to change notification settings - Fork 159
Add support for Ruby Structs #939
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
Merged
castwide
merged 9 commits into
castwide:master
from
lekemula:support-data-struct-classes
Jun 1, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6707d22
Allow multiple node processors
lekemula 9b8305a
Initial implementation of Struct via inheritance definition
lekemula cfea814
Revert "Allow multiple node processors"
lekemula 1e289d7
Process struct definition inside NamespaceNode
lekemula 0ce088c
Support keyword_init
lekemula 56f5440
Support Struct definition via const assignment
lekemula bd57f3e
Add struct @ivars
lekemula 06374e1
Improve Pin inspection - include #path
lekemula fc09f0b
Refactor code
lekemula File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# frozen_string_literal: true | ||
|
||
module Solargraph | ||
module Convention | ||
module StructDefinition | ||
autoload :StructDefintionNode, 'solargraph/convention/struct_definition/struct_definition_node' | ||
autoload :StructAssignmentNode, 'solargraph/convention/struct_definition/struct_assignment_node' | ||
|
||
module NodeProcessors | ||
class StructNode < Parser::NodeProcessor::Base | ||
def process | ||
return if struct_definition_node.nil? | ||
|
||
loc = get_node_location(node) | ||
nspin = Solargraph::Pin::Namespace.new( | ||
type: :class, | ||
location: loc, | ||
closure: region.closure, | ||
name: struct_definition_node.class_name, | ||
comments: comments_for(node), | ||
visibility: :public, | ||
gates: region.closure.gates.freeze | ||
) | ||
pins.push nspin | ||
|
||
# define initialize method | ||
initialize_method_pin = Pin::Method.new( | ||
name: 'initialize', | ||
parameters: [], | ||
scope: :instance, | ||
location: get_node_location(node), | ||
closure: nspin, | ||
visibility: :private, | ||
comments: comments_for(node) | ||
) | ||
|
||
pins.push initialize_method_pin | ||
|
||
struct_definition_node.attributes.map do |attribute_node, attribute_name| | ||
initialize_method_pin.parameters.push( | ||
Pin::Parameter.new( | ||
name: attribute_name, | ||
decl: struct_definition_node.keyword_init? ? :kwarg : :arg, | ||
location: get_node_location(attribute_node), | ||
closure: initialize_method_pin | ||
) | ||
) | ||
end | ||
|
||
# define attribute accessors and instance variables | ||
struct_definition_node.attributes.each do |attribute_node, attribute_name| | ||
[attribute_name, "#{attribute_name}="].each do |name| | ||
method_pin = Pin::Method.new( | ||
name: name, | ||
parameters: [], | ||
scope: :instance, | ||
location: get_node_location(attribute_node), | ||
closure: nspin, | ||
comments: attribute_comments(attribute_node, attribute_name), | ||
visibility: :public | ||
) | ||
|
||
pins.push method_pin | ||
|
||
next unless name.include?('=') # setter | ||
pins.push Pin::InstanceVariable.new(name: "@#{attribute_name}", | ||
closure: method_pin, | ||
location: get_node_location(attribute_node), | ||
comments: attribute_comments(attribute_node, attribute_name)) | ||
end | ||
end | ||
|
||
process_children region.update(closure: nspin, visibility: :public) | ||
end | ||
|
||
private | ||
|
||
# @return [StructDefintionNode, nil] | ||
def struct_definition_node | ||
@struct_definition_node ||= if StructDefintionNode.valid?(node) | ||
StructDefintionNode.new(node) | ||
elsif StructAssignmentNode.valid?(node) | ||
StructAssignmentNode.new(node) | ||
end | ||
end | ||
|
||
# @param attribute_node [Parser::AST::Node] | ||
# @return [String, nil] | ||
def attribute_comments(attribute_node, attribute_name) | ||
struct_comments = comments_for(attribute_node) | ||
return if struct_comments.nil? || struct_comments.empty? | ||
|
||
struct_comments.split("\n").find do |row| | ||
row.include?(attribute_name) | ||
end&.gsub('@param', '@return')&.gsub(attribute_name, '') | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
51 changes: 51 additions & 0 deletions
51
lib/solargraph/convention/struct_definition/struct_assignment_node.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
module Solargraph | ||
module Convention | ||
module StructDefinition | ||
# A node wrapper for a Struct definition via const assignment. | ||
# @example | ||
# MyStruct = Struct.new(:bar, :baz) do | ||
# def foo | ||
# end | ||
# end | ||
class StructAssignmentNode < StructDefintionNode | ||
class << self | ||
# @example | ||
# s(:casgn, nil, :Foo, | ||
# s(:block, | ||
# s(:send, | ||
# s(:const, nil, :Struct), :new, | ||
# s(:sym, :bar), | ||
# s(:sym, :baz)), | ||
# s(:args), | ||
# s(:def, :foo, | ||
# s(:args), | ||
# s(:send, nil, :bar)))) | ||
def valid?(node) | ||
return false unless node&.type == :casgn | ||
return false if node.children[2].nil? | ||
struct_node = node.children[2].children[0] | ||
|
||
struct_definition_node?(struct_node) | ||
end | ||
end | ||
|
||
def class_name | ||
if node.children[0] | ||
Parser::NodeMethods.unpack_name(node.children[0]) + "::#{node.children[1]}" | ||
else | ||
node.children[1].to_s | ||
end | ||
end | ||
|
||
private | ||
|
||
# @return [Parser::AST::Node] | ||
def struct_node | ||
node.children[2].children[0] | ||
end | ||
end | ||
end | ||
end | ||
end |
100 changes: 100 additions & 0 deletions
100
lib/solargraph/convention/struct_definition/struct_definition_node.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# frozen_string_literal: true | ||
|
||
module Solargraph | ||
module Convention | ||
module StructDefinition | ||
# A node wrapper for a Struct definition via inheritance. | ||
# @example | ||
# class MyStruct < Struct.new(:bar, :baz) | ||
# def foo | ||
# end | ||
# end | ||
class StructDefintionNode | ||
class << self | ||
# @example | ||
# s(:class, | ||
# s(:const, nil, :Foo), | ||
# s(:send, | ||
# s(:const, nil, :Struct), :new, | ||
# s(:sym, :bar), | ||
# s(:sym, :baz)), | ||
# s(:hash, | ||
# s(:pair, | ||
# s(:sym, :keyword_init), | ||
# s(:true)))), | ||
# s(:def, :foo, | ||
# s(:args), | ||
# s(:send, nil, :bar))) | ||
def valid?(node) | ||
return false unless node&.type == :class | ||
|
||
struct_definition_node?(node.children[1]) | ||
end | ||
|
||
private | ||
|
||
# @param struct_node [Parser::AST::Node] | ||
# @return [Boolean] | ||
def struct_definition_node?(struct_node) | ||
return false unless struct_node.is_a?(::Parser::AST::Node) | ||
return false unless struct_node&.type == :send | ||
return false unless struct_node.children[0]&.type == :const | ||
return false unless struct_node.children[0].children[1] == :Struct | ||
return false unless struct_node.children[1] == :new | ||
|
||
true | ||
end | ||
end | ||
|
||
# @return [Parser::AST::Node] | ||
def initialize(node) | ||
@node = node | ||
end | ||
|
||
# @return [String] | ||
def class_name | ||
Parser::NodeMethods.unpack_name(node) | ||
end | ||
|
||
# @return [Array<Array(Parser::AST::Node, String)>] | ||
def attributes | ||
struct_attribute_nodes.map do |struct_def_param| | ||
next unless struct_def_param.type == :sym | ||
[struct_def_param, struct_def_param.children[0].to_s] | ||
end.compact | ||
end | ||
|
||
def keyword_init? | ||
keyword_init_param = struct_attribute_nodes.find do |struct_def_param| | ||
struct_def_param.type == :hash && struct_def_param.children[0].type == :pair && | ||
struct_def_param.children[0].children[0].children[0] == :keyword_init | ||
end | ||
|
||
return false if keyword_init_param.nil? | ||
|
||
keyword_init_param.children[0].children[1].type == :true | ||
end | ||
|
||
# @return [Parser::AST::Node] | ||
def body_node | ||
node.children[2] | ||
end | ||
|
||
private | ||
|
||
# @return [Parser::AST::Node] | ||
attr_reader :node | ||
|
||
# @return [Parser::AST::Node] | ||
def struct_node | ||
node.children[1] | ||
end | ||
|
||
# @return [Array<Parser::AST::Node>] | ||
def struct_attribute_nodes | ||
struct_node.children[2..-1] | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,7 +48,7 @@ def desc | |
if name.nil? || name.empty? | ||
'(top-level)' | ||
else | ||
return_type.rooted_tags | ||
super | ||
end | ||
end | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like the fact that I had to change the node processor classes - do you see a better way to avoid that?
My initial attempt was to allow multiple node processors, as I explained in the PR description.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suspect that any solution would have required some sort of change to node processors. At the very least, they would need a mechanism to map anonymous class pins for declarations like
class Foo < Struct.new
. I'm good with handling it like this for now. We can try exploring more general solutions when we look into adding support forData
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good 🙌!