From 01950b18e40d723408cfa415dce8d63cdea11437 Mon Sep 17 00:00:00 2001 From: Maximus Streeter Date: Fri, 31 May 2024 16:21:45 -0400 Subject: [PATCH] ulsp: add run mode (server -s and client -c) Signed-off-by: Maximus Streeter --- uni/ulsp/launch-lsp.icn | 39 ++++++++++++++++++++------------------- uni/ulsp/server.icn | 16 ++++++++++++---- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/uni/ulsp/launch-lsp.icn b/uni/ulsp/launch-lsp.icn index 5870adc84..171be8464 100644 --- a/uni/ulsp/launch-lsp.icn +++ b/uni/ulsp/launch-lsp.icn @@ -21,36 +21,37 @@ procedure usage() write("Check your IDE for the correct LSP server invocation.") write("\nOptions:") write("\t --socket : set the lsp server port") + write("\t -s : run as a server (default)") + write("\t -c : run as a client") write("\t -h : show this help\n") exit(-1) end procedure validate_args(args) - local opts, port - opts := options(args, "--socket:") + local opts + opts := options(args, "--socket+ -h! -c! -s!", usage) if *opts = 0 then usage() - port := \opts["-socket"] | usage() - port := opts["-socket"] - return port + member(opts, "-socket") | usage() + return opts end - procedure main(args) - local port - #write("args: ", ximage(args)) - port := validate_args(args) | stop("Error: invalid args/port number.") + local opts, sock, mode + + # If validate_args() fails, it will display usage() and never return. + opts := validate_args(args) # Allow passing full host:port as an arg - if integer(port) then { - if &features == ("MacOS" | "MS Windows NT") then - port := "127.0.0.1:" || port - else - port := ":" || port + sock := opts["-socket"] + if &features == ("MacOS" | "MS Windows NT") then { + sock := "127.0.0.1:" || sock + } + else { + sock := ":" || sock } - Server(port).run() -end - - - + # Set mode for server to run in, based on opts. + member(opts, mode := "c") | (mode := "s") + Server(sock, mode).run() +end diff --git a/uni/ulsp/server.icn b/uni/ulsp/server.icn index 9782a761c..ddfd1d8a4 100644 --- a/uni/ulsp/server.icn +++ b/uni/ulsp/server.icn @@ -25,6 +25,7 @@ import json class Server( port, # Port number acquired from args on start up of server + mode, # Mode to run in (client | server) sock, # Socket connection for communication with client openFiles, # File container for open files completionHandler, # Instance of CompletionHandler class for handling completion requests @@ -282,9 +283,17 @@ class Server( end initially + local openopt := "n", mstring := "server" - every 1 to 10 do { - if sock := open(port, "n", 1000) then + case mode of { + "s" : { openopt ||:= "a" } + "c" : { mstring := "client" } + default: { write("Unknown mode: " || mode) } + } + + write("Attempting to start ulsp as a " || mstring) + every 1 to 5 do { + if sock := open(port, openopt) then break else { write("open(",port,") ERROR: ", \&errortext | "Unknown") @@ -292,12 +301,11 @@ class Server( } } - if /sock then stop("failed to connect to ",port) + if /sock then stop("Failed to establish connection on ",port) openFiles := table() lsp_database := LSPDB() lsp_database.build() - completionHandler := CompletionHandler() signatureHandler := SignatureHandler() hoverHandler := HoverHandler()