MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / readRustScopes

Function readRustScopes

src/resolution/rust-scopes.ts:424–511  ·  view source on GitHub ↗
(source: string)

Source from the content-addressed store, hash-verified

422 * the first lookup — most files read here are only looked into.
423 */
424export function readRustScopes(source: string): RustScopes {
425 const code = blankRustCode(source);
426 const n = code.length;
427 const lineStarts = [0];
428 for (let i = code.indexOf('\n'); i >= 0; i = code.indexOf('\n', i + 1)) lineStarts.push(i + 1);
429 const close = pairBrackets(code);
430
431 const fns: RustFn[] = [];
432 for (const m of code.matchAll(/\bfn\b/g)) {
433 let p = skipSpace(code, m.index! + 2);
434 const name = identAt(code, p);
435 if (!name) continue;
436 p = skipSpace(code, p + name.length);
437 let generics = '';
438 if (code[p] === '<') {
439 const end = angleClose(code, p);
440 if (end < 0) continue;
441 generics = code.slice(p + 1, end);
442 p = skipSpace(code, end + 1);
443 }
444 if (code[p] !== '(') continue;
445 const paramsEnd = close.get(p);
446 if (paramsEnd === undefined) continue;
447 // The body: the first `{` past the return type and `where` clause; a `;`
448 // first is a declaration, which binds nothing.
449 let body = -1;
450 let where = -1;
451 for (let q = paramsEnd + 1; q < n; q++) {
452 const c = code[q]!;
453 if (c === '{') { body = q; break; }
454 if (c === ';' || c === '}') break;
455 if (c === '(' || c === '[') { q = close.get(q) ?? n; continue; }
456 if (where < 0 && c === 'w' && wordAt(code, q, 'where')) where = q + 5;
457 }
458 if (body < 0) continue;
459 fns.push({ at: m.index!, generics, where: where < 0 ? '' : code.slice(where, body), from: p, to: close.get(body) ?? n, paramsEnd, body });
460 }
461
462 // `impl<T: Bound> … {` and `trait Name<T>: Super where … {` — an item, not
463 // the `impl Trait` of a type, which follows a `:`, `(`, `,`, `<`, `&` or `->`.
464 const impls: RustImplBlock[] = [];
465 for (const m of code.matchAll(/\b(impl|trait)\b/g)) {
466 const kind = m[1] as 'impl' | 'trait';
467 if (kind === 'impl') {
468 let k = m.index! - 1;
469 while (k >= 0 && /\s/.test(code[k]!)) k--;
470 if (k >= 0 && !'{};]'.includes(code[k]!) && !/\b(?:unsafe|default)$/.test(code.slice(Math.max(0, k - 7), k + 1))) continue;
471 }
472 let p = skipSpace(code, m.index! + kind.length);
473 if (kind === 'trait') {
474 const name = identAt(code, p);
475 if (!name) continue;
476 p = skipSpace(code, p + name.length);
477 }
478 let generics = '';
479 if (code[p] === '<') {
480 const end = angleClose(code, p);
481 if (end < 0) continue;

Callers

nothing calls this directly

Calls 9

blankRustCodeFunction · 0.85
pairBracketsFunction · 0.85
identAtFunction · 0.85
angleCloseFunction · 0.85
wordAtFunction · 0.85
pushMethod · 0.80
skipSpaceFunction · 0.70
getMethod · 0.65
execMethod · 0.65

Tested by

no test coverage detected