Skip to content

Commit b4768b8

Browse files
adonovangopherbot
authored andcommitted
gopls/internal/util/moreslices: add Reversed iterator
..and use it throughout gopls. Change-Id: Ic59129b521f98865e4d14bcb4a6a326ee0f8886a Reviewed-on: https://go-review.googlesource.com/c/tools/+/680435 Auto-Submit: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 8befc65 commit b4768b8

File tree

11 files changed

+42
-30
lines changed

11 files changed

+42
-30
lines changed

gopls/internal/analysis/infertypeargs/infertypeargs.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"golang.org/x/tools/go/analysis"
1313
"golang.org/x/tools/go/analysis/passes/inspect"
1414
"golang.org/x/tools/go/ast/inspector"
15+
"golang.org/x/tools/gopls/internal/util/moreslices"
1516
"golang.org/x/tools/internal/typeparams"
1617
)
1718

@@ -74,7 +75,7 @@ func diagnose(fset *token.FileSet, inspect *inspector.Inspector, start, end toke
7475
// Start removing argument expressions from the right, and check if we can
7576
// still infer the call expression.
7677
required := len(indices) // number of type expressions that are required
77-
for i := len(indices) - 1; i >= 0; i-- {
78+
for i, _ := range moreslices.Reversed(indices) {
7879
var fun ast.Expr
7980
if i == 0 {
8081
// No longer an index expression: just use the parameterized operand.

gopls/internal/cache/check.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"golang.org/x/tools/gopls/internal/protocol"
3535
"golang.org/x/tools/gopls/internal/util/bug"
3636
"golang.org/x/tools/gopls/internal/util/moremaps"
37+
"golang.org/x/tools/gopls/internal/util/moreslices"
3738
"golang.org/x/tools/gopls/internal/util/safetoken"
3839
"golang.org/x/tools/internal/analysisinternal"
3940
"golang.org/x/tools/internal/event"
@@ -1870,8 +1871,7 @@ func depsErrors(ctx context.Context, snapshot *Snapshot, mp *metadata.Package) (
18701871
// we reach the workspace.
18711872
var errors []*Diagnostic
18721873
for _, depErr := range relevantErrors {
1873-
for i := len(depErr.ImportStack) - 1; i >= 0; i-- {
1874-
item := depErr.ImportStack[i]
1874+
for _, item := range moreslices.Reversed(depErr.ImportStack) {
18751875
if snapshot.IsWorkspacePackage(PackageID(item)) {
18761876
break
18771877
}
@@ -1909,8 +1909,7 @@ func depsErrors(ctx context.Context, snapshot *Snapshot, mp *metadata.Package) (
19091909
// Add a diagnostic to the module that contained the lowest-level import of
19101910
// the missing package.
19111911
for _, depErr := range relevantErrors {
1912-
for i := len(depErr.ImportStack) - 1; i >= 0; i-- {
1913-
item := depErr.ImportStack[i]
1912+
for _, item := range moreslices.Reversed(depErr.ImportStack) {
19141913
mp := snapshot.Metadata(PackageID(item))
19151914
if mp == nil || mp.Module == nil {
19161915
continue

gopls/internal/cache/mod.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"golang.org/x/tools/gopls/internal/label"
1919
"golang.org/x/tools/gopls/internal/protocol"
2020
"golang.org/x/tools/gopls/internal/protocol/command"
21+
"golang.org/x/tools/gopls/internal/util/moreslices"
2122
"golang.org/x/tools/internal/event"
2223
"golang.org/x/tools/internal/memoize"
2324
)
@@ -380,8 +381,8 @@ func (s *Snapshot) matchErrorToModule(pm *ParsedModule, goCmdError string) (prot
380381
var reference *modfile.Line
381382
matches := moduleVersionInErrorRe.FindAllStringSubmatch(goCmdError, -1)
382383

383-
for i := len(matches) - 1; i >= 0; i-- {
384-
ver := module.Version{Path: matches[i][1], Version: matches[i][2]}
384+
for _, match := range moreslices.Reversed(matches) {
385+
ver := module.Version{Path: match[1], Version: match[2]}
385386
if err := module.Check(ver.Path, ver.Version); err != nil {
386387
continue
387388
}
@@ -412,8 +413,8 @@ func (s *Snapshot) matchErrorToModule(pm *ParsedModule, goCmdError string) (prot
412413
func (s *Snapshot) goCommandDiagnostic(pm *ParsedModule, loc protocol.Location, goCmdError string) (*Diagnostic, error) {
413414
matches := moduleVersionInErrorRe.FindAllStringSubmatch(goCmdError, -1)
414415
var innermost *module.Version
415-
for i := len(matches) - 1; i >= 0; i-- {
416-
ver := module.Version{Path: matches[i][1], Version: matches[i][2]}
416+
for _, match := range moreslices.Reversed(matches) {
417+
ver := module.Version{Path: match[1], Version: match[2]}
417418
if err := module.Check(ver.Path, ver.Version); err != nil {
418419
continue
419420
}

gopls/internal/cmd/semantictokens.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"golang.org/x/tools/gopls/internal/protocol"
1717
"golang.org/x/tools/gopls/internal/protocol/semtok"
1818
"golang.org/x/tools/gopls/internal/settings"
19+
"golang.org/x/tools/gopls/internal/util/moreslices"
1920
)
2021

2122
// generate semantic tokens and interpolate them in the file
@@ -147,8 +148,7 @@ func decorate(legend protocol.SemanticTokensLegend, file *cmdFile, data []uint32
147148
return nil
148149
}
149150
lines := bytes.Split(file.mapper.Content, []byte{'\n'})
150-
for i := len(marks) - 1; i >= 0; i-- {
151-
mx := marks[i]
151+
for _, mx := range moreslices.Reversed(marks) {
152152
markLine(mx, lines)
153153
}
154154
os.Stdout.Write(bytes.Join(lines, []byte{'\n'}))

gopls/internal/golang/completion/util.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"golang.org/x/tools/go/types/typeutil"
1313
"golang.org/x/tools/gopls/internal/golang"
1414
"golang.org/x/tools/gopls/internal/protocol"
15+
"golang.org/x/tools/gopls/internal/util/moreslices"
1516
"golang.org/x/tools/gopls/internal/util/safetoken"
1617
"golang.org/x/tools/internal/diff"
1718
"golang.org/x/tools/internal/typeparams"
@@ -271,9 +272,9 @@ func prevStmt(pos token.Pos, path []ast.Node) ast.Stmt {
271272
}
272273
}
273274

274-
for i := len(blockLines) - 1; i >= 0; i-- {
275-
if blockLines[i].End() < pos {
276-
return blockLines[i]
275+
for _, line := range moreslices.Reversed(blockLines) {
276+
if line.End() < pos {
277+
return line
277278
}
278279
}
279280

gopls/internal/golang/extract.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"golang.org/x/tools/gopls/internal/cache/parsego"
2727
goplsastutil "golang.org/x/tools/gopls/internal/util/astutil"
2828
"golang.org/x/tools/gopls/internal/util/bug"
29+
"golang.org/x/tools/gopls/internal/util/moreslices"
2930
"golang.org/x/tools/gopls/internal/util/safetoken"
3031
"golang.org/x/tools/internal/typesinternal"
3132
)
@@ -2115,8 +2116,7 @@ func isFreeBranchStmt(stack []ast.Node) bool {
21152116
}
21162117
case token.BREAK, token.CONTINUE:
21172118
// Find innermost relevant ancestor for break/continue.
2118-
for i := len(stack) - 2; i >= 0; i-- {
2119-
n := stack[i]
2119+
for _, n := range moreslices.Reversed(stack) {
21202120
if isLabeled {
21212121
l, ok := n.(*ast.LabeledStmt)
21222122
if !(ok && l.Label.Name == node.Label.Name) {

gopls/internal/golang/hover.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"golang.org/x/tools/gopls/internal/settings"
3838
gastutil "golang.org/x/tools/gopls/internal/util/astutil"
3939
"golang.org/x/tools/gopls/internal/util/bug"
40+
"golang.org/x/tools/gopls/internal/util/moreslices"
4041
"golang.org/x/tools/gopls/internal/util/safetoken"
4142
internalastutil "golang.org/x/tools/internal/astutil"
4243
"golang.org/x/tools/internal/event"
@@ -1561,8 +1562,8 @@ func findDeclInfo(files []*ast.File, pos token.Pos) (decl ast.Decl, spec ast.Spe
15611562
switch n := n.(type) {
15621563
case *ast.Field:
15631564
findEnclosingDeclAndSpec := func() {
1564-
for i := len(stack) - 1; i >= 0; i-- {
1565-
switch n := stack[i].(type) {
1565+
for _, n := range moreslices.Reversed(stack) {
1566+
switch n := n.(type) {
15661567
case ast.Spec:
15671568
spec = n
15681569
case ast.Decl:

gopls/internal/golang/semtok.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"golang.org/x/tools/gopls/internal/protocol"
3030
"golang.org/x/tools/gopls/internal/protocol/semtok"
3131
"golang.org/x/tools/gopls/internal/util/bug"
32+
"golang.org/x/tools/gopls/internal/util/moreslices"
3233
"golang.org/x/tools/gopls/internal/util/safetoken"
3334
"golang.org/x/tools/internal/astutil"
3435
"golang.org/x/tools/internal/event"
@@ -277,8 +278,7 @@ func (tv *tokenVisitor) token(start token.Pos, length int, typ semtok.Type, modi
277278
// strStack converts the stack to a string, for debugging and error messages.
278279
func (tv *tokenVisitor) strStack() string {
279280
msg := []string{"["}
280-
for i := len(tv.stack) - 1; i >= 0; i-- {
281-
n := tv.stack[i]
281+
for _, n := range moreslices.Reversed(tv.stack) {
282282
msg = append(msg, strings.TrimPrefix(fmt.Sprintf("%T", n), "*ast."))
283283
}
284284
if len(tv.stack) > 0 {
@@ -649,8 +649,8 @@ func (tv *tokenVisitor) ident(id *ast.Ident) {
649649
// isParam reports whether the position is that of a parameter name of
650650
// an enclosing function.
651651
func (tv *tokenVisitor) isParam(pos token.Pos) bool {
652-
for i := len(tv.stack) - 1; i >= 0; i-- {
653-
switch n := tv.stack[i].(type) {
652+
for _, n := range moreslices.Reversed(tv.stack) {
653+
switch n := n.(type) {
654654
case *ast.FuncDecl:
655655
for _, f := range n.Type.Params.List {
656656
for _, id := range f.Names {

gopls/internal/server/general.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -284,13 +284,11 @@ func (s *server) checkViewGoVersions() {
284284
// version, it returns -1.
285285
//
286286
// Copied from the testenv package.
287-
func go1Point() int {
288-
for i := len(build.Default.ReleaseTags) - 1; i >= 0; i-- {
289-
var version int
290-
if _, err := fmt.Sscanf(build.Default.ReleaseTags[i], "go1.%d", &version); err != nil {
291-
continue
287+
func go1Point() (version int) {
288+
for _, tag := range moreslices.Reversed(build.Default.ReleaseTags) {
289+
if _, err := fmt.Sscanf(tag, "go1.%d", &version); err == nil {
290+
return
292291
}
293-
return version
294292
}
295293
return -1
296294
}

gopls/internal/template/parse_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ package template
77
import (
88
"strings"
99
"testing"
10+
11+
"golang.org/x/tools/gopls/internal/util/moreslices"
1012
)
1113

1214
type datum struct {
@@ -135,8 +137,7 @@ func TestLineCol(t *testing.T) {
135137
t.Errorf("expected %d, %d, got %d, %d for case %d", len(lines)-1, mxlen, lastl, lastc, n)
136138
}
137139
// backwards
138-
for j := len(saved) - 1; j >= 0; j-- {
139-
s := saved[j]
140+
for j, s := range moreslices.Reversed(saved) {
140141
xl, xc := p.LineCol(s.offset)
141142
if xl != s.l || xc != s.c {
142143
t.Errorf("at offset %d(%d), got (%d,%d), expected (%d,%d)", s.offset, j, xl, xc, s.l, s.c)

0 commit comments

Comments
 (0)