initial commit
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
from logging import INFO, basicConfig, getLogger
|
||||
from pathlib import Path
|
||||
from urllib.parse import unquote
|
||||
from lsprotocol.types import (
|
||||
TEXT_DOCUMENT_DOCUMENT_SYMBOL,
|
||||
DocumentSymbol,
|
||||
DocumentSymbolParams,
|
||||
Position,
|
||||
Range,
|
||||
SymbolKind,
|
||||
)
|
||||
|
||||
from pygls.server import LanguageServer
|
||||
from parsimonious import Grammar
|
||||
from pygls.uris import urlparse
|
||||
|
||||
# from skillls.parsing.location import Range
|
||||
|
||||
from .parsing.tokenize import Locator, SkillVisitor
|
||||
|
||||
|
||||
example = """
|
||||
(skillist siomfpwqmqwepfomkjnbkjb
|
||||
'(rawlist token)
|
||||
clist(qwerfwf)
|
||||
)
|
||||
"""
|
||||
|
||||
cache = {}
|
||||
|
||||
|
||||
def parse(path: Path):
|
||||
# path = Path(__file__).parent / "grammar.peg"
|
||||
grammar = Grammar(path.read_text())
|
||||
|
||||
locator = Locator(example)
|
||||
tree = grammar.parse(example)
|
||||
|
||||
iv = SkillVisitor(locator)
|
||||
output = iv.visit(tree)
|
||||
|
||||
return output
|
||||
|
||||
|
||||
def parse_and_cache(uri: str) -> list[DocumentSymbol]:
|
||||
path = Path(unquote(urlparse(uri).path))
|
||||
if not path.exists():
|
||||
logger.error("could not find %s", path)
|
||||
return []
|
||||
|
||||
if not cache.get(path):
|
||||
logger.info("%s not yet cached, parsing...")
|
||||
out = parse(path)
|
||||
logger.info("%s", out)
|
||||
|
||||
return []
|
||||
|
||||
|
||||
basicConfig(filename="skillls.log", level=INFO)
|
||||
|
||||
logger = getLogger(__name__)
|
||||
server = LanguageServer("skillls", "v0.1")
|
||||
|
||||
|
||||
@server.feature(TEXT_DOCUMENT_DOCUMENT_SYMBOL)
|
||||
def document_symbols(params: DocumentSymbolParams) -> list[DocumentSymbol]:
|
||||
logger.info("requested document symbols for %s", params.text_document.uri)
|
||||
doc = server.workspace.documents[params.text_document.uri]
|
||||
return [
|
||||
DocumentSymbol(
|
||||
"~global_scope",
|
||||
kind=SymbolKind.Namespace,
|
||||
range=Range(
|
||||
start=Position(0, 0),
|
||||
end=Position(len(doc.lines) - 1, len(doc.lines[-1])),
|
||||
),
|
||||
selection_range=Range(Position(0, 0), Position(0, 0)),
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
def main():
|
||||
server.start_io()
|
||||
Reference in New Issue
Block a user