-
Notifications
You must be signed in to change notification settings - Fork 1
Expose method signature to Ruby #686
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
Open
soutaro
wants to merge
8
commits into
main
Choose a base branch
from
soutaro-signature_ruby_api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
988af78
Include trailing colon in RBS keyword parameter offset
soutaro 17371dd
Add MethodDefinition#signatures to Ruby API
soutaro d1f4139
Add Rubydex::Method#signatures with alias resolution
soutaro f7fe522
Update rust/rubydex-sys/src/declaration_api.rs
soutaro 8eb3392
Returns `Vec<&MethodDefinition>`
soutaro 3463eed
Fix test for windows
soutaro f741daa
Add tests with RBS
soutaro ffd8461
fix query.rs
soutaro 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
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,68 @@ | ||
| #include "signature.h" | ||
| #include "definition.h" | ||
| #include "handle.h" | ||
| #include "location.h" | ||
|
|
||
| VALUE cSignature; | ||
|
|
||
| static VALUE parameter_kind_to_symbol(ParameterKind kind) { | ||
| switch (kind) { | ||
| case ParameterKind_Req: return ID2SYM(rb_intern("req")); | ||
| case ParameterKind_Opt: return ID2SYM(rb_intern("opt")); | ||
| case ParameterKind_Rest: return ID2SYM(rb_intern("rest")); | ||
| case ParameterKind_Keyreq: return ID2SYM(rb_intern("keyreq")); | ||
| case ParameterKind_Key: return ID2SYM(rb_intern("key")); | ||
| case ParameterKind_Keyrest: return ID2SYM(rb_intern("keyrest")); | ||
| case ParameterKind_Block: return ID2SYM(rb_intern("block")); | ||
| case ParameterKind_Forward: return ID2SYM(rb_intern("forward")); | ||
| default: rb_raise(rb_eRuntimeError, "Unknown ParameterKind: %d", kind); | ||
| } | ||
| } | ||
|
|
||
| VALUE rdxi_signatures_to_ruby(SignatureArray *arr, VALUE graph_obj, VALUE default_method_def) { | ||
| if (arr == NULL || arr->len == 0) { | ||
| if (arr != NULL) { | ||
| rdx_definition_signatures_free(arr); | ||
| } | ||
| return rb_ary_new(); | ||
| } | ||
|
|
||
| VALUE signatures = rb_ary_new_capa((long)arr->len); | ||
|
|
||
| for (size_t i = 0; i < arr->len; i++) { | ||
| SignatureEntry sig_entry = arr->items[i]; | ||
|
|
||
| VALUE method_def; | ||
| if (default_method_def != Qnil) { | ||
| method_def = default_method_def; | ||
| } else { | ||
| VALUE def_argv[] = {graph_obj, ULL2NUM(sig_entry.definition_id)}; | ||
| method_def = rb_class_new_instance(2, def_argv, cMethodDefinition); | ||
| } | ||
|
|
||
| VALUE parameters = rb_ary_new_capa((long)sig_entry.parameters_len); | ||
| for (size_t j = 0; j < sig_entry.parameters_len; j++) { | ||
| ParameterEntry param_entry = sig_entry.parameters[j]; | ||
|
|
||
| VALUE kind_sym = parameter_kind_to_symbol(param_entry.kind); | ||
| VALUE name_sym = ID2SYM(rb_intern(param_entry.name)); | ||
| VALUE location = rdxi_build_location_value(param_entry.location); | ||
|
|
||
| rb_ary_push(parameters, rb_ary_new_from_args(3, kind_sym, name_sym, location)); | ||
| } | ||
|
|
||
| VALUE sig_kwargs = rb_hash_new(); | ||
| rb_hash_aset(sig_kwargs, ID2SYM(rb_intern("parameters")), parameters); | ||
| rb_hash_aset(sig_kwargs, ID2SYM(rb_intern("method_definition")), method_def); | ||
| VALUE signature = rb_class_new_instance_kw(1, &sig_kwargs, cSignature, RB_PASS_KEYWORDS); | ||
|
|
||
| rb_ary_push(signatures, signature); | ||
| } | ||
|
|
||
| rdx_definition_signatures_free(arr); | ||
| return signatures; | ||
| } | ||
|
|
||
| void rdxi_initialize_signature(VALUE mRubydex) { | ||
| cSignature = rb_define_class_under(mRubydex, "Signature", rb_cObject); | ||
| } |
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,17 @@ | ||
| #ifndef RUBYDEX_SIGNATURE_H | ||
| #define RUBYDEX_SIGNATURE_H | ||
|
|
||
| #include "ruby.h" | ||
| #include "rustbindings.h" | ||
|
|
||
| extern VALUE cSignature; | ||
|
|
||
| // Convert a SignatureArray into a Ruby array of Rubydex::Signature objects. | ||
| // If default_method_def is not Qnil, it is used as method_definition for all signatures. | ||
| // Otherwise, a new MethodDefinition handle is built from each SignatureEntry's definition_id. | ||
| // The SignatureArray is freed after conversion. | ||
| VALUE rdxi_signatures_to_ruby(SignatureArray *arr, VALUE graph_obj, VALUE default_method_def); | ||
|
|
||
| void rdxi_initialize_signature(VALUE mRubydex); | ||
|
|
||
| #endif // RUBYDEX_SIGNATURE_H |
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,37 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Rubydex | ||
| # Represents a single signature of a method definition. | ||
| # | ||
| # A method definition may have multiple signatures when defined with overloads in RBS. | ||
| class Signature | ||
| # Returns the parameters of the signature. | ||
| # | ||
| # Each parameter is a 3-element array of `[kind, name, location]`, | ||
| # following the same format as `Method#parameters` with an additional location element. | ||
| # | ||
| # The kind symbol is one of: | ||
| # - `:req` — required positional parameter (`a`) or post-rest positional parameter (`d` in `def foo(*c, d)`) | ||
| # - `:opt` — optional positional parameter (`b = 1`) | ||
| # - `:rest` — rest positional parameter (`*c`) | ||
| # - `:keyreq` — required keyword parameter (`e:`) | ||
| # - `:key` — optional keyword parameter (`f: 1`) | ||
| # - `:keyrest` — rest keyword parameter (`**g`) | ||
| # - `:block` — block parameter (`&h`) | ||
| # - `:forward` — forward parameter (`...`), Rubydex-specific (Ruby expands this to `:rest`, `:keyrest`, `:block`) | ||
| # | ||
| #: Array[[Symbol, Symbol, Location]] | ||
|
Comment on lines
+10
to
+23
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's please create Ruby objects to represent each parameter type instead of using symbols. I suspect you only need a parent class and then a bunch of empty definitions: class Parameter
def initialize(name, location)
# ...
end
end
class PositionalParameter < Parameter; end
class OptionalPositionalParameter < Parameter; end
class KeywordParameter < Parameter; end
class OptionalKeywordParameter < Parameter; end
# ... |
||
| attr_reader :parameters | ||
|
|
||
| # Returns the method definition this signature belongs to. | ||
| # | ||
| #: MethodDefinition | ||
| attr_reader :method_definition | ||
|
|
||
| #: (parameters: Array[[Symbol, Symbol, Location]], method_definition: MethodDefinition) -> void | ||
| def initialize(parameters:, method_definition:) | ||
| @parameters = parameters | ||
| @method_definition = method_definition | ||
| 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
Oops, something went wrong.
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.
Do we need to expose
Declaration#signature? Signatures are associated to the specificDefinitionthat creates them, so I'd expect us to access it like this:I think we can remove this one.
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'd like to keep
#signaturesas a utility API.#definitionscan contain method aliases, sodefinitions.flat_map(&:signatures)wouldn't work transparently — users would need to handle alias resolution themselves.We could add a separate API for alias normalization, but if we're adding an API anyway, I think
#signaturesthat does it all in one shot is the better choice.