From e07df5aacacb49798f6058e5a7d9fb8bce91457a Mon Sep 17 00:00:00 2001 From: Joel Hendrix Date: Wed, 23 Jan 2019 10:10:58 -0800 Subject: [PATCH] Fix display of shadowed variables while debugging (#2254) --- .travis.yml | 3 +-- src/debugAdapter/goDebug.ts | 44 ++++++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index d87080ccf..8c4b22607 100644 --- a/.travis.yml +++ b/.travis.yml @@ -49,8 +49,7 @@ install: - go get -u -v github.com/cweill/gotests/... - go get -u -v github.com/haya14busa/goplay/cmd/goplay - go get -u -v github.com/davidrjenni/reftools/cmd/fillstruct - - go get -u -v github.com/alecthomas/gometalinter - - gometalinter --install + - curl -fsSL https://git.io/vp6lP | sh -s -- -b $GOPATH/bin script: - npm run lint diff --git a/src/debugAdapter/goDebug.ts b/src/debugAdapter/goDebug.ts index 042d5a3f1..b7fbb6457 100644 --- a/src/debugAdapter/goDebug.ts +++ b/src/debugAdapter/goDebug.ts @@ -143,12 +143,22 @@ interface EvalOut { Variable: DebugVariable; } +enum GoVariableFlags { + VariableEscaped = 1, + VariableShadowed = 2, + VariableConstant = 4, + VariableArgument = 8, + VariableReturnArgument = 16 +} + interface DebugVariable { name: string; addr: number; type: string; realType: string; kind: GoReflectKind; + flags: GoVariableFlags; + DeclLine: number; value: string; len: number; cap: number; @@ -843,7 +853,35 @@ class GoDebugSession extends LoggingDebugSession { log('functionArgs', args); this.addFullyQualifiedName(args); let vars = args.concat(locals); - + // annotate shadowed variables in parentheses + const shadowedVars = new Map>(); + for (let i = 0; i < vars.length; ++i) { + if ((vars[i].flags & GoVariableFlags.VariableShadowed) === 0) { + continue; + } + const varName = vars[i].name; + if (!shadowedVars.has(varName)) { + const indices = new Array(); + indices.push(i); + shadowedVars.set(varName, indices); + } else { + shadowedVars.get(varName).push(i); + } + } + for (const svIndices of shadowedVars.values()) { + // sort by declared line number in descending order + svIndices.sort((lhs: number, rhs: number) => { + return vars[rhs].DeclLine - vars[lhs].DeclLine; + }); + // enclose in parentheses, one pair per scope + for (let scope = 0; scope < svIndices.length; ++scope) { + const svIndex = svIndices[scope]; + // start at -1 so scope of 0 has one pair of parens + for (let count = -1; count < scope; ++count) { + vars[svIndex].name = `(${vars[svIndex].name})`; + } + } + } let scopes = new Array(); let localVariables = { name: 'Local', @@ -851,6 +889,8 @@ class GoDebugSession extends LoggingDebugSession { type: '', realType: '', kind: 0, + flags: 0, + DeclLine: 0, value: '', len: 0, cap: 0, @@ -899,6 +939,8 @@ class GoDebugSession extends LoggingDebugSession { type: '', realType: '', kind: 0, + flags: 0, + DeclLine: 0, value: '', len: 0, cap: 0,