Files
Newton-Editor/plugins/code/event-watchers/tree_sitter/plugin.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

64 lines
1.6 KiB
Python

# Python imports
# Lib imports
# Application imports
from libs.event_factory import Event_Factory, Code_Event_Types
from plugins.plugin_types import PluginCode
from .tree_sitter import Parser, get_parser
class Plugin(PluginCode):
def __init__(self):
super(Plugin, self).__init__()
def set_ast(self, file):
if not hasattr(file, "tree_sitter"):
parser = get_parser( file.ftype )
if not parser: return
file.tree_sitter = parser
buffer = file.buffer
start_itr, \
end_itr = buffer.get_bounds()
text = buffer.get_text(start_itr, end_itr, True)
tree = file.tree_sitter.parse( text.encode("UTF-8") )
file.ast = tree
def _controller_message(self, event: Code_Event_Types.CodeEvent):
if isinstance(event, Code_Event_Types.FocusedViewEvent):
self.view = event.view
event = Event_Factory.create_event(
"get_file", buffer = self.view.get_buffer()
)
self.emit_to("files", event)
file = event.response
if not file: return
if file.ftype == "buffer": return
self.set_ast(file)
elif isinstance(event, Code_Event_Types.TextChangedEvent):
self.set_ast(event.file)
# root = tree.root_node
# print("Root type:", root.type)
# for child in root.children:
# print(child.type, child.start_point, child.end_point)
def load(self):
...
def unload(self):
...
def run(self):
...