Skip to content

Commit

Permalink
Merge pull request uniconproject#468 from mstreeter10/ulsp
Browse files Browse the repository at this point in the history
ulsp: add run mode (server -s and client -c)
  • Loading branch information
Jafaral authored Jun 1, 2024
2 parents 43da610 + 80f04b4 commit ec9480a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
39 changes: 20 additions & 19 deletions uni/ulsp/launch-lsp.icn
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,37 @@ procedure usage()
write("Check your IDE for the correct LSP server invocation.")
write("\nOptions:")
write("\t --socket <PORT> : 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
16 changes: 12 additions & 4 deletions uni/ulsp/server.icn
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -282,22 +283,29 @@ 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")
delay(1000)
}
}

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()
Expand Down

0 comments on commit ec9480a

Please sign in to comment.