Skip to content

Commit

Permalink
fix #122099
Browse files Browse the repository at this point in the history
  • Loading branch information
bpasero committed Apr 24, 2021
1 parent 6c8875d commit cb6e7b3
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 25 deletions.
2 changes: 2 additions & 0 deletions src/vs/workbench/contrib/searchEditor/browser/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export const InSearchEditor = new RawContextKey<boolean>('inSearchEditor', false

export const SearchEditorScheme = 'search-editor';

export const SearchEditorWorkingCopyTypeId = 'search/editor';

export const SearchEditorFindMatchClass = 'seaarchEditorFindMatch';

export const SearchEditorID = 'workbench.editor.searchEditor';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import { IEditorService } from 'vs/workbench/services/editor/common/editorServic
import { VIEW_ID } from 'vs/workbench/services/search/common/search';
import { EditorOverride } from 'vs/platform/editor/common/editor';
import { DEFAULT_EDITOR_ASSOCIATION } from 'vs/workbench/services/editor/common/editorOverrideService';
import { IWorkingCopyEditorService } from 'vs/workbench/services/workingCopy/common/workingCopyEditorService';
import { Disposable } from 'vs/base/common/lifecycle';


const OpenInEditorCommandId = 'search.action.openInEditor';
Expand Down Expand Up @@ -538,3 +540,36 @@ registerAction2(class OpenSearchEditorAction extends Action2 {
}
});
//#endregion

//#region Search Editor Working Copy Editor Handler
class SearchEditorWorkingCopyEditorHandler extends Disposable implements IWorkbenchContribution {

constructor(
@IInstantiationService private readonly instantiationService: IInstantiationService,
@IWorkingCopyEditorService private readonly workingCopyEditorService: IWorkingCopyEditorService,
) {
super();

this.installHandler();
}

private installHandler(): void {
this._register(this.workingCopyEditorService.registerHandler({
handles: async workingCopy => {
return workingCopy.resource.scheme === SearchEditorConstants.SearchEditorScheme;
},
isOpen: async (workingCopy, editor) => {
return editor instanceof SearchEditorInput && isEqual(workingCopy.resource, editor.modelUri);
},
createEditor: async workingCopy => {
const input = this.instantiationService.invokeFunction(getOrMakeSearchEditorInput, { modelUri: workingCopy.resource, config: {} });
input.setDirty(true);

return input;
}
}));
}
}

workbenchContributionsRegistry.registerWorkbenchContribution(SearchEditorWorkingCopyEditorHandler, LifecyclePhase.Ready);
//#endregion
16 changes: 3 additions & 13 deletions src/vs/workbench/contrib/searchEditor/browser/searchEditorInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ import { IStorageService, StorageScope, StorageTarget } from 'vs/platform/storag
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { EditorInput, GroupIdentifier, IEditorInput, IMoveResult, IRevertOptions, ISaveOptions, IEditorInputFactoryRegistry, EditorExtensions, EditorResourceAccessor } from 'vs/workbench/common/editor';
import { Memento } from 'vs/workbench/common/memento';
import { SearchEditorFindMatchClass, SearchEditorScheme } from 'vs/workbench/contrib/searchEditor/browser/constants';
import { SearchEditorFindMatchClass, SearchEditorScheme, SearchEditorWorkingCopyTypeId } from 'vs/workbench/contrib/searchEditor/browser/constants';
import { SearchEditorModel } from 'vs/workbench/contrib/searchEditor/browser/searchEditorModel';
import { defaultSearchConfig, extractSearchQueryFromModel, parseSavedSearchEditor, serializeSearchConfiguration } from 'vs/workbench/contrib/searchEditor/browser/searchEditorSerialization';
import { IPathService } from 'vs/workbench/services/path/common/pathService';
import { ISearchConfigurationProperties } from 'vs/workbench/services/search/common/search';
import { ITextFileSaveOptions, ITextFileService, toBufferOrReadable } from 'vs/workbench/services/textfile/common/textfiles';
import { IWorkingCopyService } from 'vs/workbench/services/workingCopy/common/workingCopyService';
import { IWorkingCopy, IWorkingCopyBackup, NO_TYPE_ID, WorkingCopyCapabilities } from 'vs/workbench/services/workingCopy/common/workingCopy';
import { IWorkingCopy, IWorkingCopyBackup, WorkingCopyCapabilities } from 'vs/workbench/services/workingCopy/common/workingCopy';
import { CancellationToken } from 'vs/base/common/cancellation';

export type SearchConfiguration = {
Expand Down Expand Up @@ -112,17 +112,7 @@ export class SearchEditorInput extends EditorInput {

const input = this;
const workingCopyAdapter = new class implements IWorkingCopy {
// TODO@JacksonKearl consider to enable a `typeId` that is specific for custom
// editors. Using a distinct `typeId` allows the working copy to have
// any resource (including file based resources) even if other working
// copies exist with the same resource.
//
// IMPORTANT: changing the `typeId` has an impact on backups for this
// working copy. Any value that is not the empty string will be used
// as seed to the backup. Only change the `typeId` if you have implemented
// a fallback solution to resolve any existing backups that do not have
// this seed.
readonly typeId = NO_TYPE_ID;
readonly typeId = SearchEditorWorkingCopyTypeId;
readonly resource = input.modelUri;
get name() { return input.getName(); }
readonly capabilities = input.isUntitled() ? WorkingCopyCapabilities.Untitled : WorkingCopyCapabilities.None;
Expand Down
37 changes: 27 additions & 10 deletions src/vs/workbench/contrib/searchEditor/browser/searchEditorModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { SearchConfiguration } from './searchEditorInput';
import { assertIsDefined } from 'vs/base/common/types';
import { NO_TYPE_ID } from 'vs/workbench/services/workingCopy/common/workingCopy';
import { createTextBufferFactoryFromStream } from 'vs/editor/common/model/textModel';
import { SearchEditorScheme, SearchEditorWorkingCopyTypeId } from 'vs/workbench/contrib/searchEditor/browser/constants';


export class SearchEditorModel {
Expand All @@ -36,20 +37,36 @@ export class SearchEditorModel {
@IModeService private readonly modeService: IModeService) {
this.onModelResolved = new Promise<ITextModel>(resolve => this.resolveContents = resolve);
this.onModelResolved.then(model => this.cachedContentsModel = model);
this.ongoingResolve = workingCopyBackupService.resolve({ resource: modelUri, typeId: NO_TYPE_ID })
.then(backup => {
const model = modelService.getModel(modelUri);
if (model) {
return model;

this.ongoingResolve = (async () => {
let discardLegacyBackup = false;
let backup = await workingCopyBackupService.resolve({ resource: modelUri, typeId: SearchEditorWorkingCopyTypeId });
if (!backup) {
// TODO@bpasero remove this fallback after some releases
backup = await workingCopyBackupService.resolve({ resource: modelUri, typeId: NO_TYPE_ID });

if (backup && modelUri.scheme === SearchEditorScheme) {
discardLegacyBackup = true;
}
}

let model = modelService.getModel(modelUri);
if (!model && backup) {
const factory = await createTextBufferFactoryFromStream(backup.value);

if (backup) {
return createTextBufferFactoryFromStream(backup.value).then(factory => modelService.createModel(factory, modeService.create('search-result'), modelUri));
if (discardLegacyBackup) {
await workingCopyBackupService.discardBackup({ resource: modelUri, typeId: NO_TYPE_ID });
}

return undefined;
})
.then(model => { if (model) { this.resolveContents(model); } });
model = modelService.createModel(factory, modeService.create('search-result'), modelUri);
}

if (model) {
this.resolveContents(model);
}

return model;
})();
}

async resolve(): Promise<ITextModel> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,13 @@ export class LegacyWorkingCopyBackupRestorer implements IWorkbenchContribution {

protected async doRestoreLegacyBackups(): Promise<void> {

// Resolve all backup resources that exist for this window (only those without `typeId`)
const backups = (await this.workingCopyBackupService.getBackups()).filter(backup => backup.typeId.length === 0);
// Resolve all backup resources that exist for this window
// that have not yet adopted the working copy editor handler
// - any working copy without `typeId`
// - not `search-edior:/` (supports migration to typeId)
const backups = (await this.workingCopyBackupService.getBackups())
.filter(backup => backup.typeId.length === 0)
.filter(backup => backup.resource.scheme !== 'search-editor');

// Trigger `resolve` in each opened editor that can be found
// for the given resource and keep track of backups that are
Expand Down

0 comments on commit cb6e7b3

Please sign in to comment.