Class: Rubydex::Definition

Inherits:
Object
  • Object
show all
Defined in:
ext/rubydex/definition.c

Instance Method Summary collapse

Constructor Details

#initializeObject

Instance Method Details

#commentsArray[Rubydex::Comment]

Returns the source comments associated with this definition.

Returns:



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'ext/rubydex/definition.c', line 103

static VALUE rdxr_definition_comments(VALUE self) {
    HandleData *data;
    void *graph = rdxi_graph_from_handle(self, &data);

    CommentArray *arr = rdx_definition_comments(graph, data->id);
    if (arr == NULL || arr->len == 0) {
        if (arr != NULL) {
            rdx_definition_comments_free(arr);
        }
        return rb_ary_new();
    }

    VALUE ary = rb_ary_new_capa((long)arr->len);
    for (size_t i = 0; i < arr->len; i++) {
        CommentEntry entry = arr->items[i];

        VALUE string = rb_utf8_str_new_cstr(entry.string);

        Location *loc = entry.location;
        VALUE location = rdxi_build_location_value(loc);

        VALUE comment_kwargs = rb_hash_new();
        rb_hash_aset(comment_kwargs, ID2SYM(rb_intern("string")), string);
        rb_hash_aset(comment_kwargs, ID2SYM(rb_intern("location")), location);
        VALUE comment = rb_class_new_instance_kw(1, &comment_kwargs, cComment, RB_PASS_KEYWORDS);

        rb_ary_push(ary, comment);
    }

    // Free the array and all inner allocations on the Rust side
    rdx_definition_comments_free(arr);
    return ary;
}

#declarationRubydex::Declaration?

Returns the declaration this definition belongs to or nil when it cannot be located.

Returns:



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'ext/rubydex/definition.c', line 192

static VALUE rdxr_definition_declaration(VALUE self) {
    HandleData *data;
    void *graph = rdxi_graph_from_handle(self, &data);

    const struct CDeclaration *decl = rdx_definition_declaration(graph, data->id);
    if (decl == NULL) {
        return Qnil;
    }

    VALUE decl_class = rdxi_declaration_class_for_kind(decl->kind);
    VALUE argv[] = {data->graph_obj, ULL2NUM(decl->id)};
    free_c_declaration(decl);

    return rb_class_new_instance(2, argv, decl_class);
}

#deprecated?Boolean

Returns whether this definition is marked as deprecated.

Returns:

  • (Boolean)


157
158
159
160
161
162
163
# File 'ext/rubydex/definition.c', line 157

static VALUE rdxr_definition_deprecated(VALUE self) {
    HandleData *data;
    void *graph = rdxi_graph_from_handle(self, &data);

    bool deprecated = rdx_definition_is_deprecated(graph, data->id);
    return deprecated ? Qtrue : Qfalse;
}

#lexical_nestingArray[Rubydex::Definition]

Returns the lexical nesting from the direct owner up to the root.

Returns:



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'ext/rubydex/definition.c', line 243

static VALUE rdxr_definition_lexical_nesting(VALUE self) {
    HandleData *data;
    void *graph = rdxi_graph_from_handle(self, &data);

    VALUE nesting = rb_ary_new();
    uint64_t definition_id = data->id;

    while (true) {
        const uint64_t *owner_id = rdx_definition_lexical_nesting_id(graph, definition_id);
        if (owner_id == NULL) {
            break;
        }

        rb_ary_push(nesting, rdxi_build_definition(data->graph_obj, graph, *owner_id));
        definition_id = *owner_id;
        free_u64(owner_id);
    }

    return nesting;
}

#lexical_ownerRubydex::Definition?

Returns the lexically enclosing definition, if any.

Returns:



222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'ext/rubydex/definition.c', line 222

static VALUE rdxr_definition_lexical_owner(VALUE self) {
    HandleData *data;
    void *graph = rdxi_graph_from_handle(self, &data);

    const uint64_t *owner_id = rdx_definition_lexical_nesting_id(graph, data->id);
    if (owner_id == NULL) {
        return Qnil;
    }

    VALUE owner = rdxi_build_definition(data->graph_obj, graph, *owner_id);
    free_u64(owner_id);

    return owner;
}

#locationRubydex::Location

Returns the source location for this definition.

Returns:



86
87
88
89
90
91
92
93
94
95
# File 'ext/rubydex/definition.c', line 86

static VALUE rdxr_definition_location(VALUE self) {
    HandleData *data;
    void *graph = rdxi_graph_from_handle(self, &data);

    Location *loc = rdx_definition_location(graph, data->id);
    VALUE location = rdxi_build_location_value(loc);
    rdx_location_free(loc);

    return location;
}

#nameString?

Returns the definition name.

Returns:

  • (String?)


143
144
145
146
147
148
149
# File 'ext/rubydex/definition.c', line 143

static VALUE rdxr_definition_name(VALUE self) {
    HandleData *data;
    void *graph = rdxi_graph_from_handle(self, &data);

    const char *name = rdx_definition_name(graph, data->id);
    return rdxi_owned_c_string_to_ruby(name);
}

#name_locationRubydex::Location?

For class, module, singleton class, and method definitions, returns the location of just the name, such as "Bar" in "class Foo::Bar" or "foo" in "def foo". For other definition types, returns nil.

Returns:



172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'ext/rubydex/definition.c', line 172

static VALUE rdxr_definition_name_location(VALUE self) {
    HandleData *data;
    void *graph = rdxi_graph_from_handle(self, &data);

    Location *loc = rdx_definition_name_location(graph, data->id);
    if (loc == NULL) {
        return Qnil;
    }
    VALUE location = rdxi_build_location_value(loc);
    rdx_location_free(loc);

    return location;
}