-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathnimexpand.nim
84 lines (75 loc) · 2.86 KB
/
nimexpand.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import std/[strutils]
import regex
import chronos, chronos/asyncproc
import stew/[byteutils]
import chronicles
import protocol/types
import utils
import suggestapi
import std/[strscans, strformat]
proc extractMacroExpansion*(output: string, targetLine: int): string =
var start = false
for line in output.split({'\n', '\r'}):
if line.len == 0: continue
debug "extractMacroExpansion", line = line, condMet = &".nim({targetLine}," in line
if &".nim({targetLine}," in line:
start = true
elif &".nim" in line and start:
break
if start:
result.add line & "\n"
if result.len > 0:
let macroStart = result.find("macro: ")
if macroStart != -1:
result = result.substr(macroStart + "macro: ".len)
result = result.replace("[ExpandMacro]", "")
proc nimExpandMacro*(nimPath: string, suggest: Suggest, filePath: string): Future[string] {.async.} =
let
macroName = suggest.qualifiedPath[suggest.qualifiedPath.len - 1]
line = suggest.line
debug "nimExpandMacro", macroName = macroName, line = line, filePath = filePath
debug "Executing ", cmd = &"nim c --expandMacro:{macroName} {filePath}"
let process = await startProcess(
nimPath,
arguments = @["c", "--expandMacro:" & macroName] & @[filePath],
options = {UsePath, StdErrToStdOut},
stdoutHandle = AsyncProcess.Pipe,
)
try:
let res = await process.waitForExit(10.seconds)
let output = string.fromBytes(process.stdoutStream.read().await)
result = extractMacroExpansion(output, line)
finally:
await shutdownChildProcess(process)
proc extractArcExpansion*(output: string, procName: string): string =
var start = false
let cond = &"--expandArc: {procName}"
for line in output.splitLines:
# debug "extractArcExpansion", line = line, condMet = cond in line
if cond in line:
start = true
elif &"-- end of expandArc" in line and start:
break
if start:
result.add line & "\n"
if result.len > 0:
result = result.replace(cond, "").strip()
proc nimExpandArc*(nimPath: string, suggest: Suggest, filePath: string): Future[string] {.async.} =
let procName = suggest.qualifiedPath[suggest.qualifiedPath.len - 1]
debug "nimExpandArc", procName = procName, filePath = filePath
let process = await startProcess(
nimPath,
arguments = @["c", &"--expandArc:{procName}", "--compileOnly"] & @[filePath],
options = {UsePath, StdErrToStdOut},
stdoutHandle = AsyncProcess.Pipe,
)
try:
let res = await process.waitForExit(10.seconds)
let output = string.fromBytes(process.stdoutStream.read().await)
result = extractArcExpansion(output, procName)
# debug "nimExpandArc", output = output, result = result
if result.len == 0:
result = &"#Couldnt expand arc for `{procName}`. Showing raw output instead \n"
result.add output
finally:
await shutdownChildProcess(process)