Skip to content

Commit 3c048e2

Browse files
committed
internal/lsp: support return statements in extract function
Previously, users could not extract code that contained a return statement. Now, users can extract code with return statements, as long as the statements are nested within an if, case, or other control flow statement. Updates golang/go#37170 Change-Id: I2df52d0241517472decabce3666a32392ff257bd Reviewed-on: https://go-review.googlesource.com/c/tools/+/243650 Run-TryBot: Josh Baum <joshbaum@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
1 parent cd83430 commit 3c048e2

15 files changed

+643
-170
lines changed

internal/analysisinternal/analysis.go

+19
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,22 @@ func baseIfStmt(path []ast.Node, index int) ast.Stmt {
281281
}
282282
return stmt.(ast.Stmt)
283283
}
284+
285+
// WalkASTWithParent walks the AST rooted at n. The semantics are
286+
// similar to ast.Inspect except it does not call f(nil).
287+
func WalkASTWithParent(n ast.Node, f func(n ast.Node, parent ast.Node) bool) {
288+
var ancestors []ast.Node
289+
ast.Inspect(n, func(n ast.Node) (recurse bool) {
290+
if n == nil {
291+
ancestors = ancestors[:len(ancestors)-1]
292+
return false
293+
}
294+
295+
var parent ast.Node
296+
if len(ancestors) > 0 {
297+
parent = ancestors[len(ancestors)-1]
298+
}
299+
ancestors = append(ancestors, n)
300+
return f(n, parent)
301+
})
302+
}

0 commit comments

Comments
 (0)