From 1a55b1581c4637db109fd31393d03436087fe797 Mon Sep 17 00:00:00 2001 From: Edward Muller Date: Thu, 27 Jun 2019 16:17:07 +0000 Subject: [PATCH] internal/lsp/source: move the common path to the left As per the following guidance: "Try to keep the normal code path at a minimal indentation" I know this is normally applied to error handling, but the same logic about improving readability applies here too. Change-Id: Ib20dae9975e94b40fb6ff7049782375b18ef59ba Change-Id: Ib20dae9975e94b40fb6ff7049782375b18ef59ba GitHub-Last-Rev: 97919272de76ec15845556e032985c5969a277fa GitHub-Pull-Request: golang/tools#125 Reviewed-on: https://go-review.googlesource.com/c/tools/+/183698 Reviewed-by: Suzy Mueller Run-TryBot: Suzy Mueller --- internal/lsp/source/rename.go | 39 +++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/internal/lsp/source/rename.go b/internal/lsp/source/rename.go index b373275e..f2169e5d 100644 --- a/internal/lsp/source/rename.go +++ b/internal/lsp/source/rename.go @@ -101,26 +101,29 @@ func (r *renamer) update(ctx context.Context) (map[span.URI][]TextEdit, error) { } result[refSpan.URI()] = append(result[refSpan.URI()], edit) - if ref.isDeclaration { - // Perform the rename in doc comments too (declared in the original package) - if doc := r.docComment(r.pkg, ref.ident); doc != nil { - for _, comment := range doc.List { - for _, locs := range docRegexp.FindAllStringIndex(comment.Text, -1) { - rng := span.NewRange(r.fset, comment.Pos()+token.Pos(locs[0]), comment.Pos()+token.Pos(locs[1])) - spn, err := rng.Span() - if err != nil { - return nil, err - } - result[refSpan.URI()] = append(result[refSpan.URI()], TextEdit{ - Span: spn, - NewText: r.to, - }) - } - comment.Text = docRegexp.ReplaceAllString(comment.Text, r.to) - } - } + if !ref.isDeclaration { // not a declaration + continue } + doc := r.docComment(r.pkg, ref.ident) + if doc == nil { // no doc comment + continue + } + + // Perform the rename in doc comments declared in the original package + for _, comment := range doc.List { + for _, locs := range docRegexp.FindAllStringIndex(comment.Text, -1) { + rng := span.NewRange(r.fset, comment.Pos()+token.Pos(locs[0]), comment.Pos()+token.Pos(locs[1])) + spn, err := rng.Span() + if err != nil { + return nil, err + } + result[refSpan.URI()] = append(result[refSpan.URI()], TextEdit{ + Span: spn, + NewText: r.to, + }) + } + } } return result, nil