Skip to content
Merged
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
20 changes: 20 additions & 0 deletions internal/lsp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ var handlers = sync.OnceValue(func() handlerMap {
registerNotificationHandler(handlers, lsproto.TextDocumentDidSaveInfo, (*Server).handleDidSave)
registerNotificationHandler(handlers, lsproto.TextDocumentDidCloseInfo, (*Server).handleDidClose)
registerNotificationHandler(handlers, lsproto.WorkspaceDidChangeWatchedFilesInfo, (*Server).handleDidChangeWatchedFiles)
registerNotificationHandler(handlers, lsproto.SetTraceInfo, (*Server).handleSetTrace)

registerLanguageServiceDocumentRequestHandler(handlers, lsproto.TextDocumentDiagnosticInfo, (*Server).handleDocumentDiagnostic)
registerLanguageServiceDocumentRequestHandler(handlers, lsproto.TextDocumentHoverInfo, (*Server).handleHover)
Expand Down Expand Up @@ -557,6 +558,10 @@ func (s *Server) handleInitialize(ctx context.Context, params *lsproto.Initializ
s.locale = locale
}

if s.initializeParams.Trace != nil && *s.initializeParams.Trace == "verbose" {
s.logger.SetVerbose(true)
}

response := &lsproto.InitializeResult{
ServerInfo: &lsproto.ServerInfo{
Name: "typescript-go",
Expand Down Expand Up @@ -686,6 +691,21 @@ func (s *Server) handleDidChangeWatchedFiles(ctx context.Context, params *lsprot
return nil
}

func (s *Server) handleSetTrace(ctx context.Context, params *lsproto.SetTraceParams) error {
switch params.Value {
case "verbose":
s.logger.SetVerbose(true)
case "messages":
s.logger.SetVerbose(false)
case "off":
// !!! logging cannot be completely turned off for now
s.logger.SetVerbose(false)
default:
return fmt.Errorf("unknown trace value: %s", params.Value)
}
return nil
}

func (s *Server) handleDocumentDiagnostic(ctx context.Context, ls *ls.LanguageService, params *lsproto.DocumentDiagnosticParams) (lsproto.DocumentDiagnosticResponse, error) {
return ls.ProvideDiagnostics(ctx, params.TextDocument.Uri)
}
Expand Down
2 changes: 2 additions & 0 deletions internal/project/ata/ata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ func TestATA(t *testing.T) {
Type: lsproto.FileChangeTypeChanged,
Uri: lsproto.DocumentUri("file:///user/username/projects/project/package.json"),
}})
// diagnostics refresh triggered - simulate by getting the language service
_, _ = session.GetLanguageService(context.Background(), uri)
session.WaitForBackgroundTasks()

calls = utils.NpmExecutor().NpmInstallCalls()
Expand Down
30 changes: 29 additions & 1 deletion internal/project/compilerhost.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"time"

"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/collections"
"github.com/microsoft/typescript-go/internal/compiler"
"github.com/microsoft/typescript-go/internal/ls"
"github.com/microsoft/typescript-go/internal/project/logging"
Expand All @@ -22,24 +23,49 @@ type compilerHost struct {
fs *snapshotFSBuilder
compilerFS *compilerFS
configFileRegistry *ConfigFileRegistry
seenFiles *collections.SyncSet[tspath.Path]

project *Project
builder *projectCollectionBuilder
logger *logging.LogTree
}

type builderFileSource struct {
seenFiles *collections.SyncSet[tspath.Path]
snapshotFSBuilder *snapshotFSBuilder
}

func (c *builderFileSource) GetFile(fileName string) FileHandle {
path := c.snapshotFSBuilder.toPath(fileName)
c.seenFiles.Add(path)
return c.snapshotFSBuilder.GetFileByPath(fileName, path)
}

func (c *builderFileSource) FS() vfs.FS {
return c.snapshotFSBuilder.FS()
}

func newCompilerHost(
currentDirectory string,
project *Project,
builder *projectCollectionBuilder,
logger *logging.LogTree,
) *compilerHost {
seenFiles := &collections.SyncSet[tspath.Path]{}
compilerFS := &compilerFS{
source: &builderFileSource{
seenFiles: seenFiles,
snapshotFSBuilder: builder.fs,
},
}

return &compilerHost{
configFilePath: project.configFilePath,
currentDirectory: currentDirectory,
sessionOptions: builder.sessionOptions,

compilerFS: &compilerFS{source: builder.fs},
compilerFS: compilerFS,
seenFiles: seenFiles,

fs: builder.fs,
project: project,
Expand Down Expand Up @@ -88,6 +114,7 @@ func (c *compilerHost) GetResolvedProjectReference(fileName string, path tspath.
if c.builder == nil {
return c.configFileRegistry.GetConfig(path)
} else {
c.seenFiles.Add(path)
return c.builder.configFileRegistryBuilder.acquireConfigForProject(fileName, path, c.project, c.logger)
}
}
Expand All @@ -97,6 +124,7 @@ func (c *compilerHost) GetResolvedProjectReference(fileName string, path tspath.
// be a corresponding release for each call made.
func (c *compilerHost) GetSourceFile(opts ast.SourceFileParseOptions) *ast.SourceFile {
c.ensureAlive()
c.seenFiles.Add(opts.Path)
if fh := c.fs.GetFileByPath(opts.FileName, opts.Path); fh != nil {
return c.builder.parseCache.Acquire(fh, opts, fh.Kind())
}
Expand Down
2 changes: 0 additions & 2 deletions internal/project/filechange.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ type FileChangeSummary struct {
Created collections.Set[lsproto.DocumentUri]
// Only set when file watching is enabled
Deleted collections.Set[lsproto.DocumentUri]

IncludesWatchChangesOnly bool
}

func (f FileChangeSummary) IsEmpty() bool {
Expand Down
5 changes: 0 additions & 5 deletions internal/project/overlayfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ func (fs *overlayFS) processChanges(changes []FileChange) (FileChangeSummary, ma
fs.mu.Lock()
defer fs.mu.Unlock()

var includesNonWatchChange bool
var result FileChangeSummary
newOverlays := maps.Clone(fs.overlays)

Expand Down Expand Up @@ -283,7 +282,6 @@ func (fs *overlayFS) processChanges(changes []FileChange) (FileChangeSummary, ma
if result.Opened != "" {
panic("can only process one file open event at a time")
}
includesNonWatchChange = true
result.Opened = uri
newOverlays[path] = newOverlay(
uri.FileName(),
Expand All @@ -295,7 +293,6 @@ func (fs *overlayFS) processChanges(changes []FileChange) (FileChangeSummary, ma
}

if events.closeChange != nil {
includesNonWatchChange = true
if result.Closed == nil {
result.Closed = make(map[lsproto.DocumentUri]xxh3.Uint128)
}
Expand All @@ -316,7 +313,6 @@ func (fs *overlayFS) processChanges(changes []FileChange) (FileChangeSummary, ma
}

if len(events.changes) > 0 {
includesNonWatchChange = true
result.Changed.Add(uri)
if o == nil {
panic("overlay not found for changed file: " + uri)
Expand Down Expand Up @@ -361,6 +357,5 @@ func (fs *overlayFS) processChanges(changes []FileChange) (FileChangeSummary, ma
}

fs.overlays = newOverlays
result.IncludesWatchChangesOnly = !includesNonWatchChange
return result, newOverlays
}
4 changes: 4 additions & 0 deletions internal/project/projectcollectionbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -772,12 +772,16 @@ func (b *projectCollectionBuilder) updateProgram(entry dirty.Value[*Project], lo
}
if updateProgram {
entry.Change(func(project *Project) {
oldHost := project.host
project.host = newCompilerHost(project.currentDirectory, project, b, logger.Fork("CompilerHost"))
result := project.CreateProgram()
project.Program = result.Program
project.checkerPool = result.CheckerPool
project.ProgramUpdateKind = result.UpdateKind
project.ProgramLastUpdate = b.newSnapshotID
if result.UpdateKind == ProgramUpdateKindCloned {
project.host.seenFiles = oldHost.seenFiles
}
if result.UpdateKind == ProgramUpdateKindNewFiles {
filesChanged = true
if b.sessionOptions.WatchEnabled {
Expand Down
Loading
Loading