import os, osproc, net
echo "xxx".len
proc start_nim_suggest =
var p: Process = nil
echo("start")
p = startProcess("nimsuggest", "/home/stefan/Nim/compiler",
["--port:6000", "/home/stefan/sugtest/test.nim"],
options = {poStdErrToStdOut, poUseShell})
sleep(1000)
var line = ""
var socket = newSocket()
socket.connect("localhost", 6000.Port)
socket.send("sug /home/stefan/sugtest/test.nim:3:12" & "\c\l")
while true:
socket.readLine(line)
if line.len == 0: break
echo(line)
socket.close()
if not p.running:
echo("Process not running")
else:
echo("Process shutting down")
p.terminate()
discard p.waitForExit()
p.close()
start_nim_suggest()
echo("done")
Some times it seems to work, I get many lines of output starting with
$ ./test 3 start sug skProc system.GC_ref proc (string){.gcsafe, locks: 0.} /home/stefan/Nim/lib/system.nim 2125 7 "marks the object `x` as referenced, so that it will not be freed until\x0A it is unmarked via `GC_unref`. If called n-times for the same object `x`,\x0A n calls to `GC_unref` are needed to unmark `x`." sug skProc system.GC_unref proc
Unfortunately often I get this error messages:
$ ./test 3 start Traceback (most recent call last) test.nim(29) test test.nim(14) start_nim_suggest net.nim(527) connect os.nim(277) raiseOSError Error: unhandled exception: Connection refused [OSError]
My feeling is that I may not stop the process in a clean way or do not close the socket connection properly? But I have never done something with processes or sockets before, so I have no idea.
And the other question: What am I supposed to do with the nimsuggest output -- it fills a whole terminal window. The test is related to the string "xxx" above, so it seems to give all info related to strings. But what would one offer to a user of a text editor? For "xxx".l I would assume that I am offered "len" and other methods starting with letter l by nimsuggest only.
I think there is really no documentation for nimsuggest currently, or did I miss something? The code above is obviously based on latest Aporia code.
Waiting 1 second before connecting might not be enough, and yes, nimsuggest might not be able to bind the port sometimes (it will crash with an "Address already in use" error).
Aporia uses two separate threads to handle nimsuggest because it needs to handle the process and the socket connection.
As for parsing, Aporia has a function which does the parsing.
Thanks. I noticed already myself that my observation seem to be a common problem and found SO_REUSEADDR option mentioned in http://stackoverflow.com/questions/13864247/socket-not-released-after-service-restart -- but I have not experimented with that myself yet. It is related to the fact that sockets may continue to exist even when it is closed and the related process is terminated. Indeed my early tests, starting nimsuggest in shell window and then making multiple socket connections worked without problems. And after waiting about one minute before restating the process worked always fine.
I still wonder why nimsuggest is not doing the filtering of suggestions itself. So for each editor or IDE we have to code filtering again. Maybe the idea is that it is more flexible this way.