Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit f1f8c3d

Browse files
committed
Do not parse OriginalRootURI so often
1 parent 0273dd1 commit f1f8c3d

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

buildserver/build_server.go

+16-11
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,11 @@ type BuildHandler struct {
7777
findPkg map[findPkgKey]*findPkgValue
7878
langserver.HandlerCommon
7979
*langserver.HandlerShared
80-
init *lspext.InitializeParams // set by "initialize" request
81-
rootImportPath string // root import path of the workspace (e.g., "github.com/foo/bar")
82-
cachingClient *http.Client // http.Client with a cache backed by an in-memory LRU cache
83-
closers []io.Closer // values to dispose of when Close() is called
80+
init *lspext.InitializeParams // set by "initialize" request
81+
originalRootURI *url.URL // derived from InitializeParams.OriginalRootURI
82+
rootImportPath string // root import path of the workspace (e.g., "github.com/foo/bar")
83+
cachingClient *http.Client // http.Client with a cache backed by an in-memory LRU cache
84+
closers []io.Closer // values to dispose of when Close() is called
8485
// Whether URIs in the same workspace begin with:
8586
// - `file://` (true)
8687
// - `git://` (false)
@@ -101,6 +102,11 @@ func (h *BuildHandler) reset(init *lspext.InitializeParams, conn *jsonrpc2.Conn,
101102
return err
102103
}
103104
h.init = init
105+
var err error
106+
h.originalRootURI, err = url.Parse(string(h.init.OriginalRootURI))
107+
if h.init.OriginalRootURI == "" || err != nil {
108+
h.originalRootURI = nil
109+
}
104110
// 100 MiB cache, no age-based eviction
105111
h.cachingClient = &http.Client{Transport: httpcache.NewTransport(lrucache.New(100*1024*1024, 0))}
106112
h.depURLMutex = newKeyMutex()
@@ -360,11 +366,10 @@ func (h *BuildHandler) Handle(ctx context.Context, conn *jsonrpc2.Conn, req *jso
360366
if err != nil {
361367
return uri
362368
}
363-
originalRootURI, err := url.Parse(string(h.init.OriginalRootURI))
364-
if err != nil {
369+
if h.originalRootURI == nil {
365370
return uri
366371
}
367-
if currentURL.Hostname() == originalRootURI.Hostname() && currentURL.RawPath == originalRootURI.RawPath {
372+
if currentURL.Hostname() == h.originalRootURI.Hostname() && currentURL.RawPath == h.originalRootURI.RawPath {
368373
path = currentURL.Fragment
369374
} else {
370375
return uri // refers to a resource outside of this workspace
@@ -520,10 +525,10 @@ func (h *BuildHandler) rewriteURIFromLangServer(uri lsp.DocumentURI) (lsp.Docume
520525
// analyzing).
521526
return lsp.DocumentURI("file:///" + fileInGoStdlib), nil
522527
}
523-
newURI, err := url.Parse(string(h.init.OriginalRootURI))
524-
if err != nil {
528+
if h.originalRootURI == nil {
525529
return uri, nil
526530
}
531+
newURI, _ := url.Parse(h.originalRootURI.String())
527532
newURI.Fragment = fileInGoStdlib
528533
return lsp.DocumentURI(newURI.String()), nil
529534
}
@@ -536,10 +541,10 @@ func (h *BuildHandler) rewriteURIFromLangServer(uri lsp.DocumentURI) (lsp.Docume
536541
pathInThisWorkspace := util.PathTrimPrefix(u.Path, h.RootFSPath)
537542
return lsp.DocumentURI("file:///" + pathInThisWorkspace), nil
538543
}
539-
newURI, err := url.Parse(string(h.init.OriginalRootURI))
540-
if err != nil {
544+
if h.originalRootURI == nil {
541545
return uri, nil
542546
}
547+
newURI, _ := url.Parse(h.originalRootURI.String())
543548
newURI.Fragment = util.PathTrimPrefix(u.Path, h.RootFSPath)
544549
return lsp.DocumentURI(newURI.String()), nil
545550
}

0 commit comments

Comments
 (0)