|
| 1 | +from pathlib import Path |
| 2 | +from ...entities.entity import Entity |
| 3 | +from ...entities.file import File |
| 4 | +from typing import Optional |
| 5 | +from ..analyzer import AbstractAnalyzer |
| 6 | + |
| 7 | +from multilspy import SyncLanguageServer |
| 8 | + |
| 9 | +import tree_sitter_kotlin as tskotlin |
| 10 | +from tree_sitter import Language, Node |
| 11 | + |
| 12 | +import logging |
| 13 | +logger = logging.getLogger('code_graph') |
| 14 | + |
| 15 | +class KotlinAnalyzer(AbstractAnalyzer): |
| 16 | + def __init__(self) -> None: |
| 17 | + super().__init__(Language(tskotlin.language())) |
| 18 | + |
| 19 | + def add_dependencies(self, path: Path, files: list[Path]): |
| 20 | + # For now, we skip dependency resolution for Kotlin |
| 21 | + # In the future, this could parse build.gradle or pom.xml for Kotlin projects |
| 22 | + pass |
| 23 | + |
| 24 | + def get_entity_label(self, node: Node) -> str: |
| 25 | + if node.type == 'class_declaration': |
| 26 | + # Check if it's an interface by looking for interface keyword |
| 27 | + for child in node.children: |
| 28 | + if child.type == 'interface': |
| 29 | + return "Interface" |
| 30 | + return "Class" |
| 31 | + elif node.type == 'object_declaration': |
| 32 | + return "Object" |
| 33 | + elif node.type == 'function_declaration': |
| 34 | + # Check if this is a method (inside a class) or a top-level function |
| 35 | + parent = node.parent |
| 36 | + if parent and parent.type == 'class_body': |
| 37 | + return "Method" |
| 38 | + return "Function" |
| 39 | + raise ValueError(f"Unknown entity type: {node.type}") |
| 40 | + |
| 41 | + def get_entity_name(self, node: Node) -> str: |
| 42 | + if node.type in ['class_declaration', 'object_declaration', 'function_declaration']: |
| 43 | + for child in node.children: |
| 44 | + if child.type == 'identifier': |
| 45 | + return child.text.decode('utf-8') |
| 46 | + raise ValueError(f"Cannot extract name from entity type: {node.type}") |
| 47 | + |
| 48 | + def get_entity_docstring(self, node: Node) -> Optional[str]: |
| 49 | + if node.type in ['class_declaration', 'object_declaration', 'function_declaration']: |
| 50 | + # Check for KDoc comment (/** ... */) before the node |
| 51 | + if node.prev_sibling and node.prev_sibling.type == "multiline_comment": |
| 52 | + comment_text = node.prev_sibling.text.decode('utf-8') |
| 53 | + # Only return if it's a KDoc comment (starts with /**) |
| 54 | + if comment_text.startswith('/**'): |
| 55 | + return comment_text |
| 56 | + return None |
| 57 | + raise ValueError(f"Unknown entity type: {node.type}") |
| 58 | + |
| 59 | + def get_entity_types(self) -> list[str]: |
| 60 | + return ['class_declaration', 'object_declaration', 'function_declaration'] |
| 61 | + |
| 62 | + def _get_delegation_types(self, entity: Entity) -> list[tuple]: |
| 63 | + """Extract type identifiers from delegation specifiers in order. |
| 64 | + |
| 65 | + Returns list of (node, is_constructor_invocation) tuples. |
| 66 | + constructor_invocation indicates a superclass; plain user_type indicates an interface. |
| 67 | + """ |
| 68 | + types = [] |
| 69 | + for child in entity.node.children: |
| 70 | + if child.type == 'delegation_specifiers': |
| 71 | + for spec in child.children: |
| 72 | + if spec.type == 'delegation_specifier': |
| 73 | + for sub in spec.children: |
| 74 | + if sub.type == 'constructor_invocation': |
| 75 | + for s in sub.children: |
| 76 | + if s.type == 'user_type': |
| 77 | + for id_node in s.children: |
| 78 | + if id_node.type == 'identifier': |
| 79 | + types.append((id_node, True)) |
| 80 | + elif sub.type == 'user_type': |
| 81 | + for id_node in sub.children: |
| 82 | + if id_node.type == 'identifier': |
| 83 | + types.append((id_node, False)) |
| 84 | + return types |
| 85 | + |
| 86 | + def add_symbols(self, entity: Entity) -> None: |
| 87 | + if entity.node.type == 'class_declaration': |
| 88 | + types = self._get_delegation_types(entity) |
| 89 | + for node, is_class in types: |
| 90 | + if is_class: |
| 91 | + entity.add_symbol("base_class", node) |
| 92 | + else: |
| 93 | + entity.add_symbol("implement_interface", node) |
| 94 | + |
| 95 | + elif entity.node.type == 'object_declaration': |
| 96 | + types = self._get_delegation_types(entity) |
| 97 | + for node, _ in types: |
| 98 | + entity.add_symbol("implement_interface", node) |
| 99 | + |
| 100 | + elif entity.node.type == 'function_declaration': |
| 101 | + # Find function calls |
| 102 | + captures = self._captures("(call_expression) @reference.call", entity.node) |
| 103 | + if 'reference.call' in captures: |
| 104 | + for caller in captures['reference.call']: |
| 105 | + entity.add_symbol("call", caller) |
| 106 | + |
| 107 | + # Find parameters with types |
| 108 | + captures = self._captures("(parameter (user_type (identifier) @parameter))", entity.node) |
| 109 | + if 'parameter' in captures: |
| 110 | + for parameter in captures['parameter']: |
| 111 | + entity.add_symbol("parameters", parameter) |
| 112 | + |
| 113 | + # Find return type |
| 114 | + captures = self._captures("(function_declaration (user_type (identifier) @return_type))", entity.node) |
| 115 | + if 'return_type' in captures: |
| 116 | + for return_type in captures['return_type']: |
| 117 | + entity.add_symbol("return_type", return_type) |
| 118 | + |
| 119 | + def is_dependency(self, file_path: str) -> bool: |
| 120 | + # Check if file is in a dependency directory (e.g., build, .gradle cache) |
| 121 | + return "build/" in file_path or ".gradle/" in file_path or "/cache/" in file_path |
| 122 | + |
| 123 | + def resolve_path(self, file_path: str, path: Path) -> str: |
| 124 | + # For Kotlin, just return the file path as-is for now |
| 125 | + return file_path |
| 126 | + |
| 127 | + def resolve_type(self, files: dict[Path, File], lsp: SyncLanguageServer, file_path: Path, path: Path, node: Node) -> list[Entity]: |
| 128 | + res = [] |
| 129 | + for file, resolved_node in self.resolve(files, lsp, file_path, path, node): |
| 130 | + type_dec = self.find_parent(resolved_node, ['class_declaration', 'object_declaration']) |
| 131 | + if type_dec in file.entities: |
| 132 | + res.append(file.entities[type_dec]) |
| 133 | + return res |
| 134 | + |
| 135 | + def resolve_method(self, files: dict[Path, File], lsp: SyncLanguageServer, file_path: Path, path: Path, node: Node) -> list[Entity]: |
| 136 | + res = [] |
| 137 | + # For call expressions, we need to extract the function name |
| 138 | + if node.type == 'call_expression': |
| 139 | + # Find the identifier being called |
| 140 | + for child in node.children: |
| 141 | + if child.type in ['identifier', 'navigation_expression']: |
| 142 | + for file, resolved_node in self.resolve(files, lsp, file_path, path, child): |
| 143 | + method_dec = self.find_parent(resolved_node, ['function_declaration', 'class_declaration', 'object_declaration']) |
| 144 | + if method_dec and method_dec.type in ['class_declaration', 'object_declaration']: |
| 145 | + continue |
| 146 | + if method_dec in file.entities: |
| 147 | + res.append(file.entities[method_dec]) |
| 148 | + break |
| 149 | + return res |
| 150 | + |
| 151 | + def resolve_symbol(self, files: dict[Path, File], lsp: SyncLanguageServer, file_path: Path, path: Path, key: str, symbol: Node) -> list[Entity]: |
| 152 | + if key in ["implement_interface", "base_class", "parameters", "return_type"]: |
| 153 | + return self.resolve_type(files, lsp, file_path, path, symbol) |
| 154 | + elif key in ["call"]: |
| 155 | + return self.resolve_method(files, lsp, file_path, path, symbol) |
| 156 | + else: |
| 157 | + raise ValueError(f"Unknown key {key}") |
0 commit comments