-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1 parent
ed01c8e
commit 4ba33c7
Showing
7 changed files
with
103 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import tree_sitter | ||
from doc_comments_ai.treesitter.treesitter import ( | ||
Treesitter, | ||
TreesitterNode, | ||
get_source_from_node, | ||
) | ||
from doc_comments_ai.constants import Language | ||
from doc_comments_ai.treesitter.treesitter_registry import TreesitterRegistry | ||
|
||
|
||
class TreesitterKotlin(Treesitter): | ||
def __init__(self): | ||
super().__init__(Language.KOTLIN) | ||
|
||
def parse(self, file_bytes: bytes) -> list[TreesitterNode]: | ||
super().parse(file_bytes) | ||
print(self.tree.root_node.sexp()) | ||
result = [] | ||
methods = self._query_all_methods(self.tree.root_node) | ||
for method in methods: | ||
method_name = self._query_method_name(method["method"]) | ||
doc_comment = method["doc_comment"] | ||
result.append(TreesitterNode(method_name, doc_comment, method["method"])) | ||
return result | ||
|
||
def _query_method_name(self, node: tree_sitter.Node): | ||
if node.type == "function_declaration": | ||
for child in node.children: | ||
if child.type == "simple_identifier": | ||
return child.text.decode() | ||
return None | ||
|
||
def _query_all_methods(self, node: tree_sitter.Node): | ||
methods = [] | ||
if node.type == "function_declaration": | ||
doc_comment_node = None | ||
if node.prev_named_sibling and node.prev_named_sibling.type == "comment": | ||
doc_comment_node = get_source_from_node(node.prev_named_sibling) | ||
methods.append({"method": node, "doc_comment": doc_comment_node}) | ||
else: | ||
for child in node.children: | ||
methods.extend(self._query_all_methods(child)) | ||
return methods | ||
|
||
|
||
# Register the TreesitterJava class in the registry | ||
TreesitterRegistry.register_treesitter(Language.KOTLIN, TreesitterKotlin) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "doc-comments-ai" | ||
version = "0.1.4" | ||
version = "0.1.5" | ||
description = "" | ||
authors = ["fynnfluegge <[email protected]>"] | ||
readme = "README.md" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import pytest | ||
|
||
|
||
@pytest.fixture | ||
def kotlin_code_fixture(): | ||
return """ | ||
/** | ||
* Gets the corresponding programming language based on the given file extension. | ||
* | ||
* @param fileExtension The file extension of the programming file. | ||
* @return The corresponding programming language if it exists in the mapping, otherwise Language.UNKNOWN. | ||
*/ | ||
fun getProgrammingLanguage(fileExtension: String): Language { | ||
val languageMapping = HashMap<String, Language>() | ||
languageMapping[".py"] = Language.PYTHON | ||
languageMapping[".js"] = Language.JAVASCRIPT | ||
languageMapping[".ts"] = Language.TYPESCRIPT | ||
languageMapping[".java"] = Language.JAVA | ||
languageMapping[".kt"] = Language.KOTLIN | ||
languageMapping[".lua"] = Language.LUA | ||
return languageMapping.getOrDefault(fileExtension, Language.UNKNOWN) | ||
} | ||
fun getFileExtension(fileName: String): String { | ||
return fileName.substring(fileName.lastIndexOf('.')) | ||
} | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters