diff --git a/compiler/front/options.nim b/compiler/front/options.nim index 3b86b993cd5..eecc965d139 100644 --- a/compiler/front/options.nim +++ b/compiler/front/options.nim @@ -309,6 +309,10 @@ type when defined(nimDebugUnreportedErrors): unreportedErrors*: OrderedTable[NodeId, PNode] +const + IdeLocCmds* = {ideSug, ideCon, ideDef, ideUse, ideDus} + ## IDE commands requiring source locations, related `MsgConfig.trackPos` + template `[]`*(conf: ConfigRef, idx: FileIndex): TFileInfo = conf.m.fileInfos[idx.uint32] diff --git a/compiler/sem/sem.nim b/compiler/sem/sem.nim index 965db07ac34..34ffb25f640 100644 --- a/compiler/sem/sem.nim +++ b/compiler/sem/sem.nim @@ -164,19 +164,20 @@ proc deltaTrace(stopProc, indent: string, entries: seq[StackTraceEntry]) echo: "$1| $2 $3($4)" % [indent, $e.procname, $e.filename, $e.line] -template semIdeForTemplateOrGenericCheck(conf, n, requiresCheck) = - # we check quickly if the node is where the cursor is +template semIdeForTemplateOrGenericCheck(conf, n, cursorInBody) = + # use only for idetools support; detecting cursor in generic or template body + # if so call `semIdeForTemplateOrGeneric` for semantic checking when defined(nimsuggest): - if n.info.fileIndex == conf.m.trackPos.fileIndex and n.info.line == conf.m.trackPos.line: - requiresCheck = true + if conf.ideCmd in IdeLocCmds and + n.info.fileIndex == conf.m.trackPos.fileIndex and + n.info.line == conf.m.trackPos.line: + cursorInBody = true template semIdeForTemplateOrGeneric(c: PContext; n: PNode; - requiresCheck: bool) = - # use only for idetools support; this is pretty slow so generics and - # templates perform some quick check whether the cursor is actually in - # the generic or template. + cursorInBody: bool) = + # provide incomplete information for idetools support in generic or template when defined(nimsuggest): - if c.config.cmd == cmdIdeTools and requiresCheck: + if c.config.cmd == cmdIdeTools and cursorInBody: #if optIdeDebug in gGlobalOptions: # echo "passing to safeSemExpr: ", renderTree(n) discard safeSemExpr(c, n) diff --git a/nimsuggest/tests/toutline_generic.nim b/nimsuggest/tests/toutline_generic.nim new file mode 100644 index 00000000000..5f16357a3da --- /dev/null +++ b/nimsuggest/tests/toutline_generic.nim @@ -0,0 +1,27 @@ +# Regression test for symbols within template and generic bodies being +# wrongfully reported by `outline`. + +proc generic[T]() = + var x = 0 #[!]# + return # <- when no cursor is provided, this triggered `x` being reported + +template templ() = + var x = 0 #[!]# + return # <- when no cursor is provided, this triggered `x` being reported + +# try both with and without a specified cursor position, they should be the same + +discard """ +$nimsuggest --tester $file +>outline $1 +outline;;skProc;;toutline_generic.generic;;*;;4;;5;;"";;100 +outline;;skTemplate;;toutline_generic.templ;;*;;8;;9;;"";;100 + +>outline $2 +outline;;skProc;;toutline_generic.generic;;*;;4;;5;;"";;100 +outline;;skTemplate;;toutline_generic.templ;;*;;8;;9;;"";;100 + +>outline $path/toutline_generic.nim +outline;;skProc;;toutline_generic.generic;;*;;4;;5;;"";;100 +outline;;skTemplate;;toutline_generic.templ;;*;;8;;9;;"";;100 +"""