Skip to content

Commit f114dcf

Browse files
xieyuschengopherbot
authored andcommitted
gopls/internal/protocol: refine DocumentURI Clean method and its usages
This CL tracks the comments left by Alan Donovan in CL663295, thanks again. * change protocol.Clean function to DocumentURI method * ensure the same key when access and operate viewMap * factor some logics Change-Id: Ib717705495997379ed0f872d438f238ca1308a23 Reviewed-on: https://go-review.googlesource.com/c/tools/+/678416 Auto-Submit: Alan Donovan <[email protected]> Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Alan Donovan <[email protected]>
1 parent 82ee0fd commit f114dcf

File tree

3 files changed

+18
-20
lines changed

3 files changed

+18
-20
lines changed

gopls/internal/cache/session.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ type Session struct {
6464

6565
viewMu sync.Mutex
6666
views []*View
67-
viewMap map[protocol.DocumentURI]*View // file->best view or nil; nil after shutdown
67+
viewMap map[protocol.DocumentURI]*View // file->best view or nil; nil after shutdown; the key must be a clean uri.
6868

6969
// snapshots is a counting semaphore that records the number
7070
// of unreleased snapshots associated with this session.
@@ -139,15 +139,16 @@ func (s *Session) NewView(ctx context.Context, folder *Folder) (*View, *Snapshot
139139
}
140140
view, snapshot, release := s.createView(ctx, def)
141141
s.views = append(s.views, view)
142-
s.viewMap[protocol.Clean(folder.Dir)] = view
142+
s.viewMap[folder.Dir.Clean()] = view
143143
return view, snapshot, release, nil
144144
}
145145

146146
// HasView checks whether the uri's view exists.
147147
func (s *Session) HasView(uri protocol.DocumentURI) bool {
148+
uri = uri.Clean()
148149
s.viewMu.Lock()
149150
defer s.viewMu.Unlock()
150-
_, ok := s.viewMap[protocol.Clean(uri)]
151+
_, ok := s.viewMap[uri]
151152
return ok
152153
}
153154

@@ -379,6 +380,7 @@ func (s *Session) View(id string) (*View, error) {
379380
//
380381
// On success, the caller must call the returned function to release the snapshot.
381382
func (s *Session) SnapshotOf(ctx context.Context, uri protocol.DocumentURI) (*Snapshot, func(), error) {
383+
uri = uri.Clean()
382384
// Fast path: if the uri has a static association with a view, return it.
383385
s.viewMu.Lock()
384386
v, err := s.viewOfLocked(ctx, uri)
@@ -396,7 +398,7 @@ func (s *Session) SnapshotOf(ctx context.Context, uri protocol.DocumentURI) (*Sn
396398
// View is shut down. Forget this association.
397399
s.viewMu.Lock()
398400
if s.viewMap[uri] == v {
399-
delete(s.viewMap, protocol.Clean(uri))
401+
delete(s.viewMap, uri)
400402
}
401403
s.viewMu.Unlock()
402404
}
@@ -473,7 +475,7 @@ var errNoViews = errors.New("no views")
473475
// viewOfLocked evaluates the best view for uri, memoizing its result in
474476
// s.viewMap.
475477
//
476-
// Precondition: caller holds s.viewMu lock.
478+
// Precondition: caller holds s.viewMu lock; uri must be clean.
477479
//
478480
// May return (nil, nil) if no best view can be determined.
479481
func (s *Session) viewOfLocked(ctx context.Context, uri protocol.DocumentURI) (*View, error) {
@@ -500,7 +502,7 @@ func (s *Session) viewOfLocked(ctx context.Context, uri protocol.DocumentURI) (*
500502
// (as in golang/go#60776).
501503
v = relevantViews[0]
502504
}
503-
s.viewMap[protocol.Clean(uri)] = v // may be nil
505+
s.viewMap[uri] = v // may be nil
504506
}
505507
return v, nil
506508
}
@@ -748,7 +750,7 @@ func (s *Session) ResetView(ctx context.Context, uri protocol.DocumentURI) (*Vie
748750
return nil, fmt.Errorf("session is shut down")
749751
}
750752

751-
view, err := s.viewOfLocked(ctx, uri)
753+
view, err := s.viewOfLocked(ctx, uri.Clean())
752754
if err != nil {
753755
return nil, err
754756
}

gopls/internal/protocol/uri.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (uri *DocumentURI) UnmarshalText(data []byte) (err error) {
6868
}
6969

7070
// Clean returns the cleaned uri by triggering filepath.Clean underlying.
71-
func Clean(uri DocumentURI) DocumentURI {
71+
func (uri DocumentURI) Clean() DocumentURI {
7272
return URIFromPath(filepath.Clean(uri.Path()))
7373
}
7474

gopls/internal/test/integration/fake/editor.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,11 @@ func (e *Editor) initialize(ctx context.Context) error {
327327
params.InitializationOptions = makeSettings(e.sandbox, config, nil)
328328

329329
params.WorkspaceFolders = makeWorkspaceFolders(e.sandbox, config.WorkspaceFolders, config.NoDefaultWorkspaceFiles)
330-
params.RootURI = protocol.DocumentURI(makeRootURI(e.sandbox, config.RelRootPath))
330+
params.RootURI = protocol.URIFromPath(config.RelRootPath)
331+
if !uriRE.MatchString(config.RelRootPath) { // relative file path
332+
params.RootURI = e.sandbox.Workdir.URI(config.RelRootPath)
333+
}
334+
331335
capabilities, err := clientCapabilities(config)
332336
if err != nil {
333337
return fmt.Errorf("unmarshalling EditorConfig.CapabilitiesJSON: %v", err)
@@ -447,10 +451,10 @@ var uriRE = regexp.MustCompile(`^[a-z][a-z0-9+\-.]*://\S+`)
447451
// makeWorkspaceFolders creates a slice of workspace folders to use for
448452
// this editing session, based on the editor configuration.
449453
func makeWorkspaceFolders(sandbox *Sandbox, paths []string, useEmpty bool) (folders []protocol.WorkspaceFolder) {
450-
if len(paths) == 0 && useEmpty {
451-
return nil
452-
}
453454
if len(paths) == 0 {
455+
if useEmpty {
456+
return nil
457+
}
454458
paths = []string{string(sandbox.Workdir.RelativeTo)}
455459
}
456460

@@ -468,14 +472,6 @@ func makeWorkspaceFolders(sandbox *Sandbox, paths []string, useEmpty bool) (fold
468472
return folders
469473
}
470474

471-
func makeRootURI(sandbox *Sandbox, path string) string {
472-
uri := path
473-
if !uriRE.MatchString(path) { // relative file path
474-
uri = string(sandbox.Workdir.URI(path))
475-
}
476-
return uri
477-
}
478-
479475
// onFileChanges is registered to be called by the Workdir on any writes that
480476
// go through the Workdir API. It is called synchronously by the Workdir.
481477
func (e *Editor) onFileChanges(ctx context.Context, evts []protocol.FileEvent) {

0 commit comments

Comments
 (0)