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