lsp: Log to stderr rather than a file

This commit is contained in:
James Westman 2022-10-15 11:26:18 -05:00
parent 447785ec8c
commit b3783b9c6a
No known key found for this signature in database
GPG key ID: CE2DBA0ADB654EA6
2 changed files with 9 additions and 13 deletions

View file

@ -27,6 +27,10 @@ from .lsp_utils import *
from . import tokenizer, parser, utils, xml_reader from . import tokenizer, parser, utils, xml_reader
def printerr(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
def command(json_method): def command(json_method):
def decorator(func): def decorator(func):
func._json_method = json_method func._json_method = json_method
@ -89,10 +93,9 @@ class OpenFile:
class LanguageServer: class LanguageServer:
commands: T.Dict[str, T.Callable] = {} commands: T.Dict[str, T.Callable] = {}
def __init__(self, logfile=None): def __init__(self):
self.client_capabilities = {} self.client_capabilities = {}
self._open_files: {str: OpenFile} = {} self._open_files: {str: OpenFile} = {}
self.logfile = logfile
def run(self): def run(self):
# Read <doc> tags from gir files. During normal compilation these are # Read <doc> tags from gir files. During normal compilation these are
@ -110,7 +113,7 @@ class LanguageServer:
if line.startswith("Content-Length:"): if line.startswith("Content-Length:"):
content_len = int(line.split("Content-Length:")[1].strip()) content_len = int(line.split("Content-Length:")[1].strip())
line = sys.stdin.buffer.read(content_len).decode() line = sys.stdin.buffer.read(content_len).decode()
self._log("input: " + line) printerr("input: " + line)
data = json.loads(line) data = json.loads(line)
method = data.get("method") method = data.get("method")
@ -120,22 +123,16 @@ class LanguageServer:
if method in self.commands: if method in self.commands:
self.commands[method](self, id, params) self.commands[method](self, id, params)
except Exception as e: except Exception as e:
self._log(traceback.format_exc()) printerr(traceback.format_exc())
def _send(self, data): def _send(self, data):
data["jsonrpc"] = "2.0" data["jsonrpc"] = "2.0"
line = json.dumps(data, separators=(",", ":")) + "\r\n" line = json.dumps(data, separators=(",", ":")) + "\r\n"
self._log("output: " + line) printerr("output: " + line)
sys.stdout.write(f"Content-Length: {len(line.encode())}\r\nContent-Type: application/vscode-jsonrpc; charset=utf-8\r\n\r\n{line}") sys.stdout.write(f"Content-Length: {len(line.encode())}\r\nContent-Type: application/vscode-jsonrpc; charset=utf-8\r\n\r\n{line}")
sys.stdout.flush() sys.stdout.flush()
def _log(self, msg):
if self.logfile is not None:
self.logfile.write(str(msg))
self.logfile.write("\n")
self.logfile.flush()
def _send_response(self, id, result): def _send_response(self, id, result):
self._send({ self._send({
"id": id, "id": id,

View file

@ -48,7 +48,6 @@ class BlueprintApp:
port = self.add_subcommand("port", "Interactive porting tool", self.cmd_port) port = self.add_subcommand("port", "Interactive porting tool", self.cmd_port)
lsp = self.add_subcommand("lsp", "Run the language server (for internal use by IDEs)", self.cmd_lsp) lsp = self.add_subcommand("lsp", "Run the language server (for internal use by IDEs)", self.cmd_lsp)
lsp.add_argument("--logfile", dest="logfile", default=None, type=argparse.FileType('a'))
self.add_subcommand("help", "Show this message", self.cmd_help) self.add_subcommand("help", "Show this message", self.cmd_help)
@ -125,7 +124,7 @@ class BlueprintApp:
def cmd_lsp(self, opts): def cmd_lsp(self, opts):
langserv = LanguageServer(opts.logfile) langserv = LanguageServer()
langserv.run() langserv.run()