Skip to content

Commit 2954940

Browse files
Default to document language id for scope recorder (#3044)
1 parent 56f199b commit 2954940

File tree

5 files changed

+62
-72
lines changed

5 files changed

+62
-72
lines changed
Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
1-
export interface UnknownValuesOptions {
2-
allowed: true;
1+
export interface QuickPickOptions {
2+
/**
3+
* An optional string that represents the title of the quick pick.
4+
*/
5+
title?: string;
6+
7+
/**
8+
* An optional string that represents the default value to be selected in the quick pick.
9+
*/
10+
defaultValue?: string;
311

412
/**
5-
* An optional template to use when displaying the new option to the user. The
6-
* template must include `{}` as a substring, which will be replaced with the
7-
* user's input when displaying an option to add the unknown item.
13+
* Indicates whether the quick pick should allow unknown values, and if so,
14+
* how to handle them.
15+
*
16+
* If provided as a string: An optional template to use when displaying the new
17+
* option to the user. The template must include `{}` as a substring, which will
18+
* be replaced with the user's input when displaying an option to add the unknown item.
819
*
920
* For example:
1021
*
@@ -16,18 +27,5 @@ export interface UnknownValuesOptions {
1627
*
1728
* "Add new value 'foo' →"
1829
*/
19-
newValueTemplate?: string;
20-
}
21-
22-
export interface QuickPickOptions {
23-
/**
24-
* An optional string that represents the title of the quick pick.
25-
*/
26-
title?: string;
27-
28-
/**
29-
* Indicates whether the quick pick should allow unknown values, and if so,
30-
* how to handle them.
31-
*/
32-
unknownValues?: UnknownValuesOptions;
30+
unknownValues?: boolean | string;
3331
}

packages/cursorless-vscode/src/ide/vscode/VscodeIDE.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ export class VscodeIDE implements IDE {
5858
this.editorMap = new WeakMap<vscode.TextEditor, VscodeTextEditorImpl>();
5959
}
6060

61-
async showQuickPick(
61+
showQuickPick(
6262
items: readonly string[],
6363
options?: QuickPickOptions,
6464
): Promise<string | undefined> {
65-
return await vscodeShowQuickPick(items, options);
65+
return vscodeShowQuickPick(items, options);
6666
}
6767

6868
setHighlightRanges(

packages/cursorless-vscode/src/ide/vscode/vscodeShowQuickPick.ts

Lines changed: 41 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,5 @@
1+
import type { QuickPickOptions } from "@cursorless/common";
12
import * as vscode from "vscode";
2-
import type {
3-
QuickPickOptions,
4-
UnknownValuesOptions,
5-
} from "@cursorless/common";
6-
7-
export async function vscodeShowQuickPick(
8-
items: readonly string[],
9-
options: QuickPickOptions | undefined,
10-
) {
11-
if (options?.unknownValues == null) {
12-
return await vscode.window.showQuickPick(items, options);
13-
}
14-
15-
const { unknownValues, ...rest } = options;
16-
return await showQuickPickAllowingUnknown(items, unknownValues, rest);
17-
}
18-
19-
interface CustomQuickPickItem extends vscode.QuickPickItem {
20-
value: string;
21-
}
22-
23-
const DEFAULT_NEW_VALUE_TEMPLATE = "Add new value '{}' →";
243

254
/**
265
* Show a quick pick that allows the user to enter a new value. We do this by
@@ -29,43 +8,58 @@ const DEFAULT_NEW_VALUE_TEMPLATE = "Add new value '{}' →";
298
* detect that it is an unknown value and handle that case.
309
*
3110
* Based on https://stackoverflow.com/a/69842249
32-
* @param items
33-
* @param options
3411
*/
35-
function showQuickPickAllowingUnknown(
36-
choices: readonly string[],
37-
unknownValues: UnknownValuesOptions,
38-
options: vscode.QuickPickOptions,
12+
13+
interface CustomQuickPickItem extends vscode.QuickPickItem {
14+
value: string;
15+
}
16+
17+
export function vscodeShowQuickPick(
18+
items: readonly string[],
19+
options: QuickPickOptions | undefined,
3920
) {
4021
return new Promise<string | undefined>((resolve, _reject) => {
4122
const quickPick = vscode.window.createQuickPick<CustomQuickPickItem>();
42-
const quickPickItems = choices.map((choice) => ({
43-
label: choice,
44-
value: choice,
23+
24+
const quickPickItems = items.map((item) => ({
25+
label: item,
26+
value: item,
4527
}));
28+
4629
quickPick.items = quickPickItems;
4730

48-
if (options.title != null) {
31+
if (options?.title != null) {
4932
quickPick.title = options.title;
5033
}
5134

52-
const { newValueTemplate = DEFAULT_NEW_VALUE_TEMPLATE } = unknownValues;
35+
if (options?.defaultValue != null) {
36+
quickPick.activeItems = quickPickItems.filter(
37+
(item) => item.value === options.defaultValue,
38+
);
39+
}
40+
41+
if (options?.unknownValues) {
42+
const newValueTemplate =
43+
typeof options.unknownValues === "string"
44+
? options.unknownValues
45+
: "Add new value '{}' →";
5346

54-
quickPick.onDidChangeValue(() => {
55-
quickPick.items = [
56-
...quickPickItems,
47+
quickPick.onDidChangeValue(() => {
48+
quickPick.items = [
49+
...quickPickItems,
5750

58-
// INJECT user values into proposed values
59-
...(choices.includes(quickPick.value)
60-
? []
61-
: [
62-
{
63-
label: newValueTemplate.replace("{}", quickPick.value),
64-
value: quickPick.value,
65-
},
66-
]),
67-
];
68-
});
51+
// INJECT user values into proposed values
52+
...(items.includes(quickPick.value) || quickPick.value.trim() === ""
53+
? []
54+
: [
55+
{
56+
label: newValueTemplate.replace("{}", quickPick.value),
57+
value: quickPick.value,
58+
},
59+
]),
60+
];
61+
});
62+
}
6963

7064
quickPick.onDidAccept(() => {
7165
const selection = quickPick.activeItems[0];

packages/test-case-recorder/src/ScopeTestRecorder.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export class ScopeTestRecorder {
118118
languageIds.sort();
119119
return this.ide.showQuickPick(languageIds, {
120120
title: "Select language to record scope tests for",
121+
defaultValue: this.ide.activeTextEditor?.document.languageId,
121122
});
122123
}
123124
}

packages/test-case-recorder/src/TestCaseRecorder.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -418,10 +418,7 @@ export class TestCaseRecorder {
418418
walkDirsSync(this.fixtureRoot).concat("/"),
419419
{
420420
title: "Select directory for new test cases",
421-
unknownValues: {
422-
allowed: true,
423-
newValueTemplate: "Create new directory '{}' →",
424-
},
421+
unknownValues: "Create new directory '{}' →",
425422
},
426423
);
427424

0 commit comments

Comments
 (0)