Skip to content

Commit

Permalink
Open the notebook editor find widget when opening a notebook from sea…
Browse files Browse the repository at this point in the history
…rch results #95275
  • Loading branch information
roblourens committed Apr 22, 2022
1 parent 118da3f commit 7135a30
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 182 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,28 @@ export class FindModel extends Disposable {
};
}

find(previous: boolean) {
find(option: { previous: boolean } | { index: number }) {
if (!this.findMatches.length) {
return;
}

// let currCell;
if (!this._findMatchesStarts) {
this.set(this._findMatches, true);
if ('index' in option) {
this._currentMatch = option.index;
}
} else {
// const currIndex = this._findMatchesStarts!.getIndexOf(this._currentMatch);
// currCell = this._findMatches[currIndex.index].cell;
const totalVal = this._findMatchesStarts.getTotalSum();
if (this._currentMatch === -1) {
this._currentMatch = previous ? totalVal - 1 : 0;
if ('index' in option) {
this._currentMatch = option.index;
}
else if (this._currentMatch === -1) {
this._currentMatch = option.previous ? totalVal - 1 : 0;
} else {
const nextVal = (this._currentMatch + (previous ? -1 : 1) + totalVal) % totalVal;
const nextVal = (this._currentMatch + (option.previous ? -1 : 1) + totalVal) % totalVal;
this._currentMatch = nextVal;
}
}
Expand Down Expand Up @@ -157,8 +163,8 @@ export class FindModel extends Disposable {
}

async research() {
this._throttledDelayer.trigger(() => {
this._research();
return this._throttledDelayer.trigger(() => {
return this._research();
});
}

Expand Down
135 changes: 135 additions & 0 deletions src/vs/workbench/contrib/notebook/browser/contrib/find/notebookFind.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import 'vs/css!./media/notebookFind';
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
import { Schemas } from 'vs/base/common/network';
import { isEqual } from 'vs/base/common/resources';
import { URI } from 'vs/base/common/uri';
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService';
import { EditorContextKeys } from 'vs/editor/common/editorContextKeys';
import { ITextModel } from 'vs/editor/common/model';
import { StartFindAction, StartFindReplaceAction } from 'vs/editor/contrib/find/browser/findController';
import { localize } from 'vs/nls';
import { Action2, registerAction2 } from 'vs/platform/actions/common/actions';
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';
import { NotebookFindWidget } from 'vs/workbench/contrib/notebook/browser/contrib/find/notebookFindWidget';
import { getNotebookEditorFromEditorPane } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
import { registerNotebookContribution } from 'vs/workbench/contrib/notebook/browser/notebookEditorExtensions';
import { CellUri } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { KEYBINDING_CONTEXT_NOTEBOOK_FIND_WIDGET_FOCUSED, NOTEBOOK_EDITOR_FOCUSED, NOTEBOOK_IS_ACTIVE_EDITOR } from 'vs/workbench/contrib/notebook/common/notebookContextKeys';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';

registerNotebookContribution(NotebookFindWidget.id, NotebookFindWidget);

registerAction2(class extends Action2 {
constructor() {
super({
id: 'notebook.hideFind',
title: { value: localize('notebookActions.hideFind', "Hide Find in Notebook"), original: 'Hide Find in Notebook' },
keybinding: {
when: ContextKeyExpr.and(NOTEBOOK_EDITOR_FOCUSED, KEYBINDING_CONTEXT_NOTEBOOK_FIND_WIDGET_FOCUSED),
primary: KeyCode.Escape,
weight: KeybindingWeight.WorkbenchContrib
}
});
}

async run(accessor: ServicesAccessor): Promise<void> {
const editorService = accessor.get(IEditorService);
const editor = getNotebookEditorFromEditorPane(editorService.activeEditorPane);

if (!editor) {
return;
}

const controller = editor.getContribution<NotebookFindWidget>(NotebookFindWidget.id);
controller.hide();
editor.focus();
}
});

registerAction2(class extends Action2 {
constructor() {
super({
id: 'notebook.find',
title: { value: localize('notebookActions.findInNotebook', "Find in Notebook"), original: 'Find in Notebook' },
keybinding: {
when: ContextKeyExpr.and(NOTEBOOK_EDITOR_FOCUSED, NOTEBOOK_IS_ACTIVE_EDITOR, EditorContextKeys.focus.toNegated()),
primary: KeyCode.KeyF | KeyMod.CtrlCmd,
weight: KeybindingWeight.WorkbenchContrib
}
});
}

async run(accessor: ServicesAccessor): Promise<void> {
const editorService = accessor.get(IEditorService);
const editor = getNotebookEditorFromEditorPane(editorService.activeEditorPane);

if (!editor) {
return;
}

const controller = editor.getContribution<NotebookFindWidget>(NotebookFindWidget.id);
controller.show();
}
});

function notebookContainsTextModel(uri: URI, textModel: ITextModel) {
if (textModel.uri.scheme === Schemas.vscodeNotebookCell) {
const cellUri = CellUri.parse(textModel.uri);
if (cellUri && isEqual(cellUri.notebook, uri)) {
return true;
}
}

return false;
}


StartFindAction.addImplementation(100, (accessor: ServicesAccessor, codeEditor: ICodeEditor, args: any) => {
const editorService = accessor.get(IEditorService);
const editor = getNotebookEditorFromEditorPane(editorService.activeEditorPane);

if (!editor) {
return false;
}

if (!editor.hasEditorFocus() && !editor.hasWebviewFocus()) {
const codeEditorService = accessor.get(ICodeEditorService);
// check if the active pane contains the active text editor
const textEditor = codeEditorService.getFocusedCodeEditor() || codeEditorService.getActiveCodeEditor();
if (editor.hasModel() && textEditor && textEditor.hasModel() && notebookContainsTextModel(editor.textModel.uri, textEditor.getModel())) {
// the active text editor is in notebook editor
} else {
return false;
}
}

const controller = editor.getContribution<NotebookFindWidget>(NotebookFindWidget.id);
controller.show();
return true;
});

StartFindReplaceAction.addImplementation(100, (accessor: ServicesAccessor, codeEditor: ICodeEditor, args: any) => {
const editorService = accessor.get(IEditorService);
const editor = getNotebookEditorFromEditorPane(editorService.activeEditorPane);

if (!editor) {
return false;
}

const controller = editor.getContribution<NotebookFindWidget>(NotebookFindWidget.id);
if (controller) {
controller.replace();
return true;
}

return false;
});

Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ export abstract class SimpleFindReplaceWidget extends Widget {
this._findInput.focus();
}

public show(initialInput?: string): void {
public show(initialInput?: string, options?: { focus?: boolean }): void {
if (initialInput && !this._isVisible) {
this._findInput.setValue(initialInput);
}
Expand All @@ -625,7 +625,9 @@ export abstract class SimpleFindReplaceWidget extends Widget {
this._domNode.classList.add('visible', 'visible-transition');
this._domNode.setAttribute('aria-hidden', 'false');

this.focus();
if (options?.focus ?? true) {
this.focus();
}
}, 0);
}

Expand Down
Loading

0 comments on commit 7135a30

Please sign in to comment.