Class: Rubydex::Definition

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

Instance Method Summary collapse

Constructor Details

#initializeObject

Instance Method Details

#commentsObject



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
136
137
138
139
140
# File 'ext/rubydex/definition.c', line 107

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

#declarationObject



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'ext/rubydex/definition.c', line 197

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?Object



162
163
164
165
166
167
168
# File 'ext/rubydex/definition.c', line 162

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

#documentObject



275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'ext/rubydex/definition.c', line 275

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_nestingObject



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'ext/rubydex/definition.c', line 248

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_ownerObject



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

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

#locationObject



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

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

#nameObject



148
149
150
151
152
153
154
# File 'ext/rubydex/definition.c', line 148

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_locationObject



177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'ext/rubydex/definition.c', line 177

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