@opensumi/ide-workspace 2.21.13 → 2.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. package/lib/browser/index.js.map +1 -1
  2. package/lib/browser/workspace-contextkey.js.map +1 -1
  3. package/lib/browser/workspace-contribution.js.map +1 -1
  4. package/lib/browser/workspace-preferences.d.ts +1 -1
  5. package/lib/browser/workspace-preferences.d.ts.map +1 -1
  6. package/lib/browser/workspace-service.d.ts +4 -1
  7. package/lib/browser/workspace-service.d.ts.map +1 -1
  8. package/lib/browser/workspace-service.js +18 -7
  9. package/lib/browser/workspace-service.js.map +1 -1
  10. package/lib/browser/workspace-storage-service.js.map +1 -1
  11. package/lib/browser/workspace-variable-contribution.js.map +1 -1
  12. package/lib/common/mocks/workspace-service.d.ts +4 -1
  13. package/lib/common/mocks/workspace-service.d.ts.map +1 -1
  14. package/lib/common/mocks/workspace-service.js.map +1 -1
  15. package/lib/common/workspace.interface.d.ts +5 -2
  16. package/lib/common/workspace.interface.d.ts.map +1 -1
  17. package/lib/common/workspace.interface.js.map +1 -1
  18. package/package.json +10 -9
  19. package/src/browser/index.ts +31 -0
  20. package/src/browser/workspace-contextkey.ts +18 -0
  21. package/src/browser/workspace-contribution.ts +103 -0
  22. package/src/browser/workspace-data.ts +144 -0
  23. package/src/browser/workspace-preferences.ts +45 -0
  24. package/src/browser/workspace-service.ts +809 -0
  25. package/src/browser/workspace-storage-service.ts +64 -0
  26. package/src/browser/workspace-variable-contribution.ts +133 -0
  27. package/src/common/constants.ts +4 -0
  28. package/src/common/index.ts +2 -0
  29. package/src/common/mocks/index.ts +1 -0
  30. package/src/common/mocks/workspace-service.ts +132 -0
  31. package/src/common/workspace.interface.ts +75 -0
  32. package/src/index.ts +1 -0
@@ -0,0 +1,64 @@
1
+ import { Injectable, Autowired } from '@opensumi/di';
2
+ import { GlobalBrowserStorageService } from '@opensumi/ide-core-browser/lib/services';
3
+ import { FileStat } from '@opensumi/ide-file-service';
4
+
5
+ import { IWorkspaceService, IWorkspaceStorageService } from '../common';
6
+
7
+ /*
8
+ * 为存在 Browser (LocalStorage) 的数据添加命名空间
9
+ * @Deprecated
10
+ */
11
+ @Injectable()
12
+ export class WorkspaceStorageService implements IWorkspaceStorageService {
13
+ private prefix: string;
14
+ private initialized: Promise<void>;
15
+
16
+ @Autowired(GlobalBrowserStorageService)
17
+ protected globalStorageService: GlobalBrowserStorageService;
18
+
19
+ @Autowired(IWorkspaceService)
20
+ protected workspaceService: IWorkspaceService;
21
+
22
+ constructor() {
23
+ this.init();
24
+ }
25
+
26
+ protected init() {
27
+ this.initialized = this.workspaceService.roots.then(() => {
28
+ this.updatePrefix();
29
+ this.workspaceService.onWorkspaceLocationChanged(() => this.updatePrefix());
30
+ });
31
+ }
32
+
33
+ async setData<T>(key: string, data: T): Promise<void> {
34
+ if (!this.prefix) {
35
+ await this.initialized;
36
+ }
37
+ const fullKey = this.prefixWorkspaceURI(key);
38
+ return this.globalStorageService.setData(fullKey, data);
39
+ }
40
+
41
+ async getData<T>(key: string, defaultValue?: T): Promise<T | undefined> {
42
+ await this.initialized;
43
+ const fullKey = this.prefixWorkspaceURI(key);
44
+ return this.globalStorageService.getData(fullKey, defaultValue);
45
+ }
46
+
47
+ async removeData(key: string): Promise<void> {
48
+ await this.initialized;
49
+ const fullKey = this.prefixWorkspaceURI(key);
50
+ return this.globalStorageService.removeData(fullKey);
51
+ }
52
+
53
+ protected prefixWorkspaceURI(originalKey: string): string {
54
+ return `${this.prefix}:${originalKey}`;
55
+ }
56
+
57
+ protected getPrefix(workspaceStat: FileStat | undefined): string {
58
+ return workspaceStat ? workspaceStat.uri : '_global_';
59
+ }
60
+
61
+ private updatePrefix(): void {
62
+ this.prefix = this.getPrefix(this.workspaceService.workspace);
63
+ }
64
+ }
@@ -0,0 +1,133 @@
1
+ import { Autowired } from '@opensumi/di';
2
+ import {
3
+ VariableContribution,
4
+ VariableRegistry,
5
+ Domain,
6
+ URI,
7
+ CommandService,
8
+ EDITOR_COMMANDS,
9
+ COMMON_COMMANDS,
10
+ } from '@opensumi/ide-core-browser';
11
+
12
+ import { IWorkspaceService } from '../common';
13
+
14
+ @Domain(VariableContribution)
15
+ export class WorkspaceVariableContribution implements VariableContribution {
16
+ @Autowired(IWorkspaceService)
17
+ protected readonly workspaceService: IWorkspaceService;
18
+
19
+ @Autowired(CommandService)
20
+ protected readonly commandService: CommandService;
21
+
22
+ registerVariables(variables: VariableRegistry): void {
23
+ variables.registerVariable({
24
+ name: 'workspaceRoot',
25
+ description: 'The path of the workspace root folder',
26
+ resolve: (context?: URI) => {
27
+ const uri = this.getWorkspaceRootUri(context);
28
+ return uri && uri.path.toString();
29
+ },
30
+ });
31
+ variables.registerVariable({
32
+ name: 'workspaceFolder',
33
+ description: 'The path of the workspace root folder',
34
+ resolve: (context?: URI) => {
35
+ const uri = this.getWorkspaceRootUri(context);
36
+ return uri && uri.codeUri.fsPath.toString();
37
+ },
38
+ });
39
+ variables.registerVariable({
40
+ name: 'workspaceFolderBasename',
41
+ description: 'The name of the workspace root folder',
42
+ resolve: (context?: URI) => {
43
+ const uri = this.getWorkspaceRootUri(context);
44
+ return uri && uri.displayName;
45
+ },
46
+ });
47
+ variables.registerVariable({
48
+ name: 'cwd',
49
+ description: 'The path of the current working directory',
50
+ resolve: (context?: URI) => {
51
+ const uri = this.getWorkspaceRootUri(context);
52
+ return (uri && uri.codeUri.fsPath.toString()) || '';
53
+ },
54
+ });
55
+ variables.registerVariable({
56
+ name: 'file',
57
+ description: 'The path of the currently opened file',
58
+ resolve: async () => {
59
+ const uri = await this.getResourceUri();
60
+ return uri && uri.codeUri.fsPath.toString();
61
+ },
62
+ });
63
+ variables.registerVariable({
64
+ name: 'fileBasename',
65
+ description: 'The basename of the currently opened file',
66
+ resolve: async () => {
67
+ const uri = await this.getResourceUri();
68
+ return uri && uri.path.base;
69
+ },
70
+ });
71
+ variables.registerVariable({
72
+ name: 'fileBasenameNoExtension',
73
+ description: "The currently opened file's name without extension",
74
+ resolve: async () => {
75
+ const uri = await this.getResourceUri();
76
+ return uri && uri.path.name;
77
+ },
78
+ });
79
+ variables.registerVariable({
80
+ name: 'fileDirname',
81
+ description: "The name of the currently opened file's directory",
82
+ resolve: async () => {
83
+ const uri = await this.getResourceUri();
84
+ return uri && uri.path.dir.toString();
85
+ },
86
+ });
87
+ variables.registerVariable({
88
+ name: 'fileExtname',
89
+ description: 'The extension of the currently opened file',
90
+ resolve: async () => {
91
+ const uri = await this.getResourceUri();
92
+ return uri && uri.path.ext;
93
+ },
94
+ });
95
+ variables.registerVariable({
96
+ name: 'relativeFile',
97
+ description: "The currently opened file's path relative to the workspace root",
98
+ resolve: async () => {
99
+ const uri = await this.getResourceUri();
100
+ return uri && this.getWorkspaceRelativePath(uri);
101
+ },
102
+ });
103
+ variables.registerVariable({
104
+ name: 'env',
105
+ resolve: async () => {
106
+ const envVariable = await this.commandService.executeCommand<{ [x: string]: string | undefined }>(
107
+ COMMON_COMMANDS.ENVIRONMENT_VARIABLE.id,
108
+ );
109
+ return envVariable;
110
+ },
111
+ });
112
+ }
113
+
114
+ getWorkspaceRootUri(uri?: URI): URI | undefined {
115
+ return this.workspaceService.getWorkspaceRootUri(uri);
116
+ }
117
+
118
+ async getResourceUri(): Promise<URI | undefined> {
119
+ const currentResource = await this.commandService.executeCommand<{ uri: URI }>(
120
+ EDITOR_COMMANDS.GET_CURRENT_RESOURCE.id,
121
+ );
122
+ if (currentResource) {
123
+ return currentResource.uri;
124
+ }
125
+ return undefined;
126
+ }
127
+
128
+ getWorkspaceRelativePath(uri: URI): string | undefined {
129
+ const workspaceRootUri = this.getWorkspaceRootUri(uri);
130
+ const path = workspaceRootUri && workspaceRootUri.path.relative(uri.path);
131
+ return path && path.toString();
132
+ }
133
+ }
@@ -0,0 +1,4 @@
1
+ export const DEFAULT_WORKSPACE_SUFFIX_NAME = 'sumi-workspace';
2
+ export const WORKSPACE_USER_STORAGE_FOLDER_NAME = '.sumi';
3
+ export const WORKSPACE_RECENT_DATA_FILE = 'recentdata.json';
4
+ export const UNTITLED_WORKSPACE = 'Untitled';
@@ -0,0 +1,2 @@
1
+ export * from './constants';
2
+ export * from './workspace.interface';
@@ -0,0 +1 @@
1
+ export * from './workspace-service';
@@ -0,0 +1,132 @@
1
+ import { Injectable } from '@opensumi/di';
2
+ import { Emitter, URI, Deferred } from '@opensumi/ide-core-common';
3
+ import { FileStat } from '@opensumi/ide-file-service';
4
+
5
+ import { IWorkspaceService } from '../../common';
6
+
7
+ @Injectable()
8
+ export class MockWorkspaceService implements IWorkspaceService {
9
+ private _roots: FileStat[] = [];
10
+
11
+ private _workspace: FileStat | undefined;
12
+
13
+ isMultiRootWorkspaceOpened = false;
14
+
15
+ whenReady: Promise<void>;
16
+
17
+ private deferredRoots = new Deferred<FileStat[]>();
18
+
19
+ constructor() {
20
+ this.whenReady = this.init();
21
+ }
22
+
23
+ async init() {
24
+ await this.setWorkspace();
25
+ }
26
+
27
+ async initFileServiceExclude() {
28
+ // do nothing
29
+ }
30
+
31
+ async setWorkspace(workspaceStat?: FileStat | undefined) {
32
+ await this.updateWorkspace(workspaceStat);
33
+ }
34
+
35
+ async updateWorkspace(workspaceStat?: FileStat | undefined) {
36
+ await this.updateRoots(workspaceStat);
37
+ this._onWorkspaceChanged.fire(this._roots);
38
+ }
39
+
40
+ containsSome(paths: string[]): Promise<boolean> {
41
+ throw new Error('Method not implemented.');
42
+ }
43
+
44
+ get roots(): Promise<FileStat[]> {
45
+ return this.deferredRoots.promise;
46
+ }
47
+
48
+ get workspace(): FileStat | undefined {
49
+ return this._workspace;
50
+ }
51
+
52
+ tryGetRoots(): FileStat[] {
53
+ return this._roots;
54
+ }
55
+
56
+ protected async updateRoots(workspaceStat?: FileStat | undefined): Promise<void> {
57
+ const root: FileStat = workspaceStat || {
58
+ isDirectory: true,
59
+ uri: 'file://userhome/',
60
+ lastModification: 0,
61
+ };
62
+ this._workspace = root;
63
+ this._roots = [root];
64
+ this.deferredRoots = new Deferred<FileStat[]>();
65
+ this.deferredRoots.resolve(this._roots);
66
+ this._onWorkspaceChanged.fire(this._roots);
67
+ }
68
+
69
+ _onWorkspaceChanged: Emitter<FileStat[]> = new Emitter();
70
+ onWorkspaceChanged = this._onWorkspaceChanged.event;
71
+
72
+ _onWorkspaceLocationChanged: Emitter<FileStat | undefined> = new Emitter();
73
+ onWorkspaceLocationChanged = this._onWorkspaceLocationChanged.event;
74
+
75
+ _onWorkspaceFileExcludeChangeEmitter: Emitter<void> = new Emitter();
76
+ onWorkspaceFileExcludeChanged = this._onWorkspaceFileExcludeChangeEmitter.event;
77
+
78
+ async setMostRecentlyUsedWorkspace(): Promise<void> {
79
+ return;
80
+ }
81
+ getMostRecentlyUsedWorkspace(): Promise<string> {
82
+ throw new Error('Method not implemented.');
83
+ }
84
+ getMostRecentlyUsedWorkspaces(): Promise<string[]> {
85
+ throw new Error('Method not implemented.');
86
+ }
87
+ getMostRecentlyUsedCommands(): Promise<string[]> {
88
+ throw new Error('Method not implemented.');
89
+ }
90
+ setMostRecentlyUsedCommand(commandId: string): Promise<void> {
91
+ throw new Error('Method not implemented.');
92
+ }
93
+ async setMostRecentlyOpenedFile(uri: string): Promise<void> {
94
+ return;
95
+ }
96
+ getMostRecentlyOpenedFiles(): Promise<string[] | undefined> {
97
+ throw new Error('Method not implemented.');
98
+ }
99
+ setMostRecentlySearchWord(word: string | string[]): Promise<void> {
100
+ throw new Error('Method not implemented.');
101
+ }
102
+ getMostRecentlySearchWord(): Promise<string[] | undefined> {
103
+ throw new Error('Method not implemented.');
104
+ }
105
+ async removeRoots(uri: URI[]) {
106
+ return;
107
+ }
108
+ async spliceRoots(
109
+ start: number,
110
+ deleteCount?: number | undefined,
111
+ workspaceName?: { [key: string]: string },
112
+ ...rootsToAdd: URI[]
113
+ ): Promise<URI[]> {
114
+ this._roots = rootsToAdd.map((root) => ({ isDirectory: true, uri: root.toString(), lastModification: 0 }));
115
+ this.deferredRoots = new Deferred();
116
+ this.deferredRoots.resolve(this._roots);
117
+ return rootsToAdd;
118
+ }
119
+ asRelativePath(
120
+ pathOrUri: string | URI,
121
+ includeWorkspaceFolder?: boolean | undefined,
122
+ ): Promise<{ path?: string; root?: string } | undefined> {
123
+ throw new Error('Method not implemented.');
124
+ }
125
+ getWorkspaceRootUri(uri: URI | undefined): URI | undefined {
126
+ return new URI(this._roots[0].uri);
127
+ }
128
+ getWorkspaceName(uri: URI): string {
129
+ return '';
130
+ }
131
+ isMultiRootWorkspaceEnabled: boolean;
132
+ }
@@ -0,0 +1,75 @@
1
+ import { StorageService } from '@opensumi/ide-core-browser/lib/services';
2
+ import { URI, Event } from '@opensumi/ide-core-common';
3
+ import { FileStat } from '@opensumi/ide-file-service';
4
+
5
+ export interface WorkspaceInput {
6
+ /**
7
+ * 判断是否复用相同窗口
8
+ */
9
+ preserveWindow?: boolean;
10
+ }
11
+
12
+ export const IWorkspaceService = Symbol('IWorkspaceService');
13
+
14
+ export interface IWorkspaceService {
15
+ // 获取当前的根节点
16
+ roots: Promise<FileStat[]>;
17
+ // 获取workspace
18
+ workspace: FileStat | undefined;
19
+ // 当一个混合工作区打开时,返回 true
20
+ isMultiRootWorkspaceOpened: boolean;
21
+ whenReady: Promise<void>;
22
+ // 返回根目录下是否存在对应相对路径文件
23
+ containsSome(paths: string[]): Promise<boolean>;
24
+ // 尝试获取根路径数组
25
+ tryGetRoots(): FileStat[];
26
+ // 工作区改变事件
27
+ onWorkspaceChanged: Event<FileStat[]>;
28
+ /**
29
+ * 工作区的 files.exclude 配置发生变化
30
+ */
31
+ onWorkspaceFileExcludeChanged: Event<void>;
32
+ /**
33
+ * 操作中的工作区改变事件
34
+ * 如:用户添加目录到当前workspace中触发
35
+ */
36
+ onWorkspaceLocationChanged: Event<FileStat | undefined>;
37
+ // 获取最近使用的命令
38
+ getMostRecentlyUsedCommands(): Promise<string[]>;
39
+ // 设置最近使用的command
40
+ setMostRecentlyUsedCommand(commandId: string): Promise<void>;
41
+ // 获取最近的多个工作区
42
+ getMostRecentlyUsedWorkspaces(): Promise<string[]>;
43
+ // 获取最近的一个工作区
44
+ getMostRecentlyUsedWorkspace(): Promise<string | undefined>;
45
+ // 设置最近使用的工作区
46
+ setMostRecentlyUsedWorkspace(uri: string): Promise<void>;
47
+ // 操作工作区目录
48
+ spliceRoots(
49
+ start: number,
50
+ deleteCount?: number,
51
+ workspaceToName?: { [key: string]: string },
52
+ ...rootsToAdd: URI[]
53
+ ): Promise<URI[]>;
54
+ // 从工作区中移除目录
55
+ removeRoots(roots: URI[]): Promise<void>;
56
+ // 获取相对于工作区的路径
57
+ asRelativePath(
58
+ pathOrUri: string | URI,
59
+ includeWorkspaceFolder?: boolean,
60
+ ): Promise<{ path?: string; root?: string } | undefined>;
61
+ // 根据给定的uri获取其根节点
62
+ getWorkspaceRootUri(uri: URI | undefined): URI | undefined;
63
+ // 获取工作区名称
64
+ getWorkspaceName(uri: URI): string;
65
+ // 当前存在打开的工作区同时支持混合工作区时,返回true
66
+ isMultiRootWorkspaceEnabled: boolean;
67
+ // 设置新的工作区
68
+ setWorkspace(workspaceStat: FileStat | undefined): Promise<void>;
69
+ // 初始化文件服务中 `files.exclude` 和 `watche.exclude` 配置
70
+ initFileServiceExclude(): Promise<void>;
71
+ }
72
+
73
+ export const IWorkspaceStorageService = Symbol('IWorkspaceStorageService');
74
+
75
+ export type IWorkspaceStorageService = StorageService;
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './common';