Class: Rubydex::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/rubydex/graph.rb,
ext/rubydex/graph.c

Overview

The global graph representing all declarations and their relationships for the workspace

Note: this class is partially defined in C to integrate with the Rust backend

Constant Summary collapse

INDEXABLE_EXTENSIONS =
[".rb", ".rake", ".rbs", ".ru"].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configure_for_workspace(workspace_path) ⇒ Object

Creates a new graph with the loaded configuration. For use cases where the graph must be shared between different tools, do not use this. Create and own a Config object instead.

: (String) -> instance



15
16
17
18
19
# File 'lib/rubydex/graph.rb', line 15

def configure_for_workspace(workspace_path)
  graph = new
  graph.load_config(Config.load(workspace_path))
  graph
end

Instance Method Details

#[](fully_qualified_name) ⇒ Rubydex::Declaration?

Returns the declaration for the fully qualified name, or nil when no declaration exists.

Returns:



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'ext/rubydex/graph.c', line 346

static VALUE rdxr_graph_aref(VALUE self, VALUE key) {
    void *graph;
    TypedData_Get_Struct(self, void *, &graph_type, graph);

    if (TYPE(key) != T_STRING) {
        rb_raise(rb_eTypeError, "expected String");
    }

    const CDeclaration *decl = rdx_graph_get_declaration(graph, StringValueCStr(key));
    if (decl == NULL) {
        return Qnil;
    }

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

    return rb_class_new_instance(2, argv, decl_class);
}

#check_integrityArray[Rubydex::IntegrityFailure]

Returns an array of integrity failures, or an empty array if no issues were found.

Returns:



637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
# File 'ext/rubydex/graph.c', line 637

static VALUE rdxr_graph_check_integrity(VALUE self) {
    void *graph;
    TypedData_Get_Struct(self, void *, &graph_type, graph);

    size_t error_count = 0;
    const char *const *errors = rdx_check_integrity(graph, &error_count);

    if (errors == NULL) {
        return rb_ary_new();
    }

    VALUE cIntegrityError = rb_const_get(mRubydex, rb_intern("IntegrityFailure"));
    VALUE array = rb_ary_new_capa((long)error_count);

    for (size_t i = 0; i < error_count; i++) {
        VALUE argv[] = {rb_utf8_str_new_cstr(errors[i])};
        VALUE error = rb_class_new_instance(1, argv, cIntegrityError);
        rb_ary_push(array, error);
    }

    free_c_string_array(errors, error_count);
    return array;
}

#complete_expression(nesting, self_receiver:) ⇒ Array[Rubydex::Declaration | Rubydex::Keyword]

Returns completion candidates for an expression context. The nesting array represents the lexical scope stack. The required self_receiver keyword argument overrides the self type; pass nil when the self type is unknown.



762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
# File 'ext/rubydex/graph.c', line 762

static VALUE rdxr_graph_complete_expression(int argc, VALUE *argv, VALUE self) {
    VALUE nesting, opts;
    rb_scan_args(argc, argv, "1:", &nesting, &opts);
    rdxi_check_array_of_strings(nesting);

    const char *self_receiver = extract_self_receiver(opts);

    void *graph;
    TypedData_Get_Struct(self, void *, &graph_type, graph);

    size_t nesting_count = RARRAY_LEN(nesting);
    char **converted_nesting = rdxi_str_array_to_char(nesting, nesting_count);

    struct CompletionResult result =
        rdx_graph_complete_expression(graph, (const char *const *)converted_nesting, nesting_count, self_receiver);

    rdxi_free_str_array(converted_nesting, nesting_count);
    return completion_result_to_ruby_array(result, self);
}

#complete_method_argument(name, nesting, self_receiver:) ⇒ Array[Rubydex::Declaration | Rubydex::Keyword | Rubydex::KeywordParameter]

Returns completion candidates inside a method call's argument list. See complete_expression for self_receiver semantics.



833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
# File 'ext/rubydex/graph.c', line 833

static VALUE rdxr_graph_complete_method_argument(int argc, VALUE *argv, VALUE self) {
    VALUE name, nesting, opts;
    rb_scan_args(argc, argv, "2:", &name, &nesting, &opts);

    Check_Type(name, T_STRING);
    rdxi_check_array_of_strings(nesting);
    const char *name_string = StringValueCStr(name);

    const char *self_receiver = extract_self_receiver(opts);

    void *graph;
    TypedData_Get_Struct(self, void *, &graph_type, graph);

    size_t nesting_count = RARRAY_LEN(nesting);
    char **converted_nesting = rdxi_str_array_to_char(nesting, nesting_count);

    struct CompletionResult result = rdx_graph_complete_method_argument(
        graph, name_string, (const char *const *)converted_nesting, nesting_count, self_receiver);

    rdxi_free_str_array(converted_nesting, nesting_count);
    return completion_result_to_ruby_array(result, self);
}

#complete_method_call(name, self_receiver:) ⇒ Array[Rubydex::Method]

Returns completion candidates after a method call operator such as foo. The required self_receiver keyword argument is the caller's runtime self type; pass nil when there is no caller context.

Returns:



811
812
813
814
815
816
817
818
819
820
821
822
823
824
# File 'ext/rubydex/graph.c', line 811

static VALUE rdxr_graph_complete_method_call(int argc, VALUE *argv, VALUE self) {
    VALUE name, opts;
    rb_scan_args(argc, argv, "1:", &name, &opts);
    Check_Type(name, T_STRING);

    const char *self_receiver = extract_self_receiver(opts);

    void *graph;
    TypedData_Get_Struct(self, void *, &graph_type, graph);

    struct CompletionResult result =
        rdx_graph_complete_method_call(graph, StringValueCStr(name), self_receiver);
    return completion_result_to_ruby_array(result, self);
}

#complete_namespace_access(name, self_receiver:) ⇒ Array[Rubydex::Declaration]

Returns completion candidates after a namespace access operator such as Foo::. The required self_receiver keyword argument is the caller's runtime self type; pass nil when there is no caller context.

Returns:



789
790
791
792
793
794
795
796
797
798
799
800
801
802
# File 'ext/rubydex/graph.c', line 789

static VALUE rdxr_graph_complete_namespace_access(int argc, VALUE *argv, VALUE self) {
    VALUE name, opts;
    rb_scan_args(argc, argv, "1:", &name, &opts);
    Check_Type(name, T_STRING);

    const char *self_receiver = extract_self_receiver(opts);

    void *graph;
    TypedData_Get_Struct(self, void *, &graph_type, graph);

    struct CompletionResult result =
        rdx_graph_complete_namespace_access(graph, StringValueCStr(name), self_receiver);
    return completion_result_to_ruby_array(result, self);
}

#constant_referencesEnumerator[Rubydex::ConstantReference]

Returns an enumerator that yields constant references lazily.

Returns:



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'ext/rubydex/graph.c', line 384

static VALUE rdxr_graph_constant_references(VALUE self) {
    if (!rb_block_given_p()) {
        return rb_enumeratorize_with_size(self, rb_str_new2("constant_references"), 0, NULL,
                                          graph_constant_references_size);
    }

    void *graph;
    TypedData_Get_Struct(self, void *, &graph_type, graph);

    void *iter = rdx_graph_constant_references_iter_new(graph);
    VALUE args = rb_ary_new_from_args(2, self, ULL2NUM((uintptr_t)iter));
    rb_ensure(rdxi_constant_references_yield, args, rdxi_constant_references_ensure, args);

    return self;
}

#dead_code_candidatesEnumerator[Rubydex::Declaration]

Returns an enumerator over declarations that may be unused because the analysis could not find any references to them. This misses metaprogramming or untyped code, which is why the term candidates is used.

Currently, only supports constants.

Returns:



269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'ext/rubydex/graph.c', line 269

static VALUE rdxr_graph_dead_code_candidates(VALUE self) {
    if (!rb_block_given_p()) {
        return rb_enumeratorize(self, rb_str_new2("dead_code_candidates"), 0, NULL);
    }

    void *graph;
    TypedData_Get_Struct(self, void *, &graph_type, graph);

    void *iter = rdx_graph_dead_code_candidates(graph);
    VALUE args = rb_ary_new_from_args(2, self, ULL2NUM((uintptr_t)iter));
    rb_ensure(rdxi_declarations_yield, args, rdxi_declarations_ensure, args);

    return self;
}

#declarationsEnumerator[Rubydex::Declaration]

Returns an enumerator that yields all declarations lazily.

Returns:



180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'ext/rubydex/graph.c', line 180

static VALUE rdxr_graph_declarations(VALUE self) {
    if (!rb_block_given_p()) {
        return rb_enumeratorize_with_size(self, rb_str_new2("declarations"), 0, NULL, graph_declarations_size);
    }

    void *graph;
    TypedData_Get_Struct(self, void *, &graph_type, graph);

    void *iter = rdx_graph_declarations_iter_new(graph);
    VALUE args = rb_ary_new_from_args(2, self, ULL2NUM((uintptr_t)iter));
    rb_ensure(rdxi_declarations_yield, args, rdxi_declarations_ensure, args);

    return self;
}

#delete_document(uri) ⇒ Rubydex::Document?

Deletes a document and all of its definitions from the graph. Returns the removed document, or nil if it does not exist.

Returns:



463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'ext/rubydex/graph.c', line 463

static VALUE rdxr_graph_delete_document(VALUE self, VALUE uri) {
    Check_Type(uri, T_STRING);

    void *graph;
    TypedData_Get_Struct(self, void *, &graph_type, graph);
    const uint64_t *uri_id = rdx_graph_delete_document(graph, StringValueCStr(uri));

    if (uri_id == NULL) {
        return Qnil;
    }

    VALUE argv[] = {self, ULL2NUM(*uri_id)};
    free_u64(uri_id);
    return rb_class_new_instance(2, argv, cDocument);
}

#diagnosticsArray[Rubydex::Diagnostic]

Returns diagnostics emitted while indexing or resolving the graph.

Returns:



667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
# File 'ext/rubydex/graph.c', line 667

static VALUE rdxr_graph_diagnostics(VALUE self) {
    void *graph;
    TypedData_Get_Struct(self, void *, &graph_type, graph);

    DiagnosticArray *array = rdx_graph_diagnostics(graph);
    if (array == NULL || array->len == 0) {
        if (array != NULL) {
            rdx_diagnostics_free(array);
        }
        return rb_ary_new();
    }

    VALUE diagnostics = rb_ary_new_capa((long)array->len);
    for (size_t i = 0; i < array->len; i++) {
        DiagnosticEntry entry = array->items[i];
        VALUE message = entry.message == NULL ? Qnil : rb_utf8_str_new_cstr(entry.message);
        VALUE rule = rdxi_rule_class_from_name(entry.rule.name, entry.rule.name_length);
        VALUE location = rdxi_build_location_value(entry.location);

        VALUE kwargs = rb_hash_new();
        rb_hash_aset(kwargs, ID2SYM(rb_intern("rule")), rule);
        rb_hash_aset(kwargs, ID2SYM(rb_intern("message")), message);
        rb_hash_aset(kwargs, ID2SYM(rb_intern("location")), location);

        VALUE diagnostic = rb_class_new_instance_kw(1, &kwargs, cDiagnostic, RB_PASS_KEYWORDS);
        rb_ary_push(diagnostics, diagnostic);
    }

    rdx_diagnostics_free(array);
    return diagnostics;
}

#document(uri) ⇒ Rubydex::Document?

Returns the document for the URI, or nil if it does not exist.

Returns:



440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# File 'ext/rubydex/graph.c', line 440

static VALUE rdxr_graph_document(VALUE self, VALUE uri) {
    Check_Type(uri, T_STRING);

    void *graph;
    TypedData_Get_Struct(self, void *, &graph_type, graph);
    const uint64_t *uri_id = rdx_graph_get_document(graph, StringValueCStr(uri));

    if (uri_id == NULL) {
        return Qnil;
    }

    VALUE argv[] = {self, ULL2NUM(*uri_id)};
    free_u64(uri_id);
    return rb_class_new_instance(2, argv, cDocument);
}

#documentsEnumerator[Rubydex::Document]

Returns an enumerator that yields all documents lazily.

Returns:



325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'ext/rubydex/graph.c', line 325

static VALUE rdxr_graph_documents(VALUE self) {
    if (!rb_block_given_p()) {
        return rb_enumeratorize_with_size(self, rb_str_new2("documents"), 0, NULL, graph_documents_size);
    }

    void *graph;
    TypedData_Get_Struct(self, void *, &graph_type, graph);

    void *iter = rdx_graph_documents_iter_new(graph);
    VALUE args = rb_ary_new_from_args(2, self, ULL2NUM((uintptr_t)iter));
    rb_ensure(graph_documents_yield, args, graph_documents_ensure, args);

    return self;
}

#encoding=(encoding) ⇒ nil

Sets the encoding used for transforming byte offsets into LSP code unit line and column positions.

Returns:

  • (nil)


498
499
500
501
502
503
504
505
506
507
508
509
510
# File 'ext/rubydex/graph.c', line 498

static VALUE rdxr_graph_set_encoding(VALUE self, VALUE encoding) {
    Check_Type(encoding, T_STRING);

    void *graph;
    TypedData_Get_Struct(self, void *, &graph_type, graph);

    char *encoding_string = StringValueCStr(encoding);
    if (!rdx_graph_set_encoding(graph, encoding_string)) {
        rb_raise(rb_eArgError, "invalid encoding `%s` (should be utf8, utf16 or utf32)", encoding_string);
    }

    return Qnil;
}

#exclude_patterns(patterns) ⇒ nil

Excludes files matching the given glob patterns from discovery during indexing.

Returns:

  • (nil)


862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
# File 'ext/rubydex/graph.c', line 862

static VALUE rdxr_graph_exclude_patterns(VALUE self, VALUE patterns) {
    Check_Type(patterns, T_ARRAY);
    rdxi_check_array_of_strings(patterns);

    size_t length = RARRAY_LEN(patterns);
    char **converted_patterns = rdxi_str_array_to_char(patterns, length);

    void *graph;
    TypedData_Get_Struct(self, void*, &graph_type, graph);

    rdx_graph_exclude_patterns(graph, (const char **)converted_patterns, length);
    rdxi_free_str_array(converted_patterns, length);

    return Qnil;
}

#excluded_patternsArray[String]

Returns the glob patterns currently excluded from file discovery.

Returns:

  • (Array[String])


884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
# File 'ext/rubydex/graph.c', line 884

static VALUE rdxr_graph_excluded_patterns(VALUE self) {
    void *graph;
    TypedData_Get_Struct(self, void*, &graph_type, graph);

    size_t out_count = 0;
    const char *const *results = rdx_graph_excluded_patterns(graph, &out_count);

    if (results == NULL) {
        return rb_ary_new();
    }

    VALUE array = rb_ary_new_capa((long)out_count);
    for (size_t i = 0; i < out_count; i++) {
        rb_ary_push(array, rb_utf8_str_new_cstr(results[i]));
    }

    free_c_string_array(results, out_count);
    return array;
}

#fuzzy_search(*queries) ⇒ Enumerator[Rubydex::Declaration]

Returns an enumerator that yields declarations whose name matches any of the queries fuzzily.

Returns:



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'ext/rubydex/graph.c', line 240

static VALUE rdxr_graph_fuzzy_search(int argc, VALUE *argv, VALUE self) {
    rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
    VALUE queries = rb_ary_new_from_values(argc, argv);
    rdxi_check_array_of_strings(queries);

    if (!rb_block_given_p()) {
        return rb_enumeratorize(self, rb_str_new2("fuzzy_search"), argc, argv);
    }

    void *graph;
    TypedData_Get_Struct(self, void *, &graph_type, graph);

    size_t length = (size_t)argc;
    char **converted = rdxi_str_array_to_char(queries, length);
    void *iter = rdx_graph_declarations_fuzzy_search(graph, (const char *const *)converted, length);
    rdxi_free_str_array(converted, length);

    return rdxr_graph_yield_search_results(self, iter);
}

#index_all(file_paths) ⇒ Array[String]

Returns an array of I/O error messages encountered during indexing.

Returns:

  • (Array[String])


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'ext/rubydex/graph.c', line 93

static VALUE rdxr_graph_index_all(VALUE self, VALUE file_paths) {
    rdxi_check_array_of_strings(file_paths);

    // Convert the given file paths into a char** array, so that we can pass to Rust
    size_t length = RARRAY_LEN(file_paths);
    char **converted_file_paths = rdxi_str_array_to_char(file_paths, length);

    // Get the underlying graph pointer and then invoke the Rust index all implementation
    void *graph;
    TypedData_Get_Struct(self, void *, &graph_type, graph);

    size_t error_count = 0;
    const char *const *errors = rdx_index_all(graph, (const char **)converted_file_paths, length, &error_count);

    rdxi_free_str_array(converted_file_paths, length);

    if (errors == NULL) {
        return rb_ary_new();
    }

    VALUE array = rb_ary_new_capa((long)error_count);
    for (size_t i = 0; i < error_count; i++) {
        rb_ary_push(array, rb_utf8_str_new_cstr(errors[i]));
    }

    free_c_string_array(errors, error_count);
    return array;
}

#index_source(uri, source, language_id) ⇒ nil

Indexes a single source string in memory, dispatching to the appropriate indexer based on language_id.

Returns:

  • (nil)


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'ext/rubydex/graph.c', line 128

static VALUE rdxr_graph_index_source(VALUE self, VALUE uri, VALUE source, VALUE language_id) {
    Check_Type(uri, T_STRING);
    Check_Type(source, T_STRING);
    Check_Type(language_id, T_STRING);

    void *graph;
    TypedData_Get_Struct(self, void *, &graph_type, graph);

    const char *uri_str = StringValueCStr(uri);
    const char *language_id_str = StringValueCStr(language_id);
    const char *source_str = RSTRING_PTR(source);
    size_t source_len = RSTRING_LEN(source);

    enum IndexSourceResult result = rdx_index_source(graph, uri_str, source_str, source_len, language_id_str);
    switch (result) {
    case IndexSourceResult_Success:
        break;
    case IndexSourceResult_InvalidUri:
        rb_raise(rb_eArgError, "invalid URI (not valid UTF-8)");
        break;
    case IndexSourceResult_InvalidSource:
        rb_raise(rb_eArgError, "source is not valid UTF-8");
        break;
    case IndexSourceResult_InvalidLanguageId:
        rb_raise(rb_eArgError, "invalid language_id (not valid UTF-8)");
        break;
    case IndexSourceResult_UnsupportedLanguageId:
        rb_raise(rb_eArgError, "unsupported language_id `%s`", language_id_str);
        break;
    }

    return Qnil;
}

#index_workspaceObject

Index all files and dependencies of the workspace that exists in workspace_path : -> Array



24
25
26
# File 'lib/rubydex/graph.rb', line 24

def index_workspace
  index_all(workspace_paths)
end

#initialize_clone(other) ⇒ Object

Block both paths: dup dispatches to initialize_copy, clone to initialize_clone.



81
82
83
84
85
# File 'ext/rubydex/graph.c', line 81

static VALUE rdxr_graph_initialize_copy(VALUE self, VALUE other) {
    (void)self;
    (void)other;
    rb_raise(rb_eRuntimeError, "Rubydex::Graph cannot be duplicated or cloned");
}

#initialize_copy(other) ⇒ Object

Block both paths: dup dispatches to initialize_copy, clone to initialize_clone.



81
82
83
84
85
# File 'ext/rubydex/graph.c', line 81

static VALUE rdxr_graph_initialize_copy(VALUE self, VALUE other) {
    (void)self;
    (void)other;
    rb_raise(rb_eRuntimeError, "Rubydex::Graph cannot be duplicated or cloned");
}

#keyword(name) ⇒ Rubydex::Keyword?

Returns the keyword object for the name, or nil if it is not a Ruby keyword.

Returns:



945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
# File 'ext/rubydex/graph.c', line 945

static VALUE rdxr_graph_keyword(VALUE self, VALUE name) {
    Check_Type(name, T_STRING);

    const CKeyword *kw = rdx_keyword_get(StringValueCStr(name));
    if (kw == NULL) {
        return Qnil;
    }

    VALUE argv[2] = {
        rb_utf8_str_new_cstr(kw->name),
        rb_utf8_str_new_cstr(kw->documentation),
    };

    rdx_keyword_free(kw);
    return rb_class_new_instance(2, argv, cKeyword);
}

#load_config(config) ⇒ Object

Applies a parsed Rubydex::Config to the graph.



929
930
931
932
933
934
935
936
937
# File 'ext/rubydex/graph.c', line 929

static VALUE rdxr_graph_load_config(VALUE self, VALUE config_obj) {
    void *graph;
    TypedData_Get_Struct(self, void *, &graph_type, graph);

    void *config = rdxi_config_from_object(config_obj);
    rdx_graph_load_config(graph, config);

    return Qnil;
}

#method_referencesEnumerator[Rubydex::MethodReference]

Returns an enumerator that yields method references lazily.

Returns:



418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'ext/rubydex/graph.c', line 418

static VALUE rdxr_graph_method_references(VALUE self) {
    if (!rb_block_given_p()) {
        return rb_enumeratorize_with_size(self, rb_str_new2("method_references"), 0, NULL,
                                          graph_method_references_size);
    }

    void *graph;
    TypedData_Get_Struct(self, void *, &graph_type, graph);

    void *iter = rdx_graph_method_references_iter_new(graph);
    VALUE args = rb_ary_new_from_args(2, self, ULL2NUM((uintptr_t)iter));
    rb_ensure(rdxi_method_references_yield, args, rdxi_method_references_ensure, args);

    return self;
}

#require_paths(load_paths) ⇒ Array[String]

Returns all require paths for completion.

Returns:

  • (Array[String])


604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
# File 'ext/rubydex/graph.c', line 604

static VALUE rdxr_graph_require_paths(VALUE self, VALUE load_path) {
    rdxi_check_array_of_strings(load_path);

    void *graph;
    TypedData_Get_Struct(self, void *, &graph_type, graph);

    size_t paths_len = RARRAY_LEN(load_path);
    char **converted_paths = rdxi_str_array_to_char(load_path, paths_len);

    size_t out_count = 0;
    const char *const *results = rdx_require_paths(graph, (const char **)converted_paths, paths_len, &out_count);

    rdxi_free_str_array(converted_paths, paths_len);

    if (results == NULL) {
        return rb_ary_new();
    }

    VALUE array = rb_ary_new_capa((long)out_count);
    for (size_t i = 0; i < out_count; i++) {
        rb_ary_push(array, rb_utf8_str_new_cstr(results[i]));
    }

    free_c_string_array(results, out_count);
    return array;
}

#resolveself

Runs the resolver to compute declarations and ownership.

Returns:

  • (self)


485
486
487
488
489
490
# File 'ext/rubydex/graph.c', line 485

static VALUE rdxr_graph_resolve(VALUE self) {
    void *graph;
    TypedData_Get_Struct(self, void *, &graph_type, graph);
    rdx_graph_resolve(graph);
    return self;
}

#resolve_constant(name, context) ⇒ Rubydex::Declaration?

Runs the resolver on a single constant reference using an array of nesting names or a namespace definition as its context.

Returns:



519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
# File 'ext/rubydex/graph.c', line 519

static VALUE rdxr_graph_resolve_constant(VALUE self, VALUE const_name, VALUE context) {
    Check_Type(const_name, T_STRING);
    const char *const_name_string = StringValueCStr(const_name);

    void *graph;
    TypedData_Get_Struct(self, void *, &graph_type, graph);

    const CDeclaration *decl;
    if (RB_TYPE_P(context, T_ARRAY)) {
        rdxi_check_array_of_strings(context);

        size_t length = RARRAY_LEN(context);
        char **converted_names = rdxi_str_array_to_char(context, length);
        decl = rdx_graph_resolve_constant(graph, const_name_string, (const char **)converted_names, length);
        rdxi_free_str_array(converted_names, length);
    } else if (rb_obj_is_kind_of(context, cClassDefinition) ||
               rb_obj_is_kind_of(context, cSingletonClassDefinition) ||
               rb_obj_is_kind_of(context, cModuleDefinition)) {
        uint64_t definition_id = rdxi_definition_id_for_graph(context, self);
        CDefinitionConstantResolutionResult result =
            rdx_graph_resolve_constant_from_definition(graph, const_name_string, definition_id);

        if (result.status == CDefinitionConstantResolution_DefinitionNotFound) {
            rb_raise(rb_eRuntimeError, "Definition not found");
        }
        if (result.status == CDefinitionConstantResolution_InvalidDefinitionKind) {
            // This is unreachable because the rb_obj_is_kind_of checks above only accept supported definition kinds.
            rb_raise(rb_eRuntimeError, "Unexpected definition kind");
        }

        decl = result.declaration;
    } else {
        rb_raise(
            rb_eTypeError,
            "context must be an Array of Strings, ClassDefinition, SingletonClassDefinition, or ModuleDefinition"
        );
    }

    if (decl == NULL) {
        return Qnil;
    }

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

    return rb_class_new_instance(2, argv, decl_class);
}

#resolve_require_path(require_path, load_paths) ⇒ Rubydex::Document?

Resolves a require path to its document.

Returns:



574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
# File 'ext/rubydex/graph.c', line 574

static VALUE rdxr_graph_resolve_require_path(VALUE self, VALUE require_path, VALUE load_paths) {
    Check_Type(require_path, T_STRING);
    rdxi_check_array_of_strings(load_paths);

    void *graph;
    TypedData_Get_Struct(self, void *, &graph_type, graph);
    const char *path_str = StringValueCStr(require_path);

    size_t paths_len = RARRAY_LEN(load_paths);
    char **converted_paths = rdxi_str_array_to_char(load_paths, paths_len);

    const uint64_t *uri_id = rdx_resolve_require_path(graph, path_str, (const char **)converted_paths, paths_len);

    rdxi_free_str_array(converted_paths, paths_len);

    if (uri_id == NULL) {
        return Qnil;
    }

    VALUE argv[] = {self, ULL2NUM(*uri_id)};
    free_u64(uri_id);
    return rb_class_new_instance(2, argv, cDocument);
}

#search(*queries) ⇒ Enumerator[Rubydex::Declaration]

Returns an enumerator that yields declarations whose name matches any of the queries exactly by substring.

Returns:



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'ext/rubydex/graph.c', line 214

static VALUE rdxr_graph_search(int argc, VALUE *argv, VALUE self) {
    rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
    VALUE queries = rb_ary_new_from_values(argc, argv);
    rdxi_check_array_of_strings(queries);

    if (!rb_block_given_p()) {
        return rb_enumeratorize(self, rb_str_new2("search"), argc, argv);
    }

    void *graph;
    TypedData_Get_Struct(self, void *, &graph_type, graph);

    size_t length = (size_t)argc;
    char **converted = rdxi_str_array_to_char(queries, length);
    void *iter = rdx_graph_declarations_search(graph, (const char *const *)converted, length);
    rdxi_free_str_array(converted, length);

    return rdxr_graph_yield_search_results(self, iter);
}

#workspace_pathString

Returns the root directory of the workspace being indexed.

Returns:

  • (String)


910
911
912
913
914
915
916
917
918
919
920
921
# File 'ext/rubydex/graph.c', line 910

static VALUE rdxr_graph_workspace_path(VALUE self) {
    void *graph;
    TypedData_Get_Struct(self, void*, &graph_type, graph);

    const char *result = rdx_graph_workspace_path(graph);
    if (result == NULL) {
        rb_raise(rb_eRuntimeError, "Converting workspace path to Ruby string failed");
    }

    VALUE path = rdxi_owned_c_string_to_ruby(result);
    return path;
}

#workspace_pathsObject

Returns all workspace paths that should be indexed

: -> Array



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rubydex/graph.rb', line 31

def workspace_paths
  paths = []
  root = workspace_path

  Dir.each_child(root) do |entry|
    full_path = File.join(root, entry)

    if File.directory?(full_path) || INDEXABLE_EXTENSIONS.include?(File.extname(entry))
      paths << full_path
    end
  end

  add_workspace_dependency_paths(paths)
  add_core_rbs_definition_paths(paths)
  paths.uniq!
  paths
end