From 309eb084a98de6872b4c9a365c1aaac700851f48 Mon Sep 17 00:00:00 2001 From: Rich Chiodo Date: Mon, 28 Oct 2019 09:21:47 -0700 Subject: [PATCH 1/4] Fix scrolling while typing --- .../history-react/interactiveCell.tsx | 4 +- .../interactive-common/cellInput.tsx | 10 ++-- .../interactive-common/code.tsx | 8 +-- .../interactive-common/editor.tsx | 55 ++++-------------- .../interactive-common/mainState.ts | 6 ++ .../interactive-common/markdown.tsx | 6 +- .../native-editor/nativeCell.tsx | 37 ++++++------ .../native-editor/nativeEditor.tsx | 22 +++---- .../react-common/monacoEditor.tsx | 58 +++++++++++++++++-- 9 files changed, 113 insertions(+), 93 deletions(-) diff --git a/src/datascience-ui/history-react/interactiveCell.tsx b/src/datascience-ui/history-react/interactiveCell.tsx index a437b975db2d..dcc6f68b9d9e 100644 --- a/src/datascience-ui/history-react/interactiveCell.tsx +++ b/src/datascience-ui/history-react/interactiveCell.tsx @@ -16,7 +16,7 @@ import { CollapseButton } from '../interactive-common/collapseButton'; import { ExecutionCount } from '../interactive-common/executionCount'; import { InformationMessages } from '../interactive-common/informationMessages'; import { InputHistory } from '../interactive-common/inputHistory'; -import { ICellViewModel, IFont } from '../interactive-common/mainState'; +import { CursorPos, ICellViewModel, IFont } from '../interactive-common/mainState'; import { IKeyboardEvent } from '../react-common/event'; import { getLocString } from '../react-common/locReactSide'; import { getSettings } from '../react-common/settingsReactSide'; @@ -100,7 +100,7 @@ export class InteractiveCell extends React.Component { // This depends upon what type of cell we are. if (this.props.cellVM.cell.data.cell_type === 'code') { if (this.codeRef.current) { - this.codeRef.current.giveFocus(); + this.codeRef.current.giveFocus(CursorPos.Current); } } } diff --git a/src/datascience-ui/interactive-common/cellInput.tsx b/src/datascience-ui/interactive-common/cellInput.tsx index 41ec875fcd1e..59fb3d200db9 100644 --- a/src/datascience-ui/interactive-common/cellInput.tsx +++ b/src/datascience-ui/interactive-common/cellInput.tsx @@ -12,7 +12,7 @@ import { IKeyboardEvent } from '../react-common/event'; import { getLocString } from '../react-common/locReactSide'; import { Code } from './code'; import { InputHistory } from './inputHistory'; -import { ICellViewModel, IFont } from './mainState'; +import { CursorPos, ICellViewModel, IFont } from './mainState'; import { Markdown } from './markdown'; // tslint:disable-next-line: no-require-importss @@ -56,19 +56,19 @@ export class CellInput extends React.Component { public componentDidUpdate(prevProps: ICellInputProps) { if (this.props.cellVM.focused && !prevProps.cellVM.focused) { - this.giveFocus(); + this.giveFocus(CursorPos.Current); } } - public giveFocus() { + public giveFocus(cursorPos: CursorPos) { // This depends upon what type of cell we are. if (this.props.cellVM.cell.data.cell_type === 'code') { if (this.codeRef.current) { - this.codeRef.current.giveFocus(); + this.codeRef.current.giveFocus(cursorPos); } } else { if (this.markdownRef.current) { - this.markdownRef.current.giveFocus(); + this.markdownRef.current.giveFocus(cursorPos); } this.setState({ showingMarkdownEditor: true }); } diff --git a/src/datascience-ui/interactive-common/code.tsx b/src/datascience-ui/interactive-common/code.tsx index c7b4cd2447c9..9a2d99ac0db9 100644 --- a/src/datascience-ui/interactive-common/code.tsx +++ b/src/datascience-ui/interactive-common/code.tsx @@ -8,7 +8,7 @@ import { InputHistory } from '../interactive-common/inputHistory'; import { IKeyboardEvent } from '../react-common/event'; import { getLocString } from '../react-common/locReactSide'; import { Editor } from './editor'; -import { IFont } from './mainState'; +import { CursorPos, IFont } from './mainState'; export interface ICodeProps { autoFocus: boolean; @@ -79,15 +79,15 @@ export class Code extends React.Component { ); } - public giveFocus() { + public giveFocus(cursorPos: CursorPos) { if (this.editorRef && this.editorRef.current) { - this.editorRef.current.giveFocus(); + this.editorRef.current.giveFocus(cursorPos); } } private clickWatermark = (ev: React.MouseEvent) => { ev.stopPropagation(); // Give focus to the editor - this.giveFocus(); + this.giveFocus(CursorPos.Current); } private getWatermarkString = () : string => { diff --git a/src/datascience-ui/interactive-common/editor.tsx b/src/datascience-ui/interactive-common/editor.tsx index 5265d6adfa97..bd1915155ee2 100644 --- a/src/datascience-ui/interactive-common/editor.tsx +++ b/src/datascience-ui/interactive-common/editor.tsx @@ -8,7 +8,7 @@ import { noop } from '../../client/common/utils/misc'; import { IKeyboardEvent } from '../react-common/event'; import { MonacoEditor } from '../react-common/monacoEditor'; import { InputHistory } from './inputHistory'; -import { IFont } from './mainState'; +import { CursorPos, IFont } from './mainState'; // tslint:disable-next-line: import-name export interface IEditorProps { @@ -37,7 +37,6 @@ export interface IEditorProps { interface IEditorState { editor: monacoEditor.editor.IStandaloneCodeEditor | undefined; model: monacoEditor.editor.ITextModel | null; - visibleLineCount: number; forceMonaco: boolean; } @@ -48,7 +47,7 @@ export class Editor extends React.Component { constructor(prop: IEditorProps) { super(prop); - this.state = {editor: undefined, model: null, visibleLineCount: 0, forceMonaco: false}; + this.state = {editor: undefined, model: null, forceMonaco: false}; } public componentWillUnmount = () => { @@ -65,11 +64,15 @@ export class Editor extends React.Component { ); } - public giveFocus() { + public giveFocus(cursorPos: CursorPos) { const readOnly = this.props.readOnly; if (this.state.editor && !readOnly) { this.state.editor.focus(); } + if (this.state.editor && cursorPos !== CursorPos.Current) { + const lineNumber = cursorPos === CursorPos.Top ? 1 : this.state.editor.getModel()!.getLineCount(); + this.state.editor.setPosition({ lineNumber, column: 1}); + } } private renderQuickEditor = (): JSX.Element => { @@ -130,7 +133,6 @@ export class Editor extends React.Component { options={options} openLink={this.props.openLink} ref={this.monacoRef} - lineCountChanged={this.visibleCountChanged} /> ); } @@ -145,10 +147,6 @@ export class Editor extends React.Component { this.setState({forceMonaco: true}); } - private visibleCountChanged = (newCount: number) => { - this.setState({visibleLineCount: newCount}); - } - private editorDidMount = (editor: monacoEditor.editor.IStandaloneCodeEditor) => { // Update our state const model = editor.getModel(); @@ -186,44 +184,11 @@ export class Editor extends React.Component { private onKeyDown = (e: monacoEditor.IKeyboardEvent) => { if (this.state.editor && this.state.model && this.monacoRef && this.monacoRef.current) { const cursor = this.state.editor.getPosition(); - const editorDomNode = this.state.editor.getDomNode(); - let currentLine = -1; - - // This gets the cell/monaco editor line where the cursor is located. With it we can include wrapped lines - // when the isFirstLine and the isLastLine settings are created. - if (cursor && editorDomNode) { - // Get the cursor's position on the cell/monaco editor. - const currentPosition = this.state.model.getValueInRange({ startLineNumber: 1, startColumn: 1, endLineNumber: cursor.lineNumber, endColumn: cursor.column }).length; - - if (currentPosition === 0) { - currentLine = 0; - } else { - // Get the lines as they are being displayed, including wrapped ones. - const container = editorDomNode.getElementsByClassName('view-lines')[0] as HTMLElement; - - if (container) { - let charCounter = 0; - let index = 0; - - // Go through each line, and compare if a character counter is bigger or equal than the cursor position. - // If it is, we found the current line. - while (index < container.childNodes.length) { - if (charCounter < currentPosition && container.childNodes[index].textContent) { - charCounter += container.childNodes[index].textContent!.length; - index += 1; - } else { - break; - } - } - - currentLine = index - 1; - } - } - } - + const currentLine = this.monacoRef.current.getCurrentVisibleLine(); + const visibleLineCount = this.monacoRef.current.getVisibleLineCount(); const isSuggesting = this.monacoRef.current.isSuggesting(); const isFirstLine = currentLine === 0; - const isLastLine = currentLine === this.state.visibleLineCount - 1; + const isLastLine = currentLine === visibleLineCount - 1; const isDirty = this.state.model!.getVersionId() > this.lastCleanVersionId; // See if we need to use the history or not diff --git a/src/datascience-ui/interactive-common/mainState.ts b/src/datascience-ui/interactive-common/mainState.ts index fa218535e1dc..dd0d9146aeb0 100644 --- a/src/datascience-ui/interactive-common/mainState.ts +++ b/src/datascience-ui/interactive-common/mainState.ts @@ -31,6 +31,12 @@ export interface ICellViewModel { inputBlockToggled(id: string): void; } +export enum CursorPos { + Top, + Bottom, + Current +} + export interface IMainState { cellVMs: ICellViewModel[]; editCellVM: ICellViewModel | undefined; diff --git a/src/datascience-ui/interactive-common/markdown.tsx b/src/datascience-ui/interactive-common/markdown.tsx index 7e7f5daf617f..44afe896caaf 100644 --- a/src/datascience-ui/interactive-common/markdown.tsx +++ b/src/datascience-ui/interactive-common/markdown.tsx @@ -6,7 +6,7 @@ import * as React from 'react'; import { IKeyboardEvent } from '../react-common/event'; import { Editor } from './editor'; -import { IFont } from './mainState'; +import { CursorPos, IFont } from './mainState'; export interface IMarkdownProps { autoFocus: boolean; @@ -67,9 +67,9 @@ export class Markdown extends React.Component { ); } - public giveFocus() { + public giveFocus(cursorPos: CursorPos) { if (this.editorRef && this.editorRef.current) { - this.editorRef.current.giveFocus(); + this.editorRef.current.giveFocus(cursorPos); } } diff --git a/src/datascience-ui/native-editor/nativeCell.tsx b/src/datascience-ui/native-editor/nativeCell.tsx index ce1cf3495206..82aa567df8e2 100644 --- a/src/datascience-ui/native-editor/nativeCell.tsx +++ b/src/datascience-ui/native-editor/nativeCell.tsx @@ -15,7 +15,7 @@ import { CellInput } from '../interactive-common/cellInput'; import { CellOutput } from '../interactive-common/cellOutput'; import { ExecutionCount } from '../interactive-common/executionCount'; import { InformationMessages } from '../interactive-common/informationMessages'; -import { ICellViewModel, IFont } from '../interactive-common/mainState'; +import { CursorPos, ICellViewModel, IFont } from '../interactive-common/mainState'; import { IKeyboardEvent } from '../react-common/event'; import { Image, ImageName } from '../react-common/image'; import { ImageButton } from '../react-common/imageButton'; @@ -36,9 +36,8 @@ interface INativeCellProps { monacoTheme: string | undefined; lastCell: boolean; font: IFont; - focusCell(cellId: string, focusCode: boolean): void; - selectCell(cellId: string, focusCode: boolean): void; - + focusCell(cellId: string, focusCode: boolean, cursorPos: CursorPos): void; + selectCell(cellId: string, focusCode: boolean, cursorPos: CursorPos): void; } // tslint:disable: react-this-binding-issue @@ -62,7 +61,7 @@ export class NativeCell extends React.Component { public componentDidUpdate(prevProps: INativeCellProps) { if (this.props.cellVM.selected && !prevProps.cellVM.selected) { - this.giveFocus(this.props.cellVM.focused); + this.giveFocus(this.props.cellVM.focused, CursorPos.Current); } // Anytime we update, reset the key. This object will be reused for different cell ids @@ -73,7 +72,7 @@ export class NativeCell extends React.Component { return !fastDeepEqual(this.props, nextProps); } - public giveFocus(giveCodeFocus: boolean) { + public giveFocus(giveCodeFocus: boolean, cursorPos: CursorPos) { // Start out with ourselves if (this.wrapperRef && this.wrapperRef.current) { this.wrapperRef.current.focus(); @@ -81,7 +80,7 @@ export class NativeCell extends React.Component { // Then attempt to move into the object if (giveCodeFocus) { if (this.inputRef && this.inputRef.current) { - this.inputRef.current.giveFocus(); + this.inputRef.current.giveFocus(cursorPos); } } } @@ -96,7 +95,7 @@ export class NativeCell extends React.Component { const wasFocused = this.isFocused(); const cellId = this.cellId; this.props.stateController.moveCellUp(cellId); - setTimeout(() => this.props.focusCell(cellId, wasFocused ? true : false), 1); + setTimeout(() => this.props.focusCell(cellId, wasFocused ? true : false, CursorPos.Current), 1); } } @@ -105,7 +104,7 @@ export class NativeCell extends React.Component { const wasFocused = this.isFocused(); const cellId = this.cellId; this.props.stateController.moveCellDown(cellId); - setTimeout(() => this.props.focusCell(cellId, wasFocused ? true : false), 1); + setTimeout(() => this.props.focusCell(cellId, wasFocused ? true : false, CursorPos.Current), 1); } } @@ -197,7 +196,7 @@ export class NativeCell extends React.Component { private onMouseDoubleClick = (ev: React.MouseEvent) => { // When we receive double click, propagate upwards. Might change our state ev.stopPropagation(); - this.props.focusCell(this.cellId, true); + this.props.focusCell(this.cellId, true, CursorPos.Current); } private shouldRenderCodeEditor = () : boolean => { @@ -377,7 +376,7 @@ export class NativeCell extends React.Component { if (this.wrapperRef && this.wrapperRef.current && this.isFocused()) { e.stopPropagation(); this.isCodeCell() ? this.onCodeUnfocused() : this.onMarkdownUnfocused(); - this.props.focusCell(this.cellId, false); + this.props.focusCell(this.cellId, false, CursorPos.Current); this.props.stateController.sendCommand(NativeCommandType.Unfocus, 'keyboard'); } } @@ -386,7 +385,7 @@ export class NativeCell extends React.Component { const prevCellId = this.getPrevCellId(); if (prevCellId) { e.stopPropagation(); - this.moveSelection(prevCellId, this.isFocused()); + this.moveSelection(prevCellId, this.isFocused(), CursorPos.Bottom); } this.props.stateController.sendCommand(NativeCommandType.ArrowUp, 'keyboard'); @@ -397,7 +396,7 @@ export class NativeCell extends React.Component { if (nextCellId) { e.stopPropagation(); - this.moveSelection(nextCellId, this.isFocused()); + this.moveSelection(nextCellId, this.isFocused(), CursorPos.Top); } this.props.stateController.sendCommand(NativeCommandType.ArrowDown, 'keyboard'); @@ -408,7 +407,7 @@ export class NativeCell extends React.Component { if (!this.isFocused() && !e.editorInfo && this.wrapperRef && this.wrapperRef && this.isSelected()) { e.stopPropagation(); e.preventDefault(); - this.props.focusCell(this.cellId, true); + this.props.focusCell(this.cellId, true, CursorPos.Current); } } @@ -475,8 +474,8 @@ export class NativeCell extends React.Component { this.props.stateController.sendCommand(NativeCommandType.Run, 'keyboard'); } - private moveSelection(cellId: string, focusCode: boolean) { - this.props.selectCell(cellId, focusCode); + private moveSelection(cellId: string, focusCode: boolean, cursorPos: CursorPos = CursorPos.Current) { + this.props.selectCell(cellId, focusCode, cursorPos); } private submitCell = (possibleContents?: string) => { @@ -501,7 +500,7 @@ export class NativeCell extends React.Component { this.props.stateController.sendCommand(NativeCommandType.AddToEnd, 'mouse'); if (newCell) { // Make async because the click changes focus. - setTimeout(() => this.props.focusCell(newCell, true), 0); + setTimeout(() => this.props.focusCell(newCell, true, CursorPos.Top), 0); } } @@ -577,14 +576,14 @@ export class NativeCell extends React.Component { const switchToMarkdown = () => { this.props.stateController.changeCellType(cellId, 'markdown'); this.props.stateController.sendCommand(NativeCommandType.ChangeToMarkdown, 'mouse'); - setTimeout(() => this.props.focusCell(cellId, true), 0); + setTimeout(() => this.props.focusCell(cellId, true, CursorPos.Current), 0); }; const switchToCode = () => { const handler = () => { setTimeout(() => { this.props.stateController.changeCellType(cellId, 'code'); this.props.stateController.sendCommand(NativeCommandType.ChangeToCode, 'mouse'); - this.props.focusCell(cellId, true); + this.props.focusCell(cellId, true, CursorPos.Current); }, 0); }; diff --git a/src/datascience-ui/native-editor/nativeEditor.tsx b/src/datascience-ui/native-editor/nativeEditor.tsx index c5b502f25d3a..18d1add62cc2 100644 --- a/src/datascience-ui/native-editor/nativeEditor.tsx +++ b/src/datascience-ui/native-editor/nativeEditor.tsx @@ -8,7 +8,7 @@ import * as React from 'react'; import { noop } from '../../client/common/utils/misc'; import { NativeCommandType } from '../../client/datascience/interactive-common/interactiveWindowTypes'; import { ContentPanel, IContentPanelProps } from '../interactive-common/contentPanel'; -import { ICellViewModel, IMainState } from '../interactive-common/mainState'; +import { CursorPos, ICellViewModel, IMainState } from '../interactive-common/mainState'; import { IVariablePanelProps, VariablePanel } from '../interactive-common/variablePanel'; import { ErrorBoundary } from '../react-common/errorBoundary'; import { Image, ImageName } from '../react-common/image'; @@ -99,7 +99,7 @@ export class NativeEditor extends React.Component this.focusCell(newCell, true), 0); + setTimeout(() => this.focusCell(newCell, true, CursorPos.Top), 0); } }; const addCellLine = this.state.cellVMs.length === 0 ? null : @@ -145,15 +145,15 @@ export class NativeEditor extends React.Component { + private moveSelectionToExisting = (cellId: string, focusCode: boolean, cursorPos: CursorPos) => { // Cell should already exist in the UI if (this.contentPanelRef) { this.stateController.selectCell(cellId, focusCode ? cellId : undefined); - this.focusCell(cellId, focusCode ? true : false); + this.focusCell(cellId, focusCode ? true : false, cursorPos); } } - private selectCell = (id: string, focusCode: boolean) => { + private selectCell = (id: string, focusCode: boolean, cursorPos: CursorPos) => { // Check to see that this cell already exists in our window (it's part of the rendered state) const cells = this.state.cellVMs.map(c => c.cell).filter(c => c.data.cell_type !== 'messages'); if (cells.find(c => c.id === id)) { @@ -164,7 +164,7 @@ export class NativeEditor extends React.Component this.moveSelectionToExisting(id, focusCode), 1); + setTimeout(() => this.moveSelectionToExisting(id, focusCode, cursorPos), 1); } // tslint:disable: react-this-binding-issue @@ -174,7 +174,7 @@ export class NativeEditor extends React.Component this.focusCell(newCell.cell.id, true), 0); + setTimeout(() => this.focusCell(newCell.cell.id, true, CursorPos.Top), 0); } }; const runAll = () => { @@ -376,7 +376,7 @@ export class NativeEditor extends React.Component this.focusCell(newCell, true), 0); + setTimeout(() => this.focusCell(newCell, true, CursorPos.Top), 0); } }; const lastLine = index === this.state.cellVMs.length - 1 ? @@ -416,11 +416,11 @@ export class NativeEditor extends React.Component); } - private focusCell = (cellId: string, focusCode: boolean): void => { + private focusCell = (cellId: string, focusCode: boolean, cursorPos: CursorPos): void => { this.stateController.selectCell(cellId, focusCode ? cellId : undefined); const ref = this.cellRefs.get(cellId); if (ref && ref.current) { - ref.current.giveFocus(focusCode); + ref.current.giveFocus(focusCode, cursorPos); } } @@ -430,7 +430,7 @@ export class NativeEditor extends React.Component { - this.focusCell(newCell, true); + this.focusCell(newCell, true, CursorPos.Current); }, 0); } } diff --git a/src/datascience-ui/react-common/monacoEditor.tsx b/src/datascience-ui/react-common/monacoEditor.tsx index 3ee223fd140a..6520e3faf59c 100644 --- a/src/datascience-ui/react-common/monacoEditor.tsx +++ b/src/datascience-ui/react-common/monacoEditor.tsx @@ -40,7 +40,6 @@ export interface IMonacoEditorProps { measureWidthClassName?: string; editorMounted(editor: monacoEditor.editor.IStandaloneCodeEditor): void; openLink(uri: monacoEditor.Uri): void; - lineCountChanged(newCount: number): void; } export interface IMonacoEditorState { @@ -68,6 +67,7 @@ export class MonacoEditor extends React.Component { + this.throttledUpdateWidgetPosition(); + this.throttledScrollOntoScreen(editor); })); // Update our margin to include the correct line number style @@ -301,6 +308,52 @@ export class MonacoEditor extends React.Component { + const match = l.style.top ? /(.+)px/.exec(l.style.top) : null; + return match ? parseInt(match[0], 10) : Infinity; + }); + for (let i = 0; i < lines.length; i += 1) { + if (top <= lineTops[i]) { + return i; + } + } + } + } + } + + public getVisibleLineCount(): number { + return this.getVisibleLines().length; + } + + private getVisibleLines(): HTMLDivElement[] { + if (this.state.editor && this.state.model) { + // This is going to use just the dom to compute the visible lines + const editorDom = this.state.editor.getDomNode(); + if (editorDom) { + return Array.from(editorDom.getElementsByClassName('view-line')) as HTMLDivElement[]; + } + } + return []; + } + + private scrollOntoScreen(_editor: monacoEditor.editor.IStandaloneCodeEditor) { + // Scroll to the visible line that has our current line + const visibleLineDivs = this.getVisibleLines(); + const current = this.getCurrentVisibleLine(); + if (current !== undefined && current >= 0) { + window.console.log(`Scrolling to line ${current}`); + visibleLineDivs[current].scrollIntoView({ behavior: 'auto', block: 'nearest', inline: 'nearest' }); + } + } + private watchStyles = (mutations: MutationRecord[], _observer: MutationObserver): void => { try { if (mutations && mutations.length > 0 && this.styleObserver) { @@ -419,9 +472,6 @@ export class MonacoEditor extends React.Component Date: Mon, 28 Oct 2019 09:24:49 -0700 Subject: [PATCH 2/4] Fix linting error --- src/test/datascience/nativeEditorTestHelpers.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/datascience/nativeEditorTestHelpers.tsx b/src/test/datascience/nativeEditorTestHelpers.tsx index b21e9d9b5a8b..82090ff94998 100644 --- a/src/test/datascience/nativeEditorTestHelpers.tsx +++ b/src/test/datascience/nativeEditorTestHelpers.tsx @@ -8,6 +8,7 @@ import { Uri } from 'vscode'; import { InteractiveWindowMessages } from '../../client/datascience/interactive-common/interactiveWindowTypes'; import { IJupyterExecution, INotebookEditor, INotebookEditorProvider } from '../../client/datascience/types'; +import { CursorPos } from '../../datascience-ui/interactive-common/mainState'; import { NativeCell } from '../../datascience-ui/native-editor/nativeCell'; import { NativeEditor } from '../../datascience-ui/native-editor/nativeEditor'; import { ImageButton } from '../../datascience-ui/react-common/imageButton'; @@ -22,7 +23,6 @@ import { simulateKey, waitForMessage } from './testHelpers'; -import { CursorPos } from '../../datascience-ui/interactive-common/mainState'; // tslint:disable: no-any From abb9d8f1d319f9456654232c357479c9066dfe0f Mon Sep 17 00:00:00 2001 From: Rich Chiodo Date: Mon, 28 Oct 2019 09:52:31 -0700 Subject: [PATCH 3/4] Fix column to stay the same as moving through cells --- .../interactive-common/editor.tsx | 4 +++- .../react-common/monacoEditor.tsx | 18 ++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/datascience-ui/interactive-common/editor.tsx b/src/datascience-ui/interactive-common/editor.tsx index bd1915155ee2..d288c3f87df9 100644 --- a/src/datascience-ui/interactive-common/editor.tsx +++ b/src/datascience-ui/interactive-common/editor.tsx @@ -70,8 +70,10 @@ export class Editor extends React.Component { this.state.editor.focus(); } if (this.state.editor && cursorPos !== CursorPos.Current) { + const current = this.state.editor.getPosition(); const lineNumber = cursorPos === CursorPos.Top ? 1 : this.state.editor.getModel()!.getLineCount(); - this.state.editor.setPosition({ lineNumber, column: 1}); + const column = current && current.lineNumber === lineNumber ? current.column : 1; + this.state.editor.setPosition({ lineNumber, column }); } } diff --git a/src/datascience-ui/react-common/monacoEditor.tsx b/src/datascience-ui/react-common/monacoEditor.tsx index 6520e3faf59c..5e3440018556 100644 --- a/src/datascience-ui/react-common/monacoEditor.tsx +++ b/src/datascience-ui/react-common/monacoEditor.tsx @@ -69,6 +69,8 @@ export class MonacoEditor extends React.Component { this.windowResized(); + // Also recompute our visible line heights + this.debouncedComputeLineTops(); })); // Setup our context menu to show up outside. Autocomplete doesn't have this problem so it just works @@ -316,10 +320,7 @@ export class MonacoEditor extends React.Component { - const match = l.style.top ? /(.+)px/.exec(l.style.top) : null; - return match ? parseInt(match[0], 10) : Infinity; - }); + const lineTops = lines.length === this.lineTops.length ? this.lineTops : this.computeLineTops(); for (let i = 0; i < lines.length; i += 1) { if (top <= lineTops[i]) { return i; @@ -344,6 +345,15 @@ export class MonacoEditor extends React.Component { + const match = l.style.top ? /(.+)px/.exec(l.style.top) : null; + return match ? parseInt(match[0], 10) : Infinity; + }); + return this.lineTops; + } + private scrollOntoScreen(_editor: monacoEditor.editor.IStandaloneCodeEditor) { // Scroll to the visible line that has our current line const visibleLineDivs = this.getVisibleLines(); From fc2b456946504dcdd7f7b7e282220d9384324364 Mon Sep 17 00:00:00 2001 From: Rich Chiodo Date: Mon, 28 Oct 2019 10:07:42 -0700 Subject: [PATCH 4/4] Add news entry --- news/2 Fixes/8205.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 news/2 Fixes/8205.md diff --git a/news/2 Fixes/8205.md b/news/2 Fixes/8205.md new file mode 100644 index 000000000000..9f1702ce3473 --- /dev/null +++ b/news/2 Fixes/8205.md @@ -0,0 +1 @@ +Scroll the notebook editor when giving focus or changing line of a code cell.