Skip to content

Commit 86d2109

Browse files
authored
Refactor some of the code with lsp and api layer to use DocumentStore directly now that it exists (#1218)
1 parent d561370 commit 86d2109

File tree

9 files changed

+120
-183
lines changed

9 files changed

+120
-183
lines changed

internal/api/api.go

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ func (api *API) TypingsInstaller() *project.TypingsInstaller {
8181
return nil
8282
}
8383

84-
// DocumentRegistry implements ProjectHost.
85-
func (api *API) DocumentRegistry() *project.DocumentRegistry {
86-
return api.documentStore.DocumentRegistry()
84+
// DocumentStore implements ProjectHost.
85+
func (api *API) DocumentStore() *project.DocumentStore {
86+
return api.documentStore
8787
}
8888

8989
// ConfigFileRegistry implements ProjectHost.
@@ -101,21 +101,6 @@ func (api *API) GetCurrentDirectory() string {
101101
return api.host.GetCurrentDirectory()
102102
}
103103

104-
// GetOrCreateScriptInfoForFile implements ProjectHost.
105-
func (api *API) GetOrCreateScriptInfoForFile(fileName string, path tspath.Path, scriptKind core.ScriptKind) *project.ScriptInfo {
106-
return api.getOrCreateScriptInfo(fileName, path, scriptKind)
107-
}
108-
109-
// GetScriptInfoByPath implements ProjectHost.
110-
func (api *API) GetScriptInfoByPath(path tspath.Path) *project.ScriptInfo {
111-
return api.documentStore.GetScriptInfoByPath(path)
112-
}
113-
114-
// OnDiscoveredSymlink implements ProjectHost.
115-
func (api *API) OnDiscoveredSymlink(info *project.ScriptInfo) {
116-
api.documentStore.AddRealpathMapping(info)
117-
}
118-
119104
// Log implements ProjectHost.
120105
func (api *API) Log(s string) {
121106
api.options.Logger.Info(s)
@@ -372,10 +357,6 @@ func (api *API) releaseHandle(handle string) error {
372357
return nil
373358
}
374359

375-
func (api *API) getOrCreateScriptInfo(fileName string, path tspath.Path, scriptKind core.ScriptKind) *project.ScriptInfo {
376-
return api.documentStore.GetOrCreateScriptInfo(fileName, path, scriptKind, api.host.FS())
377-
}
378-
379360
func (api *API) toAbsoluteFileName(fileName string) string {
380361
return tspath.GetNormalizedAbsolutePath(fileName, api.host.GetCurrentDirectory())
381362
}

internal/compiler/fileloader.go

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ func (p *fileLoader) resolveImportsAndModuleAugmentations(file *ast.SourceFile,
382382
continue
383383
}
384384

385-
mode := getModeForUsageLocation(file.FileName(), meta, entry, module.GetCompilerOptionsWithRedirect(p.opts.Config.CompilerOptions(), redirect))
385+
mode := getModeForUsageLocation(file.FileName(), meta, entry, optionsForFile)
386386
resolvedModule := p.resolver.ResolveModuleName(moduleName, file.FileName(), mode, redirect)
387387
resolutionsInFile[module.ModeAwareCacheKey{Name: moduleName, Mode: mode}] = resolvedModule
388388

@@ -474,25 +474,6 @@ func getResolutionDiagnostic(options *core.CompilerOptions, resolvedModule *modu
474474
}
475475
}
476476

477-
func (p *fileLoader) resolveModuleNames(entries []*ast.Node, file *ast.SourceFile, meta ast.SourceFileMetaData, redirect *tsoptions.ParsedCommandLine) []*resolution {
478-
if len(entries) == 0 {
479-
return nil
480-
}
481-
482-
resolvedModules := make([]*resolution, 0, len(entries))
483-
484-
for _, entry := range entries {
485-
moduleName := entry.Text()
486-
if moduleName == "" {
487-
continue
488-
}
489-
resolvedModule := p.resolver.ResolveModuleName(moduleName, file.FileName(), getModeForUsageLocation(file.FileName(), meta, entry, module.GetCompilerOptionsWithRedirect(p.opts.Config.CompilerOptions(), redirect)), redirect)
490-
resolvedModules = append(resolvedModules, &resolution{node: entry, resolvedModule: resolvedModule})
491-
}
492-
493-
return resolvedModules
494-
}
495-
496477
func (p *fileLoader) createSyntheticImport(text string, file *ast.SourceFile) *ast.Node {
497478
p.factoryMu.Lock()
498479
defer p.factoryMu.Unlock()

internal/project/project.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type snapshot struct {
4747
// GetLineMap implements ls.Host.
4848
func (s *snapshot) GetLineMap(fileName string) *ls.LineMap {
4949
file := s.program.GetSourceFile(fileName)
50-
scriptInfo := s.project.host.GetScriptInfoByPath(file.Path())
50+
scriptInfo := s.project.host.DocumentStore().GetScriptInfoByPath(file.Path())
5151
if s.project.getFileVersion(file) == scriptInfo.Version() {
5252
return scriptInfo.LineMap()
5353
}
@@ -80,11 +80,8 @@ type ProjectHost interface {
8080
NewLine() string
8181
DefaultLibraryPath() string
8282
TypingsInstaller() *TypingsInstaller
83-
DocumentRegistry() *DocumentRegistry
83+
DocumentStore() *DocumentStore
8484
ConfigFileRegistry() *ConfigFileRegistry
85-
GetScriptInfoByPath(path tspath.Path) *ScriptInfo
86-
GetOrCreateScriptInfoForFile(fileName string, path tspath.Path, scriptKind core.ScriptKind) *ScriptInfo
87-
OnDiscoveredSymlink(info *ScriptInfo)
8885
Log(s string)
8986
PositionEncoding() lsproto.PositionEncodingKind
9087

@@ -280,7 +277,7 @@ func (p *Project) GetSourceFile(opts ast.SourceFileParseOptions) *ast.SourceFile
280277
if p.program != nil {
281278
oldSourceFile = p.program.GetSourceFileByPath(scriptInfo.path)
282279
}
283-
return p.host.DocumentRegistry().AcquireDocument(scriptInfo, opts, oldSourceFile)
280+
return p.host.DocumentStore().documentRegistry.AcquireDocument(scriptInfo, opts, oldSourceFile)
284281
}
285282
return nil
286283
}
@@ -418,7 +415,7 @@ func (p *Project) onWatchEventForNilScriptInfo(fileName string) {
418415
}
419416

420417
func (p *Project) getOrCreateScriptInfoAndAttachToProject(fileName string, scriptKind core.ScriptKind) *ScriptInfo {
421-
if scriptInfo := p.host.GetOrCreateScriptInfoForFile(fileName, p.toPath(fileName), scriptKind); scriptInfo != nil {
418+
if scriptInfo := p.host.DocumentStore().getOrCreateScriptInfoWorker(fileName, p.toPath(fileName), scriptKind, false, "", false, p.host.FS()); scriptInfo != nil {
422419
scriptInfo.attachToProject(p)
423420
return scriptInfo
424421
}
@@ -519,7 +516,7 @@ func (p *Project) updateGraph() (*compiler.Program, bool) {
519516
if oldProgram != nil {
520517
for _, oldSourceFile := range oldProgram.GetSourceFiles() {
521518
if p.program.GetSourceFileByPath(oldSourceFile.Path()) == nil {
522-
p.host.DocumentRegistry().ReleaseDocument(oldSourceFile)
519+
p.host.DocumentStore().documentRegistry.ReleaseDocument(oldSourceFile)
523520
p.detachScriptInfoIfNotInferredRoot(oldSourceFile.Path())
524521
}
525522
}
@@ -1017,7 +1014,7 @@ func (p *Project) print(writeFileNames bool, writeFileExplanation bool, writeFil
10171014
}
10181015

10191016
func (p *Project) getFileVersion(file *ast.SourceFile) int {
1020-
return p.host.DocumentRegistry().getFileVersion(file)
1017+
return p.host.DocumentStore().documentRegistry.getFileVersion(file)
10211018
}
10221019

10231020
func (p *Project) Log(s string) {
@@ -1031,7 +1028,7 @@ func (p *Project) Logf(format string, args ...interface{}) {
10311028
func (p *Project) detachScriptInfoIfNotInferredRoot(path tspath.Path) {
10321029
// We might not find the script info in case its not associated with the project any more
10331030
// and project graph was not updated (eg delayed update graph in case of files changed/deleted on the disk)
1034-
if scriptInfo := p.host.GetScriptInfoByPath(path); scriptInfo != nil &&
1031+
if scriptInfo := p.host.DocumentStore().GetScriptInfoByPath(path); scriptInfo != nil &&
10351032
(p.kind != KindInferred || !p.isRoot(scriptInfo)) {
10361033
scriptInfo.detachFromProject(p)
10371034
}
@@ -1043,7 +1040,7 @@ func (p *Project) Close() {
10431040

10441041
if p.program != nil {
10451042
for _, sourceFile := range p.program.GetSourceFiles() {
1046-
p.host.DocumentRegistry().ReleaseDocument(sourceFile)
1043+
p.host.DocumentStore().documentRegistry.ReleaseDocument(sourceFile)
10471044
// Detach script info if its not root or is root of non inferred project
10481045
p.detachScriptInfoIfNotInferredRoot(sourceFile.Path())
10491046
}
@@ -1059,7 +1056,7 @@ func (p *Project) Close() {
10591056
if p.kind == KindInferred {
10601057
// Release root script infos for inferred projects.
10611058
for path := range p.rootFileNames.Keys() {
1062-
if info := p.host.GetScriptInfoByPath(path); info != nil {
1059+
if info := p.host.DocumentStore().GetScriptInfoByPath(path); info != nil {
10631060
info.detachFromProject(p)
10641061
}
10651062
}

0 commit comments

Comments
 (0)