Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nimsuggest: move library code into suggest #892

Merged
merged 5 commits into from
Sep 16, 2023
Merged
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
50 changes: 49 additions & 1 deletion compiler/tools/suggest.nim
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import
options,
],
compiler/modules/[
modules,
modulegraphs,
],
compiler/sem/[
Expand All @@ -66,7 +67,8 @@ import
compiler/utils/[
prefixmatches,
astrepr,
debugutils
debugutils,
pathutils
]


Expand Down Expand Up @@ -466,6 +468,52 @@ proc isTracked*(current, trackPos: TLineInfo, tokenLen: int): bool =
if col >= current.col and col <= current.col+tokenLen-1:
return true

proc findTrackedNode(n: PNode; trackPos: TLineInfo): PSym =
if n.kind == nkSym:
if isTracked(n.info, trackPos, n.sym.name.s.len): return n.sym
else:
for i in 0 ..< safeLen(n):
let res = findTrackedNode(n[i], trackPos)
if res != nil: return res

proc findTrackedSym*(g: ModuleGraph;): PSym =
let m = g.getModule(g.config.m.trackPos.fileIndex)
if m != nil and m.ast != nil:
# xxx: the node finding should be specialized per symbol kind
result = findTrackedNode(m.ast, g.config.m.trackPos)
bung87 marked this conversation as resolved.
Show resolved Hide resolved

proc executeCmd*(cmd: IdeCmd, file, dirtyfile: AbsoluteFile, line, col: int;
bung87 marked this conversation as resolved.
Show resolved Hide resolved
graph: ModuleGraph) =
## executes the given suggest command, `cmd`, for a given `file`, at the
## position described by `line` and `col`umn. If `dirtyFile` is non-empty,
## then its contents are used as part of the analysis.
let conf = graph.config
conf.ideCmd = cmd
var isKnownFile = true
let dirtyIdx = fileInfoIdx(conf, file, isKnownFile)

if dirtyfile.isEmpty: msgs.setDirtyFile(conf, dirtyIdx, AbsoluteFile"")
else: msgs.setDirtyFile(conf, dirtyIdx, dirtyfile)

conf.m.trackPos = newLineInfo(dirtyIdx, line, col)
conf.m.trackPosAttached = false
conf.errorCounter = 0
if not isKnownFile:
graph.compileProject(dirtyIdx)
if conf.ideCmd in {ideUse, ideDus} and
saem marked this conversation as resolved.
Show resolved Hide resolved
dirtyfile.isEmpty:
discard "no need to recompile anything"
else:
let modIdx = graph.parentModule(dirtyIdx)
graph.markDirty dirtyIdx
graph.markClientsDirty dirtyIdx
# partially recompiling the project means that that VM and JIT state
# would become stale, which we prevent by discarding all of it:
graph.vm = nil
if conf.ideCmd != ideMod:
if isKnownFile:
graph.compileProject(modIdx)

when defined(nimsuggest):

proc addNoDup(s: PSym; info: TLineInfo) =
Expand Down
44 changes: 3 additions & 41 deletions nimsuggest/nimsuggest.nim
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ from compiler/ast/reports import Report,

from compiler/front/main import customizeForBackend

from compiler/tools/suggest import isTracked, listUsages, suggestSym, `$`
from compiler/tools/suggest import findTrackedSym, executeCmd, listUsages, suggestSym, `$`

when defined(windows):
import winlean
Expand Down Expand Up @@ -204,20 +204,6 @@ proc listEpc(): SexpNode =
methodDesc.add(docstring)
result.add(methodDesc)

proc findNode(n: PNode; trackPos: TLineInfo): PSym =
#echo "checking node ", n.info
if n.kind == nkSym:
if isTracked(n.info, trackPos, n.sym.name.s.len): return n.sym
else:
for i in 0 ..< safeLen(n):
let res = findNode(n[i], trackPos)
if res != nil: return res

proc symFromInfo(graph: ModuleGraph; trackPos: TLineInfo): PSym =
let m = graph.getModule(trackPos.fileIndex)
if m != nil and m.ast != nil:
result = findNode(m.ast, trackPos)

proc executeNoHooks(cmd: IdeCmd, file, dirtyfile: AbsoluteFile, line, col: int;
graph: ModuleGraph) =
let conf = graph.config
Expand All @@ -227,33 +213,9 @@ proc executeNoHooks(cmd: IdeCmd, file, dirtyfile: AbsoluteFile, line, col: int;
dirtyfile.string & "[" & $line & ":" & $col & "]"
)

conf.ideCmd = cmd
var isKnownFile = true
let dirtyIdx = fileInfoIdx(conf, file, isKnownFile)

if not dirtyfile.isEmpty: msgs.setDirtyFile(conf, dirtyIdx, dirtyfile)
else: msgs.setDirtyFile(conf, dirtyIdx, AbsoluteFile"")

conf.m.trackPos = newLineInfo(dirtyIdx, line, col)
conf.m.trackPosAttached = false
conf.errorCounter = 0
if not isKnownFile:
graph.compileProject(dirtyIdx)
if conf.ideCmd in {ideUse, ideDus} and
dirtyfile.isEmpty:
discard "no need to recompile anything"
else:
let modIdx = graph.parentModule(dirtyIdx)
graph.markDirty dirtyIdx
graph.markClientsDirty dirtyIdx
# partially recompiling the project means that that VM and JIT state
# would become stale, which we prevent by discarding all of it:
graph.vm = nil
if conf.ideCmd != ideMod:
if isKnownFile:
graph.compileProject(modIdx)
executeCmd(cmd, file, dirtyfile, line, col, graph)
if conf.ideCmd in {ideUse, ideDus}:
let u = graph.symFromInfo(conf.m.trackPos)
let u = graph.findTrackedSym()
if u != nil:
listUsages(graph, u)
else:
Expand Down
Loading