- 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
58 lines
1.2 KiB
Python
58 lines
1.2 KiB
Python
# Python imports
|
|
import os
|
|
import ctypes
|
|
|
|
# Lib imports
|
|
|
|
# Application imports
|
|
from .libs.tree_sitter import Language, Parser
|
|
|
|
|
|
|
|
_LIB_PATH = os.path.join(
|
|
os.path.dirname(__file__), "languages", "languages.so"
|
|
)
|
|
_LANGUAGE_LIB = ctypes.CDLL(_LIB_PATH)
|
|
|
|
|
|
|
|
def load_language(name: str) -> Language | None:
|
|
symbol = f"tree_sitter_{name}"
|
|
|
|
try:
|
|
func = getattr(_LANGUAGE_LIB, symbol)
|
|
except AttributeError:
|
|
logger.warning(f"Tree-sitter: {name} not found in 'languages.so' shared library...")
|
|
return None
|
|
|
|
func.restype = ctypes.c_void_p
|
|
return Language(func())
|
|
|
|
|
|
LANGUAGES = {
|
|
"python": load_language("python"),
|
|
"python3": load_language("python"),
|
|
"javascript": load_language("javascript"),
|
|
"html": load_language("html"),
|
|
"css": load_language("css"),
|
|
"json": load_language("json"),
|
|
"java": load_language("java"),
|
|
"c": load_language("c"),
|
|
"cpp": load_language("cpp"),
|
|
"go": load_language("go")
|
|
}
|
|
|
|
|
|
def get_parser(lang_name: str) -> Parser | None:
|
|
if not lang_name in LANGUAGES: return
|
|
|
|
language = LANGUAGES[lang_name]
|
|
|
|
if not language: return
|
|
|
|
parser = Parser()
|
|
parser.language = language
|
|
|
|
return parser
|
|
|