Skip to content

Davidkutu/port intellisense fix #10531

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 6 commits into from
Mar 12, 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 @@ -25,6 +25,8 @@

### Fixes

1. Jupyter autocompletion will only show magic commands on empty lines, preventing them of appearing in functions.
([#10023](https://github.com/Microsoft/vscode-python/issues/10023))
1. Remove extra lines at the end of the file when formatting with Black.
([#1877](https://github.com/Microsoft/vscode-python/issues/1877))
1. Capitalize `Activate.ps1` in code for PowerShell Core on Linux.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ import {
IInteractiveWindowListener,
IInteractiveWindowProvider,
IJupyterExecution,
INotebook
INotebook,
INotebookCompletion
} from '../../types';
import {
ICancelIntellisenseRequest,
Expand Down Expand Up @@ -377,6 +378,7 @@ export class IntellisenseProvider implements IInteractiveWindowListener {
request.cellId,
cancelSource.token
);

const jupyterCompletions = this.provideJupyterCompletionItems(
request.position,
request.context,
Expand Down Expand Up @@ -469,6 +471,8 @@ export class IntellisenseProvider implements IInteractiveWindowListener {

const jupyterResults = await activeNotebook.getCompletion(data.text, offsetInCode, cancelToken);
if (jupyterResults && jupyterResults.matches) {
const filteredMatches = this.filterJupyterMatches(document, jupyterResults, cellId, position);

const baseOffset = data.offset;
const basePosition = document.positionAt(baseOffset);
const startPosition = document.positionAt(jupyterResults.cursor.start + baseOffset);
Expand All @@ -480,11 +484,7 @@ export class IntellisenseProvider implements IInteractiveWindowListener {
endColumn: endPosition.character + 1
};
return {
suggestions: convertStringsToSuggestions(
jupyterResults.matches,
range,
jupyterResults.metadata
),
suggestions: convertStringsToSuggestions(filteredMatches, range, jupyterResults.metadata),
incomplete: false
};
}
Expand All @@ -502,6 +502,23 @@ export class IntellisenseProvider implements IInteractiveWindowListener {
};
}

// The suggestions that the kernel is giving always include magic commands. That is confusing to the user.
// This function is called by provideJupyterCompletionItems to filter those magic commands when not in an empty line of code.
private filterJupyterMatches(
document: IntellisenseDocument,
jupyterResults: INotebookCompletion,
cellId: string,
position: monacoEditor.Position
) {
// If the line we're analyzing is empty or a whitespace, we filter out the magic commands
// as its confusing to see them appear after a . or inside ().
const pos = document.convertToDocumentPosition(cellId, position.lineNumber, position.column);
const line = document.lineAt(pos);
return line.isEmptyOrWhitespace
? jupyterResults.matches
: jupyterResults.matches.filter(match => !match.startsWith('%'));
}

private postTimedResponse<R, M extends IInteractiveWindowMapping, T extends keyof M>(
promises: Promise<R>[],
message: T,
Expand Down
66 changes: 65 additions & 1 deletion src/test/datascience/intellisense.functional.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { MonacoEditor } from '../../datascience-ui/react-common/monacoEditor';
import { noop } from '../core';
import { DataScienceIocContainer } from './dataScienceIocContainer';
import { getOrCreateInteractiveWindow, runMountedTest } from './interactiveWindowTestHelpers';
import { getInteractiveEditor, typeCode } from './testHelpers';
import { enterEditorKey, getInteractiveEditor, typeCode } from './testHelpers';

// tslint:disable:max-func-body-length trailing-comma no-any no-multiline-string
suite('DataScience Intellisense tests', () => {
Expand Down Expand Up @@ -69,6 +69,14 @@ suite('DataScience Intellisense tests', () => {
assert.ok(innerTexts.includes(expectedSpan), 'Intellisense row not matching');
}

function verifyIntellisenseNotVisible(
wrapper: ReactWrapper<any, Readonly<{}>, React.Component>,
expectedSpan: string
) {
const innerTexts = getIntellisenseTextLines(wrapper);
assert.ok(!innerTexts.includes(expectedSpan), 'Intellisense row is showing');
}

function waitForSuggestion(
wrapper: ReactWrapper<any, Readonly<{}>, React.Component>
): { disposable: IDisposable; promise: Promise<void> } {
Expand Down Expand Up @@ -230,4 +238,60 @@ suite('DataScience Intellisense tests', () => {
return ioc;
}
);

runMountedTest(
'Filtered Jupyter autocomplete, verify magic commands appear',
async wrapper => {
if (ioc.mockJupyter) {
// This test only works when mocking.

// Create an interactive window so that it listens to the results.
const interactiveWindow = await getOrCreateInteractiveWindow(ioc);
await interactiveWindow.show();

// Then enter some code. Don't submit, we're just testing that autocomplete appears
const suggestion = waitForSuggestion(wrapper);
typeCode(getInteractiveEditor(wrapper), 'print');
enterEditorKey(wrapper, { code: ' ', ctrlKey: true });
await suggestion.promise;
suggestion.disposable.dispose();
verifyIntellisenseNotVisible(wrapper, '%%bash');

// Force suggestion box to disappear so that shutdown doesn't try to generate suggestions
// while we're destroying the editor.
clearEditor(wrapper);
}
},
() => {
return ioc;
}
);

runMountedTest(
'Filtered Jupyter autocomplete, verify magic commands are filtered',
async wrapper => {
if (ioc.mockJupyter) {
// This test only works when mocking.

// Create an interactive window so that it listens to the results.
const interactiveWindow = await getOrCreateInteractiveWindow(ioc);
await interactiveWindow.show();

// Then enter some code. Don't submit, we're just testing that autocomplete appears
const suggestion = waitForSuggestion(wrapper);
typeCode(getInteractiveEditor(wrapper), ' ');
enterEditorKey(wrapper, { code: ' ', ctrlKey: true });
await suggestion.promise;
suggestion.disposable.dispose();
verifyIntellisenseVisible(wrapper, '%%bash');

// Force suggestion box to disappear so that shutdown doesn't try to generate suggestions
// while we're destroying the editor.
clearEditor(wrapper);
}
},
() => {
return ioc;
}
);
});
2 changes: 1 addition & 1 deletion src/test/datascience/mockJupyterSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export class MockJupyterSession implements IJupyterSession {

return {
content: {
matches: ['printly'], // This keeps this in the intellisense when the editor pairs down results
matches: ['printly', '%%bash'], // This keeps this in the intellisense when the editor pairs down results
cursor_start: 0,
cursor_end: 7,
status: 'ok',
Expand Down