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:



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'ext/rubydex/definition.c', line 118

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) {
        rb_raise(rb_eRuntimeError, "Definition must exist for a valid id");
    }
    if (arr->len == 0) {
        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:



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'ext/rubydex/definition.c', line 227

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)


192
193
194
195
196
197
198
# File 'ext/rubydex/definition.c', line 192

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;
}

#documentRubydex::Document

Returns the document this definition belongs to.

Returns:



305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'ext/rubydex/definition.c', line 305

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

    const uint64_t *uri_id = rdx_definition_document(graph, data->id);
    if (uri_id == NULL) {
        rb_raise(rb_eRuntimeError, "Definition not found");
    }

    VALUE argv[] = {data->graph_obj, ULL2NUM(*uri_id)};
    free_u64(uri_id);

    return rb_class_new_instance(2, argv, cDocument);
}

#lexical_nestingArray[Rubydex::Definition]

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

Returns:



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'ext/rubydex/definition.c', line 278

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:



257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'ext/rubydex/definition.c', line 257

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:



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'ext/rubydex/definition.c', line 98

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);
    if (loc == NULL) {
        rb_raise(rb_eRuntimeError, "Definition must exist for a valid id");
    }
    VALUE location = rdxi_build_location_value(loc);
    rdx_location_free(loc);

    return location;
}

#nameString?

Returns the definition name.

Returns:

  • (String?)


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

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:



207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'ext/rubydex/definition.c', line 207

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;
}