Skip to content

Commit f3c581f

Browse files
committed
gopls/internal/protocol: add DocumentURI.Base accessor
...and use it everywhere. Change-Id: Ib2d59840a7dbbdfc48a62edbf7264ae3e9502378 Reviewed-on: https://go-review.googlesource.com/c/tools/+/678758 LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Alan Donovan <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent d9bacab commit f3c581f

File tree

12 files changed

+22
-22
lines changed

12 files changed

+22
-22
lines changed

gopls/internal/cache/snapshot.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1421,7 +1421,7 @@ https://github.com/golang/tools/blob/master/gopls/doc/workspace.md.`, modDir, fi
14211421
fix = `This file may be excluded due to its build tags; try adding "-tags=<build tag>" to your gopls "buildFlags" configuration
14221422
See the documentation for more information on working with build tags:
14231423
https://github.com/golang/tools/blob/master/gopls/doc/settings.md#buildflags.`
1424-
} else if strings.Contains(filepath.Base(fh.URI().Path()), "_") {
1424+
} else if strings.Contains(fh.URI().Base(), "_") {
14251425
fix = `This file may be excluded due to its GOOS/GOARCH, or other build constraints.`
14261426
} else {
14271427
fix = `This file is ignored by your gopls build.` // we don't know why

gopls/internal/cache/workspace.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818

1919
// isGoWork reports if uri is a go.work file.
2020
func isGoWork(uri protocol.DocumentURI) bool {
21-
return filepath.Base(uri.Path()) == "go.work"
21+
return uri.Base() == "go.work"
2222
}
2323

2424
// goWorkModules returns the URIs of go.mod files named by the go.work file.
@@ -63,7 +63,7 @@ func localModFiles(relativeTo string, goWorkOrModPaths []string) map[protocol.Do
6363

6464
// isGoMod reports if uri is a go.mod file.
6565
func isGoMod(uri protocol.DocumentURI) bool {
66-
return filepath.Base(uri.Path()) == "go.mod"
66+
return uri.Base() == "go.mod"
6767
}
6868

6969
// isWorkspaceFile reports if uri matches a set of globs defined in workspaceFiles

gopls/internal/cmd/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ func (f *cmdFile) spanRange(s span) (protocol.Range, error) {
903903
// case-sensitive directories. The authoritative answer
904904
// requires querying the file system, and we don't want
905905
// to do that.
906-
if !strings.EqualFold(filepath.Base(string(f.mapper.URI)), filepath.Base(string(s.URI()))) {
906+
if !strings.EqualFold(f.mapper.URI.Base(), s.URI().Base()) {
907907
return protocol.Range{}, bugpkg.Errorf("mapper is for file %q instead of %q", f.mapper.URI, s.URI())
908908
}
909909
start, err := pointPosition(f.mapper, s.Start())

gopls/internal/golang/addtest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ func AddTestForFunc(ctx context.Context, snapshot *cache.Snapshot, loc protocol.
265265
return nil, err
266266
}
267267

268-
testBase := strings.TrimSuffix(filepath.Base(loc.URI.Path()), ".go") + "_test.go"
268+
testBase := strings.TrimSuffix(loc.URI.Base(), ".go") + "_test.go"
269269
goTestFileURI := protocol.URIFromPath(filepath.Join(loc.URI.DirPath(), testBase))
270270

271271
testFH, err := snapshot.ReadFile(ctx, goTestFileURI)

gopls/internal/golang/call_hierarchy.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"go/ast"
1212
"go/token"
1313
"go/types"
14-
"path/filepath"
1514

1615
"golang.org/x/tools/go/ast/astutil"
1716
"golang.org/x/tools/go/types/typeutil"
@@ -59,7 +58,7 @@ func PrepareCallHierarchy(ctx context.Context, snapshot *cache.Snapshot, fh file
5958
Name: obj.Name(),
6059
Kind: protocol.Function,
6160
Tags: []protocol.SymbolTag{},
62-
Detail: fmt.Sprintf("%s • %s", obj.Pkg().Path(), filepath.Base(declLoc.URI.Path())),
61+
Detail: fmt.Sprintf("%s • %s", obj.Pkg().Path(), declLoc.URI.Base()),
6362
URI: declLoc.URI,
6463
Range: rng,
6564
SelectionRange: rng,
@@ -182,7 +181,7 @@ func enclosingNodeCallItem(ctx context.Context, snapshot *cache.Snapshot, pkgPat
182181
Name: name,
183182
Kind: kind,
184183
Tags: []protocol.SymbolTag{},
185-
Detail: fmt.Sprintf("%s • %s", pkgPath, filepath.Base(fh.URI().Path())),
184+
Detail: fmt.Sprintf("%s • %s", pkgPath, fh.URI().Base()),
186185
URI: loc.URI,
187186
Range: rng,
188187
SelectionRange: rng,
@@ -283,7 +282,7 @@ func OutgoingCalls(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle
283282
Name: obj.Name(),
284283
Kind: protocol.Function,
285284
Tags: []protocol.SymbolTag{},
286-
Detail: fmt.Sprintf("%s • %s", obj.Pkg().Path(), filepath.Base(loc.URI.Path())),
285+
Detail: fmt.Sprintf("%s • %s", obj.Pkg().Path(), loc.URI.Base()),
287286
URI: loc.URI,
288287
Range: loc.Range,
289288
SelectionRange: loc.Range,

gopls/internal/golang/codeaction.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"go/ast"
1212
"go/token"
1313
"go/types"
14-
"path/filepath"
1514
"reflect"
1615
"slices"
1716
"strings"
@@ -1108,7 +1107,7 @@ func toggleCompilerOptDetails(ctx context.Context, req *codeActionsRequest) erro
11081107

11091108
title := fmt.Sprintf("%s compiler optimization details for %q",
11101109
cond(req.snapshot.WantCompilerOptDetails(dir), "Hide", "Show"),
1111-
filepath.Base(dir.Path()))
1110+
dir.Base())
11121111
cmd := command.NewGCDetailsCommand(title, req.fh.URI())
11131112
req.addCommandAction(cmd, false)
11141113
}

gopls/internal/mcp/context.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"fmt"
1414
"go/ast"
1515
"go/token"
16-
"path/filepath"
1716
"slices"
1817
"strings"
1918

@@ -58,7 +57,7 @@ func contextHandler(ctx context.Context, session *cache.Session, params *mcp.Cal
5857
fmt.Fprintf(&result, "Current package %q (package %s) declares the following symbols:\n\n", pkg.Metadata().PkgPath, pkg.Metadata().Name)
5958
// Write context of the current file.
6059
{
61-
fmt.Fprintf(&result, "%s (current file):\n", filepath.Base(pgf.URI.Path()))
60+
fmt.Fprintf(&result, "%s (current file):\n", pgf.URI.Base())
6261
result.WriteString("--->\n")
6362
if err := writeFileSummary(ctx, snapshot, pgf.URI, &result, false); err != nil {
6463
return nil, err
@@ -73,7 +72,7 @@ func contextHandler(ctx context.Context, session *cache.Session, params *mcp.Cal
7372
continue
7473
}
7574

76-
fmt.Fprintf(&result, "%s:\n", filepath.Base(file.URI.Path()))
75+
fmt.Fprintf(&result, "%s:\n", file.URI.Base())
7776
result.WriteString("--->\n")
7877
if err := writeFileSummary(ctx, snapshot, file.URI, &result, false); err != nil {
7978
return nil, err
@@ -86,7 +85,7 @@ func contextHandler(ctx context.Context, session *cache.Session, params *mcp.Cal
8685
if len(pgf.File.Imports) > 0 {
8786
// Write import decls of the current file.
8887
{
89-
fmt.Fprintf(&result, "Current file %q contains this import declaration:\n", filepath.Base(pgf.URI.Path()))
88+
fmt.Fprintf(&result, "Current file %q contains this import declaration:\n", pgf.URI.Base())
9089
result.WriteString("--->\n")
9190
// Add all import decl to output including all floating comment by
9291
// using GenDecl's start and end position.
@@ -127,7 +126,7 @@ func contextHandler(ctx context.Context, session *cache.Session, params *mcp.Cal
127126

128127
fmt.Fprintf(&result, "%q (package %s)\n", importPath, impMetadata.Name)
129128
for _, f := range impMetadata.CompiledGoFiles {
130-
fmt.Fprintf(&result, "%s:\n", filepath.Base(f.Path()))
129+
fmt.Fprintf(&result, "%s:\n", f.Base())
131130
result.WriteString("--->\n")
132131
if err := writeFileSummary(ctx, snapshot, f, &result, true); err != nil {
133132
return nil, err

gopls/internal/protocol/uri.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ func (uri DocumentURI) Path() string {
9191
return filepath.FromSlash(filename)
9292
}
9393

94+
// Base returns the base name of the file path of the given URI.
95+
func (uri DocumentURI) Base() string {
96+
return filepath.Base(uri.Path())
97+
}
98+
9499
// Dir returns the URI for the directory containing the receiver.
95100
func (uri DocumentURI) Dir() DocumentURI {
96101
// This function could be more efficiently implemented by avoiding any call

gopls/internal/server/command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func (h *commandHandler) Modules(ctx context.Context, args command.ModulesArgs)
9191
return false // "can't happen" (see prior Encloses check)
9292
}
9393

94-
assert(filepath.Base(goMod.Path()) == "go.mod", fmt.Sprintf("invalid go.mod path: want go.mod, got %q", goMod.Path()))
94+
assert(goMod.Base() == "go.mod", fmt.Sprintf("invalid go.mod path: want go.mod, got %q", goMod.Path()))
9595

9696
// Invariant: rel is a relative path without "../" segments and the last
9797
// segment is "go.mod"

gopls/internal/server/text_synchronization.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,7 @@ func (s *server) warnAboutModifyingGeneratedFiles(ctx context.Context, uri proto
168168
isGenerated := golang.IsGenerated(ctx, snapshot, uri)
169169
release()
170170
if isGenerated {
171-
msg := fmt.Sprintf("Warning: editing %s, a generated file.",
172-
filepath.Base(uri.Path()))
171+
msg := fmt.Sprintf("Warning: editing %s, a generated file.", uri.Base())
173172
showMessage(ctx, s.client, protocol.Warning, msg)
174173
}
175174
return nil

gopls/internal/test/integration/misc/definition_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ var _ = foo(123) // call
637637
env.OpenFile("a.go")
638638

639639
locString := func(loc protocol.Location) string {
640-
return fmt.Sprintf("%s:%s", filepath.Base(loc.URI.Path()), loc.Range)
640+
return fmt.Sprintf("%s:%s", loc.URI.Base(), loc.Range)
641641
}
642642

643643
// Definition at the call"foo(123)" takes us to the Go declaration.

gopls/internal/test/integration/modfile/modfile_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package modfile
66

77
import (
88
"os"
9-
"path/filepath"
109
"runtime"
1110
"strings"
1211
"testing"
@@ -870,7 +869,7 @@ func hello() {}
870869
// Confirm that we still have metadata with only on-disk edits.
871870
env.OpenFile("main.go")
872871
loc := env.FirstDefinition(env.RegexpSearch("main.go", "hello"))
873-
if filepath.Base(string(loc.URI)) != "hello.go" {
872+
if loc.URI.Base() != "hello.go" {
874873
t.Fatalf("expected definition in hello.go, got %s", loc.URI)
875874
}
876875
// Confirm that we no longer have metadata when the file is saved.

0 commit comments

Comments
 (0)