pi-hashline-edit-pro 7.0.0 → 7.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -170,10 +170,10 @@ export function languageLabel(key: string): string {
170
170
  export async function parseCode(
171
171
  key: string,
172
172
  source: string,
173
- ): Promise<SymbolInfo[]> {
173
+ ): Promise<{ symbols: SymbolInfo[]; hasError: boolean }> {
174
174
  // Markdown uses regex-based parsing, not tree-sitter
175
175
  if (key === "markdown") {
176
- return extractMarkdown(source);
176
+ return { symbols: extractMarkdown(source), hasError: false };
177
177
  }
178
178
 
179
179
  await ensureInit();
@@ -183,9 +183,9 @@ export async function parseCode(
183
183
 
184
184
  sharedParser.setLanguage(lang);
185
185
  const tree = sharedParser.parse(source);
186
- if (!tree) return [];
186
+ if (!tree) return { symbols: [], hasError: false };
187
187
 
188
- return extractSymbols(key, tree.rootNode, source, 0);
188
+ return { symbols: extractSymbols(key, tree.rootNode, source, 0), hasError: tree.rootNode.hasError };
189
189
  }
190
190
 
191
191
  // ---- Symbol extraction dispatch ----
@@ -275,33 +275,16 @@ function extractJavaScriptLike(
275
275
  const child = node.namedChild(i);
276
276
  if (!child) continue;
277
277
 
278
- const symbol = extractJSChild(child, source);
278
+ const symbol = extractJSChild(child, source) ?? (node.type === "program" ? commonJsExportSymbol(child, source) : null);
279
279
  if (!symbol) continue;
280
280
 
281
- const declaration =
282
- child.type === "export_statement"
283
- ? findChildByType(child, [
284
- "class_declaration",
285
- "class",
286
- "abstract_class_declaration",
287
- "interface_declaration",
288
- "enum_declaration",
289
- ]) ?? child
290
- : child;
291
-
292
- // For class/interface/enum, look inside their bodies
293
- if (
294
- symbol.type === "class" ||
295
- symbol.type === "interface" ||
296
- symbol.type === "enum"
297
- ) {
298
- const bodyNode = findChildByType(declaration, [
299
- "class_body",
300
- "interface_body",
301
- "enum_body",
302
- "object_type",
303
- ]);
304
- if (bodyNode && depth < MAX_DEPTH) {
281
+ // Look inside the bodies of container declarations
282
+ if (JS_CONTAINER_TYPES.has(symbol.type ?? "") && depth < MAX_DEPTH) {
283
+ const declaration = jsDeclarationNode(child);
284
+ const bodyNode =
285
+ declaration.childForFieldName("body") ??
286
+ findChildByType(declaration, ["class_body", "interface_body", "enum_body", "object_type", "statement_block"]);
287
+ if (bodyNode) {
305
288
  symbol.children = extractJavaScriptLike(bodyNode, source, depth + 1);
306
289
  }
307
290
  }
@@ -312,23 +295,60 @@ function extractJavaScriptLike(
312
295
  return results;
313
296
  }
314
297
 
298
+ const JS_CONTAINER_TYPES = new Set(["class", "interface", "enum", "namespace"]);
299
+
300
+ /** Unwrap `export`, `declare` and the expression statement TypeScript puts around `namespace`. */
301
+ function jsDeclarationNode(child: Node): Node {
302
+ if (child.type === "export_statement" || child.type === "ambient_declaration" || child.type === "expression_statement") {
303
+ const inner = child.namedChildren.find((c) => c.type !== "decorator" && c.type !== "comment");
304
+ if (inner && inner.type !== "statement_block") return jsDeclarationNode(inner);
305
+ }
306
+ return child;
307
+ }
308
+
315
309
  const JS_SYMBOL_TYPES: Record<string, string> = {
316
310
  function_declaration: "function",
311
+ generator_function_declaration: "function",
312
+ function_signature: "function",
317
313
  function_expression: "function",
318
314
  arrow_function: "arrow_function",
319
315
  method_definition: "method",
316
+ method_signature: "method",
317
+ abstract_method_signature: "method",
320
318
  class_declaration: "class",
321
319
  class: "class",
322
320
  interface_declaration: "interface",
323
321
  enum_declaration: "enum",
322
+ enum_assignment: "enum_member",
324
323
  type_alias_declaration: "type",
325
324
  abstract_class_declaration: "class",
325
+ internal_module: "namespace",
326
+ module: "namespace",
327
+ public_field_definition: "field",
328
+ field_definition: "field",
329
+ property_signature: "field",
326
330
  lexical_declaration: "variable",
327
331
  variable_declaration: "variable",
328
332
  };
329
333
 
330
334
  function isFunctionNode(node: Node): boolean {
331
- return node.type === "arrow_function" || node.type === "function_expression" || node.type === "function";
335
+ return node.type === "arrow_function" || node.type === "function_expression" || node.type === "function" || node.type === "generator_function";
336
+ }
337
+
338
+ const COMMONJS_TARGET = /^(module\.exports(\.[\w$]+)?|exports\.[\w$]+|[\w$]+\.prototype\.[\w$]+)$/;
339
+
340
+ /** Top-level `module.exports = ...`, `exports.x = ...` and `Foo.prototype.m = ...`. */
341
+ function commonJsExportSymbol(child: Node, source: string): SymbolInfo | null {
342
+ if (child.type !== "expression_statement") return null;
343
+ const expr = child.namedChild(0);
344
+ if (!expr || expr.type !== "assignment_expression") return null;
345
+ const left = expr.childForFieldName("left");
346
+ const right = expr.childForFieldName("right");
347
+ if (!left || left.type !== "member_expression") return null;
348
+ const name = source.slice(left.startIndex, left.endIndex);
349
+ if (!COMMONJS_TARGET.test(name)) return null;
350
+ const [sl, el] = nodeRange(child);
351
+ return { name, type: right && isFunctionNode(right) ? "function" : "variable", startLine: sl, endLine: el };
332
352
  }
333
353
 
334
354
  function extractJSChild(
@@ -356,7 +376,6 @@ function extractJSChild(
356
376
  type: value.type === "arrow_function" ? "arrow_function" : "function",
357
377
  startLine: sl,
358
378
  endLine: el,
359
- detail: extractFnDetail(value, source),
360
379
  });
361
380
  } else {
362
381
  const [sl, el] = nodeRange(d);
@@ -390,6 +409,10 @@ function extractJSChild(
390
409
  const result = extractJSChild(inner, source);
391
410
  if (result) {
392
411
  if (isDefault && result.name === "<anonymous>") result.name = "default";
412
+ const [sl, el] = nodeRange(child);
413
+ // Keep the row on the `export` line when the declaration starts there.
414
+ if (inner.type === "expression_statement" && result.startLine > sl) result.startLine = sl;
415
+ if (result.endLine < el && inner.type === "expression_statement") result.endLine = el;
393
416
  return result;
394
417
  }
395
418
  }
@@ -397,26 +420,52 @@ function extractJSChild(
397
420
  return null;
398
421
  }
399
422
 
400
- if (type === "expression_statement") return testCallSymbol(child, source);
423
+ // `declare module "x" {}`, `declare function f(): void;`, `declare global {}`
424
+ if (type === "ambient_declaration") {
425
+ const inner = child.namedChildren.find((c) => c.type !== "comment");
426
+ if (!inner) return null;
427
+ const [sl, el] = nodeRange(child);
428
+ if (inner.type === "statement_block") return { name: "global", type: "namespace", startLine: sl, endLine: el };
429
+ const result = extractJSChild(inner, source);
430
+ if (result) {
431
+ result.startLine = sl;
432
+ result.endLine = el;
433
+ }
434
+ return result;
435
+ }
436
+
437
+ if (type === "expression_statement") {
438
+ const inner = child.namedChild(0);
439
+ if (inner?.type === "internal_module") {
440
+ const result = extractJSChild(inner, source);
441
+ if (result) [result.startLine, result.endLine] = nodeRange(child);
442
+ return result;
443
+ }
444
+ return testCallSymbol(child, source);
445
+ }
446
+
447
+ // Bare enum members: `enum E { Red, Green }`
448
+ if (type === "property_identifier" && child.parent?.type === "enum_body") {
449
+ const [sl, el] = nodeRange(child);
450
+ return { name: source.slice(child.startIndex, child.endIndex), type: "enum_member", startLine: sl, endLine: el };
451
+ }
401
452
 
402
453
  const symbolType = JS_SYMBOL_TYPES[type];
403
454
  if (!symbolType) return null;
404
455
 
405
456
  const name =
406
457
  fieldChildText(child, "name", source) ||
458
+ fieldChildText(child, "property", source) ||
407
459
  childText(child, "property_identifier", source) ||
408
460
  childText(child, "type_identifier", source) ||
409
461
  "<anonymous>";
410
462
 
411
- const [sl, el] = nodeRange(child);
412
-
413
- // For functions/methods, try to get the parameters as detail
414
- let detail: string | undefined;
415
- if (type === "function_declaration" || type === "function_expression" || type === "method_definition" || type === "arrow_function") {
416
- detail = extractFnDetail(child, source);
417
- }
463
+ // Decorators on their own lines are children of fields and non-exported classes; start at the declaration.
464
+ const declarationStart = child.children.find((part) => part.type !== "decorator" && part.type !== "comment");
465
+ const [, el] = nodeRange(child);
466
+ const sl = (declarationStart ?? child).startPosition.row + 1;
418
467
 
419
- return { name, type: symbolType, startLine: sl, endLine: el, detail };
468
+ return { name, type: symbolType, startLine: sl, endLine: el };
420
469
  }
421
470
 
422
471
  const TEST_CALLEES = new Set(["describe", "test", "it", "suite", "context", "specify"]);
@@ -455,17 +504,6 @@ function extractTestCalls(call: Node, source: string): SymbolInfo[] {
455
504
  return results;
456
505
  }
457
506
 
458
- function extractFnDetail(
459
- node: Node,
460
- source: string,
461
- ): string | undefined {
462
- const params = node.childForFieldName("parameters");
463
- if (params) {
464
- return source.slice(params.startIndex, params.endIndex);
465
- }
466
- return undefined;
467
- }
468
-
469
507
  function findChildByType(
470
508
  node: Node,
471
509
  types: string[],
@@ -495,34 +533,15 @@ function extractPython(
495
533
 
496
534
  let symbol: SymbolInfo | null = null;
497
535
 
498
- if (child.type === "class_definition") {
499
- const name = fieldChildText(child, "name", source) || "<anonymous>";
500
- const [sl, el] = nodeRange(child);
501
- symbol = { name, type: "class", startLine: sl, endLine: el };
502
-
503
- // Recurse into body for methods
504
- const body = child.childForFieldName("body");
505
- if (body && depth < MAX_DEPTH) {
506
- symbol.children = extractPython(body, source, depth + 1);
507
- }
508
- } else if (child.type === "function_definition") {
509
- const name = fieldChildText(child, "name", source) || "<anonymous>";
510
- const [sl, el] = nodeRange(child);
511
- const params = child.childForFieldName("parameters");
512
- const detail = params
513
- ? source.slice(params.startIndex, params.endIndex)
514
- : undefined;
515
- symbol = { name, type: "function", startLine: sl, endLine: el, detail };
516
- } else if (child.type === "decorated_definition") {
517
- // Unwrap to the inner definition
518
- const def = child.namedChild(child.namedChildCount - 1);
519
- if (def) {
520
- const inner = extractPythonSingle(def, source);
521
- if (inner) {
522
- inner.type = "decorated_" + inner.type;
523
- symbol = inner;
524
- }
525
- }
536
+ if (child.type === "decorated_definition") {
537
+ // The row and span start at the definition line; the decorators above it are excluded.
538
+ const def = child.childForFieldName("definition");
539
+ symbol = def ? extractPythonDefinition(def, source, depth, MAX_DEPTH) : null;
540
+ if (symbol) symbol.type = "decorated_" + symbol.type;
541
+ } else if (child.type === "expression_statement" || child.type === "type_alias_statement") {
542
+ symbol = extractPythonAssignment(child, source);
543
+ } else {
544
+ symbol = extractPythonDefinition(child, source, depth, MAX_DEPTH);
526
545
  }
527
546
 
528
547
  if (symbol) results.push(symbol);
@@ -531,27 +550,44 @@ function extractPython(
531
550
  return results;
532
551
  }
533
552
 
534
- function extractPythonSingle(
553
+ function extractPythonDefinition(
535
554
  child: Node,
536
555
  source: string,
556
+ depth: number,
557
+ maxDepth: number,
537
558
  ): SymbolInfo | null {
538
559
  if (child.type === "class_definition") {
539
560
  const name = fieldChildText(child, "name", source) || "<anonymous>";
540
561
  const [sl, el] = nodeRange(child);
541
- return { name, type: "class", startLine: sl, endLine: el };
562
+ const symbol: SymbolInfo = { name, type: "class", startLine: sl, endLine: el };
563
+ const body = child.childForFieldName("body");
564
+ if (body && depth < maxDepth) {
565
+ symbol.children = extractPython(body, source, depth + 1);
566
+ }
567
+ return symbol;
542
568
  }
543
569
  if (child.type === "function_definition") {
544
570
  const name = fieldChildText(child, "name", source) || "<anonymous>";
545
571
  const [sl, el] = nodeRange(child);
546
- const params = child.childForFieldName("parameters");
547
- const detail = params
548
- ? source.slice(params.startIndex, params.endIndex)
549
- : undefined;
550
- return { name, type: "function", startLine: sl, endLine: el, detail };
572
+ return { name, type: "function", startLine: sl, endLine: el };
551
573
  }
552
574
  return null;
553
575
  }
554
576
 
577
+ /** `NAME = ...`, `NAME: T = ...` and `type NAME = ...` at module or class level; other targets are skipped. */
578
+ function extractPythonAssignment(child: Node, source: string): SymbolInfo | null {
579
+ const [sl, el] = nodeRange(child);
580
+ if (child.type === "type_alias_statement") {
581
+ const left = child.namedChild(0);
582
+ return left ? { name: source.slice(left.startIndex, left.endIndex), type: "type", startLine: sl, endLine: el } : null;
583
+ }
584
+ const assignment = child.namedChild(0);
585
+ if (!assignment || assignment.type !== "assignment") return null;
586
+ const left = assignment.childForFieldName("left");
587
+ if (!left || left.type !== "identifier") return null;
588
+ return { name: source.slice(left.startIndex, left.endIndex), type: "variable", startLine: sl, endLine: el };
589
+ }
590
+
555
591
  // ========================================================================
556
592
  // Go
557
593
  // ========================================================================
@@ -588,7 +624,7 @@ function extractGo(
588
624
  const specs: SymbolInfo[] = [];
589
625
  for (let j = 0; j < child.namedChildCount; j++) {
590
626
  const spec = child.namedChild(j);
591
- if (!spec || spec.type !== "type_spec") continue;
627
+ if (!spec || (spec.type !== "type_spec" && spec.type !== "type_alias")) continue;
592
628
  const name = fieldChildText(spec, "name", source) || "<anonymous>";
593
629
  const [sl, el] = nodeRange(spec);
594
630
  const typeNode = spec.childForFieldName("type");
@@ -601,60 +637,62 @@ function extractGo(
601
637
 
602
638
  // Recurse into struct/interface body
603
639
  if (typeNode && depth < MAX_DEPTH) {
604
- const body = findChildByType(typeNode, [
605
- "field_declaration_list",
606
- ]);
640
+ const body = typeNode.type === "interface_type"
641
+ ? typeNode
642
+ : findChildByType(typeNode, ["field_declaration_list"]);
607
643
  if (body) {
608
- entry.children = extractGoFields(body, source);
644
+ const fields = extractGoFields(body, source);
645
+ if (fields.length > 0) entry.children = fields;
609
646
  }
610
647
  }
611
648
 
612
649
  specs.push(entry);
613
650
  }
614
- if (specs.length === 1) {
651
+ if (child.children.some((part) => part.type === "(")) {
652
+ const [sl, el] = nodeRange(child);
653
+ symbol = { name: "type", type: "type", startLine: sl, endLine: el, children: specs };
654
+ } else if (specs.length === 1) {
615
655
  symbol = specs[0];
616
- } else if (specs.length > 1) {
617
- symbol = {
618
- name: "",
619
- type: "type",
620
- startLine: specs[0].startLine,
621
- endLine: specs[specs.length - 1].endLine,
622
- children: specs,
623
- };
624
656
  }
625
657
  if (symbol) results.push(symbol);
626
658
  continue;
627
659
  } else if (child.type === "const_declaration") {
628
660
  const [sl, el] = nodeRange(child);
629
- const names: string[] = [];
661
+ const specs: SymbolInfo[] = [];
630
662
  for (let j = 0; j < child.namedChildCount; j++) {
631
663
  const spec = child.namedChild(j);
632
- if (spec && spec.type === "const_spec") {
633
- const n = fieldChildText(spec, "name", source);
634
- if (n) names.push(n);
635
- }
664
+ if (!spec || spec.type !== "const_spec") continue;
665
+ const name = fieldChildText(spec, "name", source) || "<anonymous>";
666
+ const [specStart, specEnd] = nodeRange(spec);
667
+ specs.push({ name, type: "const", startLine: specStart, endLine: specEnd });
636
668
  }
669
+ const grouped = child.children.some((part) => part.type === "(");
637
670
  symbol = {
638
- name: names.join(", ") || "const",
671
+ name: grouped ? "const" : specs.map((spec) => spec.name).join(", ") || "const",
639
672
  type: "const",
640
673
  startLine: sl,
641
674
  endLine: el,
675
+ ...(grouped ? { children: specs } : {}),
642
676
  };
643
677
  } else if (child.type === "var_declaration") {
644
678
  const [sl, el] = nodeRange(child);
645
- const names: string[] = [];
646
- for (let j = 0; j < child.namedChildCount; j++) {
647
- const spec = child.namedChild(j);
648
- if (spec && spec.type === "var_spec") {
649
- const n = fieldChildText(spec, "name", source);
650
- if (n) names.push(n);
651
- }
679
+ const specs: SymbolInfo[] = [];
680
+ const list = findChildByType(child, ["var_spec_list"]);
681
+ const container = list ?? child;
682
+ for (let j = 0; j < container.namedChildCount; j++) {
683
+ const spec = container.namedChild(j);
684
+ if (!spec || spec.type !== "var_spec") continue;
685
+ const name = fieldChildText(spec, "name", source) || "<anonymous>";
686
+ const [specStart, specEnd] = nodeRange(spec);
687
+ specs.push({ name, type: "var", startLine: specStart, endLine: specEnd });
652
688
  }
689
+ const grouped = list !== null;
653
690
  symbol = {
654
- name: names.join(", ") || "var",
691
+ name: grouped ? "var" : specs.map((spec) => spec.name).join(", ") || "var",
655
692
  type: "var",
656
693
  startLine: sl,
657
694
  endLine: el,
695
+ ...(grouped ? { children: specs } : {}),
658
696
  };
659
697
  }
660
698
 
@@ -672,21 +710,18 @@ function extractGoFields(
672
710
 
673
711
  for (let i = 0; i < node.namedChildCount; i++) {
674
712
  const child = node.namedChild(i);
675
- if (!child || child.type !== "field_declaration") continue;
713
+ if (!child || !["field_declaration", "method_elem", "type_elem"].includes(child.type)) continue;
676
714
 
677
- const name = fieldChildText(child, "name", source);
715
+ const first = child.namedChild(0);
716
+ const name = fieldChildText(child, "name", source) ||
717
+ (first ? source.slice(first.startIndex, first.endIndex) : "");
678
718
  if (!name) continue;
679
719
  const [sl, el] = nodeRange(child);
680
- const typeNode = child.childForFieldName("type");
681
- const typeText = typeNode
682
- ? source.slice(typeNode.startIndex, typeNode.endIndex)
683
- : "";
684
720
  results.push({
685
721
  name,
686
- type: "field",
722
+ type: child.type === "method_elem" ? "method" : "field",
687
723
  startLine: sl,
688
724
  endLine: el,
689
- detail: typeText,
690
725
  });
691
726
  }
692
727
 
@@ -701,6 +736,7 @@ function extractRust(
701
736
  node: Node,
702
737
  source: string,
703
738
  depth: number,
739
+ inImplOrTrait = false,
704
740
  ): SymbolInfo[] {
705
741
  const results: SymbolInfo[] = [];
706
742
  const MAX_DEPTH = depth + 10;
@@ -709,96 +745,77 @@ function extractRust(
709
745
  const child = node.namedChild(i);
710
746
  if (!child) continue;
711
747
 
748
+ const [sl, el] = nodeRange(child);
749
+ const name = fieldChildText(child, "name", source) || "<anonymous>";
712
750
  let symbol: SymbolInfo | null = null;
713
-
714
- if (child.type === "function_item") {
715
- const name = fieldChildText(child, "name", source) || "<anonymous>";
716
- const [sl, el] = nodeRange(child);
717
- const params = child.childForFieldName("parameters");
718
- const detail = params
719
- ? source.slice(params.startIndex, params.endIndex)
720
- : undefined;
721
- symbol = { name, type: "function", startLine: sl, endLine: el, detail };
722
- } else if (child.type === "struct_item") {
723
- const name = fieldChildText(child, "name", source) || "<anonymous>";
724
- const [sl, el] = nodeRange(child);
725
- symbol = { name, type: "struct", startLine: sl, endLine: el };
726
- } else if (child.type === "enum_item") {
727
- const name = fieldChildText(child, "name", source) || "<anonymous>";
728
- const [sl, el] = nodeRange(child);
729
- symbol = { name, type: "enum", startLine: sl, endLine: el };
730
- } else if (child.type === "trait_item") {
731
- const name = fieldChildText(child, "name", source) || "<anonymous>";
732
- const [sl, el] = nodeRange(child);
733
- symbol = { name, type: "trait", startLine: sl, endLine: el };
734
- } else if (child.type === "impl_item") {
735
- const trait = child.childForFieldName("trait");
736
- const implType = child.childForFieldName("type");
737
- const traitStr = trait
738
- ? source.slice(trait.startIndex, trait.endIndex) + " for "
739
- : "";
740
- const typeStr = implType
741
- ? source.slice(implType.startIndex, implType.endIndex)
742
- : "";
743
- const [sl, el] = nodeRange(child);
744
- const name = `impl ${traitStr}${typeStr}`;
745
- symbol = { name, type: "impl", startLine: sl, endLine: el };
746
-
747
- // Recurse into body for associated functions/methods
748
- const body = child.childForFieldName("body");
749
- if (body && depth < MAX_DEPTH) {
750
- symbol.children = extractRustImplBody(body, source);
751
+ // Body whose items become children, and whether its functions are methods.
752
+ let body: Node | null = null;
753
+ let bodyIsImplOrTrait = false;
754
+
755
+ switch (child.type) {
756
+ case "function_item":
757
+ case "function_signature_item":
758
+ symbol = { name, type: inImplOrTrait ? "method" : "function", startLine: sl, endLine: el };
759
+ break;
760
+ case "struct_item":
761
+ case "union_item":
762
+ symbol = { name, type: child.type === "struct_item" ? "struct" : "union", startLine: sl, endLine: el };
763
+ body = child.childForFieldName("body");
764
+ break;
765
+ case "enum_item":
766
+ symbol = { name, type: "enum", startLine: sl, endLine: el };
767
+ body = child.childForFieldName("body");
768
+ break;
769
+ case "field_declaration":
770
+ symbol = { name, type: "field", startLine: sl, endLine: el };
771
+ break;
772
+ case "enum_variant":
773
+ symbol = { name, type: "enum_variant", startLine: sl, endLine: el };
774
+ break;
775
+ case "trait_item":
776
+ symbol = { name, type: "trait", startLine: sl, endLine: el };
777
+ body = child.childForFieldName("body");
778
+ bodyIsImplOrTrait = true;
779
+ break;
780
+ case "impl_item": {
781
+ const trait = child.childForFieldName("trait");
782
+ const implType = child.childForFieldName("type");
783
+ const traitStr = trait ? source.slice(trait.startIndex, trait.endIndex) + " for " : "";
784
+ const typeStr = implType ? source.slice(implType.startIndex, implType.endIndex) : "";
785
+ symbol = { name: `impl ${traitStr}${typeStr}`, type: "impl", startLine: sl, endLine: el };
786
+ body = child.childForFieldName("body");
787
+ bodyIsImplOrTrait = true;
788
+ break;
751
789
  }
752
- } else if (child.type === "type_item") {
753
- const name = fieldChildText(child, "name", source) || "<anonymous>";
754
- const [sl, el] = nodeRange(child);
755
- symbol = { name, type: "type", startLine: sl, endLine: el };
756
- } else if (child.type === "const_item") {
757
- const name = fieldChildText(child, "name", source) || "<anonymous>";
758
- const [sl, el] = nodeRange(child);
759
- symbol = { name, type: "const", startLine: sl, endLine: el };
760
- } else if (child.type === "macro_definition") {
761
- const name = fieldChildText(child, "name", source) || "<anonymous>";
762
- const [sl, el] = nodeRange(child);
763
- symbol = { name: `${name}!`, type: "macro", startLine: sl, endLine: el };
790
+ case "mod_item":
791
+ symbol = { name, type: "module", startLine: sl, endLine: el };
792
+ body = child.childForFieldName("body");
793
+ break;
794
+ case "foreign_mod_item": {
795
+ const modifier = findChildByType(child, ["extern_modifier"]);
796
+ symbol = { name: modifier ? source.slice(modifier.startIndex, modifier.endIndex) : "extern", type: "extern", startLine: sl, endLine: el };
797
+ body = child.childForFieldName("body") ?? findChildByType(child, ["declaration_list"]);
798
+ break;
799
+ }
800
+ case "type_item":
801
+ case "associated_type":
802
+ symbol = { name, type: "type", startLine: sl, endLine: el };
803
+ break;
804
+ case "const_item":
805
+ symbol = { name, type: "const", startLine: sl, endLine: el };
806
+ break;
807
+ case "static_item":
808
+ symbol = { name, type: "static", startLine: sl, endLine: el };
809
+ break;
810
+ case "macro_definition":
811
+ symbol = { name: `${name}!`, type: "macro", startLine: sl, endLine: el };
812
+ break;
764
813
  }
765
814
 
766
- if (symbol) results.push(symbol);
767
- }
768
-
769
- return results;
770
- }
771
-
772
- function extractRustImplBody(
773
- node: Node,
774
- source: string,
775
- ): SymbolInfo[] {
776
- const results: SymbolInfo[] = [];
777
-
778
- for (let i = 0; i < node.namedChildCount; i++) {
779
- const child = node.namedChild(i);
780
- if (!child) continue;
781
-
782
- let symbol: SymbolInfo | null = null;
783
-
784
- if (child.type === "function_item") {
785
- const name = fieldChildText(child, "name", source) || "<anonymous>";
786
- const [sl, el] = nodeRange(child);
787
- const params = child.childForFieldName("parameters");
788
- const detail = params
789
- ? source.slice(params.startIndex, params.endIndex)
790
- : undefined;
791
- symbol = { name, type: "method", startLine: sl, endLine: el, detail };
792
- } else if (child.type === "const_item") {
793
- const name = fieldChildText(child, "name", source) || "<anonymous>";
794
- const [sl, el] = nodeRange(child);
795
- symbol = { name, type: "const", startLine: sl, endLine: el };
796
- } else if (child.type === "type_item") {
797
- const name = fieldChildText(child, "name", source) || "<anonymous>";
798
- const [sl, el] = nodeRange(child);
799
- symbol = { name, type: "type", startLine: sl, endLine: el };
815
+ if (symbol && body && depth < MAX_DEPTH) {
816
+ const children = extractRust(body, source, depth + 1, bodyIsImplOrTrait);
817
+ if (children.length > 0) symbol.children = children;
800
818
  }
801
-
802
819
  if (symbol) results.push(symbol);
803
820
  }
804
821
 
@@ -827,20 +844,7 @@ function extractSolidity(
827
844
  const name = fieldChildText(child, "name", source) || "<anonymous>";
828
845
  const [sl, el] = nodeRange(child);
829
846
 
830
- // Collect inheritance detail
831
- const inherits: string[] = [];
832
- for (let j = 0; j < child.namedChildCount; j++) {
833
- const c = child.namedChild(j);
834
- if (c && c.type === "inheritance_specifier") {
835
- const ancestor = c.childForFieldName("ancestor");
836
- if (ancestor) {
837
- inherits.push(source.slice(ancestor.startIndex, ancestor.endIndex));
838
- }
839
- }
840
- }
841
- const detail = inherits.length > 0 ? `is ${inherits.join(", ")}` : undefined;
842
-
843
- symbol = { name, type: "contract", startLine: sl, endLine: el, detail };
847
+ symbol = { name, type: "contract", startLine: sl, endLine: el };
844
848
 
845
849
  // Recurse into contract body
846
850
  const body = child.childForFieldName("body");
@@ -851,19 +855,7 @@ function extractSolidity(
851
855
  const name = fieldChildText(child, "name", source) || "<anonymous>";
852
856
  const [sl, el] = nodeRange(child);
853
857
 
854
- const inherits: string[] = [];
855
- for (let j = 0; j < child.namedChildCount; j++) {
856
- const c = child.namedChild(j);
857
- if (c && c.type === "inheritance_specifier") {
858
- const ancestor = c.childForFieldName("ancestor");
859
- if (ancestor) {
860
- inherits.push(source.slice(ancestor.startIndex, ancestor.endIndex));
861
- }
862
- }
863
- }
864
- const detail = inherits.length > 0 ? `is ${inherits.join(", ")}` : undefined;
865
-
866
- symbol = { name, type: "interface", startLine: sl, endLine: el, detail };
858
+ symbol = { name, type: "interface", startLine: sl, endLine: el };
867
859
 
868
860
  const body = child.childForFieldName("body");
869
861
  if (body && depth < MAX_DEPTH) {
@@ -914,17 +906,12 @@ function extractSolidityBody(
914
906
  if (!m) continue;
915
907
  if (m.type === "struct_member") {
916
908
  const memberName = fieldChildText(m, "name", source) || "";
917
- const memberType = m.childForFieldName("type");
918
- const typeStr = memberType
919
- ? source.slice(memberType.startIndex, memberType.endIndex)
920
- : "";
921
909
  const [msl, mel] = nodeRange(m);
922
910
  symbol.children.push({
923
911
  name: memberName,
924
912
  type: "field",
925
913
  startLine: msl,
926
914
  endLine: mel,
927
- detail: typeStr,
928
915
  });
929
916
  }
930
917
  }
@@ -953,22 +940,6 @@ function extractSolidityBody(
953
940
  return results;
954
941
  }
955
942
 
956
- /** Collect all unnamed `parameter`/`event_parameter`/`error_parameter` named children and return their source ranges joined. */
957
- function solidityParamsText(
958
- node: Node,
959
- source: string,
960
- paramType: string = "parameter",
961
- ): string | undefined {
962
- const parts: string[] = [];
963
- for (let j = 0; j < node.namedChildCount; j++) {
964
- const c = node.namedChild(j);
965
- if (c && c.type === paramType) {
966
- parts.push(source.slice(c.startIndex, c.endIndex));
967
- }
968
- }
969
- return parts.length > 0 ? `(${parts.join(", ")})` : undefined;
970
- }
971
-
972
943
  function extractSolidityItem(
973
944
  child: Node,
974
945
  source: string,
@@ -978,71 +949,18 @@ function extractSolidityItem(
978
949
  if (type === "function_definition") {
979
950
  const name = fieldChildText(child, "name", source) || "<anonymous>";
980
951
  const [sl, el] = nodeRange(child);
981
- const params = solidityParamsText(child, source, "parameter");
982
-
983
- // Check for return type
984
- let returnType: string | undefined;
985
- for (let j = 0; j < child.namedChildCount; j++) {
986
- const c = child.namedChild(j);
987
- if (c && c.type === "return_type_definition") {
988
- returnType = source.slice(c.startIndex, c.endIndex);
989
- break;
990
- }
991
- }
992
-
993
- // Collect qualifiers: visibility, state_mutability, virtual, override, modifiers
994
- const qualifiers: string[] = [];
995
- for (let j = 0; j < child.namedChildCount; j++) {
996
- const c = child.namedChild(j);
997
- if (!c) continue;
998
- if (c.type === "visibility" || c.type === "state_mutability") {
999
- qualifiers.push(source.slice(c.startIndex, c.endIndex));
1000
- } else if (c.type === "virtual") {
1001
- qualifiers.push("virtual");
1002
- } else if (c.type === "override_specifier") {
1003
- qualifiers.push(source.slice(c.startIndex, c.endIndex));
1004
- } else if (c.type === "modifier_invocation") {
1005
- qualifiers.push(source.slice(c.startIndex, c.endIndex));
1006
- }
1007
- }
1008
-
1009
- const parts: string[] = [];
1010
- if (params) parts.push(params);
1011
- if (qualifiers.length > 0) parts.push(qualifiers.join(" "));
1012
- if (returnType) parts.push(returnType);
1013
- const detail = parts.length > 0 ? parts.join(" ") : undefined;
1014
-
1015
- return { name, type: "function", startLine: sl, endLine: el, detail };
952
+ return { name, type: "function", startLine: sl, endLine: el };
1016
953
  }
1017
954
 
1018
955
  if (type === "constructor_definition") {
1019
956
  const [sl, el] = nodeRange(child);
1020
- const params = solidityParamsText(child, source, "parameter");
1021
-
1022
- const qualifiers: string[] = [];
1023
- for (let j = 0; j < child.namedChildCount; j++) {
1024
- const c = child.namedChild(j);
1025
- if (!c) continue;
1026
- if (c.type === "visibility" || c.type === "state_mutability") {
1027
- qualifiers.push(source.slice(c.startIndex, c.endIndex));
1028
- } else if (c.type === "modifier_invocation") {
1029
- qualifiers.push(source.slice(c.startIndex, c.endIndex));
1030
- }
1031
- }
1032
-
1033
- const parts: string[] = [];
1034
- if (params) parts.push(params);
1035
- if (qualifiers.length > 0) parts.push(qualifiers.join(" "));
1036
- const detail = parts.length > 0 ? parts.join(" ") : undefined;
1037
-
1038
- return { name: "constructor", type: "constructor", startLine: sl, endLine: el, detail };
957
+ return { name: "constructor", type: "constructor", startLine: sl, endLine: el };
1039
958
  }
1040
959
 
1041
960
  if (type === "modifier_definition") {
1042
961
  const name = fieldChildText(child, "name", source) || "<anonymous>";
1043
962
  const [sl, el] = nodeRange(child);
1044
- const detail = solidityParamsText(child, source, "parameter");
1045
- return { name, type: "modifier", startLine: sl, endLine: el, detail };
963
+ return { name, type: "modifier", startLine: sl, endLine: el };
1046
964
  }
1047
965
 
1048
966
  if (type === "event_definition") {
@@ -1054,21 +972,7 @@ function extractSolidityItem(
1054
972
  if (type === "error_declaration") {
1055
973
  const name = childText(child, "identifier", source) || "<anonymous>";
1056
974
  const [sl, el] = nodeRange(child);
1057
- // Collect error parameters for detail
1058
- const params: string[] = [];
1059
- for (let j = 0; j < child.namedChildCount; j++) {
1060
- const c = child.namedChild(j);
1061
- if (c && c.type === "error_parameter") {
1062
- const typeName = c.childForFieldName("type");
1063
- if (typeName) {
1064
- const typeText = source.slice(typeName.startIndex, typeName.endIndex);
1065
- const paramName = fieldChildText(c, "name", source);
1066
- params.push(paramName ? `${typeText} ${paramName}` : typeText);
1067
- }
1068
- }
1069
- }
1070
- const detail = params.length > 0 ? `(${params.join(", ")})` : undefined;
1071
- return { name, type: "error", startLine: sl, endLine: el, detail };
975
+ return { name, type: "error", startLine: sl, endLine: el };
1072
976
  }
1073
977
 
1074
978
  if (type === "struct_declaration") {
@@ -1096,36 +1000,19 @@ function extractSolidityItem(
1096
1000
  }
1097
1001
  }
1098
1002
 
1099
- const qualifiers: string[] = [];
1100
- for (let j = 0; j < child.namedChildCount; j++) {
1101
- const c = child.namedChild(j);
1102
- if (!c) continue;
1103
- if (c.type === "visibility" || c.type === "state_mutability") {
1104
- qualifiers.push(source.slice(c.startIndex, c.endIndex));
1105
- } else if (c.type === "virtual") {
1106
- qualifiers.push("virtual");
1107
- } else if (c.type === "override_specifier") {
1108
- qualifiers.push(source.slice(c.startIndex, c.endIndex));
1109
- } else if (c.type === "modifier_invocation") {
1110
- qualifiers.push(source.slice(c.startIndex, c.endIndex));
1111
- }
1112
- }
1113
1003
 
1114
1004
  return {
1115
1005
  name: symbolType,
1116
1006
  type: symbolType,
1117
1007
  startLine: sl,
1118
1008
  endLine: el,
1119
- detail: qualifiers.length > 0 ? qualifiers.join(" ") : undefined,
1120
1009
  };
1121
1010
  }
1122
1011
 
1123
1012
  if (type === "state_variable_declaration") {
1124
1013
  const name = fieldChildText(child, "name", source) || "<anonymous>";
1125
1014
  const [sl, el] = nodeRange(child);
1126
- const varType = child.childForFieldName("type");
1127
- const detail = varType ? source.slice(varType.startIndex, varType.endIndex) : undefined;
1128
- return { name, type: "variable", startLine: sl, endLine: el, detail };
1015
+ return { name, type: "variable", startLine: sl, endLine: el };
1129
1016
  }
1130
1017
 
1131
1018
  return null;
@@ -1521,86 +1408,72 @@ function extractSolidityImports(node: Node, source: string): ImportInfo[] {
1521
1408
  */
1522
1409
  function extractMarkdown(source: string): SymbolInfo[] {
1523
1410
  const lines = source.split("\n");
1411
+ // A trailing newline does not start another line.
1412
+ if (lines.length > 0 && lines[lines.length - 1] === "") lines.pop();
1524
1413
  const results: SymbolInfo[] = [];
1525
-
1414
+
1526
1415
  // Track heading hierarchy for nesting
1527
1416
  const stack: SymbolInfo[] = [];
1528
-
1529
- // First pass: extract headings and build hierarchy
1530
- for (let i = 0; i < lines.length; i++) {
1531
- const line = lines[i];
1532
- const headingMatch = line.match(/^(#{1,6})\s+(.+)$/);
1533
-
1534
- if (headingMatch) {
1535
- const level = headingMatch[1].length;
1536
- const title = headingMatch[2].trim();
1537
- const startLine = i + 1; // 1-indexed
1538
-
1539
- const symbol: SymbolInfo = {
1540
- name: title,
1541
- type: `heading${level}`,
1542
- startLine,
1543
- endLine: lines.length, // Will be updated later
1544
- children: [],
1545
- detail: `#`.repeat(level),
1546
- };
1547
-
1548
- // Find parent: pop stack until we find a heading with lower level
1549
- while (stack.length > 0) {
1550
- const parentLevel = parseInt(stack[stack.length - 1].type?.replace('heading', '') || '0', 10);
1551
- if (parentLevel >= level) {
1552
- stack.pop();
1553
- } else {
1554
- break;
1555
- }
1556
- }
1557
-
1558
- // Add to parent or root
1559
- if (stack.length > 0) {
1560
- stack[stack.length - 1].children!.push(symbol);
1561
- } else {
1562
- results.push(symbol);
1563
- }
1564
-
1565
- stack.push(symbol);
1417
+ const addHeading = (level: number, title: string, startLine: number): void => {
1418
+ const symbol: SymbolInfo = { name: title, type: `heading${level}`, startLine, endLine: lines.length, children: [] };
1419
+ while (stack.length > 0 && parseInt(stack[stack.length - 1].type?.replace("heading", "") || "0", 10) >= level) {
1420
+ stack.pop();
1566
1421
  }
1422
+ if (stack.length > 0) stack[stack.length - 1].children!.push(symbol);
1423
+ else results.push(symbol);
1424
+ stack.push(symbol);
1425
+ };
1426
+
1427
+ let fence: { char: string; length: number } | null = null;
1428
+ // Whether the previous line is paragraph text that a setext underline can turn into a heading.
1429
+ let paragraph = false;
1430
+ let i = 0;
1431
+
1432
+ if (lines[0]?.trim() === "---") {
1433
+ const close = lines.findIndex((line, index) => index > 0 && (line.trim() === "---" || line.trim() === "..."));
1434
+ const first = close === -1 ? undefined : lines.slice(1, close).find((line) => {
1435
+ const trimmed = line.trim();
1436
+ return trimmed.length > 0 && !trimmed.startsWith("#");
1437
+ });
1438
+ if (first !== undefined && /^[A-Za-z0-9_.-]+\s*:/.test(first.trim())) i = close + 1;
1567
1439
  }
1568
-
1569
- // Update endLine for each heading: it ends just before the next sibling or parent's next sibling
1570
- updateEndLines(results, lines.length);
1571
-
1572
- // Second pass: extract code blocks and nest them under appropriate headings
1573
- const codeBlockRegex = /^```(\w+)\s*$/gm; // Only match opening ``` with a language
1574
- let match;
1575
- while ((match = codeBlockRegex.exec(source)) !== null) {
1576
- const language = match[1];
1577
- const startLine = source.slice(0, match.index).split("\n").length;
1578
-
1579
- // Find closing ```
1580
- const afterMatch = source.slice(match.index + match[0].length);
1581
- const closingIdx = afterMatch.search(/^```\s*$/m);
1582
- let endLine = startLine + 1;
1583
- if (closingIdx !== -1) {
1584
- endLine = startLine + afterMatch.slice(0, closingIdx).split("\n").length + 1;
1440
+
1441
+ for (; i < lines.length; i++) {
1442
+ const line = lines[i];
1443
+
1444
+ if (fence) {
1445
+ const close = line.match(/^ {0,3}(`{3,}|~{3,})\s*$/);
1446
+ if (close && close[1][0] === fence.char && close[1].length >= fence.length) fence = null;
1447
+ continue;
1585
1448
  }
1586
-
1587
- const codeBlock: SymbolInfo = {
1588
- name: `code_block_${language}`,
1589
- type: "code_block",
1590
- startLine,
1591
- endLine,
1592
- detail: language,
1593
- };
1594
-
1595
- // Find the heading that contains this code block
1596
- const parent = findParentHeading(results, startLine);
1597
- if (parent) {
1598
- parent.children!.push(codeBlock);
1599
- } else {
1600
- results.push(codeBlock);
1449
+ const open = line.match(/^ {0,3}(`{3,}|~{3,})/);
1450
+ if (open) {
1451
+ fence = { char: open[1][0], length: open[1].length };
1452
+ paragraph = false;
1453
+ continue;
1454
+ }
1455
+
1456
+ const atx = line.match(/^ {0,3}(#{1,6})[ \t]+(.*?)(?:[ \t]+#+)?[ \t]*$/);
1457
+ if (atx && atx[2].length > 0) {
1458
+ addHeading(atx[1].length, atx[2].trim(), i + 1);
1459
+ paragraph = false;
1460
+ continue;
1461
+ }
1462
+
1463
+ const setext = paragraph ? line.match(/^ {0,3}(=+|-+)[ \t]*$/) : null;
1464
+ if (setext) {
1465
+ addHeading(setext[1][0] === "=" ? 1 : 2, lines[i - 1].trim(), i);
1466
+ paragraph = false;
1467
+ continue;
1601
1468
  }
1469
+
1470
+ // Blank lines, indented code, quotes, list items and thematic breaks are not setext paragraph text.
1471
+ paragraph = line.trim() !== "" && !/^( {4}|\t| {0,3}(>|[-*+][ \t]|\d+[.)][ \t]|[-*_]{3,}\s*$))/.test(line);
1602
1472
  }
1603
-
1473
+
1474
+ // Update endLine for each heading: it ends just before the next sibling or parent's next sibling
1475
+ updateEndLines(results, lines.length);
1476
+
1604
1477
  return results;
1605
1478
  }
1606
1479
 
@@ -1620,33 +1493,10 @@ function updateEndLines(symbols: SymbolInfo[], totalLines: number): void {
1620
1493
  // Recurse into children
1621
1494
  if (symbol.children && symbol.children.length > 0) {
1622
1495
  updateEndLines(symbol.children, symbol.endLine);
1623
- // The last child's endLine might extend to parent's endLine
1624
- const lastChild = symbol.children[symbol.children.length - 1];
1625
- if (lastChild.endLine < symbol.endLine) {
1626
- lastChild.endLine = symbol.endLine;
1627
- }
1628
1496
  }
1629
1497
  }
1630
1498
  }
1631
1499
 
1632
- /** Find the heading that contains the given line number. */
1633
- function findParentHeading(symbols: SymbolInfo[], line: number): SymbolInfo | null {
1634
- for (const symbol of symbols) {
1635
- if (symbol.type?.startsWith("heading")) {
1636
- // Check if line is within this heading's range
1637
- if (line >= symbol.startLine && line <= symbol.endLine) {
1638
- // Try to find a more specific child
1639
- if (symbol.children && symbol.children.length > 0) {
1640
- const child = findParentHeading(symbol.children, line);
1641
- if (child) return child;
1642
- }
1643
- return symbol;
1644
- }
1645
- }
1646
- }
1647
- return null;
1648
- }
1649
-
1650
1500
  /**
1651
1501
  * Extract links from Markdown content as "imports".
1652
1502
  * Links to local files are considered imports.