Skip to content

Commit f97ca76

Browse files
committed
Fix issues surrounding persisted workspaces.
- Moved URI helper.
1 parent 0e9d248 commit f97ca76

File tree

3 files changed

+63
-18
lines changed

3 files changed

+63
-18
lines changed

src/vs/base/common/uri.ts

+10
Original file line numberDiff line numberDiff line change
@@ -701,3 +701,13 @@ function percentDecode(str: string): string {
701701
}
702702
return str.replace(_rEncodedAsHex, (match) => decodeURIComponentGraceful(match));
703703
}
704+
705+
/**
706+
* Encode a path for opening via the folder or workspace query parameter. This
707+
* preserves slashes so it can be edited by hand more easily.
708+
*
709+
* @author coder
710+
*/
711+
export const encodePath = (path: string): string => {
712+
return path.split('/').map((p) => encodeURIComponent(p)).join('/');
713+
};

src/vs/code/browser/workbench/workbench.ts

+5-15
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Emitter, Event } from 'vs/base/common/event';
1010
import { Disposable } from 'vs/base/common/lifecycle';
1111
import { Schemas } from 'vs/base/common/network';
1212
import { isEqual } from 'vs/base/common/resources';
13-
import { URI, UriComponents } from 'vs/base/common/uri';
13+
import { encodePath, URI, UriComponents } from 'vs/base/common/uri';
1414
import { generateUuid } from 'vs/base/common/uuid';
1515
import { request } from 'vs/base/parts/request/browser/request';
1616
import { localize } from 'vs/nls';
@@ -37,16 +37,6 @@ function doCreateUri(path: string, queryValues: Map<string, string>): URI {
3737
return URI.parse(window.location.href).with({ path, query });
3838
}
3939

40-
/**
41-
* Encode a path for opening via the folder or workspace query parameter. This
42-
* preserves slashes so it can be edited by hand more easily.
43-
*
44-
* @author coder
45-
*/
46-
export const encodePath = (path: string): string => {
47-
return path.split('/').map((p) => encodeURIComponent(p)).join('/');
48-
};
49-
5040
interface ICredential {
5141
service: string;
5242
account: string;
@@ -454,11 +444,11 @@ class WindowIndicator implements IWindowIndicator {
454444
* @author coder
455445
*/
456446
const toRemote = (value: string): string => {
457-
if (value.startsWith("/")) {
458-
return "vscode-remote://" + value;
447+
if (value.startsWith('/')) {
448+
return 'vscode-remote://' + value;
459449
}
460-
return value
461-
}
450+
return value;
451+
};
462452

463453
const query = new URL(document.location.href).searchParams;
464454
query.forEach((value, key) => {

src/vs/workbench/browser/web.main.ts

+48-3
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ import { IRemoteAgentService } from 'vs/workbench/services/remote/common/remoteA
2222
import { IFileService } from 'vs/platform/files/common/files';
2323
import { FileService } from 'vs/platform/files/common/fileService';
2424
import { Schemas } from 'vs/base/common/network';
25-
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
25+
import { IWorkspaceContextService, toWorkspaceFolder } from 'vs/platform/workspace/common/workspace';
2626
import { IWorkbenchConfigurationService } from 'vs/workbench/services/configuration/common/configuration';
2727
import { onUnexpectedError } from 'vs/base/common/errors';
2828
import { setFullscreen } from 'vs/base/browser/browser';
29-
import { URI } from 'vs/base/common/uri';
30-
import { IWorkspaceInitializationPayload } from 'vs/platform/workspaces/common/workspaces';
29+
import { encodePath, URI } from 'vs/base/common/uri';
30+
import { isRecentFolder, IWorkspaceInitializationPayload, IWorkspacesService } from 'vs/platform/workspaces/common/workspaces';
3131
import { WorkspaceService } from 'vs/workbench/services/configuration/browser/configurationService';
3232
import { ConfigurationCache } from 'vs/workbench/services/configuration/browser/configurationCache';
3333
import { ISignService } from 'vs/platform/sign/common/sign';
@@ -68,6 +68,7 @@ import { safeStringify } from 'vs/base/common/objects';
6868
import { ICredentialsService } from 'vs/workbench/services/credentials/common/credentials';
6969
import { IndexedDB } from 'vs/base/browser/indexedDB';
7070
import { CodeServerClientAdditions } from 'vs/workbench/browser/client';
71+
import { BrowserWorkspacesService } from 'vs/workbench/services/workspaces/browser/workspacesService';
7172

7273
class BrowserMain extends Disposable {
7374

@@ -219,6 +220,50 @@ class BrowserMain extends Disposable {
219220
})
220221
]);
221222

223+
/**
224+
* Added to persist recent workspaces in the browser.
225+
* @author coder
226+
* @example User specified a directory at startup.
227+
* ```sh
228+
* code-server ./path/to/project/
229+
* ```
230+
*
231+
* @example Blank project without CLI arguments,
232+
* using the last opened directory in the browser.
233+
* ```sh
234+
* code-server
235+
* open http://localhost:8000/
236+
* ```
237+
*
238+
* @example Query params override CLI arguments.
239+
* ```sh
240+
* code-server ./path/to/project/
241+
* open http://localhost:8000/?folder=/path/to/different/project
242+
* ```
243+
*/
244+
const browserWorkspacesService = new BrowserWorkspacesService(storageService, configurationService, logService, fileService, environmentService, uriIdentityService);
245+
serviceCollection.set(IWorkspacesService, browserWorkspacesService);
246+
const workspace = configurationService.getWorkspace();
247+
248+
logService.debug('Workspace Folders:', workspace.folders);
249+
250+
if (workspace.folders.length === 0) {
251+
logService.debug('Workspace is empty. Checking for recent folders...');
252+
253+
const recentlyOpened = await browserWorkspacesService.getRecentlyOpened();
254+
255+
for (const recent of recentlyOpened.workspaces) {
256+
if (isRecentFolder(recent)) {
257+
logService.debug('Recent folder found...');
258+
const folder = toWorkspaceFolder(recent.folderUri);
259+
workspace.folders = [folder];
260+
261+
window.history.replaceState(null, '', `?folder=${encodePath(folder.uri.path)}`);
262+
break;
263+
}
264+
}
265+
}
266+
222267
// Workspace Trust Service
223268
const workspaceTrustEnablementService = new WorkspaceTrustEnablementService(configurationService, environmentService);
224269
serviceCollection.set(IWorkspaceTrustEnablementService, workspaceTrustEnablementService);

0 commit comments

Comments
 (0)