mirror of
https://github.com/catlog22/Claude-Code-Workflow.git
synced 2026-02-28 09:23:08 +08:00
Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -324,6 +324,15 @@ class AstGrepPythonProcessor(BaseAstGrepProcessor):
|
||||
for node in from_matches:
|
||||
module = self._get_match(node, "MODULE")
|
||||
names = self._get_match(node, "NAMES")
|
||||
# Prefer parsing from full node text to handle multiple imports
|
||||
# (ast-grep-py capture may only include the first name).
|
||||
try:
|
||||
node_text = self._binding._get_node_text(node) if self._binding else ""
|
||||
except Exception:
|
||||
node_text = ""
|
||||
parsed_names = self._extract_import_names_from_text(node_text) if node_text else ""
|
||||
if parsed_names:
|
||||
names = parsed_names
|
||||
start_line, end_line = self._get_line_range(node)
|
||||
if module:
|
||||
all_matches.append((start_line, end_line, "from_import", f"{module}:{names}", node))
|
||||
@@ -478,29 +487,46 @@ class AstGrepPythonProcessor(BaseAstGrepProcessor):
|
||||
module = parts[0]
|
||||
names = parts[1] if len(parts) > 1 else ""
|
||||
|
||||
# Record the import relationship
|
||||
relationships.append(CodeRelationship(
|
||||
source_symbol=get_current_scope(),
|
||||
target_symbol=module,
|
||||
relationship_type=RelationshipType.IMPORTS,
|
||||
source_file=source_file,
|
||||
target_file=None,
|
||||
source_line=start_line,
|
||||
))
|
||||
names = (names or "").strip()
|
||||
if names.startswith("(") and names.endswith(")"):
|
||||
names = names[1:-1].strip()
|
||||
|
||||
# Add aliases for imported names
|
||||
# Record IMPORTS edges for the imported names (module.symbol), and
|
||||
# update aliases for call/usage resolution.
|
||||
if names and names != "*":
|
||||
for name in names.split(","):
|
||||
name = name.strip()
|
||||
# Handle "name as alias" syntax
|
||||
if not name or name == "*":
|
||||
continue
|
||||
|
||||
if " as " in name:
|
||||
as_parts = name.split(" as ")
|
||||
as_parts = name.split(" as ", 1)
|
||||
original = as_parts[0].strip()
|
||||
alias = as_parts[1].strip()
|
||||
if not original:
|
||||
continue
|
||||
target = f"{module}.{original}" if module else original
|
||||
if alias:
|
||||
update_aliases({alias: f"{module}.{original}"})
|
||||
elif name:
|
||||
update_aliases({name: f"{module}.{name}"})
|
||||
update_aliases({alias: target})
|
||||
relationships.append(CodeRelationship(
|
||||
source_symbol=get_current_scope(),
|
||||
target_symbol=target,
|
||||
relationship_type=RelationshipType.IMPORTS,
|
||||
source_file=source_file,
|
||||
target_file=None,
|
||||
source_line=start_line,
|
||||
))
|
||||
else:
|
||||
target = f"{module}.{name}" if module else name
|
||||
update_aliases({name: target})
|
||||
relationships.append(CodeRelationship(
|
||||
source_symbol=get_current_scope(),
|
||||
target_symbol=target,
|
||||
relationship_type=RelationshipType.IMPORTS,
|
||||
source_file=source_file,
|
||||
target_file=None,
|
||||
source_line=start_line,
|
||||
))
|
||||
|
||||
elif match_type == "call":
|
||||
# Resolve alias for call target
|
||||
|
||||
@@ -595,7 +595,7 @@ class TreeSitterSymbolParser:
|
||||
targets: List[str] = []
|
||||
|
||||
if node.type == "import_statement":
|
||||
for child in node.children:
|
||||
for i, child in enumerate(node.children):
|
||||
if child.type == "aliased_import":
|
||||
name_node = child.child_by_field_name("name")
|
||||
alias_node = child.child_by_field_name("alias")
|
||||
@@ -632,7 +632,7 @@ class TreeSitterSymbolParser:
|
||||
if module_node is not None:
|
||||
module_name = self._node_text(source_bytes, module_node).strip()
|
||||
|
||||
for child in node.children:
|
||||
for i, child in enumerate(node.children):
|
||||
if child.type == "aliased_import":
|
||||
name_node = child.child_by_field_name("name")
|
||||
alias_node = child.child_by_field_name("alias")
|
||||
@@ -650,7 +650,18 @@ class TreeSitterSymbolParser:
|
||||
if bound_name:
|
||||
aliases[bound_name] = target
|
||||
targets.append(target)
|
||||
elif child.type == "identifier":
|
||||
elif child.type == "dotted_name" and node.field_name_for_child(i) == "name":
|
||||
# tree-sitter-python represents `from X import A, B, C` as
|
||||
# multiple dotted_name nodes (field: "name").
|
||||
imported_name = self._node_text(source_bytes, child).strip()
|
||||
if not imported_name:
|
||||
continue
|
||||
target = (
|
||||
f"{module_name}.{imported_name}" if module_name else imported_name
|
||||
)
|
||||
aliases[imported_name] = target
|
||||
targets.append(target)
|
||||
elif child.type == "identifier" and node.field_name_for_child(i) == "name":
|
||||
imported_name = self._node_text(source_bytes, child).strip()
|
||||
if not imported_name or imported_name in {"from", "import", "*"}:
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user