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

LSP-conformant originalRootURI #369

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions buildserver/build_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,19 @@ func (h *BuildHandler) Handle(ctx context.Context, conn *jsonrpc2.Conn, req *jso
return nil, err
}

// Fall back to reading the `originalRootURI` from
// `InitializeParams.intitializationOptions` if absent from the
// `InitializeParams`. This makes go-langserver more LSP-compliant so that
// clients don't need to go outside of the LSP specification to communicate
// with it.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this is still not really compliant from a semantics perspective because initializationOptions is intended to be options you would pass through CLI arguments, i.e. not values that change for every workspace.

if params.OriginalRootURI == "" {
if initializationOptions, ok := params.InitializationOptions.(map[string]interface{}); ok {
if originalRootURI, ok := initializationOptions["originalRootURI"].(string); ok {
params.OriginalRootURI = lsp.DocumentURI(originalRootURI)
}
}
}

if Debug {
var b []byte
if req.Params != nil {
Expand Down
68 changes: 52 additions & 16 deletions buildserver/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,33 @@ import (
"github.com/sourcegraph/jsonrpc2"
)

type OriginalRootURIParent int

const (
parentInitializeParams OriginalRootURIParent = iota
parentInitializationOptions
)

func TestProxy(t *testing.T) {
if testing.Short() {
t.Skip()
}

tests := map[string]struct {
rootURI lsp.DocumentURI
mode string
fs map[string]string
wantHover map[string]string
wantDefinition map[string]string
wantXDefinition map[string]string
wantReferences map[string][]string
wantSymbols map[string][]string
wantXDependencies string
wantXReferences map[*lsext.WorkspaceReferencesParams][]string
wantXPackages []string
depFS map[string]map[string]string // dep clone URL -> map VFS
rootURI lsp.DocumentURI
mode string
// defaults to "intializeParams"
originalRootURIParent OriginalRootURIParent
fs map[string]string
wantHover map[string]string
wantDefinition map[string]string
wantXDefinition map[string]string
wantReferences map[string][]string
wantSymbols map[string][]string
wantXDependencies string
wantXReferences map[*lsext.WorkspaceReferencesParams][]string
wantXPackages []string
depFS map[string]map[string]string // dep clone URL -> map VFS
}{
"go basic": {
rootURI: "git://test/pkg?deadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
Expand Down Expand Up @@ -569,6 +578,17 @@ func yza() {}
"is:exported": []string{"git://test/pkg?deadbeefdeadbeefdeadbeefdeadbeefdeadbeef#abc.go:class:XYZ:2:5", "git://test/pkg?deadbeefdeadbeefdeadbeefdeadbeefdeadbeef#bcd.go:class:YZA:2:5", "git://test/pkg?deadbeefdeadbeefdeadbeefdeadbeefdeadbeef#abc.go:method:XYZ.ABC:4:13", "git://test/pkg?deadbeefdeadbeefdeadbeefdeadbeefdeadbeef#bcd.go:method:YZA.BCD:4:13"},
},
},
"originalRootURI in initalizationOptions instead of initializeParams": {
rootURI: "git://test/pkg?deadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
originalRootURIParent: parentInitializationOptions,
mode: "go",
fs: map[string]string{
"a.go": "package p; func A() { A() }",
},
wantHover: map[string]string{
"a.go:1:17": "func A()",
},
},
}
for label, test := range tests {
t.Run(label, func(t *testing.T) {
Expand Down Expand Up @@ -610,10 +630,26 @@ func yza() {}
defer done()

// Prepare the connection.
if err := c.Call(ctx, "initialize", lspext.InitializeParams{
InitializeParams: lsp.InitializeParams{RootURI: "file:///"},
OriginalRootURI: test.rootURI,
}, nil); err != nil {
var initializeParams lspext.InitializeParams
switch test.originalRootURIParent {
case parentInitializeParams:
initializeParams = lspext.InitializeParams{
InitializeParams: lsp.InitializeParams{RootURI: "file:///"},
OriginalRootURI: test.rootURI,
}
case parentInitializationOptions:
initializeParams = lspext.InitializeParams{
InitializeParams: lsp.InitializeParams{
RootURI: "file:///",
InitializationOptions: map[string]string{
"originalRootURI": string(test.rootURI),
},
},
}
default:
t.Fatalf("invalid originalRootURIParent: %d", test.originalRootURIParent)
}
if err := c.Call(ctx, "initialize", initializeParams, nil); err != nil {
t.Fatal("initialize:", err)
}

Expand Down
2 changes: 1 addition & 1 deletion langserver/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ func packageForFile(pkgs map[string]*ast.Package, filename string) (string, *ast
}
}
}
return "", nil, fmt.Errorf("failed to find %q in packages %q", filename, pkgs)
return "", nil, fmt.Errorf("failed to find %q in packages %+v", filename, pkgs)
}

// inRange tells if x is in the range of a-b inclusive.
Expand Down