Skip to content

Commit ec9480a

Browse files
authored
Merge pull request uniconproject#468 from mstreeter10/ulsp
ulsp: add run mode (server -s and client -c)
2 parents 43da610 + 80f04b4 commit ec9480a

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

uni/ulsp/launch-lsp.icn

+20-19
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,37 @@ procedure usage()
2121
write("Check your IDE for the correct LSP server invocation.")
2222
write("\nOptions:")
2323
write("\t --socket <PORT> : set the lsp server port")
24+
write("\t -s : run as a server (default)")
25+
write("\t -c : run as a client")
2426
write("\t -h : show this help\n")
2527
exit(-1)
2628
end
2729

2830
procedure validate_args(args)
29-
local opts, port
30-
opts := options(args, "--socket:")
31+
local opts
32+
opts := options(args, "--socket+ -h! -c! -s!", usage)
3133
if *opts = 0 then usage()
32-
port := \opts["-socket"] | usage()
33-
port := opts["-socket"]
34-
return port
34+
member(opts, "-socket") | usage()
35+
return opts
3536
end
3637

37-
3838
procedure main(args)
39-
local port
40-
#write("args: ", ximage(args))
41-
port := validate_args(args) | stop("Error: invalid args/port number.")
39+
local opts, sock, mode
40+
41+
# If validate_args() fails, it will display usage() and never return.
42+
opts := validate_args(args)
4243

4344
# Allow passing full host:port as an arg
44-
if integer(port) then {
45-
if &features == ("MacOS" | "MS Windows NT") then
46-
port := "127.0.0.1:" || port
47-
else
48-
port := ":" || port
45+
sock := opts["-socket"]
46+
if &features == ("MacOS" | "MS Windows NT") then {
47+
sock := "127.0.0.1:" || sock
48+
}
49+
else {
50+
sock := ":" || sock
4951
}
5052

51-
Server(port).run()
52-
end
53-
54-
55-
53+
# Set mode for server to run in, based on opts.
54+
member(opts, mode := "c") | (mode := "s")
5655

56+
Server(sock, mode).run()
57+
end

uni/ulsp/server.icn

+12-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import json
2525

2626
class Server(
2727
port, # Port number acquired from args on start up of server
28+
mode, # Mode to run in (client | server)
2829
sock, # Socket connection for communication with client
2930
openFiles, # File container for open files
3031
completionHandler, # Instance of CompletionHandler class for handling completion requests
@@ -282,22 +283,29 @@ class Server(
282283
end
283284

284285
initially
286+
local openopt := "n", mstring := "server"
285287

286-
every 1 to 10 do {
287-
if sock := open(port, "n", 1000) then
288+
case mode of {
289+
"s" : { openopt ||:= "a" }
290+
"c" : { mstring := "client" }
291+
default: { write("Unknown mode: " || mode) }
292+
}
293+
294+
write("Attempting to start ulsp as a " || mstring)
295+
every 1 to 5 do {
296+
if sock := open(port, openopt) then
288297
break
289298
else {
290299
write("open(",port,") ERROR: ", \&errortext | "Unknown")
291300
delay(1000)
292301
}
293302
}
294303

295-
if /sock then stop("failed to connect to ",port)
304+
if /sock then stop("Failed to establish connection on ",port)
296305

297306
openFiles := table()
298307
lsp_database := LSPDB()
299308
lsp_database.build()
300-
301309
completionHandler := CompletionHandler()
302310
signatureHandler := SignatureHandler()
303311
hoverHandler := HoverHandler()

0 commit comments

Comments
 (0)