Files
Newton-Editor/plugins/code/ui/code_fold/folding_engine.py
itdominator 62a866d9bb feat(tree-sitter, views): initialize AST on focus and emit source view creation event
- Add set_ast helper to centralize Tree-sitter parsing logic
- Parse and attach AST on FocusedViewEvent and TextChangedEvent
- Request file from buffer on view focus before parsing
- Fix parser guard condition in get_parser (handle missing language properly)

- Emit CreatedSourceViewEvent when a new source view is added
- Register CreatedSourceViewEvent in DTO exports

- Update TODO:
  - Remove completed collapsible code blocks task
  - Add fix note for code block icon desync issue

chore:
- Add scaffolding for code_fold UI plugin
- Add created_source_view_event DTO
2026-03-29 03:09:43 -05:00

32 lines
721 B
Python

# Python imports
# Lib imports
# Application imports
from .fold_types import FOLD_NODES
def get_folding_ranges(lang_name: str, ast):
root = ast.root_node
fold_types = FOLD_NODES.get(lang_name, set())
ranges = []
def visit(node):
if node.type in fold_types:
start_line = node.start_point[0]
end_line = node.end_point[0]
if end_line > start_line:
ranges.append({
"start_line": start_line,
"end_line": end_line,
"id": (start_line, end_line, node.type),
})
for child in node.children:
visit(child)
visit(root)
return ranges