Skip to content

Commit 5d3612f

Browse files
committed
refactor: streamline import handling and enhance language query support
1 parent 032d150 commit 5d3612f

File tree

10 files changed

+113
-90
lines changed

10 files changed

+113
-90
lines changed

scubatrace/cpp/file.py

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,9 @@
11
from functools import cached_property
22

33
from ..file import File
4-
from . import language
54

65

76
class CFile(File):
8-
@cached_property
9-
def imports(self) -> list[File]:
10-
include_node = self.parser.query_all(
11-
self.text, language.C.query_include
12-
)
13-
import_files = []
14-
for node in include_node:
15-
include_path_node = node.child_by_field_name("path")
16-
if include_path_node is None:
17-
continue
18-
include = self.lsp.request_definition(
19-
self.relpath,
20-
include_path_node.start_point[0],
21-
include_path_node.start_point[1],
22-
)
23-
if len(include) == 0:
24-
continue
25-
include = include[0]
26-
include_abspath = include["absolutePath"]
27-
if include_abspath not in self.project.files_abspath:
28-
continue
29-
import_files.append(self.project.files_abspath[include_abspath])
30-
return import_files
31-
327
@cached_property
338
def source_header(self) -> File | None:
349
"""

scubatrace/cpp/language.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,22 @@ class C(Language):
99
tslanguage = TSLanguage(tscpp.language())
1010

1111
query_function = "(function_definition)@name"
12-
query_identifier = "(identifier)@name"
1312
query_return = "(return_statement)@name"
1413
query_call = "(call_expression)@name"
14+
query_import_identifier = """
15+
(preproc_include
16+
path: [
17+
(system_lib_string)@name
18+
(string_literal)@name
19+
]
20+
)
21+
"""
22+
1523
query_struct = "(struct_specifier)@name"
1624
query_class = "(class_specifier)@name"
17-
query_method = "(function_definition)@name"
1825
query_field = "(field_declaration)@name"
1926
query_include = "(preproc_include)@name"
27+
2028
query_global_statement = (
2129
"(declaration)@name"
2230
"(struct_specifier)@name"

scubatrace/file.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,26 @@ def imports(self) -> list[File]:
192192
For example, in Python, this would include files imported using the `import` statement.
193193
In C/C++, this would include files included using the `#include` directive.
194194
"""
195-
...
195+
import_identifier_node = self.parser.query_all(
196+
self.text, self.language.query_import_identifier
197+
)
198+
import_files = []
199+
for node in import_identifier_node:
200+
include = self.lsp.request_definition(
201+
self.relpath,
202+
node.start_point[0],
203+
node.start_point[1],
204+
)
205+
if len(include) == 0:
206+
continue
207+
include = include[0]
208+
include_abspath = include["absolutePath"]
209+
if include_abspath in self.project.files_abspath:
210+
import_files.append(self.project.files_abspath[include_abspath])
211+
else:
212+
# If the file is not in the project, we still add it to the imports
213+
import_files.append(File.File(include_abspath, self.project))
214+
return import_files
196215

197216
@cached_property
198217
def functions(self) -> list[Function]:

scubatrace/java/file.py

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from functools import cached_property
2-
31
from ..file import File
42
from . import language
53

@@ -12,34 +10,3 @@ def package(self) -> str:
1210
return ""
1311
package = package_node.text.decode() # type: ignore
1412
return package
15-
16-
@cached_property
17-
def imports(self) -> list[File]:
18-
import_name_node = self.parser.query_all(
19-
self.text, language.JAVA.query_import_name
20-
)
21-
import_files = []
22-
for node in import_name_node:
23-
include = self.lsp.request_definition(
24-
self.relpath,
25-
node.start_point[0],
26-
node.start_point[1],
27-
)
28-
if len(include) == 0:
29-
continue
30-
include = include[0]
31-
include_abspath = include["absolutePath"]
32-
if include_abspath not in self.project.files_abspath:
33-
continue
34-
import_files.append(self.project.files_abspath[include_abspath])
35-
return import_files
36-
37-
@cached_property
38-
def import_class(self) -> list[str]:
39-
import_node = self.parser.query_all(self.text, language.JAVA.query_import)
40-
imports = []
41-
for node in import_node:
42-
assert node.text is not None
43-
scoped_identifier = node.text.decode()
44-
imports.append(scoped_identifier)
45-
return imports

scubatrace/java/language.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,23 @@
77
class JAVA(Language):
88
extensions = ["java"]
99
tslanguage = TSLanguage(tsjava.language())
10-
query_import = "(import_declaration(scoped_identifier)@name)"
10+
11+
query_function = query_method = "(method_declaration)@name"
12+
query_return = "(return_statement)@name"
13+
query_call = "(method_invocation)@name"
14+
query_import_identifier = """
15+
(import_declaration
16+
(scoped_identifier
17+
name: (identifier)@name
18+
)
19+
)
20+
(import_declaration
21+
(identifier)@name
22+
)
23+
"""
24+
1125
query_package = "(package_declaration)@name"
1226
query_class = "(class_declaration)@name"
13-
query_function = query_method = "(method_declaration)@name"
14-
query_identifier = "(identifier)@name"
1527

1628
jump_statements = [
1729
"break_statement",
@@ -50,18 +62,12 @@ class JAVA(Language):
5062
"switch_expression",
5163
]
5264

53-
loop_statements = ["for_statement", "while_statement", "do_statement", "enhanced_for_statement"]
54-
55-
query_import_name = """
56-
(import_declaration
57-
(scoped_identifier
58-
name: (identifier)@name
59-
)
60-
)
61-
(import_declaration
62-
(identifier)@name
63-
)
64-
"""
65+
loop_statements = [
66+
"for_statement",
67+
"while_statement",
68+
"do_statement",
69+
"enhanced_for_statement",
70+
]
6571

6672
@staticmethod
6773
def query_left_value(text):

scubatrace/javascript/language.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,29 @@ class JAVASCRIPT(Language):
88
extensions = ["js"]
99
tslanguage = TSLanguage(tsjavascript.language())
1010

11-
query_function = "(function_declaration)@name"
12-
query_method = "(method_definition)@name"
13-
query_identifier = "(identifier)@name"
14-
query_import = "(import_statement)@name"
15-
query_export = "(export_statement)@name"
11+
query_function = """
12+
(function_declaration)@name
13+
(method_definition)@name
14+
"""
15+
query_return = "(return_statement)@name"
1616
query_call = "(call_expression)@name"
17+
query_import = "(import_statement)@name"
18+
query_import_identifier = """
19+
(call_expression
20+
function: [
21+
(identifier)@require
22+
(import)
23+
]
24+
arguments: (arguments
25+
(string)@name
26+
)
27+
(#eq? @require "require")
28+
)
29+
(import_statement
30+
source: (string)@name
31+
)
32+
"""
33+
1734
query_class = "(class_declaration)@name"
1835

1936
jump_statements = [

scubatrace/language.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,33 @@ class Language:
2424
This is used to identify syntax errors in the code.
2525
"""
2626

27-
query_function = "(function_definition)@name"
27+
query_identifier = "(identifier)@name"
2828
"""
29-
The tree-sitter query to match function definitions.
29+
The tree-sitter query to match identifiers.
3030
"""
3131

32-
query_identifier = "(identifier)@name"
32+
query_function: str
3333
"""
34-
The tree-sitter query to match identifiers.
34+
The tree-sitter query to match function definitions.
3535
"""
3636

37-
query_return = "(return_statement)@name"
37+
query_return: str
3838
"""
3939
The tree-sitter query to match return statements.
4040
"""
4141

42-
query_call = "(call_expression)@name"
42+
query_call: str
4343
"""
4444
The tree-sitter query to match function calls.
4545
"""
4646

47+
query_import_identifier: str
48+
"""
49+
The tree-sitter query to match import identifiers.
50+
51+
For example, in C/C++, this would match the `header.h` in `#include <header.h>`.
52+
"""
53+
4754
jump_statements: list[str]
4855
"""
4956
The tree-sitter AST types of jump statements.

scubatrace/python/language.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,28 @@ class PYTHON(Language):
99
tslanguage = TSLanguage(tspython.language())
1010

1111
query_function = "(function_definition)@name"
12-
query_identifier = "(identifier)@name"
12+
query_return = "(return_statement)@name"
13+
query_call = "(call)@name"
14+
query_import_identifier = """
15+
(import_statement
16+
name: [
17+
(dotted_name)@name
18+
(aliased_import
19+
name: (dotted_name)@name
20+
)
21+
]
22+
)
23+
(import_from_statement
24+
module_name: [
25+
(dotted_name)@name
26+
(relative_import
27+
(dotted_name)@name
28+
)
29+
]
30+
)
31+
"""
32+
1333
query_class = "(class_definition)@name"
14-
query_import = "(import_statement)@name"
1534

1635
jump_statements = [
1736
"break_statement",

tests/c/test.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "include/sub.h"
2+
#include <stdio.h>
23
int add(int a, int b)
34
{
45
return a + sub(a, b);
@@ -32,6 +33,7 @@ int main(int argc, char** argv)
3233
a += i;
3334
break;
3435
}
36+
printf("a: %d, b: %d, count: %d\n", a, b, count);
3537
return 0;
3638
}
3739

tests/javascript/test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import "./admin.js";
22
import { admin } from "./admin.js";
33
var fs = require("fs");
4-
4+
const fs = require("fs");
5+
const path = require("path");
6+
const module = await import('./admin.js');
7+
const { default: defaultExport } = await import('./admin.js');
58
admin.name = "Pete";
69

710
class Runoob {

0 commit comments

Comments
 (0)