Skip to content

Commit 8188edd

Browse files
authored
Move version from source file to document registry (#1075)
1 parent 4822b79 commit 8188edd

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

internal/ast/ast.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9998,10 +9998,6 @@ type SourceFile struct {
99989998
lineMapMu sync.RWMutex
99999999
lineMap []core.TextPos
1000010000

10001-
// Fields set by document registry
10002-
10003-
Version int
10004-
1000510001
// Fields set by language service
1000610002

1000710003
tokenCacheMu sync.Mutex

internal/project/documentregistry.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func newRegistryKey(options *core.CompilerOptions, path tspath.Path, scriptKind
2727

2828
type registryEntry struct {
2929
sourceFile *ast.SourceFile
30+
version int
3031
refCount int
3132
mu sync.Mutex
3233
}
@@ -94,22 +95,22 @@ func (r *DocumentRegistry) getDocumentWorker(
9495
if entry, ok := r.documents.Load(key); ok {
9596
// We have an entry for this file. However, it may be for a different version of
9697
// the script snapshot. If so, update it appropriately.
97-
if entry.sourceFile.Version != scriptInfoVersion {
98+
if entry.version != scriptInfoVersion {
9899
sourceFile := parser.ParseSourceFile(scriptInfo.fileName, scriptInfo.path, scriptInfoText, scriptTarget, scanner.JSDocParsingModeParseAll)
99-
sourceFile.Version = scriptInfoVersion
100100
entry.mu.Lock()
101101
defer entry.mu.Unlock()
102102
entry.sourceFile = sourceFile
103+
entry.version = scriptInfoVersion
103104
}
104105
entry.refCount++
105106
return entry.sourceFile
106107
} else {
107108
// Have never seen this file with these settings. Create a new source file for it.
108109
sourceFile := parser.ParseSourceFile(scriptInfo.fileName, scriptInfo.path, scriptInfoText, scriptTarget, scanner.JSDocParsingModeParseAll)
109-
sourceFile.Version = scriptInfoVersion
110110
entry, _ := r.documents.LoadOrStore(key, &registryEntry{
111111
sourceFile: sourceFile,
112112
refCount: 0,
113+
version: scriptInfoVersion,
113114
})
114115
entry.mu.Lock()
115116
defer entry.mu.Unlock()
@@ -118,6 +119,14 @@ func (r *DocumentRegistry) getDocumentWorker(
118119
}
119120
}
120121

122+
func (r *DocumentRegistry) getFileVersion(file *ast.SourceFile, options *core.CompilerOptions) int {
123+
key := newRegistryKey(options, file.Path(), file.ScriptKind)
124+
if entry, ok := r.documents.Load(key); ok && entry.sourceFile == file {
125+
return entry.version
126+
}
127+
return -1
128+
}
129+
121130
// size should only be used for testing.
122131
func (r *DocumentRegistry) size() int {
123132
return r.documents.Size()

internal/project/project.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type snapshot struct {
4848
func (s *snapshot) GetLineMap(fileName string) *ls.LineMap {
4949
file := s.program.GetSourceFile(fileName)
5050
scriptInfo := s.project.host.GetScriptInfoByPath(file.Path())
51-
if file.Version == scriptInfo.Version() {
51+
if s.project.getFileVersion(file, s.program.Options()) == scriptInfo.Version() {
5252
return scriptInfo.LineMap()
5353
}
5454
return ls.ComputeLineStarts(file.Text())
@@ -1015,12 +1015,13 @@ func (p *Project) print(writeFileNames bool, writeFileExplanation bool, writeFil
10151015
builder.WriteString("\n\tFiles (0) NoProgram\n")
10161016
} else {
10171017
sourceFiles := p.program.GetSourceFiles()
1018+
options := p.program.Options()
10181019
builder.WriteString(fmt.Sprintf("\n\tFiles (%d)\n", len(sourceFiles)))
10191020
if writeFileNames {
10201021
for _, sourceFile := range sourceFiles {
10211022
builder.WriteString("\n\t\t" + sourceFile.FileName())
10221023
if writeFileVersionAndText {
1023-
builder.WriteString(fmt.Sprintf(" %d %s", sourceFile.Version, sourceFile.Text()))
1024+
builder.WriteString(fmt.Sprintf(" %d %s", p.getFileVersion(sourceFile, options), sourceFile.Text()))
10241025
}
10251026
}
10261027
// !!!
@@ -1031,6 +1032,10 @@ func (p *Project) print(writeFileNames bool, writeFileExplanation bool, writeFil
10311032
return builder.String()
10321033
}
10331034

1035+
func (p *Project) getFileVersion(file *ast.SourceFile, options *core.CompilerOptions) int {
1036+
return p.host.DocumentRegistry().getFileVersion(file, options)
1037+
}
1038+
10341039
func (p *Project) Log(s string) {
10351040
p.host.Log(s)
10361041
}

0 commit comments

Comments
 (0)