提交 e17815a8 编写于 作者: R rebornix

set selection for cell

上级 c6c03851
...@@ -26,7 +26,7 @@ const NLS_NEXT_MATCH_BTN_LABEL = nls.localize('label.nextMatchButton', "Next mat ...@@ -26,7 +26,7 @@ const NLS_NEXT_MATCH_BTN_LABEL = nls.localize('label.nextMatchButton', "Next mat
const NLS_CLOSE_BTN_LABEL = nls.localize('label.closeButton', "Close"); const NLS_CLOSE_BTN_LABEL = nls.localize('label.closeButton', "Close");
export abstract class SimpleFindWidget extends Widget { export abstract class SimpleFindWidget extends Widget {
private readonly _findInput: FindInput; protected readonly _findInput: FindInput;
private readonly _domNode: HTMLElement; private readonly _domNode: HTMLElement;
private readonly _innerDomNode: HTMLElement; private readonly _innerDomNode: HTMLElement;
private readonly _focusTracker: dom.IFocusTracker; private readonly _focusTracker: dom.IFocusTracker;
......
...@@ -12,6 +12,8 @@ import { ModelDecorationOptions } from 'vs/editor/common/model/textModel'; ...@@ -12,6 +12,8 @@ import { ModelDecorationOptions } from 'vs/editor/common/model/textModel';
import { IModelDeltaDecoration } from 'vs/editor/common/model'; import { IModelDeltaDecoration } from 'vs/editor/common/model';
import { ICellModelDeltaDecorations, ICellModelDecorations } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel'; import { ICellModelDeltaDecorations, ICellModelDecorations } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel';
import { PrefixSumComputer } from 'vs/editor/common/viewModel/prefixSumComputer'; import { PrefixSumComputer } from 'vs/editor/common/viewModel/prefixSumComputer';
import { IKeyboardEvent } from 'vs/base/browser/keyboardEvent';
import { KeyCode } from 'vs/base/common/keyCodes';
export class NotebookFindWidget extends SimpleFindWidget { export class NotebookFindWidget extends SimpleFindWidget {
protected _findWidgetFocused: IContextKey<boolean>; protected _findWidgetFocused: IContextKey<boolean>;
...@@ -28,22 +30,42 @@ export class NotebookFindWidget extends SimpleFindWidget { ...@@ -28,22 +30,42 @@ export class NotebookFindWidget extends SimpleFindWidget {
) { ) {
super(contextViewService, contextKeyService); super(contextViewService, contextKeyService);
this._findWidgetFocused = KEYBINDING_CONTEXT_NOTEBOOK_FIND_WIDGET_FOCUSED.bindTo(contextKeyService); this._findWidgetFocused = KEYBINDING_CONTEXT_NOTEBOOK_FIND_WIDGET_FOCUSED.bindTo(contextKeyService);
this._register(this._findInput.onKeyDown((e) => this._onFindInputKeyDown(e)));
}
private _onFindInputKeyDown(e: IKeyboardEvent): void {
if (e.equals(KeyCode.Enter)) {
if (this._findMatches.length) {
this.set(this._findMatches);
if (this._currentMatch !== -1) {
const nextIndex = this._findMatchesStarts!.getIndexOf(this._currentMatch);
const cellIndex = nextIndex.index;
const matchIndex = nextIndex.remainder;
this._findMatches[cellIndex].cell.isEditing = true;
this._notebookEditor.setSelection(this._findMatches[cellIndex].cell, this._findMatches[cellIndex].matches[matchIndex].range);
this._notebookEditor.revealRangeInCenterIfOutsideViewport(this._findMatches[cellIndex].cell, this._findMatches[cellIndex].matches[matchIndex].range);
}
} else {
this.set(null);
}
e.preventDefault();
return;
}
} }
protected onInputChanged(): boolean { protected onInputChanged(): boolean {
const val = this.inputValue; const val = this.inputValue;
if (val) { if (val) {
const newMatches = this._notebookEditor.viewModel!.find(val).filter(match => match.matches.length > 0); this._findMatches = this._notebookEditor.viewModel!.find(val).filter(match => match.matches.length > 0);
if (newMatches.length) { if (this._findMatches.length) {
this.set(newMatches);
return true; return true;
} else { } else {
this.set(null);
return false; return false;
} }
} else {
this.set([]);
} }
return false; return false;
} }
...@@ -62,6 +84,7 @@ export class NotebookFindWidget extends SimpleFindWidget { ...@@ -62,6 +84,7 @@ export class NotebookFindWidget extends SimpleFindWidget {
this.setCurrentFindMatchDecoration(cellIndex, matchIndex); this.setCurrentFindMatchDecoration(cellIndex, matchIndex);
this._findMatches[cellIndex].cell.isEditing = true; this._findMatches[cellIndex].cell.isEditing = true;
this._notebookEditor.setSelection(this._findMatches[cellIndex].cell, this._findMatches[cellIndex].matches[matchIndex].range);
this._notebookEditor.revealRangeInCenterIfOutsideViewport(this._findMatches[cellIndex].cell, this._findMatches[cellIndex].matches[matchIndex].range); this._notebookEditor.revealRangeInCenterIfOutsideViewport(this._findMatches[cellIndex].cell, this._findMatches[cellIndex].matches[matchIndex].range);
} }
......
...@@ -141,6 +141,8 @@ export interface INotebookEditor { ...@@ -141,6 +141,8 @@ export interface INotebookEditor {
*/ */
revealRangeInCenterIfOutsideViewport(cell: CellViewModel, range: Range): void; revealRangeInCenterIfOutsideViewport(cell: CellViewModel, range: Range): void;
setSelection(cell: CellViewModel, selection: Range): void;
/** /**
* Change the decorations on cells. * Change the decorations on cells.
* The notebook is virtualized and this method should be called to create/delete editor decorations safely. * The notebook is virtualized and this method should be called to create/delete editor decorations safely.
......
...@@ -483,6 +483,14 @@ export class NotebookEditor extends BaseEditor implements INotebookEditor { ...@@ -483,6 +483,14 @@ export class NotebookEditor extends BaseEditor implements INotebookEditor {
} }
} }
setSelection(cell: CellViewModel, range: Range): void {
const index = this.notebookViewModel?.getViewCellIndex(cell);
if (index !== undefined) {
this.list?.setCellSelection(index, range);
}
}
changeDecorations(callback: (changeAccessor: IModelDecorationsChangeAccessor) => any): any { changeDecorations(callback: (changeAccessor: IModelDecorationsChangeAccessor) => any): any {
return this.notebookViewModel?.changeDecorations(callback); return this.notebookViewModel?.changeDecorations(callback);
} }
......
...@@ -209,10 +209,15 @@ export class NotebookCellList extends WorkbenchList<CellViewModel> { ...@@ -209,10 +209,15 @@ export class NotebookCellList extends WorkbenchList<CellViewModel> {
if (!element.editorAttached) { if (!element.editorAttached) {
getEditorAttachedPromise(element).then(() => reveal(index, range, revealType)); getEditorAttachedPromise(element).then(() => reveal(index, range, revealType));
} else { } else {
// should not happen // for example markdown
} }
} else { } else {
element.revealRangeInCenter(range); if (element.editorAttached) {
element.revealRangeInCenter(range);
} else {
// for example, markdown cell in preview mode
getEditorAttachedPromise(element).then(() => reveal(index, range, revealType));
}
} }
} }
...@@ -257,6 +262,11 @@ export class NotebookCellList extends WorkbenchList<CellViewModel> { ...@@ -257,6 +262,11 @@ export class NotebookCellList extends WorkbenchList<CellViewModel> {
this._revealInternal(index, true, CellRevealPosition.Center); this._revealInternal(index, true, CellRevealPosition.Center);
} }
setCellSelection(index: number, range: Range) {
const element = this.view.element(index);
element.setSelection(range);
}
} }
function getEditorAttachedPromise(element: CellViewModel) { function getEditorAttachedPromise(element: CellViewModel) {
......
...@@ -255,6 +255,10 @@ export class CellViewModel extends Disposable { ...@@ -255,6 +255,10 @@ export class CellViewModel extends Disposable {
} }
attachTextEditor(editor: ICodeEditor) { attachTextEditor(editor: ICodeEditor) {
if (this._textEditor === editor) {
return;
}
this._textEditor = editor; this._textEditor = editor;
if (this._editorViewStates) { if (this._editorViewStates) {
...@@ -295,6 +299,10 @@ export class CellViewModel extends Disposable { ...@@ -295,6 +299,10 @@ export class CellViewModel extends Disposable {
this._textEditor?.revealRangeInCenter(range); this._textEditor?.revealRangeInCenter(range);
} }
setSelection(range: Range) {
this._textEditor?.setSelection(range);
}
getLineScrollTopOffset(line: number): number { getLineScrollTopOffset(line: number): number {
if (!this._textEditor) { if (!this._textEditor) {
return 0; return 0;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册