Skip to content

Commit

Permalink
suggest: refactor Suggest stringify procedure (#887)
Browse files Browse the repository at this point in the history
## Summary

Move `nimsuggest` specific string serialization for `Suggest` into the
`nimsuggest` module itself. This is an internal refactoring, no
functional change.

## Details

Creating string representations of a  `Suggest`  via  `$`  was 
`nimsuggest`  focused,
this has been moved into the  `nimsuggest`  module and renamed 
`toSuggestMsg` .

As part of this change, `Suggest.isGlobal` was removed, and global and
deprecation is now tracked via the  `flags`  field, backed by the newly
introduced
`SuggestFlag` enum.
  • Loading branch information
bung87 authored Oct 17, 2023
1 parent 7db09b1 commit 895c4da
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 46 deletions.
6 changes: 5 additions & 1 deletion compiler/front/options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ type

CfileList* = seq[Cfile]

SuggestFlag* {.pure.} = enum
deprecated = 1
isGlobal = 2

Suggest* = ref object
section*: IdeCmd
qualifiedPath*: seq[string]
Expand All @@ -151,13 +155,13 @@ type
doc*: string ## Unescaped documentation string
forth*: string ## type
quality*: range[0..100] ## matching quality
isGlobal*: bool ## is a global variable
contextFits*: bool ## type/non-type context matches
prefix*: PrefixMatch
symkind*: byte
scope*:int
localUsages*, globalUsages*: int # usage counters
tokenLen*: int
flags*: set[SuggestFlag]

Suggestions* = seq[Suggest]

Expand Down
46 changes: 4 additions & 42 deletions compiler/tools/suggest.nim
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ import
when defined(nimsuggest):
import compiler/sem/passes, compiler/utils/pathutils # importer

const
sep = '\t'

proc findDocComment(n: PNode): PNode =
if n == nil: return nil
if n.comment.len > 0: return n
Expand Down Expand Up @@ -149,9 +146,12 @@ proc symToSuggest(g: ModuleGraph; s: PSym, isLocal: bool, section: IdeCmd, info:
inTypeContext: bool; scope: int;
useSuppliedInfo = false): Suggest =
new(result)
if sfDeprecated in s.flags:
result.flags.incl SuggestFlag.deprecated
if sfGlobal in s.flags:
result.flags.incl SuggestFlag.isGlobal
result.section = section
result.quality = quality
result.isGlobal = sfGlobal in s.flags
result.prefix = prefix
if section in {ideSug, ideCon}:
result.contextFits = inTypeContext == (s.kind in {skType, skGenericParam})
Expand Down Expand Up @@ -199,44 +199,6 @@ proc symToSuggest(g: ModuleGraph; s: PSym, isLocal: bool, section: IdeCmd, info:
else:
getTokenLenFromSource(g.config, s.name.s, infox)

proc `$`*(suggest: Suggest): string =
result = $suggest.section
result.add(sep)
if suggest.section == ideHighlight:
if suggest.symkind.TSymKind == skVar and suggest.isGlobal:
result.add("skGlobalVar")
elif suggest.symkind.TSymKind == skLet and suggest.isGlobal:
result.add("skGlobalLet")
else:
result.add($suggest.symkind.TSymKind)
result.add(sep)
result.add($suggest.line)
result.add(sep)
result.add($suggest.column)
result.add(sep)
result.add($suggest.tokenLen)
else:
result.add($suggest.symkind.TSymKind)
result.add(sep)
if suggest.qualifiedPath.len != 0:
result.add(suggest.qualifiedPath.join("."))
result.add(sep)
result.add(suggest.forth)
result.add(sep)
result.add(suggest.filePath)
result.add(sep)
result.add($suggest.line)
result.add(sep)
result.add($suggest.column)
result.add(sep)
when defined(nimsuggest) and not defined(noDocgen) and not defined(leanCompiler):
result.add(suggest.doc.escape)
result.add(sep)
result.add($suggest.quality)
if suggest.section == ideSug:
result.add(sep)
result.add($suggest.prefix)

proc suggestResult(conf: ConfigRef; s: Suggest) =
if not isNil(conf.suggestionResultHook):
conf.suggestionResultHook(s)
Expand Down
46 changes: 43 additions & 3 deletions nimsuggest/nimsuggest.nim
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,14 @@ from compiler/ast/reports import Report,

from compiler/front/main import customizeForBackend

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

when defined(windows):
import winlean
else:
import posix

const sep = '\t'
const DummyEof = "!EOF!"
const Usage = """
Nimsuggest - Tool to give every editor IDE like capabilities for Nim
Expand Down Expand Up @@ -261,6 +262,45 @@ template checkSanity(client, sizeHex, size, messageBuffer: typed) =
if client.recv(messageBuffer, size) != size:
raise newException(ValueError, "didn't get all the bytes")

proc toSuggestMsg(suggest: Suggest): string =
result = $suggest.section
result.add(sep)
if suggest.section == ideHighlight:
let isGlobal = SuggestFlag.isGlobal in suggest.flags
if suggest.symkind.TSymKind == skVar and isGlobal:
result.add("skGlobalVar")
elif suggest.symkind.TSymKind == skLet and isGlobal:
result.add("skGlobalLet")
else:
result.add($suggest.symkind.TSymKind)
result.add(sep)
result.add($suggest.line)
result.add(sep)
result.add($suggest.column)
result.add(sep)
result.add($suggest.tokenLen)
else:
result.add($suggest.symkind.TSymKind)
result.add(sep)
if suggest.qualifiedPath.len != 0:
result.add(suggest.qualifiedPath.join("."))
result.add(sep)
result.add(suggest.forth)
result.add(sep)
result.add(suggest.filePath)
result.add(sep)
result.add($suggest.line)
result.add(sep)
result.add($suggest.column)
result.add(sep)
when defined(nimsuggest) and not defined(noDocgen) and not defined(leanCompiler):
result.add(suggest.doc.escape)
result.add(sep)
result.add($suggest.quality)
if suggest.section == ideSug:
result.add(sep)
result.add($suggest.prefix)

proc toStdout() {.gcsafe.} =
while true:
let res = results.recv()
Expand All @@ -269,7 +309,7 @@ proc toStdout() {.gcsafe.} =
of ideMsg: echo res.doc
of ideKnown: echo res.quality == 1
of ideProject: echo res.filePath
else: echo res
else: echo toSuggestMsg(res)

proc toSocket(stdoutSocket: Socket) {.gcsafe.} =
while true:
Expand All @@ -279,7 +319,7 @@ proc toSocket(stdoutSocket: Socket) {.gcsafe.} =
of ideMsg: stdoutSocket.send(res.doc & "\c\L")
of ideKnown: stdoutSocket.send($(res.quality == 1) & "\c\L")
of ideProject: stdoutSocket.send(res.filePath & "\c\L")
else: stdoutSocket.send($res & "\c\L")
else: stdoutSocket.send(toSuggestMsg(res) & "\c\L")

proc toEpc(client: Socket; uid: BiggestInt) {.gcsafe.} =
var list = newSList()
Expand Down

0 comments on commit 895c4da

Please sign in to comment.