11import std/ [algorithm, hashes, os, osproc, sets,
2- <<<<<<< HEAD
32 streams, strformat, strutils, tables]
4- ====== =
5- streams, strformat, strutils, tables, uri]
6- >>>>>>> 19 a04042ad (tool: LSP support)
73
84import nimlsppkg/ [baseprotocol, logger, suggestlib, utfmapping]
95include nimlsppkg/ [messages, messageenums]
106
117
128const
13- <<<<<<< HEAD
14- # This is used to explicitly set the default source path
15- explicitSourcePath {.strdefine .} = getCurrentCompilerExe ().parentDir.parentDir
16-
17- var nimpath = explicitSourcePath
18- ====== =
19- version = block :
20- var version = " 0.0.0"
21- let nimbleFile = staticRead (currentSourcePath ().parentDir / " nimlsp.nimble" )
22- for line in nimbleFile.splitLines:
23- let keyval = line.split ('=' )
24- if keyval.len == 2 :
25- if keyval[0 ].strip == " version" :
26- version = keyval[1 ].strip (chars = Whitespace + {'"' })
27- break
28- version
299 # This is used to explicitly set the default source path
3010 explicitSourcePath {.strdefine .} = getCurrentCompilerExe ().parentDir.parentDir
3111
32- type
33- UriParseError * = object of Defect
34- uri: string
35-
3612var nimpath = explicitSourcePath
37-
38- infoLog (" Version: " , version)
39- infoLog (" explicitSourcePath: " , explicitSourcePath)
40- for i in 1 .. paramCount ():
41- infoLog (" Argument " , i, " : " , paramStr (i))
42-
43- >>>>>>> 19 a04042ad (tool: LSP support)
4413var
4514 gotShutdown = false
4615 initialized = false
@@ -84,58 +53,6 @@ template textDocumentNotification(message: typed; kind: typed; name, body: untyp
8453 else :
8554 debugLog (" Unable to parse data as " , kind)
8655
87- <<<<<<< HEAD
88- ====== =
89- proc pathToUri (path: string ): string =
90- # This is a modified copy of encodeUrl in the uri module. This doesn't encode
91- # the / character, meaning a full file path can be passed in without breaking
92- # it.
93- result = newStringOfCap (path.len + path.len shr 2 ) # assume 12% non-alnum-chars
94- when defined (windows):
95- result .add '/'
96- for c in path:
97- case c
98- # https://tools.ietf.org/html/rfc3986#section-2.3
99- of 'a' .. 'z' , 'A' .. 'Z' , '0' .. '9' , '-' , '.' , '_' , '~' , '/' : result .add c
100- of '\\ ' :
101- when defined (windows):
102- result .add '/'
103- else :
104- result .add '%'
105- result .add toHex (ord (c), 2 )
106- else :
107- result .add '%'
108- result .add toHex (ord (c), 2 )
109-
110- proc uriToPath (uri: string ): string =
111- # # Convert an RFC 8089 file URI to a native, platform-specific, absolute path.
112- # let startIdx = when defined(windows): 8 else: 7
113- # normalizedPath(uri[startIdx..^1])
114- let parsed = uri.parseUri
115- if parsed.scheme != " file" :
116- var e = newException (UriParseError , & " Invalid scheme: { parsed.scheme} , only \" file\" is supported " )
117- e.uri = uri
118- raise e
119- if parsed.hostname != " " :
120- var e = newException (UriParseError , & " Invalid hostname: { parsed.hostname} , only empty hostname is supported " )
121- e.uri = uri
122- raise e
123- return normalizedPath (
124- when defined (windows):
125- parsed.path[1 ..^ 1 ]
126- else :
127- parsed.path).decodeUrl
128-
129- proc parseId (node: JsonNode ): string =
130- if node == nil : return
131- if node.kind == JString :
132- node.getStr
133- elif node.kind == JInt :
134- $ node.getInt
135- else :
136- " "
137-
138- >>>>>>> 19 a04042ad (tool: LSP support)
13956proc respond (outs: Stream , request: JsonNode , data: JsonNode ) =
14057 let resp = create (ResponseMessage , " 2.0" , parseId (request[" id" ]), some (data), none (ResponseError )).JsonNode
14158 outs.sendJson resp
@@ -192,29 +109,6 @@ proc getProjectFile(file: string): string =
192109template getNimsuggest (fileuri: string ): Nimsuggest =
193110 projectFiles[openFiles[fileuri].projectFile].nimsuggest
194111
195- <<<<<<< HEAD
196- ====== =
197- if paramCount () == 1 :
198- case paramStr (1 ):
199- of " --help" :
200- echo " Usage: nimlsp [OPTION | PATH]\n "
201- echo " --help, shows this message"
202- echo " --version, shows only the version"
203- echo " PATH, path to the Nim source directory, defaults to \" " , nimpath, " \" "
204- quit 0
205- of " --version" :
206- echo " nimlsp v" , version
207- when defined (debugLogging): echo " Compiled with debug logging"
208- when defined (debugCommunication): echo " Compiled with communication logging"
209- quit 0
210- else : nimpath = expandFilename (paramStr (1 ))
211- if not fileExists (nimpath / " config/nim.cfg" ):
212- stderr.write & """ Unable to find "config/nim.cfg" in "{ nimpath
213- } ". Supply the Nim project folder by adding it as an argument.
214- """
215- quit 1
216-
217- >>>>>>> 19 a04042ad (tool: LSP support)
218112proc checkVersion (outs: Stream ) =
219113 let
220114 nimoutputTuple =
@@ -229,7 +123,6 @@ proc checkVersion(outs: Stream) =
229123 if version != NimVersion :
230124 outs.notify (" window/showMessage" , create (ShowMessageParams , MessageType .Warning .int , message = " Current Nim version does not match the one NimLSP is built against " & version & " != " & NimVersion ).JsonNode )
231125
232- <<<<<<< HEAD
233126proc createMarkupContent (label: string ; content: string ): MarkupContent =
234127 let label = " ```nim\n " & label & " \n ```\n "
235128 var
@@ -269,8 +162,6 @@ proc createMarkupContent(label: string; content: string): MarkupContent =
269162 )
270163 result = create (MarkupContent , " markdown" , label & c)
271164
272- ====== =
273- >>>>>>> 19 a04042ad (tool: LSP support)
274165proc main (ins: Stream , outs: Stream ) =
275166 checkVersion (outs)
276167 var message: JsonNode
@@ -403,24 +294,9 @@ proc main(ins: Stream, outs: Stream) =
403294 create (Position , req.rawLine, req.rawChar),
404295 create (Position , req.rawLine, req.rawChar + suggestions[0 ].qualifiedPath[^ 1 ].len)
405296 ))
406- <<<<<<< HEAD
407297 markupContent = createMarkupContent (label, suggestions[0 ].doc)
408298 resp = create (Hover , markupContent, rangeopt).JsonNode
409299 outs.respond (message, resp)
410- ====== =
411- markedString = create (MarkedStringOption , " nim" , label)
412- if suggestions[0 ].doc != " " :
413- resp = create (Hover ,
414- @ [
415- markedString,
416- create (MarkedStringOption , " " , suggestions[0 ].doc),
417- ],
418- rangeopt
419- ).JsonNode
420- else :
421- resp = create (Hover , markedString, rangeopt).JsonNode ;
422- outs.respond (message, resp)
423- >>>>>>> 19 a04042ad (tool: LSP support)
424300 of " textDocument/references" :
425301 textDocumentRequest (message, ReferenceParams , req):
426302 debugLog " Running equivalent of: use " , req.fileuri, " " , req.filestash, " (" ,
@@ -517,21 +393,16 @@ proc main(ins: Stream, outs: Stream) =
517393 debugLog " Found outlines: " , syms[0 ..< min (syms.len, 10 )],
518394 if syms.len > 10 : & " and { syms.len- 10 } more " else : " "
519395 var resp: JsonNode
520- <<<<<<< HEAD
521396 var flags = newSeq [int ]()
522- ====== =
523- >>>>>>> 19 a04042ad (tool: LSP support)
524397 if syms.len == 0 :
525398 resp = newJNull ()
526399 else :
527400 resp = newJarray ()
528401 for sym in syms.sortedByIt ((it.line,it.column,it.quality)):
529402 if sym.qualifiedPath.len != 2 :
530403 continue
531- <<<<<<< HEAD
532404 flags.setLen (0 )
533405 for f in sym.flags:
534- flags.add f.int
535406 resp.add create (DocumentSymbol ,
536407 sym.qualifiedPath[^ 1 ],
537408 some (symKindToString (sym.symKind)),
@@ -549,21 +420,6 @@ proc main(ins: Stream, outs: Stream) =
549420====== =
550421 resp.add create (
551422 SymbolInformation ,
552- sym.qualifiedPath[^ 1 ],
553- nimSymToLSPKind (sym.symKind).int ,
554- some (false ),
555- create (Location ,
556- " file://" & pathToUri (sym.filepath),
557- create (Range ,
558- create (Position , sym.line- 1 , sym.column),
559- create (Position , sym.line- 1 , sym.column + sym.qualifiedPath[^ 1 ].len)
560- )
561- ),
562- none (string )
563- >>>>>>> 19 a04042ad (tool: LSP support)
564- ).JsonNode
565- outs.respond (message, resp)
566- of " textDocument/signatureHelp" :
567423 textDocumentRequest (message, TextDocumentPositionParams , req):
568424 debugLog " Running equivalent of: con " , req.filePath, " " , req.filestash, " (" ,
569425 req.rawLine + 1 , " :" ,
@@ -753,7 +609,6 @@ proc main(ins: Stream, outs: Stream) =
753609 warnLog " Got exception: " , e.msg
754610 continue
755611
756- <<<<<<< HEAD
757612when isMainModule :
758613 infoLog (" explicitSourcePath: " , explicitSourcePath)
759614 for i in 1 .. paramCount ():
@@ -777,9 +632,3 @@ when isMainModule:
777632 ins = newFileStream (stdin)
778633 outs = newFileStream (stdout)
779634 main (ins, outs)
780- ====== =
781- var
782- ins = newFileStream (stdin)
783- outs = newFileStream (stdout)
784- main (ins, outs)
785- >>>>>>> 19 a04042ad (tool: LSP support)
0 commit comments