Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gopls/internal/golang: improve extract variable edits #482

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions gopls/internal/golang/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@ func extractVariable(fset *token.FileSet, start, end token.Pos, src []byte, file
if err := format.Node(&buf, fset, assignStmt); err != nil {
return nil, nil, err
}
assignment := strings.ReplaceAll(buf.String(), "\n", newLineIndent) + newLineIndent
var edits []analysis.TextEdit

return fset, &analysis.SuggestedFix{
TextEdits: []analysis.TextEdit{
switch insertBeforeStmt := insertBeforeStmt.(type) {
case *ast.DeclStmt, *ast.AssignStmt:
assignment := strings.ReplaceAll(buf.String(), "\n", newLineIndent) + newLineIndent
edits = []analysis.TextEdit{
{
Pos: insertBeforeStmt.Pos(),
End: insertBeforeStmt.Pos(),
Expand All @@ -92,7 +94,19 @@ func extractVariable(fset *token.FileSet, start, end token.Pos, src []byte, file
End: end,
NewText: []byte(lhs),
},
},
}
default:
assignment := strings.ReplaceAll(buf.String(), "\n", newLineIndent)
edits = []analysis.TextEdit{
{
Pos: start,
End: end,
NewText: []byte(assignment),
},
}
}
return fset, &analysis.SuggestedFix{
TextEdits: edits,
}, nil
}

Expand Down