-
Notifications
You must be signed in to change notification settings - Fork 29.9k
/
searchEditorInput.ts
359 lines (293 loc) · 12.9 KB
/
searchEditorInput.ts
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as network from 'vs/base/common/network';
import { endsWith } from 'vs/base/common/strings';
import { URI } from 'vs/base/common/uri';
import 'vs/css!./media/searchEditor';
import { ITextModel, ITextBufferFactory } from 'vs/editor/common/model';
import { localize } from 'vs/nls';
import { IInstantiationService, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { IEditorInputFactory, GroupIdentifier, EditorInput, IRevertOptions, ISaveOptions, IEditorInput } from 'vs/workbench/common/editor';
import { IModelService } from 'vs/editor/common/services/modelService';
import { IModeService } from 'vs/editor/common/services/modeService';
import { IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService';
import { ITextFileSaveOptions, ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
import type { IWorkbenchContribution } from 'vs/workbench/common/contributions';
import { FileEditorInput } from 'vs/workbench/contrib/files/common/editors/fileEditorInput';
import { joinPath, isEqual, toLocalResource } from 'vs/base/common/resources';
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
import { IFileDialogService } from 'vs/platform/dialogs/common/dialogs';
import { basename } from 'vs/base/common/path';
import { IWorkingCopyService, WorkingCopyCapabilities, IWorkingCopy, IWorkingCopyBackup } from 'vs/workbench/services/workingCopy/common/workingCopyService';
import { IBackupFileService } from 'vs/workbench/services/backup/common/backup';
import { extractSearchQuery, serializeSearchConfiguration } from 'vs/workbench/contrib/search/browser/searchEditorSerialization';
import type { ICodeEditorViewState } from 'vs/editor/common/editorCommon';
import { IFilesConfigurationService, AutoSaveMode } from 'vs/workbench/services/filesConfiguration/common/filesConfigurationService';
import { Emitter, Event } from 'vs/base/common/event';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
export type SearchConfiguration = {
query: string,
includes: string,
excludes: string
contextLines: number,
wholeWord: boolean,
caseSensitive: boolean,
regexp: boolean,
useIgnores: boolean,
showIncludesExcludes: boolean,
};
type SearchEditorViewState =
| { focused: 'input' }
| { focused: 'editor', state: ICodeEditorViewState };
const searchEditorScheme = 'search-editor';
export class SearchEditorInput extends EditorInput {
static readonly ID: string = 'workbench.editorinputs.searchEditorInput';
private dirty: boolean = false;
private readonly model: Promise<ITextModel>;
private query: Partial<SearchConfiguration> | undefined;
private readonly _onDidChangeContent = new Emitter<void>();
readonly onDidChangeContent: Event<void> = this._onDidChangeContent.event;
viewState: SearchEditorViewState = { focused: 'input' };
constructor(
public readonly resource: URI,
getModel: () => Promise<ITextModel>,
startingConfig: Partial<SearchConfiguration> | undefined,
@IModelService private readonly modelService: IModelService,
@IEditorService protected readonly editorService: IEditorService,
@IEditorGroupsService protected readonly editorGroupService: IEditorGroupsService,
@ITextFileService protected readonly textFileService: ITextFileService,
@IWorkbenchEnvironmentService private readonly environmentService: IWorkbenchEnvironmentService,
@IFileDialogService private readonly fileDialogService: IFileDialogService,
@IInstantiationService private readonly instantiationService: IInstantiationService,
@IWorkingCopyService private readonly workingCopyService: IWorkingCopyService,
@IFilesConfigurationService private readonly filesConfigurationService: IFilesConfigurationService,
@ITelemetryService private readonly telemetryService: ITelemetryService,
) {
super();
this.model = getModel()
.then(model => {
this._register(model.onDidChangeContent(() => this._onDidChangeContent.fire()));
return model;
});
const input = this;
const workingCopyAdapter = new class implements IWorkingCopy {
readonly resource = input.getResource();
get name() { return input.getName(); }
readonly capabilities = input.isUntitled() ? WorkingCopyCapabilities.Untitled : 0;
readonly onDidChangeDirty = input.onDidChangeDirty;
readonly onDidChangeContent = input.onDidChangeContent;
isDirty(): boolean { return input.isDirty(); }
backup(): Promise<IWorkingCopyBackup> { return input.backup(); }
save(options?: ISaveOptions): Promise<boolean> { return input.save(0, options).then(editor => !!editor); }
revert(options?: IRevertOptions): Promise<boolean> { return input.revert(0, options); }
};
this.workingCopyService.registerWorkingCopy(workingCopyAdapter);
this.query = startingConfig;
}
getResource() {
return this.resource;
}
async save(group: GroupIdentifier, options?: ITextFileSaveOptions): Promise<IEditorInput | undefined> {
if (this.isUntitled()) {
return this.saveAs(group, options);
} else {
await this.textFileService.write(this.resource, (await this.model).getValue(), options);
this.setDirty(false);
return this;
}
}
async saveAs(group: GroupIdentifier, options?: ITextFileSaveOptions): Promise<IEditorInput | undefined> {
const path = await this.fileDialogService.pickFileToSave(await this.suggestFileName(), options?.availableFileSystems);
if (path) {
this.telemetryService.publicLog2('searchEditor/saveSearchResults');
if (await this.textFileService.saveAs(this.resource, path, options)) {
this.setDirty(false);
if (!isEqual(path, this.resource)) {
return this.instantiationService.invokeFunction(getOrMakeSearchEditorInput, { uri: path });
}
return this;
}
}
return undefined;
}
getTypeId(): string {
return SearchEditorInput.ID;
}
getName(): string {
if (this.isUntitled()) {
return (this.query?.query
? localize('searchTitle.withQuery', "Search: {0}", this.query.query)
: localize('searchTitle', "Search"));
}
return localize('searchTitle.withQuery', "Search: {0}", basename(this.resource.path, '.code-search'));
}
async reloadModel() {
const model = await this.model;
const query = extractSearchQuery(model);
this.query = query;
this._onDidChangeLabel.fire();
return { model, query };
}
getConfigSync() {
return this.query;
}
async resolve() {
return null;
}
setDirty(dirty: boolean) {
this.dirty = dirty;
this._onDidChangeDirty.fire();
}
isDirty() {
return this.dirty;
}
isSaving(): boolean {
if (!this.isDirty()) {
return false; // the editor needs to be dirty for being saved
}
if (this.isUntitled()) {
return false; // untitled are not saving automatically
}
if (this.filesConfigurationService.getAutoSaveMode() === AutoSaveMode.AFTER_SHORT_DELAY) {
return true; // a short auto save is configured, treat this as being saved
}
return false;
}
isReadonly() {
return false;
}
isUntitled() {
return this.resource.scheme === searchEditorScheme;
}
dispose() {
this.modelService.destroyModel(this.resource);
super.dispose();
}
matches(other: unknown) {
if (this === other) { return true; }
if (other instanceof SearchEditorInput) {
if (
(other.resource.path && other.resource.path === this.resource.path) ||
(other.resource.fragment && other.resource.fragment === this.resource.fragment)
) {
return true;
}
}
return false;
}
async revert(group: GroupIdentifier, options?: IRevertOptions) {
// TODO: this should actually revert the contents. But it needs to set dirty false.
super.revert(group, options);
this.setDirty(false);
return true;
}
private async backup(): Promise<IWorkingCopyBackup> {
const content = (await this.model).createSnapshot();
return { content };
}
// Bringing this over from textFileService because it only suggests for untitled scheme.
// In the future I may just use the untitled scheme. I dont get particular benefit from using search-editor...
private async suggestFileName(): Promise<URI> {
const query = (await this.reloadModel()).query.query;
const searchFileName = (query.replace(/[^\w \-_]+/g, '_') || 'Search') + '.code-search';
const remoteAuthority = this.environmentService.configuration.remoteAuthority;
const schemeFilter = remoteAuthority ? network.Schemas.vscodeRemote : network.Schemas.file;
const defaultFilePath = this.fileDialogService.defaultFilePath(schemeFilter);
if (defaultFilePath) {
return joinPath(defaultFilePath, searchFileName);
}
return toLocalResource(URI.from({ scheme: schemeFilter, path: searchFileName }), remoteAuthority);
}
}
export class SearchEditorContribution implements IWorkbenchContribution {
constructor(
@IEditorService private readonly editorService: IEditorService,
@ITextFileService protected readonly textFileService: ITextFileService,
@IInstantiationService protected readonly instantiationService: IInstantiationService,
@IModelService protected readonly modelService: IModelService,
@ITelemetryService protected readonly telemetryService: ITelemetryService,
) {
this.editorService.overrideOpenEditor((editor, options, group) => {
const resource = editor.getResource();
if (!resource ||
!(endsWith(resource.path, '.code-search') || resource.scheme === searchEditorScheme) ||
!(editor instanceof FileEditorInput || (resource.scheme === searchEditorScheme))) {
return undefined;
}
if (group.isOpened(editor)) {
return undefined;
}
this.telemetryService.publicLog2('searchEditor/openSavedSearchEditor');
const input = instantiationService.invokeFunction(getOrMakeSearchEditorInput, { uri: resource });
const opened = editorService.openEditor(input, { ...options, pinned: resource.scheme === searchEditorScheme, ignoreOverrides: true }, group);
return { override: Promise.resolve(opened) };
});
}
}
export class SearchEditorInputFactory implements IEditorInputFactory {
canSerialize() { return true; }
serialize(input: SearchEditorInput) {
let resource = undefined;
if (input.resource.path || input.resource.fragment) {
resource = input.resource.toString();
}
const config = input.getConfigSync();
return JSON.stringify({ resource, dirty: input.isDirty(), config, viewState: input.viewState, name: input.getName() });
}
deserialize(instantiationService: IInstantiationService, serializedEditorInput: string): SearchEditorInput | undefined {
const { resource, dirty, config, viewState } = JSON.parse(serializedEditorInput);
if (config && (config.query !== undefined)) {
const input = instantiationService.invokeFunction(getOrMakeSearchEditorInput, { config, uri: URI.parse(resource) });
input.viewState = viewState;
input.setDirty(dirty);
return input;
}
return undefined;
}
}
const inputs = new Map<string, SearchEditorInput>();
export const getOrMakeSearchEditorInput = (
accessor: ServicesAccessor,
existingData:
{ uri: URI, config?: Partial<SearchConfiguration>, text?: never } |
{ text: string, uri?: never, config?: never } |
{ config: Partial<SearchConfiguration>, text?: never, uri?: never }
): SearchEditorInput => {
const uri = existingData.uri ?? URI.from({ scheme: searchEditorScheme, fragment: `${Math.random()}` });
const instantiationService = accessor.get(IInstantiationService);
const modelService = accessor.get(IModelService);
const textFileService = accessor.get(ITextFileService);
const backupService = accessor.get(IBackupFileService);
const modeService = accessor.get(IModeService);
const existing = inputs.get(uri.toString());
if (existing) {
return existing;
}
const config = existingData.config ?? (existingData.text ? extractSearchQuery(existingData.text) : {});
const getModel = async () => {
const existing = modelService.getModel(uri);
if (existing) { return existing; }
const backup = await backupService.resolve(uri);
backupService.discardBackup(uri);
let contents: string | ITextBufferFactory;
if (backup) {
contents = backup.value;
} else if (uri.scheme !== searchEditorScheme) {
contents = (await textFileService.read(uri)).value;
} else if (existingData.text) {
contents = existingData.text;
} else if (existingData.config) {
contents = serializeSearchConfiguration(existingData.config);
} else {
throw new Error('no initial contents for search editor');
}
return modelService.createModel(contents, modeService.create('search-result'), uri);
};
const input = instantiationService.createInstance(SearchEditorInput, uri, getModel, config);
inputs.set(uri.toString(), input);
input.onDispose(() => inputs.delete(uri.toString()));
return input;
};