Skip to content

Commit 4d91505

Browse files
committed
Add a way to open the most recent workspace/folder
If you add openRecent to the VS Code link, it will open the most recent instead of whatever is in the folder param (if there is a most recent). Also decided to open the most recent when opening from the sidebar, seemed reasonable to me anyway.
1 parent a6e43e9 commit 4d91505

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

src/commands.ts

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ export class Commands {
223223
treeItem.workspaceName,
224224
treeItem.workspaceAgent,
225225
treeItem.workspaceFolderPath,
226+
true,
226227
)
227228
}
228229
}
@@ -232,6 +233,7 @@ export class Commands {
232233
let workspaceName: string
233234
let workspaceAgent: string | undefined
234235
let folderPath: string | undefined
236+
let openRecent: boolean | undefined
235237

236238
if (args.length === 0) {
237239
const quickPick = vscode.window.createQuickPick()
@@ -340,9 +342,10 @@ export class Commands {
340342
workspaceName = args[1] as string
341343
// workspaceAgent is reserved for args[2], but multiple agents aren't supported yet.
342344
folderPath = args[3] as string | undefined
345+
openRecent = args[4] as boolean | undefined
343346
}
344347

345-
await openWorkspace(workspaceOwner, workspaceName, workspaceAgent, folderPath)
348+
await openWorkspace(workspaceOwner, workspaceName, workspaceAgent, folderPath, openRecent)
346349
}
347350

348351
public async updateWorkspace(): Promise<void> {
@@ -369,6 +372,7 @@ async function openWorkspace(
369372
workspaceName: string,
370373
workspaceAgent: string | undefined,
371374
folderPath: string | undefined,
375+
openRecent: boolean | undefined,
372376
) {
373377
// A workspace can have multiple agents, but that's handled
374378
// when opening a workspace unless explicitly specified.
@@ -383,36 +387,38 @@ async function openWorkspace(
383387
newWindow = false
384388
}
385389

386-
// If a folder isn't specified, we can try to open a recently opened folder.
387-
if (!folderPath) {
390+
// If a folder isn't specified or we have been asked to open the most recent,
391+
// we can try to open a recently opened folder/workspace.
392+
if (!folderPath || openRecent) {
388393
const output: {
389394
workspaces: { folderUri: vscode.Uri; remoteAuthority: string }[]
390395
} = await vscode.commands.executeCommand("_workbench.getRecentlyOpened")
391396
const opened = output.workspaces.filter(
392-
// Filter out `/` since that's added below.
397+
// Remove recents that do not belong to this connection. The remote
398+
// authority maps to a workspace or workspace/agent combination (using the
399+
// SSH host name). This means, at the moment, you can have a different
400+
// set of recents for a workspace versus workspace/agent combination, even
401+
// if that agent is the default for the workspace.
393402
(opened) => opened.folderUri?.authority === remoteAuthority,
394403
)
395-
if (opened.length > 0) {
396-
let selected: (typeof opened)[0]
397404

398-
if (opened.length > 1) {
399-
const items: vscode.QuickPickItem[] = opened.map((folder): vscode.QuickPickItem => {
400-
return {
401-
label: folder.folderUri.path,
402-
}
403-
})
404-
const item = await vscode.window.showQuickPick(items, {
405-
title: "Select a recently opened folder",
406-
})
407-
if (!item) {
408-
return
405+
// openRecent will always use the most recent. Otherwise, if there are
406+
// multiple we ask the user which to use.
407+
if (opened.length === 1 || (opened.length > 1 && openRecent)) {
408+
folderPath = opened[0].folderUri.path
409+
} else if (opened.length > 1) {
410+
const items: vscode.QuickPickItem[] = opened.map((folder): vscode.QuickPickItem => {
411+
return {
412+
label: folder.folderUri.path,
409413
}
410-
selected = opened[items.indexOf(item)]
411-
} else {
412-
selected = opened[0]
414+
})
415+
const item = await vscode.window.showQuickPick(items, {
416+
title: "Select a recently opened folder",
417+
})
418+
if (!item) {
419+
return
413420
}
414-
415-
folderPath = selected.folderUri.path
421+
folderPath = opened[items.indexOf(item)].folderUri.path
416422
}
417423
}
418424

src/extension.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
140140
const workspace = params.get("workspace")
141141
const agent = params.get("agent")
142142
const folder = params.get("folder")
143+
const openRecent =
144+
params.has("openRecent") && (!params.get("openRecent") || params.get("openRecent") === "true")
145+
143146
if (!owner) {
144147
throw new Error("owner must be specified as a query parameter")
145148
}
@@ -166,7 +169,7 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
166169
await storage.setSessionToken(token)
167170
}
168171

169-
vscode.commands.executeCommand("coder.open", owner, workspace, agent, folder)
172+
vscode.commands.executeCommand("coder.open", owner, workspace, agent, folder, openRecent)
170173
}
171174
},
172175
})

0 commit comments

Comments
 (0)