Skip to content

Commit b14424d

Browse files
authored
fix(lsp): include scriptKind and isNotebookCell in document data (#29)
1 parent ed4dd59 commit b14424d

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

internal/lsp/lsproto/lsp.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"net/url"
7+
"runtime"
78
"strings"
89

910
"github.com/go-json-experiment/json"
@@ -53,6 +54,9 @@ func (uri DocumentUri) Path(useCaseSensitiveFileNames bool) tspath.Path {
5354
}
5455

5556
func fixWindowsURIPath(path string) string {
57+
if runtime.GOOS != "windows" {
58+
return path
59+
}
5660
if rest, ok := strings.CutPrefix(path, "/"); ok {
5761
if volume, rest, ok := tspath.SplitVolumePath(rest); ok {
5862
return volume + rest

internal/lsp/lsproto/lsp_generated.go

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/lsp/server.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/microsoft/typescript-go/internal/lsp/lsproto"
3232
"github.com/microsoft/typescript-go/internal/module"
3333
"github.com/microsoft/typescript-go/internal/packagejson"
34+
"github.com/microsoft/typescript-go/internal/parser"
3435
"github.com/microsoft/typescript-go/internal/project"
3536
"github.com/microsoft/typescript-go/internal/project/ata"
3637
"github.com/microsoft/typescript-go/internal/sourcemap"
@@ -215,7 +216,7 @@ func (h *DenoLanguageServiceHost) FormatOptions() *format.FormatCodeSettings {
215216

216217
func (h *DenoLanguageServiceHost) GetECMALineInfo(fileName string) *sourcemap.ECMALineInfo {
217218
if data := h.vfs.GetDocument(fileName); data != nil {
218-
return sourcemap.CreateECMALineInfo(*data.Text, data.LineStarts)
219+
return sourcemap.CreateECMALineInfo(data.Text, data.LineStarts)
219220
}
220221
return nil
221222
}
@@ -268,7 +269,7 @@ func (v *DenoVFS) GetDocument(path string) *lsproto.DenoDocumentData {
268269
}
269270
var response lsproto.DenoDocumentData
270271
err := v.server.sendRequestSync(v.ctx, lsproto.MethodDenoCallback, params, &response)
271-
if err != nil || response.Text == nil {
272+
if err != nil {
272273
return nil
273274
}
274275
if v.server.deno.documentCache != nil {
@@ -279,7 +280,7 @@ func (v *DenoVFS) GetDocument(path string) *lsproto.DenoDocumentData {
279280

280281
func (v *DenoVFS) ReadFile(path string) (contents string, ok bool) {
281282
if data := v.GetDocument(path); data != nil {
282-
return *data.Text, true
283+
return data.Text, true
283284
}
284285
return "", false
285286
}
@@ -362,7 +363,7 @@ func (h *denoAutoImportHost) Dispose() {
362363
type denoCompilerHost struct {
363364
inner compiler.CompilerHost
364365
server *Server
365-
ctx context.Context
366+
vfs *DenoVFS
366367
compilerOptionsKey string
367368
notebookUri *string
368369
}
@@ -386,7 +387,19 @@ func (h *denoCompilerHost) Trace(msg *diagnostics.Message, args ...any) {
386387
}
387388

388389
func (h *denoCompilerHost) GetSourceFile(opts ast.SourceFileParseOptions) *ast.SourceFile {
389-
return h.inner.GetSourceFile(opts)
390+
data := h.vfs.GetDocument(opts.FileName)
391+
if data == nil {
392+
return nil
393+
}
394+
scriptKind := data.ScriptKind
395+
if scriptKind == core.ScriptKindUnknown {
396+
scriptKind = core.GetScriptKindFromFileName(opts.FileName)
397+
}
398+
sourceFile := parser.ParseSourceFile(opts, data.Text, scriptKind)
399+
if data.IsNotebookCell {
400+
sourceFile.ExternalModuleIndicator = nil
401+
}
402+
return sourceFile
390403
}
391404

392405
func (h *denoCompilerHost) GetResolvedProjectReference(fileName string, path tspath.Path) *tsoptions.ParsedCommandLine {
@@ -408,7 +421,7 @@ func (h *denoCompilerHost) MakeResolver(host module.ResolutionHost, options *cor
408421
return &denoResolver{
409422
inner: baseResolver,
410423
server: h.server,
411-
ctx: h.ctx,
424+
ctx: h.vfs.ctx,
412425
compilerOptionsKey: h.compilerOptionsKey,
413426
notebookUri: h.notebookUri,
414427
}
@@ -1384,7 +1397,7 @@ func (s *Server) handleDenoRequest(ctx context.Context, params *lsproto.DenoRequ
13841397
newHost := &denoCompilerHost{
13851398
inner: baseHost,
13861399
server: s,
1387-
ctx: pe.vfs.ctx,
1400+
vfs: pe.vfs,
13881401
compilerOptionsKey: pe.compilerOptionsKey,
13891402
notebookUri: pe.notebookUri,
13901403
}
@@ -1596,7 +1609,7 @@ func (s *Server) createDenoProgramEntry(ctx context.Context, compilerOptionsKey
15961609
compilerHost := &denoCompilerHost{
15971610
inner: baseHost,
15981611
server: s,
1599-
ctx: ctx,
1612+
vfs: denoVFS,
16001613
compilerOptionsKey: compilerOptionsKey,
16011614
notebookUri: notebookUri,
16021615
}

0 commit comments

Comments
 (0)