csharp.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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', 'field_declaration'],
  16. nameField: 'name',
  17. bodyField: 'body',
  18. paramsField: 'parameter_list',
  19. getVisibility: (node) => {
  20. for (let i = 0; i < node.childCount; i++) {
  21. const child = node.child(i);
  22. if (child?.type === 'modifier') {
  23. const text = child.text;
  24. if (text === 'public') return 'public';
  25. if (text === 'private') return 'private';
  26. if (text === 'protected') return 'protected';
  27. if (text === 'internal') return 'internal';
  28. }
  29. }
  30. return 'private'; // C# defaults to private
  31. },
  32. isStatic: (node) => {
  33. for (let i = 0; i < node.childCount; i++) {
  34. const child = node.child(i);
  35. if (child?.type === 'modifier' && child.text === 'static') {
  36. return true;
  37. }
  38. }
  39. return false;
  40. },
  41. isAsync: (node) => {
  42. for (let i = 0; i < node.childCount; i++) {
  43. const child = node.child(i);
  44. if (child?.type === 'modifier' && child.text === 'async') {
  45. return true;
  46. }
  47. }
  48. return false;
  49. },
  50. extractImport: (node, source) => {
  51. const importText = source.substring(node.startIndex, node.endIndex).trim();
  52. // C# using directives: using System, using System.Collections.Generic, using static X, using Alias = X
  53. const qualifiedName = node.namedChildren.find((c: SyntaxNode) => c.type === 'qualified_name');
  54. if (qualifiedName) {
  55. return { moduleName: getNodeText(qualifiedName, source), signature: importText };
  56. }
  57. // Simple namespace like "using System;" - get the first identifier
  58. const identifier = node.namedChildren.find((c: SyntaxNode) => c.type === 'identifier');
  59. if (identifier) {
  60. return { moduleName: getNodeText(identifier, source), signature: importText };
  61. }
  62. return null;
  63. },
  64. };