Skip to content
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
5 changes: 4 additions & 1 deletion tsc/internal/ls/documenthighlights.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ func (l *LanguageService) ProvideDocumentHighlights(ctx context.Context, documen
// Extract highlights for the current file only.
var documentHighlights []*lsproto.DocumentHighlight
if result.MultiDocumentHighlights != nil {
// Compare file paths, not raw URI strings: clients are not required to
// percent-encode the same characters we do.
documentPathKey := documentUri.PathKey(l.CaseSensitivity())
for _, mh := range *result.MultiDocumentHighlights {
if mh.Uri == documentUri {
if mh.Uri.PathKey(l.CaseSensitivity()) == documentPathKey {
documentHighlights = append(documentHighlights, mh.Highlights...)
}
}
Expand Down
80 changes: 80 additions & 0 deletions tsc/internal/lsp/server_documenthighlight_test.go
Comment thread
joshbarros marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package lsp_test

import (
"context"
"io"
"testing"

"github.com/microsoft/TypeScript/tsc/internal/bundled"
"github.com/microsoft/TypeScript/tsc/internal/lsp"
"github.com/microsoft/TypeScript/tsc/internal/lsp/lsproto"
"github.com/microsoft/TypeScript/tsc/internal/testutil/lsptestutil"
"github.com/microsoft/TypeScript/tsc/internal/tspath"
"github.com/microsoft/TypeScript/tsc/internal/vfs/vfstest"
"gotest.tools/v3/assert"
)

// TestDocumentHighlightUnescapedAtSign reproduces #64758: clients are not
// required to percent-encode every character the server encodes, and Neovim
// sends an unescaped @. The server used to filter highlights with a raw URI
// string comparison, which dropped every result for such paths.
func TestDocumentHighlightUnescapedAtSign(t *testing.T) {
t.Parallel()
if !bundled.Embedded {
t.Skip("bundled files are not embedded")
}

const file = "/home/projects/a@b/x.ts"
files := map[string]string{
"/home/projects/tsconfig.json": `{}`,
file: "const foo = 1;\nconsole.log(foo);\nconsole.log(foo);\n",
}
fs := bundled.WrapFS(vfstest.FromMap(files, tspath.CaseInsensitive))

onServerRequest := func(_ context.Context, req *lsproto.RequestMessage) *lsproto.ResponseMessage {
switch req.Method {
case lsproto.MethodClientRegisterCapability, lsproto.MethodClientUnregisterCapability, lsproto.MethodWindowWorkDoneProgressCreate:
return &lsproto.ResponseMessage{
ID: req.ID,
JSONRPC: req.JSONRPC,
Result: lsproto.Null{},
}
default:
return nil
}
}

client, closeClient := lsptestutil.NewLSPClient(t, lsp.ServerOptions{
Err: io.Discard,
Cwd: "/home/projects",
FS: fs,
DefaultLibraryPath: bundled.LibPath(),
}, onServerRequest)
t.Cleanup(func() { _ = closeClient() })

initMsg, _, ok := client.SendRequest(t, lsproto.InitializeInfo, &lsproto.InitializeParams{
Capabilities: &lsproto.ClientCapabilities{},
})
assert.Assert(t, ok && initMsg.AsResponse().Error == nil, "Initialize failed")
client.SendNotification(t, lsproto.InitializedInfo, &lsproto.InitializedParams{})
<-client.Server.InitComplete()

// Deliberately leave @ unescaped.
uri := lsproto.DocumentUri("file:///home/projects/a@b/x.ts")
client.SendNotification(t, lsproto.TextDocumentDidOpenInfo, &lsproto.DidOpenTextDocumentParams{
TextDocument: &lsproto.TextDocumentItem{
Uri: uri,
LanguageId: "typescript",
Text: files[file],
},
})

msg, highlights, ok := client.SendRequest(t, lsproto.TextDocumentDocumentHighlightInfo, &lsproto.DocumentHighlightParams{
TextDocument: lsproto.TextDocumentIdentifier{Uri: uri},
Position: lsproto.Position{Line: 0, Character: 7},
})
assert.Assert(t, ok)
assert.Assert(t, msg.AsResponse().Error == nil)
assert.Assert(t, highlights.DocumentHighlights != nil)
assert.Equal(t, len(*highlights.DocumentHighlights), 3)
}