java.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. export const javaExtractor: LanguageExtractor = {
  5. functionTypes: [],
  6. classTypes: ['class_declaration'],
  7. methodTypes: ['method_declaration', 'constructor_declaration'],
  8. interfaceTypes: ['interface_declaration'],
  9. structTypes: [],
  10. enumTypes: ['enum_declaration'],
  11. enumMemberTypes: ['enum_constant'],
  12. typeAliasTypes: [],
  13. importTypes: ['import_declaration'],
  14. callTypes: ['method_invocation'],
  15. variableTypes: ['local_variable_declaration'],
  16. fieldTypes: ['field_declaration'],
  17. nameField: 'name',
  18. bodyField: 'body',
  19. paramsField: 'parameters',
  20. returnField: 'type',
  21. getSignature: (node, source) => {
  22. const params = getChildByField(node, 'parameters');
  23. const returnType = getChildByField(node, 'type');
  24. if (!params) return undefined;
  25. const paramsText = getNodeText(params, source);
  26. return returnType ? getNodeText(returnType, source) + ' ' + paramsText : paramsText;
  27. },
  28. getVisibility: (node) => {
  29. for (let i = 0; i < node.childCount; i++) {
  30. const child = node.child(i);
  31. if (child?.type === 'modifiers') {
  32. const text = child.text;
  33. if (text.includes('public')) return 'public';
  34. if (text.includes('private')) return 'private';
  35. if (text.includes('protected')) return 'protected';
  36. }
  37. }
  38. return undefined;
  39. },
  40. isStatic: (node) => {
  41. for (let i = 0; i < node.childCount; i++) {
  42. const child = node.child(i);
  43. if (child?.type === 'modifiers' && child.text.includes('static')) {
  44. return true;
  45. }
  46. }
  47. return false;
  48. },
  49. extractImport: (node, source) => {
  50. const importText = source.substring(node.startIndex, node.endIndex).trim();
  51. const scopedId = node.namedChildren.find((c: SyntaxNode) => c.type === 'scoped_identifier');
  52. if (scopedId) {
  53. const moduleName = source.substring(scopedId.startIndex, scopedId.endIndex);
  54. return { moduleName, signature: importText };
  55. }
  56. return null;
  57. },
  58. };