githubexplorer / app /services /resolver_service.py
Kareman's picture
initial commit: full implemntation of git explorer project
acc643d
Raw
History Blame Contribute Delete
2.74 kB
from copy import deepcopy
# TODO:
# If multiple symbols have the same name,
# keep all of them instead of overwriting.
class ResolverService:
def resolve(self, code_index: list[dict]) -> list[dict]:
resolved = deepcopy(code_index)
symbols = self._build_symbol_table(resolved)
for file in resolved:
import_table = self._build_import_table(file)
self._resolve_functions(
file["functions"],
symbols,
import_table,
)
for cls in file["classes"]:
self._resolve_functions(
cls["methods"],
symbols,
import_table,
)
return resolved
def _build_symbol_table(self, code_index):
table = {}
for file in code_index:
for function in file["functions"]:
table[
function["qualified_name"]
.split(".")[-1]
] = function["qualified_name"]
for cls in file["classes"]:
table[
cls["qualified_name"]
.split(".")[-1]
] = cls["qualified_name"]
for method in cls["methods"]:
table[
method["qualified_name"]
.split(".")[-1]
] = method["qualified_name"]
return table
def _resolve_functions(
self,
functions,
symbols,
imports,
):
for function in functions:
for call in function["calls"]:
raw = call["name"]
parts = raw.split(".")
first = parts[0]
# اول importهای فایل
if first in imports:
resolved = imports[first]
if len(parts) > 1:
resolved += "." + ".".join(parts[1:])
call["qualified_name"] = resolved
# بعد symbolهای پروژه
elif first in symbols:
resolved = symbols[first]
if len(parts) > 1:
resolved += "." + ".".join(parts[1:])
call["qualified_name"] = resolved
def _build_import_table(self, file):
table = {}
for imp in file["imports"]:
key = imp["alias"] or imp["name"]
if imp["module"]:
if imp["module"] == imp["name"]:
table[key] = imp["module"]
else:
table[key] = f'{imp["module"]}.{imp["name"]}'
return table