swift.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import type { Node as SyntaxNode } from 'web-tree-sitter';
  2. import { getNodeText, getChildByField } from '../tree-sitter-helpers';
  3. import type { LanguageExtractor } from '../tree-sitter-types';
  4. /**
  5. * A Swift function's declared return type, normalized to the bare class name a
  6. * chained `Foo.make().draw()` could be called on (the #645/#608 mechanism).
  7. * tree-sitter-swift labels BOTH the function name (`simple_identifier`) and the
  8. * return type (a `user_type`) with the field `name`, so `childForFieldName`
  9. * returns the name; the return type is found positionally — the first type node
  10. * after the `simple_identifier` name, before the body. Optionals (`Foo?`) are
  11. * unwrapped; arrays/tuples/function types and `Void` yield undefined.
  12. */
  13. function extractSwiftReturnType(node: SyntaxNode, source: string): string | undefined {
  14. let seenName = false;
  15. for (let i = 0; i < node.namedChildCount; i++) {
  16. const child = node.namedChild(i);
  17. if (!child) continue;
  18. if (child.type === 'simple_identifier' && !seenName) {
  19. seenName = true;
  20. continue;
  21. }
  22. if (!seenName) continue;
  23. if (child.type === 'function_body') return undefined; // body reached: no return type
  24. let typeNode: SyntaxNode | null = null;
  25. if (child.type === 'user_type') typeNode = child;
  26. else if (child.type === 'optional_type') {
  27. typeNode = child.namedChildren.find((c: SyntaxNode) => c.type === 'user_type') ?? null;
  28. }
  29. if (typeNode) {
  30. // Use the whole type node's text, strip generics, then take the LAST
  31. // dotted segment — a member type `KF.Builder` resolves to `Builder` (its
  32. // first type_identifier is the OUTER `KF`, which would be wrong).
  33. const name = getNodeText(typeNode, source).trim().replace(/<[^>]*>/g, '');
  34. const last = name.split('.').pop()?.trim();
  35. if (!last || !/^[A-Za-z_]\w*$/.test(last) || last === 'Void') return undefined;
  36. return last;
  37. }
  38. }
  39. return undefined;
  40. }
  41. export const swiftExtractor: LanguageExtractor = {
  42. functionTypes: ['function_declaration'],
  43. classTypes: ['class_declaration'],
  44. methodTypes: ['function_declaration'], // Methods are functions inside classes
  45. interfaceTypes: ['protocol_declaration'],
  46. structTypes: ['struct_declaration'],
  47. enumTypes: ['enum_declaration'],
  48. enumMemberTypes: ['enum_entry'],
  49. typeAliasTypes: ['typealias_declaration'],
  50. importTypes: ['import_declaration'],
  51. callTypes: ['call_expression'],
  52. variableTypes: ['property_declaration', 'constant_declaration'],
  53. nameField: 'name',
  54. bodyField: 'body',
  55. paramsField: 'parameter',
  56. returnField: 'return_type',
  57. getReturnType: extractSwiftReturnType,
  58. resolveName: (node, source) => {
  59. // A nested-type extension `extension KF.Builder { … }` parses as a
  60. // class_declaration whose `name` is a multi-segment `user_type` (`KF.Builder`
  61. // = type_identifiers `KF`, `Builder`). Name the node by the LAST segment
  62. // (`Builder`) so it shares the simple name of the extended type's own
  63. // declaration (`struct Builder` → `KF::Builder`) instead of becoming a
  64. // distinct `KF.Builder` node. Without this, the extension's conformances and
  65. // members are invisible to a chained call on the type — supertype lookup and
  66. // method matching both key off the simple name (#750). Simple names (regular
  67. // class/struct/enum, or `extension Plain`) fall through to default extraction.
  68. if (node.type !== 'class_declaration') return undefined;
  69. const nameNode = getChildByField(node, 'name');
  70. if (!nameNode || nameNode.type !== 'user_type') return undefined;
  71. const ids = nameNode.namedChildren.filter((c: SyntaxNode) => c.type === 'type_identifier');
  72. return ids.length > 1 ? getNodeText(ids[ids.length - 1]!, source) : undefined;
  73. },
  74. getSignature: (node, source) => {
  75. // Swift function signature: func name(params) -> ReturnType
  76. const params = getChildByField(node, 'parameter');
  77. const returnType = getChildByField(node, 'return_type');
  78. if (!params) return undefined;
  79. let sig = getNodeText(params, source);
  80. if (returnType) {
  81. sig += ' -> ' + getNodeText(returnType, source);
  82. }
  83. return sig;
  84. },
  85. getVisibility: (node) => {
  86. // Check for visibility modifiers in Swift
  87. for (let i = 0; i < node.childCount; i++) {
  88. const child = node.child(i);
  89. if (child?.type === 'modifiers') {
  90. const text = child.text;
  91. if (text.includes('public')) return 'public';
  92. if (text.includes('private')) return 'private';
  93. if (text.includes('internal')) return 'internal';
  94. if (text.includes('fileprivate')) return 'private';
  95. }
  96. }
  97. return 'internal'; // Swift defaults to internal
  98. },
  99. isStatic: (node) => {
  100. for (let i = 0; i < node.childCount; i++) {
  101. const child = node.child(i);
  102. if (child?.type === 'modifiers') {
  103. if (child.text.includes('static') || child.text.includes('class')) {
  104. return true;
  105. }
  106. }
  107. }
  108. return false;
  109. },
  110. classifyClassNode: (node) => {
  111. // Swift uses class_declaration for classes, structs, and enums
  112. for (let i = 0; i < node.childCount; i++) {
  113. const child = node.child(i);
  114. if (child?.type === 'struct') return 'struct';
  115. if (child?.type === 'enum') return 'enum';
  116. }
  117. return 'class';
  118. },
  119. isAsync: (node) => {
  120. for (let i = 0; i < node.childCount; i++) {
  121. const child = node.child(i);
  122. if (child?.type === 'modifiers' && child.text.includes('async')) {
  123. return true;
  124. }
  125. }
  126. return false;
  127. },
  128. extractImport: (node, source) => {
  129. const importText = source.substring(node.startIndex, node.endIndex).trim();
  130. const identifier = node.namedChildren.find((c: SyntaxNode) => c.type === 'identifier');
  131. if (identifier) {
  132. return { moduleName: source.substring(identifier.startIndex, identifier.endIndex), signature: importText };
  133. }
  134. return null;
  135. },
  136. };