Skip to content

Davidkutu/ds interactive window focus fix #9895

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@

### Fixes

1. Fixed the focus on the interactive window when pressing ctrl + 1/ ctrl + 2
([#9693](https://github.com/microsoft/vscode-python/issues/9693))
1. Fix variable explorer in Interactive and Notebook editors from interfering with execution.
([#5980](https://github.com/Microsoft/vscode-python/issues/5980))
1. Fix a crash when using pytest to discover doctests with unknown line number.
Expand Down
2 changes: 2 additions & 0 deletions src/datascience-ui/history-react/interactiveCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ interface IInteractiveCellBaseProps {
editorMeasureClassName?: string;
font: IFont;
settings: IDataScienceExtraSettings;
focusPending: number;
}

type IInteractiveCellProps = IInteractiveCellBaseProps & typeof actionCreators;
Expand Down Expand Up @@ -266,6 +267,7 @@ export class InteractiveCell extends React.Component<IInteractiveCellProps> {
keyDown={this.isEditCell() ? this.onEditCellKeyDown : undefined}
showLineNumbers={this.props.cellVM.showLineNumbers}
font={this.props.font}
focusPending={this.props.focusPending}
/>
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/datascience-ui/history-react/interactivePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ export class InteractivePanel extends React.Component<IInteractivePanelProps> {
monacoTheme={this.props.monacoTheme}
font={this.props.font}
settings={this.props.settings}
focusPending={this.props.activateCount}
/>
</ErrorBoundary>
</div>
Expand Down Expand Up @@ -279,6 +280,7 @@ export class InteractivePanel extends React.Component<IInteractivePanelProps> {
monacoTheme={this.props.monacoTheme}
font={this.props.font}
settings={this.props.settings}
focusPending={this.props.activateCount}
/>
</ErrorBoundary>
</div>
Expand Down
2 changes: 2 additions & 0 deletions src/datascience-ui/interactive-common/cellInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ interface ICellInputProps {
editorMeasureClassName?: string;
showLineNumbers?: boolean;
font: IFont;
focusPending: number;
onCodeChange(changes: monacoEditor.editor.IModelContentChange[], cellId: string, modelId: string): void;
onCodeCreated(code: string, file: string, cellId: string, modelId: string): void;
openLink(uri: monacoEditor.Uri): void;
Expand Down Expand Up @@ -110,6 +111,7 @@ export class CellInput extends React.Component<ICellInputProps> {
showLineNumbers={this.props.showLineNumbers}
useQuickEdit={this.props.cellVM.useQuickEdit}
font={this.props.font}
focusPending={this.props.focusPending}
/>
</div>
);
Expand Down
7 changes: 7 additions & 0 deletions src/datascience-ui/interactive-common/code.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface ICodeProps {
font: IFont;
hasFocus: boolean;
cursorPos: CursorPos;
focusPending: number;
onCreated(code: string, modelId: string): void;
onChange(changes: monacoEditor.editor.IModelContentChange[], modelId: string): void;
openLink(uri: monacoEditor.Uri): void;
Expand All @@ -46,6 +47,12 @@ export class Code extends React.Component<ICodeProps, ICodeState> {
this.state = { allowWatermark: true };
}

public componentDidUpdate(prevProps: ICodeProps) {
if (prevProps.focusPending !== this.props.focusPending) {
this.giveFocus(CursorPos.Current);
}
}

public render() {
const readOnly = this.props.readOnly;
const waterMarkClass = this.props.showWatermark && this.state.allowWatermark && !readOnly ? 'code-watermark' : 'hide';
Expand Down
2 changes: 2 additions & 0 deletions src/datascience-ui/native-editor/nativeCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ interface INativeCellBaseProps {
enableGather: boolean | undefined;
editorOptions: monacoEditor.editor.IEditorOptions;
themeMatplotlibPlots: boolean | undefined;
focusPending: number;
}

type INativeCellProps = INativeCellBaseProps & typeof actionCreators;
Expand Down Expand Up @@ -597,6 +598,7 @@ export class NativeCell extends React.Component<INativeCellProps> {
keyDown={this.keyDownInput}
showLineNumbers={this.props.cellVM.showLineNumbers}
font={this.props.font}
focusPending={this.props.focusPending}
/>
</div>
);
Expand Down
1 change: 1 addition & 0 deletions src/datascience-ui/native-editor/nativeEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ export class NativeEditor extends React.Component<INativeEditorProps> {
editorOptions={this.props.editorOptions}
enableGather={this.props.settings.enableGather}
themeMatplotlibPlots={this.props.settings.themeMatplotlibPlots}
focusPending={this.props.activateCount}
/>
</ErrorBoundary>
{lastLine}
Expand Down
22 changes: 11 additions & 11 deletions src/test/datascience/dataScienceIocContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -898,17 +898,7 @@ export class DataScienceIocContainer extends UnitTestIocContainer {
}
}

private findPythonPath(): string {
try {
// Give preference to the CI test python (could also be set in launch.json for debugging).
const output = child_process.execFileSync(process.env.CI_PYTHON_PATH || 'python', ['-c', 'import sys;print(sys.executable)'], { encoding: 'utf8' });
return output.replace(/\r?\n/g, '');
} catch (ex) {
return 'python';
}
}

private postMessageToWebPanel(msg: any) {
public postMessageToWebPanel(msg: any) {
if (this.webPanelListener) {
this.webPanelListener.onMessage(msg.type, msg.payload);
} else {
Expand All @@ -923,6 +913,16 @@ export class DataScienceIocContainer extends UnitTestIocContainer {
}
}

private findPythonPath(): string {
try {
// Give preference to the CI test python (could also be set in launch.json for debugging).
const output = child_process.execFileSync(process.env.CI_PYTHON_PATH || 'python', ['-c', 'import sys;print(sys.executable)'], { encoding: 'utf8' });
return output.replace(/\r?\n/g, '');
} catch (ex) {
return 'python';
}
}

private mountReactControl(mount: () => ReactWrapper<any, Readonly<{}>, React.Component>) {
// This is a remount (or first time). Clear out messages that were sent
// by the last mount
Expand Down
46 changes: 45 additions & 1 deletion src/test/datascience/interactiveWindow.functional.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { defaultDataScienceSettings } from './helpers';
import { addCode, getInteractiveCellResults, getOrCreateInteractiveWindow, runMountedTest } from './interactiveWindowTestHelpers';
import { MockDocumentManager } from './mockDocumentManager';
import { MockEditor } from './mockTextEditor';
import { waitForUpdate } from './reactHelpers';
import { createMessageEvent, waitForUpdate } from './reactHelpers';
import {
addContinuousMockData,
addInputMockData,
Expand All @@ -36,9 +36,12 @@ import {
enterInput,
escapePath,
findButton,
getInteractiveEditor,
getLastOutputCell,
srcDirectory,
submitInput,
toggleCellExpansion,
typeCode,
verifyHtmlOnCell,
verifyLastCellInputState,
waitForMessage,
Expand Down Expand Up @@ -143,6 +146,47 @@ suite('DataScience Interactive Window output tests', () => {
}
);

runMountedTest(
'Ctrl + 1/Ctrl + 2',
async wrapper => {
// Create an interactive window so that it listens to the results.
const interactiveWindow = await getOrCreateInteractiveWindow(ioc);
await interactiveWindow.show();

// Type in the input box
const editor = getInteractiveEditor(wrapper);
typeCode(editor, 'a=1\na');

// Give focus to a random div
const reactDiv = wrapper
.find('div')
.first()
.getDOMNode();

const domDiv = reactDiv.querySelector('div');

if (domDiv && ioc.postMessage) {
domDiv.tabIndex = -1;
domDiv.focus();

// send the ctrl + 1/2 message, this should put focus back on the input box
const message = createMessageEvent({ type: InteractiveWindowMessages.Activate, payload: undefined });
ioc.postMessage(message);

// Then enter press shift + enter on the active element
const activeElement = document.activeElement;
if (activeElement) {
await submitInput(ioc, activeElement as HTMLTextAreaElement);
}
}

verifyHtmlOnCell(wrapper, 'InteractiveCell', '<span>1</span>', CellPosition.Last);
},
() => {
return ioc;
}
);

runMountedTest(
'Collapse / expand cell',
async wrapper => {
Expand Down
2 changes: 1 addition & 1 deletion src/test/datascience/testHelpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ export function simulateKey(domNode: HTMLTextAreaElement, key: string, shiftDown
}
}

async function submitInput(ioc: DataScienceIocContainer, textArea: HTMLTextAreaElement): Promise<void> {
export async function submitInput(ioc: DataScienceIocContainer, textArea: HTMLTextAreaElement): Promise<void> {
// Get a render promise with the expected number of renders (how many updates a the shift + enter will cause)
// Should be 6 - 1 for the shift+enter and 5 for the new cell.
const renderPromise = waitForMessage(ioc, InteractiveWindowMessages.ExecutionRendered);
Expand Down