Skip to content

Commit 9e69ee9

Browse files
author
Jackson Kearl
committed
Clean up some confguration handling logic
1 parent 5c610fb commit 9e69ee9

4 files changed

Lines changed: 27 additions & 25 deletions

File tree

src/vs/workbench/contrib/search/browser/searchEditor.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,10 @@ export class SearchEditor extends BaseEditor {
365365
await super.setInput(newInput, options, token);
366366
this.inSearchEditorContextKey.set(true);
367367

368-
const { model } = await newInput.reloadModel();
368+
const { model, query } = await newInput.reloadModel();
369369
this.searchResultEditor.setModel(model);
370370

371371
this.pauseSearching = true;
372-
const { query } = await newInput.reloadModel();
373372

374373
this.queryEditorWidget.setValue(query.query, true);
375374
this.queryEditorWidget.searchInput.setCaseSensitive(query.caseSensitive);

src/vs/workbench/contrib/search/browser/searchEditorActions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { SearchResult } from 'vs/workbench/contrib/search/common/searchModel';
1414
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
1515
import { SearchEditor } from 'vs/workbench/contrib/search/browser/searchEditor';
1616
import { getOrMakeSearchEditorInput, SearchEditorInput } from 'vs/workbench/contrib/search/browser/searchEditorInput';
17-
import { serializeSearchResultForEditor, serializeSearchConfiguration } from 'vs/workbench/contrib/search/browser/searchEditorSerialization';
17+
import { serializeSearchResultForEditor } from 'vs/workbench/contrib/search/browser/searchEditorSerialization';
1818
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
1919

2020

@@ -48,7 +48,7 @@ export const openNewSearchEditor =
4848

4949
telemetryService.publicLog2<{}, {}>('searchEditor/openNewSearchEditor');
5050

51-
const input = instantiationService.invokeFunction(getOrMakeSearchEditorInput, { text: serializeSearchConfiguration({ query: selected }) });
51+
const input = instantiationService.invokeFunction(getOrMakeSearchEditorInput, { config: { query: selected } });
5252
await editorService.openEditor(input, { pinned: true });
5353
};
5454

src/vs/workbench/contrib/search/browser/searchEditorInput.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,14 @@ export class SearchEditorInput extends EditorInput {
5252

5353
private dirty: boolean = false;
5454
private readonly model: Promise<ITextModel>;
55-
private resolvedModel?: { model: ITextModel, query: SearchConfiguration };
55+
private query: Partial<SearchConfiguration> | undefined;
56+
5657
viewState: SearchEditorViewState = { focused: 'input' };
57-
_restoredName: string | undefined;
5858

5959
constructor(
6060
public readonly resource: URI,
6161
getModel: () => Promise<ITextModel>,
62+
startingConfig: Partial<SearchConfiguration> | undefined,
6263
@IModelService private readonly modelService: IModelService,
6364
@IEditorService protected readonly editorService: IEditorService,
6465
@IEditorGroupsService protected readonly editorGroupService: IEditorGroupsService,
@@ -87,6 +88,8 @@ export class SearchEditorInput extends EditorInput {
8788
};
8889

8990
this.workingCopyService.registerWorkingCopy(workingCopyAdapter);
91+
92+
this.query = startingConfig;
9093
}
9194

9295
getResource() {
@@ -123,10 +126,9 @@ export class SearchEditorInput extends EditorInput {
123126

124127
getName(): string {
125128
if (this.isUntitled()) {
126-
return this._restoredName ??
127-
(this.resolvedModel?.query.query
128-
? localize('searchTitle.withQuery', "Search: {0}", this.resolvedModel?.query.query)
129-
: localize('searchTitle', "Search"));
129+
return (this.query?.query
130+
? localize('searchTitle.withQuery', "Search: {0}", this.query.query)
131+
: localize('searchTitle', "Search"));
130132
}
131133

132134
return localize('searchTitle.withQuery', "Search: {0}", basename(this.resource.path, '.code-search'));
@@ -135,18 +137,13 @@ export class SearchEditorInput extends EditorInput {
135137
async reloadModel() {
136138
const model = await this.model;
137139
const query = extractSearchQuery(model);
138-
this.resolvedModel = { model, query };
139-
this._restoredName = undefined; // not needed after the model has been resolved
140+
this.query = query;
140141
this._onDidChangeLabel.fire();
141142
return { model, query };
142143
}
143144

144145
getConfigSync() {
145-
if (!this.resolvedModel) {
146-
console.error('Requested config for Search Editor before initalization');
147-
}
148-
149-
return this.resolvedModel?.query;
146+
return this.query;
150147
}
151148

152149
async resolve() {
@@ -281,11 +278,10 @@ export class SearchEditorInputFactory implements IEditorInputFactory {
281278
}
282279

283280
deserialize(instantiationService: IInstantiationService, serializedEditorInput: string): SearchEditorInput | undefined {
284-
const { resource, dirty, config, viewState, name } = JSON.parse(serializedEditorInput);
281+
const { resource, dirty, config, viewState } = JSON.parse(serializedEditorInput);
285282
if (config && (config.query !== undefined)) {
286-
const input = instantiationService.invokeFunction(getOrMakeSearchEditorInput, { text: serializeSearchConfiguration(config), uri: URI.parse(resource) });
283+
const input = instantiationService.invokeFunction(getOrMakeSearchEditorInput, { config, uri: URI.parse(resource) });
287284
input.viewState = viewState;
288-
input._restoredName = name;
289285
input.setDirty(dirty);
290286
return input;
291287
}
@@ -297,7 +293,10 @@ export class SearchEditorInputFactory implements IEditorInputFactory {
297293
const inputs = new Map<string, SearchEditorInput>();
298294
export const getOrMakeSearchEditorInput = (
299295
accessor: ServicesAccessor,
300-
existingData: { uri: URI, text?: string } | { text: string, uri?: URI }
296+
existingData:
297+
{ uri: URI, config?: Partial<SearchConfiguration>, text?: never } |
298+
{ text: string, uri?: never, config?: never } |
299+
{ config: Partial<SearchConfiguration>, text?: never, uri?: never }
301300
): SearchEditorInput => {
302301

303302
const uri = existingData.uri ?? URI.from({ scheme: searchEditorScheme, fragment: `${Math.random()}` });
@@ -313,6 +312,7 @@ export const getOrMakeSearchEditorInput = (
313312
return existing;
314313
}
315314

315+
const config = existingData.config ?? (existingData.text ? extractSearchQuery(existingData.text) : {});
316316

317317
const getModel = async () => {
318318
const existing = modelService.getModel(uri);
@@ -328,12 +328,13 @@ export const getOrMakeSearchEditorInput = (
328328
} else if (uri.scheme !== searchEditorScheme) {
329329
contents = (await textFileService.read(uri)).value;
330330
} else {
331-
contents = existingData.text ?? '';
331+
contents = existingData.text ??
332+
(existingData.config ? serializeSearchConfiguration(existingData.config) : '');
332333
}
333334
return modelService.createModel(contents, modeService.create('search-result'), uri);
334335
};
335336

336-
const input = instantiationService.createInstance(SearchEditorInput, uri, getModel);
337+
const input = instantiationService.createInstance(SearchEditorInput, uri, getModel, config);
337338

338339
inputs.set(uri.toString(), input);
339340
input.onDispose(() => inputs.delete(uri.toString()));

src/vs/workbench/contrib/search/browser/searchEditorSerialization.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,10 @@ export const serializeSearchConfiguration = (config: Partial<SearchConfiguration
151151
};
152152

153153

154-
export const extractSearchQuery = (model: ITextModel): SearchConfiguration => {
155-
const header = model.getValueInRange(new Range(1, 1, 6, 1)).split(lineDelimiter);
154+
export const extractSearchQuery = (model: ITextModel | string): SearchConfiguration => {
155+
const header = (typeof model === 'string')
156+
? model
157+
: model.getValueInRange(new Range(1, 1, 6, 1)).split(lineDelimiter);
156158

157159
const query: SearchConfiguration = {
158160
query: '',

0 commit comments

Comments
 (0)