mirror of
https://gitlab.gnome.org/jwestman/blueprint-compiler.git
synced 2025-06-22 23:19:25 -04:00
Add a formatter
This commit is contained in:
parent
2faa9207de
commit
4fa64cdf33
12 changed files with 613 additions and 11 deletions
|
@ -22,11 +22,13 @@ import json
|
|||
import sys
|
||||
import traceback
|
||||
import typing as T
|
||||
from difflib import SequenceMatcher
|
||||
|
||||
from . import decompiler, parser, tokenizer, utils, xml_reader
|
||||
from .ast_utils import AstNode
|
||||
from .completions import complete
|
||||
from .errors import CompileError, MultipleErrors
|
||||
from .formatter import Formatter
|
||||
from .lsp_utils import *
|
||||
from .outputs.xml import XmlOutput
|
||||
from .tokenizer import Token
|
||||
|
@ -211,6 +213,7 @@ class LanguageServer:
|
|||
"hoverProvider": True,
|
||||
"documentSymbolProvider": True,
|
||||
"definitionProvider": True,
|
||||
"documentFormattingProvider": True,
|
||||
},
|
||||
"serverInfo": {
|
||||
"name": "Blueprint",
|
||||
|
@ -280,6 +283,38 @@ class LanguageServer:
|
|||
id, [completion.to_json(True) for completion in completions]
|
||||
)
|
||||
|
||||
@command("textDocument/formatting")
|
||||
def formatting(self, id, params):
|
||||
open_file = self._open_files[params["textDocument"]["uri"]]
|
||||
|
||||
if open_file.text is None:
|
||||
self._send_error(id, ErrorCode.RequestFailed, "Document is not open")
|
||||
return
|
||||
|
||||
try:
|
||||
formatted_blp = Format.format(
|
||||
open_file.text,
|
||||
params["options"]["tabSize"],
|
||||
params["options"]["insertSpaces"],
|
||||
)
|
||||
except PrintableError:
|
||||
self._send_error(id, ErrorCode.RequestFailed, "Could not format document")
|
||||
return
|
||||
|
||||
lst = []
|
||||
for tag, i1, i2, j1, j2 in SequenceMatcher(
|
||||
None, open_file.text, formatted_blp
|
||||
).get_opcodes():
|
||||
if tag in ("replace", "insert", "delete"):
|
||||
lst.append(
|
||||
TextEdit(
|
||||
Range(i1, i2, open_file.text),
|
||||
"" if tag == "delete" else formatted_blp[j1:j2],
|
||||
).to_json()
|
||||
)
|
||||
|
||||
self._send_response(id, lst)
|
||||
|
||||
@command("textDocument/x-blueprint-compile")
|
||||
def compile(self, id, params):
|
||||
open_file = self._open_files[params["textDocument"]["uri"]]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue