Skip to content

Commit 7a34ec5

Browse files
committed
1 parent 057c1f1 commit 7a34ec5

File tree

4 files changed

+127
-6
lines changed

4 files changed

+127
-6
lines changed

src/vs/editor/browser/editorExtensions.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,44 @@ export abstract class EditorAction extends EditorCommand {
337337
public abstract run(accessor: ServicesAccessor, editor: ICodeEditor, args: any): void | Promise<void>;
338338
}
339339

340+
export abstract class MultiEditorAction extends EditorAction {
341+
private readonly _implementations: [number, CommandImplementation][] = [];
342+
343+
constructor(opts: IActionOptions) {
344+
super(opts);
345+
}
346+
347+
public addImplementation(priority: number, implementation: CommandImplementation): IDisposable {
348+
this._implementations.push([priority, implementation]);
349+
this._implementations.sort((a, b) => b[0] - a[0]);
350+
return {
351+
dispose: () => {
352+
for (let i = 0; i < this._implementations.length; i++) {
353+
if (this._implementations[i][1] === implementation) {
354+
this._implementations.splice(i, 1);
355+
return;
356+
}
357+
}
358+
}
359+
};
360+
}
361+
362+
public runEditorCommand(accessor: ServicesAccessor, editor: ICodeEditor, args: any): void | Promise<void> {
363+
this.reportTelemetry(accessor, editor);
364+
365+
for (const impl of this._implementations) {
366+
if (impl[1](accessor, args)) {
367+
return;
368+
}
369+
}
370+
371+
return this.run(accessor, editor, args || {});
372+
}
373+
374+
public abstract run(accessor: ServicesAccessor, editor: ICodeEditor, args: any): void | Promise<void>;
375+
376+
}
377+
340378
//#endregion EditorAction
341379

342380
//#region EditorAction2
@@ -474,6 +512,11 @@ export function registerEditorAction<T extends EditorAction>(ctor: { new(): T; }
474512
return action;
475513
}
476514

515+
export function registerMultiEditorAction<T extends MultiEditorAction>(action: T): T {
516+
EditorContributionRegistry.INSTANCE.registerEditorAction(action);
517+
return action;
518+
}
519+
477520
export function registerInstantiatedEditorAction(editorAction: EditorAction): void {
478521
EditorContributionRegistry.INSTANCE.registerEditorAction(editorAction);
479522
}

src/vs/editor/contrib/find/findController.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
99
import { Disposable } from 'vs/base/common/lifecycle';
1010
import * as strings from 'vs/base/common/strings';
1111
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
12-
import { EditorAction, EditorCommand, ServicesAccessor, registerEditorAction, registerEditorCommand, registerEditorContribution } from 'vs/editor/browser/editorExtensions';
12+
import { EditorAction, EditorCommand, ServicesAccessor, registerEditorAction, registerEditorCommand, registerEditorContribution, MultiEditorAction, registerMultiEditorAction } from 'vs/editor/browser/editorExtensions';
1313
import { IEditorContribution } from 'vs/editor/common/editorCommon';
1414
import { EditorContextKeys } from 'vs/editor/common/editorContextKeys';
1515
import { CONTEXT_FIND_INPUT_FOCUSED, CONTEXT_FIND_WIDGET_VISIBLE, FIND_IDS, FindModelBoundToEditorModel, ToggleCaseSensitiveKeybinding, ToggleRegexKeybinding, ToggleSearchScopeKeybinding, ToggleWholeWordKeybinding, CONTEXT_REPLACE_INPUT_FOCUSED } from 'vs/editor/contrib/find/findModel';
@@ -457,7 +457,7 @@ export class FindController extends CommonFindController implements IFindControl
457457
}
458458
}
459459

460-
export class StartFindAction extends EditorAction {
460+
export class StartFindAction extends MultiEditorAction {
461461

462462
constructor() {
463463
super({
@@ -706,7 +706,7 @@ export class PreviousSelectionMatchFindAction extends SelectionMatchFindAction {
706706
}
707707
}
708708

709-
export class StartFindReplaceAction extends EditorAction {
709+
export class StartFindReplaceAction extends MultiEditorAction {
710710

711711
constructor() {
712712
super({
@@ -769,15 +769,17 @@ export class StartFindReplaceAction extends EditorAction {
769769

770770
registerEditorContribution(CommonFindController.ID, FindController);
771771

772-
registerEditorAction(StartFindAction);
772+
export const EditorStartFindAction = new StartFindAction();
773+
registerMultiEditorAction(EditorStartFindAction);
773774
registerEditorAction(StartFindWithSelectionAction);
774775
registerEditorAction(NextMatchFindAction);
775776
registerEditorAction(NextMatchFindAction2);
776777
registerEditorAction(PreviousMatchFindAction);
777778
registerEditorAction(PreviousMatchFindAction2);
778779
registerEditorAction(NextSelectionMatchFindAction);
779780
registerEditorAction(PreviousSelectionMatchFindAction);
780-
registerEditorAction(StartFindReplaceAction);
781+
export const EditorStartFindReplaceAction = new StartFindReplaceAction();
782+
registerMultiEditorAction(EditorStartFindReplaceAction);
781783

782784
const FindCommand = EditorCommand.bindToContribution<CommonFindController>(CommonFindController.get);
783785

src/vs/workbench/contrib/codeEditor/browser/find/simpleFindReplaceWidget.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export abstract class SimpleFindReplaceWidget extends Widget {
4343
private readonly prevBtn: SimpleButton;
4444
private readonly nextBtn: SimpleButton;
4545

46-
private readonly _replaceInput!: ReplaceInput;
46+
protected readonly _replaceInput!: ReplaceInput;
4747
private readonly _innerReplaceDomNode!: HTMLElement;
4848
private _toggleReplaceBtn!: SimpleButton;
4949
private readonly _replaceInputFocusTracker!: dom.IFocusTracker;
@@ -372,6 +372,34 @@ export abstract class SimpleFindReplaceWidget extends Widget {
372372
}, 0);
373373
}
374374

375+
public showWithReplace(initialInput?: string, replaceInput?: string): void {
376+
if (initialInput && !this._isVisible) {
377+
this._findInput.setValue(initialInput);
378+
}
379+
380+
if (replaceInput && !this._isVisible) {
381+
this._replaceInput.setValue(replaceInput);
382+
}
383+
384+
this._isVisible = true;
385+
this._isReplaceVisible = true;
386+
this._state.change({ isReplaceRevealed: this._isReplaceVisible }, false);
387+
if (this._isReplaceVisible) {
388+
this._innerReplaceDomNode.style.display = 'flex';
389+
} else {
390+
this._innerReplaceDomNode.style.display = 'none';
391+
}
392+
393+
setTimeout(() => {
394+
dom.addClass(this._domNode, 'visible');
395+
dom.addClass(this._domNode, 'visible-transition');
396+
this._domNode.setAttribute('aria-hidden', 'false');
397+
this._updateButtons();
398+
399+
this._replaceInput.focus();
400+
}, 0);
401+
}
402+
375403
public hide(): void {
376404
if (this._isVisible) {
377405
dom.removeClass(this._domNode, 'visible-transition');

src/vs/workbench/contrib/notebook/browser/contrib/find/findController.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { getActiveNotebookEditor } from 'vs/workbench/contrib/notebook/browser/c
2727
import { FindReplaceState } from 'vs/editor/contrib/find/findState';
2828
import { INotebookSearchOptions } from 'vs/workbench/contrib/notebook/common/notebookCommon';
2929
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
30+
import { EditorStartFindAction, EditorStartFindReplaceAction } from 'vs/editor/contrib/find/findController';
3031

3132
const FIND_HIDE_TRANSITION = 'find-hide-transition';
3233
const FIND_SHOW_TRANSITION = 'find-show-transition';
@@ -285,6 +286,27 @@ export class NotebookFindWidget extends SimpleFindReplaceWidget implements INote
285286
}
286287
}
287288

289+
replace(initialFindInput?: string, initialReplaceInput?: string) {
290+
super.showWithReplace(initialFindInput, initialReplaceInput);
291+
this._replaceInput.select();
292+
293+
if (this._showTimeout === null) {
294+
if (this._hideTimeout !== null) {
295+
window.clearTimeout(this._hideTimeout);
296+
this._hideTimeout = null;
297+
this._notebookEditor.removeClassName(FIND_HIDE_TRANSITION);
298+
}
299+
300+
this._notebookEditor.addClassName(FIND_SHOW_TRANSITION);
301+
this._showTimeout = window.setTimeout(() => {
302+
this._notebookEditor.removeClassName(FIND_SHOW_TRANSITION);
303+
this._showTimeout = null;
304+
}, 200);
305+
} else {
306+
// no op
307+
}
308+
}
309+
288310
hide() {
289311
super.hide();
290312
this.set([], false);
@@ -371,3 +393,29 @@ registerAction2(class extends Action2 {
371393
controller.show();
372394
}
373395
});
396+
397+
EditorStartFindAction.addImplementation(100, (accessor: ServicesAccessor, args: any) => {
398+
let editorService = accessor.get(IEditorService);
399+
let editor = getActiveNotebookEditor(editorService);
400+
401+
if (!editor) {
402+
return false;
403+
}
404+
405+
const controller = editor.getContribution<NotebookFindWidget>(NotebookFindWidget.id);
406+
controller.show();
407+
return true;
408+
});
409+
410+
EditorStartFindReplaceAction.addImplementation(100, (accessor: ServicesAccessor, args: any) => {
411+
let editorService = accessor.get(IEditorService);
412+
let editor = getActiveNotebookEditor(editorService);
413+
414+
if (!editor) {
415+
return false;
416+
}
417+
418+
const controller = editor.getContribution<NotebookFindWidget>(NotebookFindWidget.id);
419+
controller.replace();
420+
return true;
421+
});

0 commit comments

Comments
 (0)