Skip to content

Commit

Permalink
Remove TPromise from search land #53526
Browse files Browse the repository at this point in the history
  • Loading branch information
roblourens committed Nov 7, 2018
1 parent 9669edc commit 9bea4fa
Show file tree
Hide file tree
Showing 16 changed files with 155 additions and 138 deletions.
33 changes: 33 additions & 0 deletions src/vs/base/test/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,39 @@ export class DeferredTPromise<T> extends TPromise<T> {
}
}

export class DeferredPromise<T> {

private completeCallback: TValueCallback<T>;
private errorCallback: (err: any) => void;

public p: Promise<any>;

constructor() {
this.p = new Promise<any>((c, e) => {
this.completeCallback = c;
this.errorCallback = e;
});
}

public complete(value: T) {
process.nextTick(() => {
this.completeCallback(value);
});
}

public error(err: any) {
process.nextTick(() => {
this.errorCallback(err);
});
}

public cancel() {
process.nextTick(() => {
this.errorCallback(canceled());
});
}
}

export function toResource(this: any, path: string) {
return URI.file(paths.join('C:\\', Buffer.from(this.test.fullTitle()).toString('base64'), path));
}
Expand Down
21 changes: 10 additions & 11 deletions src/vs/workbench/parts/search/browser/replaceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import * as nls from 'vs/nls';
import * as errors from 'vs/base/common/errors';
import { TPromise } from 'vs/base/common/winjs.base';
import { URI } from 'vs/base/common/uri';
import * as network from 'vs/base/common/network';
import { Disposable } from 'vs/base/common/lifecycle';
Expand Down Expand Up @@ -47,7 +46,7 @@ export class ReplacePreviewContentProvider implements ITextModelContentProvider,
this.textModelResolverService.registerTextModelContentProvider(network.Schemas.internal, this);
}

public provideTextContent(uri: URI): TPromise<ITextModel> {
public provideTextContent(uri: URI): Thenable<ITextModel> {
if (uri.fragment === REPLACE_PREVIEW) {
return this.instantiationService.createInstance(ReplacePreviewModel).resolve(uri);
}
Expand All @@ -66,7 +65,7 @@ class ReplacePreviewModel extends Disposable {
super();
}

resolve(replacePreviewUri: URI): TPromise<ITextModel> {
resolve(replacePreviewUri: URI): Thenable<ITextModel> {
const fileResource = toFileResource(replacePreviewUri);
const fileMatch = <FileMatch>this.searchWorkbenchService.searchModel.searchResult.matches().filter(match => match.resource().toString() === fileResource.toString())[0];
return this.textModelResolverService.createModelReference(fileResource).then(ref => {
Expand Down Expand Up @@ -101,17 +100,17 @@ export class ReplaceService implements IReplaceService {
@IBulkEditService private bulkEditorService: IBulkEditService
) { }

public replace(match: Match): TPromise<any>;
public replace(files: FileMatch[], progress?: IProgressRunner): TPromise<any>;
public replace(match: FileMatchOrMatch, progress?: IProgressRunner, resource?: URI): TPromise<any>;
public replace(arg: any, progress: IProgressRunner | null = null, resource: URI | null = null): TPromise<any> {
public replace(match: Match): Promise<any>;
public replace(files: FileMatch[], progress?: IProgressRunner): Promise<any>;
public replace(match: FileMatchOrMatch, progress?: IProgressRunner, resource?: URI): Promise<any>;
public replace(arg: any, progress: IProgressRunner | null = null, resource: URI | null = null): Promise<any> {

const edits: ResourceTextEdit[] = this.createEdits(arg, resource);
return this.bulkEditorService.apply({ edits }, { progress }).then(() => this.textFileService.saveAll(edits.map(e => e.resource)));

}

public openReplacePreview(element: FileMatchOrMatch, preserveFocus?: boolean, sideBySide?: boolean, pinned?: boolean): TPromise<any> {
public openReplacePreview(element: FileMatchOrMatch, preserveFocus?: boolean, sideBySide?: boolean, pinned?: boolean): Thenable<any> {
const fileMatch = element instanceof Match ? element.parent() : element;

return this.editorService.openEditor({
Expand Down Expand Up @@ -139,13 +138,13 @@ export class ReplaceService implements IReplaceService {
}, errors.onUnexpectedError);
}

public updateReplacePreview(fileMatch: FileMatch, override: boolean = false): TPromise<void> {
public updateReplacePreview(fileMatch: FileMatch, override: boolean = false): Promise<void> {
const replacePreviewUri = toReplaceResource(fileMatch.resource());
return TPromise.join([this.textModelResolverService.createModelReference(fileMatch.resource()), this.textModelResolverService.createModelReference(replacePreviewUri)])
return Promise.all([this.textModelResolverService.createModelReference(fileMatch.resource()), this.textModelResolverService.createModelReference(replacePreviewUri)])
.then(([sourceModelRef, replaceModelRef]) => {
const sourceModel = sourceModelRef.object.textEditorModel;
const replaceModel = replaceModelRef.object.textEditorModel;
let returnValue = TPromise.wrap(null);
let returnValue = Promise.resolve(null);
// If model is disposed do not update
if (sourceModel && replaceModel) {
if (override) {
Expand Down
51 changes: 25 additions & 26 deletions src/vs/workbench/parts/search/browser/searchActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { normalizeDriveLetter } from 'vs/base/common/labels';
import { Schemas } from 'vs/base/common/network';
import { isWindows, OS } from 'vs/base/common/platform';
import { URI } from 'vs/base/common/uri';
import { TPromise } from 'vs/base/common/winjs.base';
import { ITree } from 'vs/base/parts/tree/browser/tree';
import * as nls from 'vs/nls';
import { IClipboardService } from 'vs/platform/clipboard/common/clipboardService';
Expand Down Expand Up @@ -45,7 +44,7 @@ export function appendKeyBindingLabel(label: string, keyBinding: number | Resolv
}
}

export function openSearchView(viewletService: IViewletService, panelService: IPanelService, focus?: boolean): TPromise<SearchView> {
export function openSearchView(viewletService: IViewletService, panelService: IPanelService, focus?: boolean): Thenable<SearchView> {
if (viewletService.getViewlets().filter(v => v.id === VIEW_ID).length) {
return viewletService.openViewlet(VIEW_ID, focus).then(viewlet => <SearchView>viewlet);
}
Expand Down Expand Up @@ -97,10 +96,10 @@ export class FocusNextInputAction extends Action {
super(id, label);
}

public run(): TPromise<any> {
public run(): Thenable<any> {
const searchView = getSearchView(this.viewletService, this.panelService);
searchView.focusNextInputBox();
return TPromise.as(null);
return Promise.resolve(null);
}
}

Expand All @@ -115,10 +114,10 @@ export class FocusPreviousInputAction extends Action {
super(id, label);
}

public run(): TPromise<any> {
public run(): Thenable<any> {
const searchView = getSearchView(this.viewletService, this.panelService);
searchView.focusPreviousInputBox();
return TPromise.as(null);
return Promise.resolve(null);
}
}

Expand All @@ -130,7 +129,7 @@ export abstract class FindOrReplaceInFilesAction extends Action {
super(id, label);
}

public run(): TPromise<any> {
public run(): Thenable<any> {
return openSearchView(this.viewletService, this.panelService, false).then(openedView => {
const searchAndReplaceWidget = openedView.searchAndReplaceWidget;
searchAndReplaceWidget.toggleReplace(this.expandSearchReplaceWidget);
Expand Down Expand Up @@ -165,7 +164,7 @@ export class OpenSearchViewletAction extends FindOrReplaceInFilesAction {
super(id, label, viewletService, panelService, /*expandSearchReplaceWidget=*/false);
}

public run(): TPromise<any> {
public run(): Thenable<any> {

// Pass focus to viewlet if not open or focused
if (this.otherViewletShowing() || !isSearchViewFocused(this.viewletService, this.panelService)) {
Expand All @@ -175,7 +174,7 @@ export class OpenSearchViewletAction extends FindOrReplaceInFilesAction {
// Otherwise pass focus to editor group
this.editorGroupService.activeGroup.focus();

return TPromise.as(true);
return Promise.resolve(true);
}

private otherViewletShowing(): boolean {
Expand Down Expand Up @@ -205,11 +204,11 @@ export class CloseReplaceAction extends Action {
super(id, label);
}

public run(): TPromise<any> {
public run(): Thenable<any> {
const searchView = getSearchView(this.viewletService, this.panelService);
searchView.searchAndReplaceWidget.toggleReplace(false);
searchView.searchAndReplaceWidget.focus();
return TPromise.as(null);
return Promise.resolve(null);
}
}

Expand All @@ -231,12 +230,12 @@ export class RefreshAction extends Action {
this.enabled = searchView && searchView.isSearchSubmitted();
}

public run(): TPromise<void> {
public run(): Thenable<void> {
const searchView = getSearchView(this.viewletService, this.panelService);
if (searchView) {
searchView.onQueryChanged();
}
return TPromise.as(null);
return Promise.resolve(null);
}
}

Expand All @@ -258,12 +257,12 @@ export class CollapseDeepestExpandedLevelAction extends Action {
this.enabled = searchView && searchView.hasSearchResults();
}

public run(): TPromise<void> {
public run(): Thenable<void> {
const searchView = getSearchView(this.viewletService, this.panelService);
if (searchView) {
const viewer = searchView.getControl();
if (viewer.getHighlight()) {
return TPromise.as(null); // Global action disabled if user is in edit mode from another action
return Promise.resolve(null); // Global action disabled if user is in edit mode from another action
}

/**
Expand Down Expand Up @@ -299,7 +298,7 @@ export class CollapseDeepestExpandedLevelAction extends Action {
viewer.domFocus();
viewer.focusFirst();
}
return TPromise.as(null);
return Promise.resolve(null);
}
}

Expand All @@ -321,12 +320,12 @@ export class ClearSearchResultsAction extends Action {
this.enabled = searchView && searchView.isSearchSubmitted();
}

public run(): TPromise<void> {
public run(): Thenable<void> {
const searchView = getSearchView(this.viewletService, this.panelService);
if (searchView) {
searchView.clearSearchResults();
}
return TPromise.as(null);
return Promise.resolve(null);
}
}

Expand All @@ -348,13 +347,13 @@ export class CancelSearchAction extends Action {
this.enabled = searchView && searchView.isSearching();
}

public run(): TPromise<void> {
public run(): Thenable<void> {
const searchView = getSearchView(this.viewletService, this.panelService);
if (searchView) {
searchView.cancelSearch();
}

return TPromise.as(null);
return Promise.resolve(null);
}
}

Expand All @@ -369,7 +368,7 @@ export class FocusNextSearchResultAction extends Action {
super(id, label);
}

public run(): TPromise<any> {
public run(): Thenable<any> {
return openSearchView(this.viewletService, this.panelService).then(searchView => {
searchView.selectNextMatch();
});
Expand All @@ -387,7 +386,7 @@ export class FocusPreviousSearchResultAction extends Action {
super(id, label);
}

public run(): TPromise<any> {
public run(): Thenable<any> {
return openSearchView(this.viewletService, this.panelService).then(searchView => {
searchView.selectPreviousMatch();
});
Expand Down Expand Up @@ -470,7 +469,7 @@ export class RemoveAction extends AbstractSearchAndReplaceAction {
super('remove', RemoveAction.LABEL, 'action-remove');
}

public run(): TPromise<any> {
public run(): Thenable<any> {
const currentFocusElement = this.viewer.getFocus();
const nextFocusElement = !currentFocusElement || currentFocusElement instanceof SearchResult || elementIsEqualOrParent(currentFocusElement, this.element) ?
this.getElementToFocusAfterRemoved(this.viewer, this.element) :
Expand Down Expand Up @@ -521,7 +520,7 @@ export class ReplaceAllAction extends AbstractSearchAndReplaceAction {
super(Constants.ReplaceAllInFileActionId, appendKeyBindingLabel(ReplaceAllAction.LABEL, keyBindingService.lookupKeybinding(Constants.ReplaceAllInFileActionId), keyBindingService), 'action-replace-all');
}

public run(): TPromise<any> {
public run(): Thenable<any> {
let nextFocusElement = this.getElementToFocusAfterRemoved(this.viewer, this.fileMatch);
return this.fileMatch.parent().replace(this.fileMatch).then(() => {
if (nextFocusElement) {
Expand All @@ -543,7 +542,7 @@ export class ReplaceAllInFolderAction extends AbstractSearchAndReplaceAction {
super(Constants.ReplaceAllInFolderActionId, appendKeyBindingLabel(ReplaceAllInFolderAction.LABEL, keyBindingService.lookupKeybinding(Constants.ReplaceAllInFolderActionId), keyBindingService), 'action-replace-all');
}

public run(): TPromise<any> {
public run(): Thenable<any> {
let nextFocusElement = this.getElementToFocusAfterRemoved(this.viewer, this.folderMatch);
return this.folderMatch.replaceAll()
.then(() => {
Expand All @@ -567,7 +566,7 @@ export class ReplaceAction extends AbstractSearchAndReplaceAction {
super(Constants.ReplaceActionId, appendKeyBindingLabel(ReplaceAction.LABEL, keyBindingService.lookupKeybinding(Constants.ReplaceActionId), keyBindingService), 'action-replace');
}

public run(): TPromise<any> {
public run(): Thenable<any> {
this.enabled = false;

return this.element.parent().replace(this.element).then(() => {
Expand Down
11 changes: 5 additions & 6 deletions src/vs/workbench/parts/search/browser/searchResultsView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { IAction } from 'vs/base/common/actions';
import { Disposable, dispose, IDisposable } from 'vs/base/common/lifecycle';
import * as paths from 'vs/base/common/paths';
import * as resources from 'vs/base/common/resources';
import { TPromise } from 'vs/base/common/winjs.base';
import { ContextMenuEvent, IAccessibilityProvider, IDataSource, IFilter, IRenderer, ISorter, ITree } from 'vs/base/parts/tree/browser/tree';
import * as nls from 'vs/nls';
import { fillInContextMenuActions } from 'vs/platform/actions/browser/menuItemActionItem';
Expand Down Expand Up @@ -80,15 +79,15 @@ export class SearchDataSource implements IDataSource {
return [];
}

public getChildren(tree: ITree, element: any): TPromise<any[]> {
return TPromise.as(this._getChildren(element));
public getChildren(tree: ITree, element: any): Thenable<any[]> {
return Promise.resolve(this._getChildren(element));
}

public hasChildren(tree: ITree, element: any): boolean {
return element instanceof FileMatch || element instanceof FolderMatch || element instanceof SearchResult;
}

public getParent(tree: ITree, element: any): TPromise<any> {
public getParent(tree: ITree, element: any): Thenable<any> {
let value: any = null;

if (element instanceof Match) {
Expand All @@ -99,7 +98,7 @@ export class SearchDataSource implements IDataSource {
value = element.parent();
}

return TPromise.as(value);
return Promise.resolve(value);
}

public shouldAutoexpand(tree: ITree, element: any): boolean {
Expand Down Expand Up @@ -425,7 +424,7 @@ export class SearchTreeController extends WorkbenchTreeController {
getActions: () => {
const actions: IAction[] = [];
fillInContextMenuActions(this.contextMenu, { shouldForwardArgs: true }, actions, this.contextMenuService);
return actions;
return Promise.resolve(actions);
},

getActionsContext: () => element
Expand Down
Loading

0 comments on commit 9bea4fa

Please sign in to comment.