csharp.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import type { Node as SyntaxNode } from 'web-tree-sitter';
  2. import { getNodeText } from '../tree-sitter-helpers';
  3. import type { LanguageExtractor } from '../tree-sitter-types';
  4. export const csharpExtractor: LanguageExtractor = {
  5. functionTypes: [],
  6. classTypes: ['class_declaration'],
  7. methodTypes: ['method_declaration', 'constructor_declaration'],
  8. interfaceTypes: ['interface_declaration'],
  9. structTypes: ['struct_declaration'],
  10. enumTypes: ['enum_declaration'],
  11. enumMemberTypes: ['enum_member_declaration'],
  12. typeAliasTypes: [],
  13. importTypes: ['using_directive'],
  14. callTypes: ['invocation_expression'],
  15. variableTypes: ['local_declaration_statement'],
  16. fieldTypes: ['field_declaration'],
  17. nameField: 'name',
  18. bodyField: 'body',
  19. paramsField: 'parameter_list',
  20. getVisibility: (node) => {
  21. for (let i = 0; i < node.childCount; i++) {
  22. const child = node.child(i);
  23. if (child?.type === 'modifier') {
  24. const text = child.text;
  25. if (text === 'public') return 'public';
  26. if (text === 'private') return 'private';
  27. if (text === 'protected') return 'protected';
  28. if (text === 'internal') return 'internal';
  29. }
  30. }
  31. return 'private'; // C# defaults to private
  32. },
  33. isStatic: (node) => {
  34. for (let i = 0; i < node.childCount; i++) {
  35. const child = node.child(i);
  36. if (child?.type === 'modifier' && child.text === 'static') {
  37. return true;
  38. }
  39. }
  40. return false;
  41. },
  42. isAsync: (node) => {
  43. for (let i = 0; i < node.childCount; i++) {
  44. const child = node.child(i);
  45. if (child?.type === 'modifier' && child.text === 'async') {
  46. return true;
  47. }
  48. }
  49. return false;
  50. },
  51. extractImport: (node, source) => {
  52. const importText = source.substring(node.startIndex, node.endIndex).trim();
  53. // C# using directives: using System, using System.Collections.Generic, using static X, using Alias = X
  54. const qualifiedName = node.namedChildren.find((c: SyntaxNode) => c.type === 'qualified_name');
  55. if (qualifiedName) {
  56. return { moduleName: getNodeText(qualifiedName, source), signature: importText };
  57. }
  58. // Simple namespace like "using System;" - get the first identifier
  59. const identifier = node.namedChildren.find((c: SyntaxNode) => c.type === 'identifier');
  60. if (identifier) {
  61. return { moduleName: getNodeText(identifier, source), signature: importText };
  62. }
  63. return null;
  64. },
  65. };