Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion nimlsp.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ requires "nim >= 1.0.0"
#requires "packedjson"
requires "astpatternmatching"
requires "jsonschema >= 0.2.1"

# nimble test does not work for me out of the box
#task test, "Runs the test suite":
#exec "nim c -r tests/test_messages.nim"
Expand Down
20 changes: 20 additions & 0 deletions src/config.nims
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
import os

switch "path", getCurrentCompilerExe().parentDir.parentDir
--gc:markAndSweep

switch "hint","XDeclaredButNotUsed:off"

--path:"$lib/packages/docutils"

--define:useStdoutAsStdmsg
--define:nimsuggest
--define:nimcore

# die when nimsuggest uses more than 4GB:
when defined(cpu32):
switch "define","nimMaxHeap=2000"
else:
switch "define","nimMaxHeap=4000"

--threads:on
switch "warning","[Spacing]:off" # The JSON schema macro uses a syntax similar to TypeScript
switch "warning","[CaseTransition]:off"
switch "define","nimOldCaseObjects"
260 changes: 125 additions & 135 deletions src/nimlsp.nim

Large diffs are not rendered by default.

21 changes: 0 additions & 21 deletions src/nimlsp.nim.cfg

This file was deleted.

14 changes: 14 additions & 0 deletions src/nimlsppkg/logger.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import os
const
storage* = getTempDir() / "nimlsp"

when defined(debugLogging):
var logFile = open(storage / "nimlsp.log", fmWrite)

template debug*(args: varargs[string, `$`]) =
when defined(debugLogging):
stderr.write(join args)
stderr.write("\n")
logFile.write(join args)
logFile.write("\n\n")
logFile.flushFile()
135 changes: 108 additions & 27 deletions src/nimlsppkg/suggestlib.nim
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import macros, os
import logger

const explicitSourcePath {.strdefine.} = getCurrentCompilerExe().parentDir.parentDir

macro mImport(path: static[string]): untyped =
result = newNimNode(nnkStmtList)
result.add(quote do:
import `path`
)
result = nnkImportStmt.newTree(newLit(path))

mImport(explicitSourcePath / "nimsuggest" / "nimsuggest.nim")
import messageenums
Expand Down Expand Up @@ -97,32 +95,115 @@ func nimSymDetails*(suggest: Suggest): string =
of "skVar": "var of " & suggest.forth
else: suggest.forth

func nimDocstring*(suggest: Suggest): string =
suggest.doc
macro createCommands(fileOnly:static[bool] = false, commands: varargs[untyped]) =

result = nnkStmtList.newTree
for cmd in commands:
let cmdStr = cmd.strVal
var params = nnkFormalParams.newTree(
nnkBracketExpr.newTree(
ident("seq"),
ident"Suggest"
),
nnkIdentDefs.newTree(
ident("nimsuggest"),
ident("NimSuggest"),
newEmptyNode()
),
nnkIdentDefs.newTree(
ident("file"),
ident("string"),
newEmptyNode()
),
nnkIdentDefs.newTree(
ident("dirtyfile"),
newEmptyNode(),
newLit("")
),

)
if not fileOnly:
params.add nnkIdentDefs.newTree(
ident("line"),
ident("int"),
newEmptyNode()
)
params.add nnkIdentDefs.newTree(
ident("col"),
ident("int"),
newEmptyNode()
)
var call = nnkCall.newTree(
nnkDotExpr.newTree(
ident("nimsuggest"),
ident("runCmd")
),
ident("ide" & cmdStr)
,
nnkCommand.newTree(
ident("AbsoluteFile"),
ident("file")
),
nnkCommand.newTree(
ident("AbsoluteFile"),
ident("dirtyfile")
)
)
if not fileOnly:
call.add ident("line")
call.add ident("col")
else:
call.add newLit(0)
call.add newLit(0)
var tryCatch = nnkTryStmt.newTree(
nnkStmtList.newTree(
nnkAsgn.newTree(
newIdentNode("result"),
call
)
),
nnkExceptBranch.newTree(
nnkInfix.newTree(
newIdentNode("as"),
newIdentNode("Exception"),
newIdentNode("e")
),
nnkStmtList.newTree(
nnkCommand.newTree(
newIdentNode("debug"),
nnkPrefix.newTree(
newIdentNode("$"),
nnkCall.newTree(
newIdentNode("getStackTraceEntries")
)
)
)
)
)
)
result.add nnkStmtList.newTree(
nnkProcDef.newTree(
nnkPostfix.newTree(
ident("*"),
ident(cmdStr)
),
newEmptyNode(),
newEmptyNode(),
params,
newEmptyNode(),
newEmptyNode(),
nnkStmtList.newTree(
tryCatch
)
)
)

template createFullCommand(command: untyped) {.dirty.} =
proc command*(nimsuggest: NimSuggest, file: string, dirtyfile = "",
line: int, col: int): seq[Suggest] =
nimsuggest.runCmd(`ide command`, AbsoluteFile file, AbsoluteFile dirtyfile, line, col)

template createFileOnlyCommand(command: untyped) {.dirty.} =
proc command*(nimsuggest: NimSuggest, file: string, dirtyfile = ""): seq[Suggest] =
nimsuggest.runCmd(`ide command`, AbsoluteFile file, AbsoluteFile dirtyfile, 0, 0)

createFullCommand(sug)
createFullCommand(con)
createFullCommand(def)
createFullCommand(use)
createFullCommand(dus)
createFileOnlyCommand(chk)
#createFileOnlyCommand(`mod`)
createFileOnlyCommand(highlight)
createFileOnlyCommand(outline)
createFileOnlyCommand(known)
createCommands(false,sug,con,def,use,dus)
createCommands(true,chk,highlight,outline,known)

when isMainModule:
var graph = initNimSuggest("/home/peter/div/nimlsp/suglibtest.nim", nimPath = "/home/peter/div/Nim")
var suggestions = graph.sug("/home/peter/div/nimlsp/suglibtest.nim", "/home/peter/div/nimlsp/suglibtest.nim", 7, 2)
var graph = initNimSuggest(currentSourcePath, nimPath = getCurrentCompilerExe().parentDir.parentDir)
var suggestions = graph.sug(currentSourcePath, currentSourcePath, 206, 26)
echo "Got ", suggestions.len, " suggestions"
for suggestion in suggestions:
echo suggestion
6 changes: 6 additions & 0 deletions src/nimlsppkg/suggestlib.nims
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import os

switch "path", getCurrentCompilerExe().parentDir.parentDir
--define:nimsuggest
--define:nimcore
--threads:on