工作空间workspace模块分析
# 简介
主要是workspace
模块;
# 初始化
core/packages/file-service/src/browser/file-service-contribution.ts
@Domain(ClientAppContribution)
export class FileServiceContribution implements ClientAppContribution {
@Autowired(IFileServiceClient)
protected readonly fileSystem: FileServiceClient;
@Autowired(IDiskFileProvider)
private diskFileServiceProvider: IDiskFileProvider;
@Autowired(FsProviderContribution)
contributionProvider: ContributionProvider<FsProviderContribution>;
constructor() {
// 初始化资源读取逻辑,需要在最早初始化时注册
// 否则后续注册的 debug\user_stroage 等将无法正常使用
this.fileSystem.registerProvider(Schemes.file, this.diskFileServiceProvider);
}
async initialize() {
const fsProviderContributions = this.contributionProvider.getContributions();
for (const contribution of fsProviderContributions) {
contribution.registerProvider && (await contribution.registerProvider(this.fileSystem));
}
for (const contribution of fsProviderContributions) {
contribution.onFileServiceReady && (await contribution.onFileServiceReady());
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
core/packages/workspace/src/browser/workspace-contribution.ts
onFileServiceReady() {
this.workspaceService.init();
}
1
2
3
2
3
core/packages/workspace/src/browser/workspace-service.ts
第一次文件初始化;
public init() {
this.doInit();
}
protected async doInit(): Promise<void> {
// 这里的 `appName` 存在默认值
this.applicationName = this.appConfig.appName!;
const wpUriString = this.getDefaultWorkspacePath();
this.listenPreference();
if (wpUriString) {
const wpStat = await this.toFileStat(wpUriString);
await this.setWorkspace(wpStat);
this.toDisposableCollection.push(
this.fileServiceClient.onFilesChanged((event) => {
if (this._workspace && FileChangeEvent.isAffected(event, new URI(this._workspace.uri))) {
this.updateWorkspace();
}
}),
);
} else {
// 处理空工作区情况
this.deferredRoots.resolve([]);
}
this._whenReady.resolve();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 设置工作空间
public async setWorkspace(workspaceStat: FileStat | undefined): Promise<void> {
if (FileStat.equals(this._workspace, workspaceStat)) {
return;
}
this.toDisposeOnWorkspace.dispose();
this._workspace = workspaceStat;
if (this._workspace) {
const uri = new URI(this._workspace.uri);
this.toDisposeOnWorkspace.push(await this.fileServiceClient.watchFileChanges(uri));
}
this.updateTitle();
await this.updateWorkspace();
}
protected async updateWorkspace(): Promise<void> {
if (this._workspace) {
this.toFileStat(this._workspace.uri).then((stat) => (this._workspace = stat));
this.setMostRecentlyUsedWorkspace(this._workspace.uri);
}
await this.updateRoots();
if (!this._workspace?.isDirectory) {
// 工作区模式才需要额外监听根目录,否则会出现重复监听问题
this.watchRoots();
}
}
protected async updateRoots(): Promise<void> {
const newRoots = await this.computeRoots();
let rootsChanged = false;
if (newRoots.length !== this._roots.length || newRoots.length === 0) {
rootsChanged = true;
} else {
for (const newRoot of newRoots) {
if (!this._roots.some((r) => r.uri === newRoot.uri)) {
rootsChanged = true;
break;
}
}
}
if (rootsChanged) {
this._roots = newRoots;
this.deferredRoots.resolve(this._roots); // in order to resolve first
this.deferredRoots = new Deferred<FileStat[]>();
this.deferredRoots.resolve(this._roots);
this.onWorkspaceChangeEmitter.fire(this._roots);
// 重新根据工作区Roots设置 fileExclude 及 watchExclude
this.setFileServiceExcludes();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 创建文件并打开编辑器
core/packages/file-tree-next/src/browser/services/file-tree-api.service.ts
async createFile(uri: URI) {
try {
await this.workspaceEditService.apply({
edits: [
{
newResource: uri,
options: {},
},
],
});
} catch (e) {
return e.message;
}
this.commandService.executeCommand(EDITOR_COMMANDS.OPEN_RESOURCE.id, uri, { disableNavigate: true, focus: true });
return;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
core/packages/workspace-edit/src/browser/workspace-edit.service.ts
1
上次更新: 2023/11/17, 05:08:19