Skip to content

Commit 12c0e93

Browse files
committed
Create ancestor tree
1 parent 5470a9d commit 12c0e93

File tree

31 files changed

+773
-60
lines changed

31 files changed

+773
-60
lines changed

internal/project/project.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ type Project struct {
6868
ProgramUpdateKind ProgramUpdateKind
6969
// The ID of the snapshot that created the program stored in this project.
7070
ProgramLastUpdate uint64
71+
// Set of projects that this project could be referencing.
72+
// Only set before actually loading config file to get actual project references
73+
PotentialProjectReferences *collections.Set[tspath.Path]
7174

7275
programFilesWatch *WatchedFiles[PatternsAndIgnored]
7376
failedLookupsWatch *WatchedFiles[map[tspath.Path]string]
@@ -224,6 +227,7 @@ func (p *Project) Clone() *Project {
224227
Program: p.Program,
225228
ProgramUpdateKind: ProgramUpdateKindNone,
226229
ProgramLastUpdate: p.ProgramLastUpdate,
230+
PotentialProjectReferences: p.PotentialProjectReferences,
227231

228232
programFilesWatch: p.programFilesWatch,
229233
failedLookupsWatch: p.failedLookupsWatch,
@@ -271,6 +275,15 @@ func (p *Project) getCommandLineWithTypingsFiles() *tsoptions.ParsedCommandLine
271275
return p.commandLineWithTypingsFiles
272276
}
273277

278+
func (p *Project) setPotentialProjectReference(configFilePath tspath.Path) {
279+
if p.PotentialProjectReferences == nil {
280+
p.PotentialProjectReferences = &collections.Set[tspath.Path]{}
281+
} else {
282+
p.PotentialProjectReferences = p.PotentialProjectReferences.Clone()
283+
}
284+
p.PotentialProjectReferences.Add(configFilePath)
285+
}
286+
274287
type CreateProgramResult struct {
275288
Program *compiler.Program
276289
UpdateKind ProgramUpdateKind

internal/project/projectcollectionbuilder.go

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -506,25 +506,52 @@ func (b *ProjectCollectionBuilder) findDefaultConfiguredProject(fileName string,
506506
func (b *ProjectCollectionBuilder) ensureConfiguredProjectAndAncestorsForFile(fileName string, path tspath.Path, logger *logging.LogTree) searchResult {
507507
result := b.findOrCreateDefaultConfiguredProjectForFile(fileName, path, projectLoadKindCreate, logger)
508508
if result.project != nil && b.openedFiles.Has(path) {
509-
// !!! sheetal todo this later
510-
// // Create ancestor tree for findAllRefs (dont load them right away)
511-
// forEachAncestorProjectLoad(
512-
// info,
513-
// tsconfigProject!,
514-
// ancestor => {
515-
// seenProjects.set(ancestor.project, kind);
516-
// },
517-
// kind,
518-
// `Creating project possibly referencing default composite project ${defaultProject.getProjectName()} of open file ${info.fileName}`,
519-
// allowDeferredClosed,
520-
// reloadedProjects,
521-
// /*searchOnlyPotentialSolution*/ true,
522-
// delayReloadedConfiguredProjects,
523-
// );
509+
// TODO!!! sheetal - keep these alive as well along with default projects
510+
// !!! sheetal we want to keep referenced projects if any as well alive so they can be used for FAR later
511+
b.createAncestorTree(fileName, path, &result, logger)
524512
}
525513
return result
526514
}
527515

516+
func (b *ProjectCollectionBuilder) createAncestorTree(fileName string, path tspath.Path, openResult *searchResult, logger *logging.LogTree) {
517+
project := openResult.project.Value()
518+
for {
519+
// Skip if project is not composite and we are only looking for solution
520+
if project.CommandLine != nil &&
521+
(!project.CommandLine.CompilerOptions().Composite.IsTrue() ||
522+
project.CommandLine.CompilerOptions().DisableSolutionSearching.IsTrue()) {
523+
return
524+
}
525+
526+
// Get config file name
527+
ancestorConfigName := b.configFileRegistryBuilder.getAncestorConfigFileName(fileName, path, project.configFileName, logger)
528+
if ancestorConfigName == "" {
529+
return
530+
}
531+
532+
// find or delay load the project
533+
ancestorPath := b.toPath(ancestorConfigName)
534+
ancestor := b.findOrCreateProject(ancestorConfigName, ancestorPath, projectLoadKindCreate, logger)
535+
if ancestor == nil {
536+
return
537+
}
538+
539+
openResult.retain.Add(ancestorPath)
540+
541+
// If this ancestor is new and was not updated because we are just creating it for future loading
542+
// eg when invoking find all references or rename that could span multiple projects
543+
// we would make the current project as its potential project reference
544+
if ancestor.Value().CommandLine == nil &&
545+
(project.CommandLine == nil || project.CommandLine.CompilerOptions().Composite.IsTrue()) {
546+
ancestor.Change(func(ancestorProject *Project) {
547+
ancestorProject.setPotentialProjectReference(project.configFilePath)
548+
})
549+
}
550+
551+
project = ancestor.Value()
552+
}
553+
}
554+
528555
type searchNode struct {
529556
configFileName string
530557
loadKind projectLoadKind
@@ -830,6 +857,7 @@ func (b *ProjectCollectionBuilder) updateProgram(entry dirty.Value[*Project], lo
830857
entry.Change(func(p *Project) {
831858
p.CommandLine = commandLine
832859
p.commandLineWithTypingsFiles = nil
860+
p.PotentialProjectReferences = nil
833861
})
834862
}
835863
}

internal/project/projectcollectionbuilder_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,11 @@ func TestProjectCollectionBuilder(t *testing.T) {
320320
session.DidOpenFile(context.Background(), uri, 1, content, lsproto.LanguageKindTypeScript)
321321
snapshot, release := session.Snapshot()
322322
defer release()
323-
assert.Equal(t, len(snapshot.ProjectCollection.Projects()), 1)
323+
assert.Equal(t, len(snapshot.ProjectCollection.Projects()), 2)
324324
demoProject := snapshot.ProjectCollection.ConfiguredProject(tspath.Path("/home/src/projects/project/demos/tsconfig.json"))
325325
assert.Assert(t, demoProject != nil)
326+
solutionProject := snapshot.ProjectCollection.ConfiguredProject(tspath.Path("/home/src/projects/project/tsconfig.json"))
327+
assert.Assert(t, solutionProject != nil)
326328

327329
// Verify the default project is the demos project (not the app project that excludes demos files)
328330
defaultProject := snapshot.GetDefaultProject(uri)

testdata/baselines/reference/lspservertests/declarationMaps/findAllReferences-starting-at-definition.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ Config::
109109
Config File Names::
110110
[/home/src/projects/project/a/a.ts] *new*
111111
NearestConfigFileName: /home/src/projects/project/a/tsconfig.json
112+
Ancestors:
113+
/home/src/projects/project/a/tsconfig.json
112114
[/home/src/projects/project/user/user.ts]
113115
NearestConfigFileName:
114116
{
@@ -181,6 +183,8 @@ Config::
181183
Config File Names::
182184
[/home/src/projects/project/a/a.ts]
183185
NearestConfigFileName: /home/src/projects/project/a/tsconfig.json
186+
Ancestors:
187+
/home/src/projects/project/a/tsconfig.json
184188
[/home/src/projects/project/dummy/dummy.ts] *new*
185189
NearestConfigFileName: /home/src/projects/project/dummy/tsconfig.json
186190
[/home/src/projects/project/user/user.ts] *deleted*

testdata/baselines/reference/lspservertests/declarationMaps/rename-before-project-is-built.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,32 @@ export const a = 10;
6363
Projects::
6464
[/user/username/projects/myproject/dependency/tsconfig.json] *new*
6565
/user/username/projects/myproject/dependency/FnS.ts
66+
[/user/username/projects/myproject/tsconfig.json] *new*
67+
/user/username/projects/myproject/dependency/FnS.ts
68+
/user/username/projects/myproject/main/main.ts
6669
Open Files::
6770
[/user/username/projects/myproject/dependency/FnS.ts] *new*
6871
/user/username/projects/myproject/dependency/tsconfig.json (default)
72+
/user/username/projects/myproject/tsconfig.json
6973
Config::
7074
[/user/username/projects/myproject/dependency/tsconfig.json] *new*
7175
RetainingProjects:
7276
/user/username/projects/myproject/dependency/tsconfig.json
77+
/user/username/projects/myproject/tsconfig.json
7378
RetainingOpenFiles:
7479
/user/username/projects/myproject/dependency/fns.ts
80+
[/user/username/projects/myproject/main/tsconfig.json] *new*
81+
RetainingProjects:
82+
/user/username/projects/myproject/tsconfig.json
83+
[/user/username/projects/myproject/tsconfig.json] *new*
84+
RetainingProjects:
85+
/user/username/projects/myproject/tsconfig.json
7586
Config File Names::
7687
[/user/username/projects/myproject/dependency/fns.ts] *new*
7788
NearestConfigFileName: /user/username/projects/myproject/dependency/tsconfig.json
89+
Ancestors:
90+
/user/username/projects/myproject/dependency/tsconfig.json /user/username/projects/myproject/tsconfig.json
91+
/user/username/projects/myproject/tsconfig.json
7892
{
7993
"method": "textDocument/didOpen",
8094
"params": {
@@ -89,19 +103,26 @@ Config File Names::
89103
Projects::
90104
[/user/username/projects/myproject/dependency/tsconfig.json]
91105
/user/username/projects/myproject/dependency/FnS.ts
106+
[/user/username/projects/myproject/tsconfig.json] *deleted*
107+
/user/username/projects/myproject/dependency/FnS.ts
108+
/user/username/projects/myproject/main/main.ts
92109
[/user/username/projects/random/tsconfig.json] *new*
93110
/user/username/projects/random/random.ts
94111
Open Files::
95-
[/user/username/projects/myproject/dependency/FnS.ts]
112+
[/user/username/projects/myproject/dependency/FnS.ts] *modified*
96113
/user/username/projects/myproject/dependency/tsconfig.json (default)
114+
/user/username/projects/myproject/tsconfig.json *deleted*
97115
[/user/username/projects/random/random.ts] *new*
98116
/user/username/projects/random/tsconfig.json (default)
99117
Config::
100-
[/user/username/projects/myproject/dependency/tsconfig.json]
101-
RetainingProjects:
118+
[/user/username/projects/myproject/dependency/tsconfig.json] *modified*
119+
RetainingProjects: *modified*
102120
/user/username/projects/myproject/dependency/tsconfig.json
121+
/user/username/projects/myproject/tsconfig.json *deleted*
103122
RetainingOpenFiles:
104123
/user/username/projects/myproject/dependency/fns.ts
124+
[/user/username/projects/myproject/main/tsconfig.json] *deleted*
125+
[/user/username/projects/myproject/tsconfig.json] *deleted*
105126
[/user/username/projects/random/tsconfig.json] *new*
106127
RetainingProjects:
107128
/user/username/projects/random/tsconfig.json
@@ -110,6 +131,9 @@ Config::
110131
Config File Names::
111132
[/user/username/projects/myproject/dependency/fns.ts]
112133
NearestConfigFileName: /user/username/projects/myproject/dependency/tsconfig.json
134+
Ancestors:
135+
/user/username/projects/myproject/dependency/tsconfig.json /user/username/projects/myproject/tsconfig.json
136+
/user/username/projects/myproject/tsconfig.json
113137
[/user/username/projects/random/random.ts] *new*
114138
NearestConfigFileName: /user/username/projects/random/tsconfig.json
115139
{

testdata/baselines/reference/lspservertests/declarationMaps/rename-on-edit-at-end-with-disableSourceOfProjectReferenceRedirect.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,18 +239,32 @@ export const a = 10;
239239
Projects::
240240
[/user/username/projects/myproject/dependency/tsconfig.json] *new*
241241
/user/username/projects/myproject/dependency/FnS.ts
242+
[/user/username/projects/myproject/tsconfig.json] *new*
243+
/user/username/projects/myproject/dependency/FnS.ts
244+
/user/username/projects/myproject/main/main.ts
242245
Open Files::
243246
[/user/username/projects/myproject/dependency/FnS.ts] *new*
244247
/user/username/projects/myproject/dependency/tsconfig.json (default)
248+
/user/username/projects/myproject/tsconfig.json
245249
Config::
246250
[/user/username/projects/myproject/dependency/tsconfig.json] *new*
247251
RetainingProjects:
248252
/user/username/projects/myproject/dependency/tsconfig.json
253+
/user/username/projects/myproject/tsconfig.json
249254
RetainingOpenFiles:
250255
/user/username/projects/myproject/dependency/fns.ts
256+
[/user/username/projects/myproject/main/tsconfig.json] *new*
257+
RetainingProjects:
258+
/user/username/projects/myproject/tsconfig.json
259+
[/user/username/projects/myproject/tsconfig.json] *new*
260+
RetainingProjects:
261+
/user/username/projects/myproject/tsconfig.json
251262
Config File Names::
252263
[/user/username/projects/myproject/dependency/fns.ts] *new*
253264
NearestConfigFileName: /user/username/projects/myproject/dependency/tsconfig.json
265+
Ancestors:
266+
/user/username/projects/myproject/dependency/tsconfig.json /user/username/projects/myproject/tsconfig.json
267+
/user/username/projects/myproject/tsconfig.json
254268
{
255269
"method": "textDocument/rename",
256270
"params": {
@@ -264,13 +278,54 @@ Config File Names::
264278
"newName": "?"
265279
}
266280
}
281+
Projects::
282+
[/user/username/projects/myproject/dependency/tsconfig.json]
283+
/user/username/projects/myproject/dependency/FnS.ts
284+
[/user/username/projects/myproject/main/tsconfig.json] *new*
285+
/user/username/projects/myproject/decls/FnS.d.ts
286+
/user/username/projects/myproject/main/main.ts
287+
[/user/username/projects/myproject/tsconfig.json]
288+
/user/username/projects/myproject/dependency/FnS.ts
289+
/user/username/projects/myproject/main/main.ts
290+
Config::
291+
[/user/username/projects/myproject/dependency/tsconfig.json] *modified*
292+
RetainingProjects: *modified*
293+
/user/username/projects/myproject/dependency/tsconfig.json
294+
/user/username/projects/myproject/main/tsconfig.json *new*
295+
/user/username/projects/myproject/tsconfig.json
296+
RetainingOpenFiles:
297+
/user/username/projects/myproject/dependency/fns.ts
298+
[/user/username/projects/myproject/main/tsconfig.json] *modified*
299+
RetainingProjects: *modified*
300+
/user/username/projects/myproject/main/tsconfig.json *new*
301+
/user/username/projects/myproject/tsconfig.json
302+
RetainingOpenFiles: *modified*
303+
/user/username/projects/myproject/main/main.ts *new*
304+
[/user/username/projects/myproject/tsconfig.json]
305+
RetainingProjects:
306+
/user/username/projects/myproject/tsconfig.json
267307
// === /user/username/projects/myproject/dependency/FnS.ts ===
268308
// export function fn1() { }
269309
// export function fn2() { }
270310
// export function /*RENAME*/[|fn3RENAME|]() { }
271311
// export function fn4() { }
272312
// export function fn5() { }
273313
//
314+
315+
// === /user/username/projects/myproject/main/main.ts ===
316+
// import {
317+
// fn1,
318+
// fn2,
319+
// [|[|fn3RENAME|]RENAME|],
320+
// fn4,
321+
// fn5
322+
// } from "../decls/FnS";
323+
//
324+
// fn1();
325+
// fn2();
326+
// [|fn3RENAME|]();
327+
// fn4();
328+
// fn5();
274329
{
275330
"method": "textDocument/didChange",
276331
"params": {
@@ -311,10 +366,31 @@ Config File Names::
311366
Projects::
312367
[/user/username/projects/myproject/dependency/tsconfig.json] *modified*
313368
/user/username/projects/myproject/dependency/FnS.ts *modified*
369+
[/user/username/projects/myproject/main/tsconfig.json]
370+
/user/username/projects/myproject/decls/FnS.d.ts
371+
/user/username/projects/myproject/main/main.ts
372+
[/user/username/projects/myproject/tsconfig.json] *modified*
373+
/user/username/projects/myproject/dependency/FnS.ts *modified*
374+
/user/username/projects/myproject/main/main.ts
314375
// === /user/username/projects/myproject/dependency/FnS.ts ===
315376
// export function fn1() { }
316377
// export function fn2() { }
317378
// export function /*RENAME*/[|fn3RENAME|]() { }
318379
// export function fn4() { }
319380
// export function fn5() { }
320381
// const x = 10;
382+
383+
// === /user/username/projects/myproject/main/main.ts ===
384+
// import {
385+
// fn1,
386+
// fn2,
387+
// /*START PREFIX*/fn3 as [|/*START PREFIX*/fn3 as [|fn3RENAME|]RENAME|],
388+
// fn4,
389+
// fn5
390+
// } from "../decls/FnS";
391+
//
392+
// fn1();
393+
// fn2();
394+
// [|fn3RENAME|]();
395+
// fn4();
396+
// fn5();

0 commit comments

Comments
 (0)