|
| 1 | +""" |
| 2 | +Copyright (c) 2026, Jairus Martin. |
| 3 | +
|
| 4 | +Distributed under the terms of the GPL v3 License. |
| 5 | +
|
| 6 | +The full license is in the file LICENSE, distributed with this software. |
| 7 | +""" |
| 8 | + |
| 9 | +import ast as python_ast |
| 10 | +import asyncio |
| 11 | +import os |
| 12 | +from ast import parse as python_parse |
| 13 | +from typing import Optional |
| 14 | + |
| 15 | +from atom.api import Atom, Enum, Int, List, Str, Typed |
| 16 | +from enaml.core import enaml_ast |
| 17 | +from enaml.core.parser import parse as enaml_parse |
| 18 | + |
| 19 | +from declaracad.core.protocol import JsonRpcProtocol |
| 20 | +from declaracad.core.utils import log |
| 21 | + |
| 22 | + |
| 23 | +class Outline(Atom): |
| 24 | + lineno = Int() |
| 25 | + label = Str() |
| 26 | + type = Enum("function", "class", "enamldef", "error") |
| 27 | + |
| 28 | + |
| 29 | +class Problem(Atom): |
| 30 | + lineno = Int() |
| 31 | + offset = Int() |
| 32 | + end_lineno = Int() |
| 33 | + end_offset = Int() |
| 34 | + msg = Str() |
| 35 | + type = Str() |
| 36 | + level = Enum("error", "warning", "info", "hint") |
| 37 | + |
| 38 | + @classmethod |
| 39 | + def from_syntaxerror(cls, e: SyntaxError) -> "Problem": |
| 40 | + return cls( |
| 41 | + lineno=e.lineno, |
| 42 | + offset=e.offset, |
| 43 | + end_offset=e.end_offset, |
| 44 | + end_lineno=e.end_lineno, |
| 45 | + msg=e.msg, |
| 46 | + level="error", |
| 47 | + type=e.__class__.__name__, |
| 48 | + ) |
| 49 | + |
| 50 | + |
| 51 | +class ParseResult(Atom): |
| 52 | + problems = List(Problem) |
| 53 | + outline = List(Outline) |
| 54 | + |
| 55 | + |
| 56 | +def parse_python(filename: str, source: str) -> ParseResult: |
| 57 | + result = ParseResult() |
| 58 | + try: |
| 59 | + ast = python_parse(source, filename) |
| 60 | + outline = [] |
| 61 | + # Walk ast and pull out nodes we're insterested in |
| 62 | + for node in ast.body: |
| 63 | + if isinstance(node, python_ast.ClassDef): |
| 64 | + outline.append( |
| 65 | + Outline(lineno=node.lineno, type="class", label=node.name) |
| 66 | + ) |
| 67 | + elif isinstance(node, python_ast.FunctionDef): |
| 68 | + outline.append( |
| 69 | + Outline(lineno=node.lineno, type="function", label=node.name) |
| 70 | + ) |
| 71 | + result.outline = outline |
| 72 | + except SyntaxError as e: |
| 73 | + log.debug(e) |
| 74 | + result.problems = [Problem.from_syntaxerror(e)] |
| 75 | + result.outline = [Outline(lineno=e.lineno, label=f"{e}", type="error")] |
| 76 | + return result |
| 77 | + |
| 78 | + |
| 79 | +def parse_enaml(filename: str, source: str) -> ParseResult: |
| 80 | + result = ParseResult() |
| 81 | + try: |
| 82 | + ast = enaml_parse(source, filename) |
| 83 | + outline = [] |
| 84 | + # Walk ast and pull out nodes we're insterested in |
| 85 | + for node in ast.body: |
| 86 | + if isinstance(node, enaml_ast.EnamlDef): |
| 87 | + outline.append( |
| 88 | + Outline(lineno=node.lineno, type="enamldef", label=node.typename) |
| 89 | + ) |
| 90 | + elif isinstance(node, enaml_ast.PythonModule): |
| 91 | + for n in node.ast.body: |
| 92 | + if isinstance(n, python_ast.ClassDef): |
| 93 | + outline.append( |
| 94 | + Outline(lineno=n.lineno, type="class", label=n.name) |
| 95 | + ) |
| 96 | + elif isinstance(n, python_ast.FunctionDef): |
| 97 | + outline.append( |
| 98 | + Outline(lineno=n.lineno, type="function", label=n.name) |
| 99 | + ) |
| 100 | + result.outline = outline |
| 101 | + except SyntaxError as e: |
| 102 | + log.debug(e) |
| 103 | + result.problems = [Problem.from_syntaxerror(e)] |
| 104 | + result.outline = [Outline(lineno=e.lineno, label=f"{e}", type="error")] |
| 105 | + return result |
| 106 | + |
| 107 | + |
| 108 | +def parse_gcode(filename: str, source: str) -> ParseResult: |
| 109 | + from declaracad.cnc import gcode |
| 110 | + |
| 111 | + return [gcode.parse(source)] |
| 112 | + |
| 113 | + |
| 114 | +PARSERS = { |
| 115 | + "enaml": parse_enaml, |
| 116 | + "python": parse_python, |
| 117 | +} |
| 118 | + |
| 119 | + |
| 120 | +class LangServerProtocol(JsonRpcProtocol): |
| 121 | + """A very simple langserver to provide background auto-complete, |
| 122 | + linting, and parsing services for the editors. |
| 123 | + """ |
| 124 | + |
| 125 | + finished = Typed(asyncio.Event, ()) |
| 126 | + |
| 127 | + def connection_made(self, transport): |
| 128 | + self.transport = transport |
| 129 | + self.connected = True |
| 130 | + log.debug("Connected to workbench") |
| 131 | + |
| 132 | + def connection_lost(self, exc): |
| 133 | + self.connected = False |
| 134 | + self.finished.set() |
| 135 | + |
| 136 | + def on_parse(self, filename: str, source: str) -> Optional[ParseResult]: |
| 137 | + """Parse a file and return any errors""" |
| 138 | + log.debug(f"Parsing {filename}") |
| 139 | + try: |
| 140 | + if not filename: |
| 141 | + return |
| 142 | + ext = os.path.splitext(filename)[-1].lstrip(".") |
| 143 | + if ext in PARSERS: |
| 144 | + return PARSERS[ext](filename, source) |
| 145 | + except Exception as e: |
| 146 | + log.exception(e) |
| 147 | + return None |
| 148 | + |
| 149 | + |
| 150 | +async def main(port: int): |
| 151 | + try: |
| 152 | + log.debug("Running langserver") |
| 153 | + loop = asyncio.get_event_loop() |
| 154 | + transport, protocol = await loop.create_connection( |
| 155 | + lambda: LangServerProtocol(), "127.0.0.1", port |
| 156 | + ) |
| 157 | + log.debug("Remote langserver connected!") |
| 158 | + await protocol.finished.wait() |
| 159 | + log.debug("Remote langserver finished!") |
| 160 | + except Exception as e: |
| 161 | + log.error(f"Could not connect to workbench: {e}") |
| 162 | + return |
0 commit comments