forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettingsUtils.ts
More file actions
178 lines (167 loc) · 7.32 KB
/
Copy pathsettingsUtils.ts
File metadata and controls
178 lines (167 loc) · 7.32 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as vscode from 'vscode';
import { commands } from './executeCommands';
import { CHAT_SETTINGS_NAMESPACE, DISABLE_AI_FEATURES, PR_SETTINGS_NAMESPACE, QUERIES, USE_REVIEW_MODE } from './settingKeys';
export function getReviewMode(): { merged: boolean, closed: boolean } {
const desktopDefaults = { merged: false, closed: false };
const config = vscode.workspace.getConfiguration(PR_SETTINGS_NAMESPACE)
.get<{ merged: boolean, closed: boolean } | 'auto'>(USE_REVIEW_MODE, desktopDefaults);
if (config !== 'auto') {
return config;
}
if (vscode.env.appHost === 'vscode.dev' || vscode.env.appHost === 'github.dev') {
return { merged: true, closed: true };
}
return desktopDefaults;
}
export function initBasedOnSettingChange(namespace: string, key: string, isEnabled: () => boolean, initializer: () => void, disposables: vscode.Disposable[]): void {
const eventDisposable = vscode.workspace.onDidChangeConfiguration((e) => {
if (e.affectsConfiguration(`${namespace}.${key}`)) {
if (isEnabled()) {
initializer();
eventDisposable.dispose();
}
}
});
disposables.push(eventDisposable);
}
interface QueryInspect {
key: string;
defaultValue?: { label: string; query: string }[];
globalValue?: { label: string; query: string }[];
workspaceValue?: { label: string; query: string }[];
workspaceFolderValue?: { label: string; query: string }[];
defaultLanguageValue?: { label: string; query: string }[];
globalLanguageValue?: { label: string; query: string }[];
workspaceLanguageValue?: { label: string; query: string }[];
workspaceFolderLanguageValue?: { label: string; query: string }[];
languageIds?: string[]
}
export function editQuery(namespace: string, queryName: string) {
const config = vscode.workspace.getConfiguration(namespace);
const inspect = config.inspect<{ label: string; query: string }[]>(QUERIES);
const queryValue = config.get<{ label: string; query: string }[]>(QUERIES)?.find((query) => query.label === queryName)?.query;
const inputBox = vscode.window.createQuickPick();
inputBox.title = vscode.l10n.t('Edit Query "{0}"', queryName ?? '');
inputBox.value = queryValue ?? '';
const items: vscode.QuickPickItem[] = [
{ iconPath: new vscode.ThemeIcon('pencil'), label: vscode.l10n.t('Save edits'), alwaysShow: true },
{ iconPath: new vscode.ThemeIcon('add'), label: vscode.l10n.t('Add new query'), alwaysShow: true },
{ iconPath: new vscode.ThemeIcon('settings'), label: vscode.l10n.t('Edit in settings.json'), alwaysShow: true }
];
const aiDisabled = vscode.workspace.getConfiguration(CHAT_SETTINGS_NAMESPACE).get<boolean>(DISABLE_AI_FEATURES, false);
const editWithAIItem = { iconPath: new vscode.ThemeIcon('sparkle'), label: vscode.l10n.t('Edit with AI'), alwaysShow: true };
if (!aiDisabled) {
items.push(editWithAIItem);
}
inputBox.items = items;
inputBox.activeItems = [];
inputBox.selectedItems = [];
inputBox.onDidAccept(async () => {
inputBox.busy = true;
if (inputBox.selectedItems[0] === inputBox.items[0]) {
const newQuery = inputBox.value;
if (newQuery !== queryValue) {
let newValue: { label: string; query: string }[];
let target: vscode.ConfigurationTarget;
if (inspect?.workspaceFolderValue) {
target = vscode.ConfigurationTarget.WorkspaceFolder;
newValue = inspect.workspaceFolderValue;
} else if (inspect?.workspaceValue) {
target = vscode.ConfigurationTarget.Workspace;
newValue = inspect.workspaceValue;
} else {
target = vscode.ConfigurationTarget.Global;
newValue = config.get<{ label: string; query: string }[]>(QUERIES) ?? [];
}
newValue.find((query) => query.label === queryName)!.query = newQuery;
await config.update(QUERIES, newValue, target);
}
inputBox.dispose();
} else if (inputBox.selectedItems[0] === inputBox.items[1]) {
addNewQuery(config, inspect, inputBox.value);
inputBox.dispose();
} else if (inputBox.selectedItems[0] === inputBox.items[2]) {
openSettingsAtQuery(config, inspect, queryName);
inputBox.dispose();
} else if (inputBox.selectedItems[0] === editWithAIItem) {
inputBox.ignoreFocusOut = true;
await openCopilotForQuery(inputBox.value);
inputBox.busy = false;
}
});
inputBox.onDidHide(() => inputBox.dispose());
inputBox.show();
}
function addNewQuery(config: vscode.WorkspaceConfiguration, inspect: QueryInspect | undefined, startingValue: string) {
const inputBox = vscode.window.createInputBox();
inputBox.title = vscode.l10n.t('Enter the title of the new query');
inputBox.placeholder = vscode.l10n.t('Title');
inputBox.step = 1;
inputBox.totalSteps = 2;
inputBox.show();
let title: string | undefined;
inputBox.onDidAccept(async () => {
inputBox.validationMessage = '';
if (inputBox.step === 1) {
if (!inputBox.value) {
inputBox.validationMessage = vscode.l10n.t('Title is required');
return;
}
title = inputBox.value;
inputBox.value = startingValue;
inputBox.title = vscode.l10n.t('Enter the GitHub search query');
inputBox.step++;
} else {
if (!inputBox.value) {
inputBox.validationMessage = vscode.l10n.t('Query is required');
return;
}
inputBox.busy = true;
if (inputBox.value && title) {
if (inspect?.workspaceValue) {
inspect.workspaceValue.push({ label: title, query: inputBox.value });
await config.update(QUERIES, inspect.workspaceValue, vscode.ConfigurationTarget.Workspace);
} else {
const value = config.get<{ label: string; query: string }[]>(QUERIES);
value?.push({ label: title, query: inputBox.value });
await config.update(QUERIES, value, vscode.ConfigurationTarget.Global);
}
}
inputBox.dispose();
}
});
inputBox.onDidHide(() => inputBox.dispose());
}
async function openSettingsAtQuery(config: vscode.WorkspaceConfiguration, inspect: QueryInspect | undefined, queryName: string) {
let command: string;
if (inspect?.workspaceValue) {
command = 'workbench.action.openWorkspaceSettingsFile';
} else {
const value = config.get<{ label: string; query: string }[]>(QUERIES);
if (inspect?.defaultValue && JSON.stringify(inspect?.defaultValue) === JSON.stringify(value)) {
await config.update(QUERIES, inspect.defaultValue, vscode.ConfigurationTarget.Global);
}
command = 'workbench.action.openSettingsJson';
}
await vscode.commands.executeCommand(command);
const editor = vscode.window.activeTextEditor;
if (editor) {
const text = editor.document.getText();
const search = text.search(queryName);
if (search >= 0) {
const position = editor.document.positionAt(search);
editor.revealRange(new vscode.Range(position, position));
editor.selection = new vscode.Selection(position, position);
}
}
}
async function openCopilotForQuery(currentQuery: string) {
const chatMessage = vscode.l10n.t('I want to edit this GitHub search query: \n```\n{0}\n```\nOutput only one, minimally modified query in a codeblock.\nModify it so that it ', currentQuery);
// Open chat with the query pre-populated
await vscode.commands.executeCommand(commands.NEW_CHAT, { inputValue: chatMessage, isPartialQuery: true, agentMode: false });
}