Skip to content

Commit fabc2b0

Browse files
authored
Merge pull request #1711 from benrfairless/feat/ruby-lsp-per-file
feat(lsp): add the Ruby per-file resolver
2 parents 2921415 + a508219 commit fabc2b0

9 files changed

Lines changed: 4046 additions & 2 deletions

File tree

‎Makefile.cbm‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,7 @@ TEST_CS_LSP_SRCS = tests/test_cs_lsp.c
887887
TEST_CS_LSP_BENCH_SRCS = tests/test_cs_lsp_bench.c
888888

889889
TEST_PERL_LSP_SRCS = tests/test_perl_lsp.c
890+
TEST_RUBY_LSP_SRCS = tests/test_ruby_lsp.c
890891

891892
TEST_SCOPE_SRCS = tests/test_scope.c
892893

@@ -1010,7 +1011,7 @@ TEST_REPRO_SRCS = \
10101011
tests/repro/repro_lsp_java_cs.c \
10111012
tests/repro/repro_lsp_kt_php_rust.c
10121013

1013-
ALL_TEST_SRCS =$(TEST_FOUNDATION_SRCS) $(TEST_EXTRACTION_SRCS) $(TEST_STORE_SRCS) $(TEST_CYPHER_SRCS) $(TEST_MCP_SRCS) $(TEST_DAEMON_SRCS) $(TEST_DISCOVER_SRCS) $(TEST_GRAPH_BUFFER_SRCS) $(TEST_PIPELINE_SRCS) $(TEST_WATCHER_SRCS) $(TEST_LZ4_SRCS) $(TEST_ZSTD_SRCS) $(TEST_ARTIFACT_SRCS) $(TEST_SQLITE_WRITER_SRCS) $(TEST_GO_LSP_SRCS) $(TEST_C_LSP_SRCS) $(TEST_PHP_LSP_SRCS) $(TEST_CS_LSP_SRCS) $(TEST_CS_LSP_BENCH_SRCS) $(TEST_PERL_LSP_SRCS) $(TEST_SCOPE_SRCS) $(TEST_TYPE_REP_SRCS) $(TEST_PY_LSP_SRCS) $(TEST_PY_LSP_BENCH_SRCS) $(TEST_PY_LSP_STRESS_SRCS) $(TEST_PY_LSP_SCALE_SRCS) $(TEST_TS_LSP_SRCS) $(TEST_JAVA_LSP_SRCS) $(TEST_KOTLIN_LSP_SRCS) $(TEST_RUST_LSP_SRCS) $(TEST_TRACES_SRCS) $(TEST_CLI_SRCS) $(TEST_MEM_SRCS) $(TEST_UI_SRCS) $(TEST_HTTPD_SRCS) $(TEST_SECURITY_SRCS) $(TEST_YAML_SRCS) $(TEST_SEMANTIC_SRCS) $(TEST_AST_PROFILE_SRCS) $(TEST_SLAB_ALLOC_SRCS) $(TEST_SIMHASH_SRCS) $(TEST_STACK_OVERFLOW_SRCS) $(TEST_INTEGRATION_SRCS)
1014+
ALL_TEST_SRCS =$(TEST_FOUNDATION_SRCS) $(TEST_EXTRACTION_SRCS) $(TEST_STORE_SRCS) $(TEST_CYPHER_SRCS) $(TEST_MCP_SRCS) $(TEST_DAEMON_SRCS) $(TEST_DISCOVER_SRCS) $(TEST_GRAPH_BUFFER_SRCS) $(TEST_PIPELINE_SRCS) $(TEST_WATCHER_SRCS) $(TEST_LZ4_SRCS) $(TEST_ZSTD_SRCS) $(TEST_ARTIFACT_SRCS) $(TEST_SQLITE_WRITER_SRCS) $(TEST_GO_LSP_SRCS) $(TEST_C_LSP_SRCS) $(TEST_PHP_LSP_SRCS) $(TEST_CS_LSP_SRCS) $(TEST_CS_LSP_BENCH_SRCS) $(TEST_PERL_LSP_SRCS) $(TEST_RUBY_LSP_SRCS) $(TEST_SCOPE_SRCS) $(TEST_TYPE_REP_SRCS) $(TEST_PY_LSP_SRCS) $(TEST_PY_LSP_BENCH_SRCS) $(TEST_PY_LSP_STRESS_SRCS) $(TEST_PY_LSP_SCALE_SRCS) $(TEST_TS_LSP_SRCS) $(TEST_JAVA_LSP_SRCS) $(TEST_KOTLIN_LSP_SRCS) $(TEST_RUST_LSP_SRCS) $(TEST_TRACES_SRCS) $(TEST_CLI_SRCS) $(TEST_MEM_SRCS) $(TEST_UI_SRCS) $(TEST_HTTPD_SRCS) $(TEST_SECURITY_SRCS) $(TEST_YAML_SRCS) $(TEST_SEMANTIC_SRCS) $(TEST_AST_PROFILE_SRCS) $(TEST_SLAB_ALLOC_SRCS) $(TEST_SIMHASH_SRCS) $(TEST_STACK_OVERFLOW_SRCS) $(TEST_INTEGRATION_SRCS)
10141015

10151016

10161017
# ── Build directories ────────────────────────────────────────────

‎internal/cbm/cbm.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "lsp/php_lsp.h"
1717
#include "lsp/perl_lsp.h"
1818
#include "lsp/py_lsp.h"
19+
#include "lsp/ruby_lsp.h"
1920
#include "lsp/ts_lsp.h"
2021
#include "lsp/cs_lsp.h"
2122
#include "lsp/java_lsp.h"
@@ -3951,6 +3952,9 @@ static CBMFileResult *extract_file_ex_body(const char *source, int source_len, C
39513952
if (language == CBM_LANG_PERL) {
39523953
cbm_run_perl_lsp(a, result, source, source_len, root);
39533954
}
3955+
if (language == CBM_LANG_RUBY) {
3956+
cbm_run_ruby_lsp(a, result, source, source_len, root);
3957+
}
39543958
if (language == CBM_LANG_PYTHON) {
39553959
cbm_run_py_lsp(a, result, source, source_len, root);
39563960
}

‎internal/cbm/lsp/generated/ruby_stdlib_data.c‎

Lines changed: 465 additions & 0 deletions
Large diffs are not rendered by default.

‎internal/cbm/lsp/ruby_lsp.c‎

Lines changed: 1824 additions & 0 deletions
Large diffs are not rendered by default.

‎internal/cbm/lsp/ruby_lsp.h‎

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
#ifndef CBM_LSP_RUBY_LSP_H
2+
#define CBM_LSP_RUBY_LSP_H
3+
4+
#include "type_rep.h"
5+
#include "scope.h"
6+
#include "type_registry.h"
7+
#include "../cbm.h"
8+
9+
/* Ruby mixin kinds. Lookup order for an instance method on class C:
10+
* prepended modules (last prepended first) → C's own methods →
11+
* included modules (last included first) → superclass chain.
12+
* `extend` adds a module's instance methods to C's singleton (class-side). */
13+
typedef enum {
14+
RUBY_MIXIN_INCLUDE = 0,
15+
RUBY_MIXIN_PREPEND,
16+
RUBY_MIXIN_EXTEND,
17+
} RubyMixinKind;
18+
19+
/* One class/module discovered in the file (or, cross-file, in the project).
20+
* `path` is the dotted constant path used ONLY for constant lookup ("Admin.User"
21+
* for both `module Admin; class User` and `class Admin::User`). `qn` is the
22+
* graph QN built exactly the way the structural extractor builds it: each
23+
* declaration's raw name text appended to the enclosing class QN (or the
24+
* module QN at top level), so a compact `class Admin::User` yields
25+
* "<module_qn>.Admin::User" while the lexically nested form yields
26+
* "<module_qn>.Admin.User". Emitted rows must carry the extractor spelling
27+
* or the pipeline's exact-name join silently drops them. `superclass_qn` is
28+
* resolved against the class table + stdlib after collection; NULL when
29+
* unknown. */
30+
typedef struct {
31+
const char *path;
32+
const char *qn;
33+
const char *superclass_ref; /* raw dotted superclass spelling, or NULL */
34+
const char *superclass_qn; /* resolved QN (table/stdlib), or NULL */
35+
bool is_module; /* `module` (mixin source) vs `class` */
36+
/* Method-lookup visit marks: the stamp of the last walk that searched
37+
* this class on the instance / singleton side (ruby_walk_seen). */
38+
unsigned int seen_instance;
39+
unsigned int seen_singleton;
40+
} RubyClassInfo;
41+
42+
/* One `include`/`prepend`/`extend` record: owner class QN → module ref. */
43+
typedef struct {
44+
const char *owner_qn; /* class/module whose body held the call */
45+
const char *module_ref; /* raw dotted module spelling */
46+
const char *module_qn; /* resolved QN, or NULL */
47+
RubyMixinKind kind;
48+
} RubyMixinInfo;
49+
50+
/* One inferred instance-variable type: (class QN, @name) → type QN.
51+
* `conflicted` latches when two assignments disagree — lookup then returns
52+
* nothing (zero-edge guarantee beats a coin-flip type). */
53+
typedef struct {
54+
const char *class_qn;
55+
const char *ivar_name; /* including the leading '@' */
56+
const char *type_qn;
57+
bool conflicted;
58+
} RubyIvarInfo;
59+
60+
/* RubyLSPContext — per-file state for Ruby type-aware call resolution.
61+
* Mirrors PerlLSPContext / PHPLSPContext structure.
62+
*
63+
* Ruby specifics that shape this context:
64+
* - Classes/modules nest lexically (`module A; class B`), and constants
65+
* resolve through the lexical nesting outward (A::B sees A's constants
66+
* before top-level ones). `nesting` tracks the current dotted path.
67+
* - Inheritance is `class C < Base`; mixins arrive via include/prepend/
68+
* extend and participate in method lookup (see RubyMixinKind).
69+
* - `Foo.new` constructs a Foo and dispatches to Foo#initialize.
70+
* - `def m` defines an instance method; `def self.m` a singleton (class)
71+
* method. Singleton methods are registered under the receiver key
72+
* "<class_qn>.self" — constants cannot be lowercase, so the suffix can
73+
* never collide with a real nested-constant path. */
74+
typedef struct {
75+
CBMArena *arena;
76+
const char *source;
77+
int source_len;
78+
const CBMTypeRegistry *registry;
79+
/* Mutable alias of `registry`, set only while PASS 1 populates method
80+
* tables (mirrors perl_lsp's mutable-reg build phase). NULL afterwards. */
81+
CBMTypeRegistry *build_reg;
82+
CBMScope *current_scope;
83+
84+
/* Lexical nesting: one frame per enclosing class/module declaration,
85+
* frames joined by '.', each frame keeping its raw source spelling ("" at
86+
* top level, "M.A::B" inside `module M; class A::B`). Ruby's constant
87+
* lookup walks the frames outward — inside `class A::B` the nesting is
88+
* [A::B] only, so A's constants are NOT probed. */
89+
const char *nesting;
90+
91+
/* Class/module table (pass 1). */
92+
RubyClassInfo *classes;
93+
int class_count;
94+
int class_cap;
95+
96+
/* Mixin table (pass 1). */
97+
RubyMixinInfo *mixins;
98+
int mixin_count;
99+
int mixin_cap;
100+
101+
/* Instance-variable type table (pass 1). */
102+
RubyIvarInfo *ivars;
103+
int ivar_count;
104+
int ivar_cap;
105+
106+
/* Cross-file import map (require/require_relative resolved by the
107+
* pipeline). Unused per-file: Ruby constants are file-global. */
108+
const char **import_names;
109+
const char **import_qns;
110+
int import_count;
111+
112+
/* Current enclosing context during the resolution walk. */
113+
const char *enclosing_class_qn; /* class QN, or NULL at top level */
114+
const char *enclosing_func_qn; /* enclosing method QN, or NULL */
115+
bool in_singleton_method; /* `def self.m` / class << self scope */
116+
/* Inside a block that rebinds `self` (instance_eval/exec, class_eval/
117+
* exec, module_eval/exec, define_method, Class.new/Module.new): the
118+
* receiver of bare and `self.` calls is NOT the enclosing class, and
119+
* the resolver cannot know what it is — so they emit nothing. */
120+
bool self_unknown;
121+
/* Number of enclosing blocks (`{ }`, `do end`, `-> { }`) since the
122+
* nearest class/module body. A `def` met here is NOT a method of the
123+
* enclosing class — ActiveSupport's `class_methods do … end`,
124+
* `included do … end`, RSpec groups — so PASS 1 registers nothing for
125+
* it and collects no mixins from it. */
126+
int block_depth;
127+
const char *module_qn;
128+
129+
/* Output: resolved calls accumulate here. */
130+
CBMResolvedCallArray *resolved_calls;
131+
132+
/* Recursion guards. */
133+
int eval_depth;
134+
int walk_depth;
135+
136+
/* Stamp of the current method-lookup walk; each walk takes a fresh one
137+
* so marks left by earlier walks never match. */
138+
unsigned int lookup_stamp;
139+
140+
/* Debug mode (CBM_LSP_DEBUG env). */
141+
bool debug;
142+
} RubyLSPContext;
143+
144+
/* Initialize a RubyLSPContext for processing one file. */
145+
void ruby_lsp_init(RubyLSPContext *ctx, CBMArena *arena, const char *source, int source_len,
146+
const CBMTypeRegistry *registry, const char *module_qn,
147+
CBMResolvedCallArray *out);
148+
149+
/* Resolve a dotted constant path (e.g. "Foo", "A.B") against the class
150+
* table using the current lexical nesting, then the stdlib. Returns the
151+
* resolved QN or NULL. */
152+
const char *ruby_resolve_constant(RubyLSPContext *ctx, const char *path);
153+
154+
/* Look up an instance method on a class, walking prepends → own → includes →
155+
* superclass chain. Returns the resolved CBMRegisteredFunc or NULL. */
156+
const CBMRegisteredFunc *ruby_lookup_instance_method(RubyLSPContext *ctx, const char *class_qn,
157+
const char *method_name);
158+
159+
/* Look up a singleton (class-side) method: own singleton methods →
160+
* `extend`ed modules' instance methods → superclass singleton chain. */
161+
const CBMRegisteredFunc *ruby_lookup_singleton_method(RubyLSPContext *ctx, const char *class_qn,
162+
const char *method_name);
163+
164+
/* Evaluate a Ruby expression's type. May return CBM_TYPE_UNKNOWN. */
165+
const CBMType *ruby_eval_expr_type(RubyLSPContext *ctx, TSNode node);
166+
167+
/* Entry point: build registry from file defs + stdlib, then run resolution.
168+
* Called from cbm_extract_file() via the language dispatch in cbm.c. */
169+
void cbm_run_ruby_lsp(CBMArena *arena, CBMFileResult *result, const char *source, int source_len,
170+
TSNode root);
171+
172+
/* Register Ruby core stdlib types/methods + a curated Rails surface
173+
* (ActiveRecord/ActiveSupport/ActionController) into a registry. */
174+
void cbm_ruby_stdlib_register(CBMTypeRegistry *reg, CBMArena *arena);
175+
176+
#endif /* CBM_LSP_RUBY_LSP_H */

‎internal/cbm/lsp_all.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include "lsp/generated/php_stdlib_data.c"
1515
#include "lsp/perl_lsp.c"
1616
#include "lsp/generated/perl_stdlib_data.c"
17+
#include "lsp/ruby_lsp.c"
18+
#include "lsp/generated/ruby_stdlib_data.c"
1719
#include "lsp/generated/python_stdlib_data.c"
1820
#include "lsp/py_lsp.c"
1921
#include "lsp/ts_lsp.c"

‎scripts/check-lsp-originality.sh‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
# USAGE
2828
# bash scripts/check-lsp-originality.sh [--lang NAME] [--refresh]
2929
# [--list-candidates] [--help]
30-
# --lang NAME scan against ONE reference only (py|ts|go|cs|c|java|kotlin|rust|php|perl)
30+
# --lang NAME scan against ONE reference only (py|ts|go|cs|c|java|kotlin|rust|php|perl|ruby)
3131
# --refresh re-fetch reference sources even if cached
3232
# --list-candidates print the extracted local tokens and exit (no fetch; self-test)
3333
# Exit 0 = no verbatim overlap found. Exit 1 = overlap(s) to review by a human.
@@ -64,6 +64,10 @@ REFS=(
6464
# Perl (PR #461) was authored clean-room. We scan against PerlNavigator (MIT)
6565
# — the leading OSS Perl language server — for defensive copy-detection.
6666
"perl|https://github.com/bscan/PerlNavigator|server/src"
67+
# Ruby was authored clean-room from the tree-sitter-ruby grammar + Ruby
68+
# language semantics. We scan against Shopify's ruby-lsp (MIT) — the
69+
# leading OSS Ruby language server — for defensive copy-detection.
70+
"ruby|https://github.com/Shopify/ruby-lsp|lib"
6771
# Infrastructure hybrid LSPs (K8s YAML, Kustomize bundles, Flux/Argo deploy
6872
# scopes, the Go operator extension) were written from the Kubernetes, Flux,
6973
# Argo, Cluster API, cert-manager and Kubermatic API types. Scanned against

‎tests/test_main.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1802,6 +1802,7 @@ extern void suite_php_lsp(void);
18021802
extern void suite_cs_lsp(void);
18031803
extern void suite_cs_lsp_bench(void);
18041804
extern void suite_perl_lsp(void);
1805+
extern void suite_ruby_lsp(void);
18051806
extern void suite_scope(void);
18061807
extern void suite_type_rep(void);
18071808
extern void suite_py_lsp(void);
@@ -2435,6 +2436,7 @@ int main(int argc, char **argv) {
24352436
RUN_SELECTED_SUITE(cs_lsp);
24362437
RUN_SELECTED_SUITE_PERF(cs_lsp_bench);
24372438
RUN_SELECTED_SUITE(perl_lsp);
2439+
RUN_SELECTED_SUITE(ruby_lsp);
24382440
RUN_SELECTED_SUITE(py_lsp);
24392441
RUN_SELECTED_SUITE(kotlin_lsp);
24402442
RUN_SELECTED_SUITE(kotlin_typed_calls);

0 commit comments

Comments
 (0)