Skip to content
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

Ensure method parameter tooltip/popup never occurs within strings or comments #2072

Merged
merged 8 commits into from
Jul 2, 2018
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions news/2 Fixes/2057.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix bug where tooltips would popup whenever a comma is typed within a string.
15 changes: 4 additions & 11 deletions src/client/providers/completionSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,18 @@ export class CompletionSource {

private async getCompletionResult(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken)
: Promise<proxy.ICompletionResult | undefined> {
if (position.character <= 0) {
return undefined;
}
const filename = document.fileName;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not entirely clear why this block was here? Perhaps I'm missing something...

This code would skip processing for any line that begins with "[whitespace]//" which I believe is invalid for Python.

Please let me know if I should put this back (maybe embedded C-code within a .py file?).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is possible

x = \
"   //"

Copy link
Author

@d3r3kk d3r3kk Jun 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's true. However, the test here is for a line like this:
// some important thing in a comment...
or this:
// some comment...
...which isn't what Python would hold, I am certain.
(and the quotation marks in your example would fail the test I removed)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// some important thing in a comment...

This is NOT a comment.

const lineText = document.lineAt(position.line).text;
if (lineText.match(/^\s*\/\//)) {
return undefined;
}
// Suppress completion inside string and comments.
if (isPositionInsideStringOrComment(document, position)) {
if (position.character <= 0 ||
isPositionInsideStringOrComment(document, position)) {
return undefined;
}

const type = proxy.CommandType.Completions;
const columnIndex = position.character;

const source = document.getText();
const cmd: proxy.ICommand<proxy.ICommandResult> = {
command: type,
fileName: filename,
fileName: document.fileName,
columnIndex: columnIndex,
lineIndex: position.line,
source: source
Expand Down
16 changes: 8 additions & 8 deletions src/client/providers/providerUtilities.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import * as vscode from 'vscode';
import { Position, Range, TextDocument } from 'vscode';
import { Tokenizer } from '../language/tokenizer';
import { ITextRangeCollection, IToken, TokenizerMode, TokenType } from '../language/types';

export function getDocumentTokens(document: vscode.TextDocument, tokenizeTo: vscode.Position, mode: TokenizerMode): ITextRangeCollection<IToken> {
const text = document.getText(new vscode.Range(new vscode.Position(0, 0), tokenizeTo));
export function getDocumentTokens(document: TextDocument, tokenizeTo: Position, mode: TokenizerMode): ITextRangeCollection<IToken> {
const text = document.getText(new Range(new Position(0, 0), tokenizeTo));
return new Tokenizer().tokenize(text, 0, text.length, mode);
}

export function isPositionInsideStringOrComment(document: vscode.TextDocument, position: vscode.Position): boolean {
export function isPositionInsideStringOrComment(document: TextDocument, position: Position): boolean {
const tokenizeTo = position.translate(1, 0);
const tokens = getDocumentTokens(document, tokenizeTo, TokenizerMode.CommentsAndStrings);
const offset = document.offsetAt(position);
let index = tokens.getItemContaining(offset);
let index = tokens.getItemContaining(offset - 1);
if (index >= 0) {
const token = tokens.getItemAt(index);
return token.type === TokenType.String || token.type === TokenType.Comment;
}
if (offset > 0) {
if (offset > 1) {
// In case position is at the every end of the comment or unterminated string
index = tokens.getItemContaining(offset - 1);
index = tokens.getItemContaining(offset - 2);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am having difficulty understanding this one. The given comment is misleading a bit - the test only looks for tokens that are of type TokenType.Comment, but in Python I don't think there is an end to a comment (like there is in Cx languages) - isn't it just # -> everything to EOL for comments in Python? Or is there a special <# ... #> style syntax?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# is a single line comment token.
We use """ or ''' for multilines
Let me if that answers your question.
If that's ok, then I can approve this PR.

Copy link
Author

@d3r3kk d3r3kk Jun 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally answers my question... so removing this block here was the right move I would think.

I'm actually writing a few more tests for the 'isInsideStringOrComment' at the moment, I'll see if those multi-line strings are handled properly or not.

...sorry - responded to the wrong thing. Blaming my tired brain.

if (index >= 0) {
const token = tokens.getItemAt(index);
return token.end === offset && token.type === TokenType.Comment;
return token.end === (offset - 1) && token.type === TokenType.Comment;
}
}
return false;
Expand Down
10 changes: 9 additions & 1 deletion src/client/providers/signatureProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { JediFactory } from '../languageServices/jediProxyFactory';
import { captureTelemetry } from '../telemetry';
import { SIGNATURE } from '../telemetry/constants';
import * as proxy from './jediProxy';
import { isPositionInsideStringOrComment } from './providerUtilities';

const DOCSTRING_PARAM_PATTERNS = [
'\\s*:type\\s*PARAMNAME:\\s*([^\\n, ]+)', // Sphinx
Expand Down Expand Up @@ -71,7 +72,7 @@ export class PythonSignatureProvider implements SignatureHelpProvider {

if (validParamInfo) {
const docLines = def.docstring.splitLines();
label = docLines.shift().trim();
label = docLines.shift()!.trim();
documentation = docLines.join(EOL).trim();
} else {
if (def.params && def.params.length > 0) {
Expand Down Expand Up @@ -111,6 +112,13 @@ export class PythonSignatureProvider implements SignatureHelpProvider {
}
@captureTelemetry(SIGNATURE)
public provideSignatureHelp(document: TextDocument, position: Position, token: CancellationToken): Thenable<SignatureHelp> {
// early exit if we're in a string or comment (or in an undefined position)
if (position.character <= 0 ||
isPositionInsideStringOrComment(document, position))
{
return Promise.resolve(new SignatureHelp());
}

const cmd: proxy.ICommand<proxy.IArgumentsResult> = {
command: proxy.CommandType.Arguments,
fileName: document.fileName,
Expand Down
107 changes: 107 additions & 0 deletions src/test/providers/pythonSignatureProvider.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

'use strict';

// tslint:disable:max-func-body-length

import { assert, expect, use } from 'chai';
import * as chaipromise from 'chai-as-promised';
import * as TypeMoq from 'typemoq';
import { CancellationToken, Position, SignatureHelp,
TextDocument, TextLine, Uri } from 'vscode';
import { JediFactory } from '../../client/languageServices/jediProxyFactory';
import { IArgumentsResult, JediProxyHandler } from '../../client/providers/jediProxy';
import { PythonSignatureProvider } from '../../client/providers/signatureProvider';

use(chaipromise);

suite('Signature Provider unit tests', () => {
let pySignatureProvider: PythonSignatureProvider;
let jediHandler: TypeMoq.IMock<JediProxyHandler<IArgumentsResult>>;
let argResultItems: IArgumentsResult;
setup(() => {
const jediFactory = TypeMoq.Mock.ofType(JediFactory);
jediHandler = TypeMoq.Mock.ofType<JediProxyHandler<IArgumentsResult>>();
jediFactory.setup(j => j.getJediProxyHandler(TypeMoq.It.isAny()))
.returns(() => jediHandler.object);
pySignatureProvider = new PythonSignatureProvider(jediFactory.object);
argResultItems = {
definitions: [
{
description: 'The result',
docstring: 'Some docstring goes here.',
name: 'print',
paramindex: 0,
params: [
{
description: 'Some parameter',
docstring: 'gimme docs',
name: 'param',
value: 'blah'
}
]
}
],
requestId: 1
};
});

function testSignatureReturns(source: string, pos: number): Thenable<SignatureHelp> {
const doc = TypeMoq.Mock.ofType<TextDocument>();
const position = new Position(0, pos);
const lineText = TypeMoq.Mock.ofType<TextLine>();
const argsResult = TypeMoq.Mock.ofType<IArgumentsResult>();
const cancelToken = TypeMoq.Mock.ofType<CancellationToken>();
cancelToken.setup(ct => ct.isCancellationRequested).returns(() => false);

doc.setup(d => d.fileName).returns(() => '');
doc.setup(d => d.getText(TypeMoq.It.isAny())).returns(() => source);
doc.setup(d => d.lineAt(TypeMoq.It.isAny())).returns(() => lineText.object);
doc.setup(d => d.offsetAt(TypeMoq.It.isAny())).returns(() => pos - 1); // pos is 1-based
const docUri = TypeMoq.Mock.ofType<Uri>();
docUri.setup(u => u.scheme).returns(() => 'http');
doc.setup(d => d.uri).returns(() => docUri.object);
lineText.setup(l => l.text).returns(() => source);
argsResult.setup(c => c.requestId).returns(() => 1);
argsResult.setup(c => c.definitions).returns(() => argResultItems[0].definitions);
jediHandler.setup(j => j.sendCommand(TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => {
return Promise.resolve(argResultItems);
});

return pySignatureProvider.provideSignatureHelp(doc.object, position, cancelToken.object);
}

test('Ensure no signature is given within a string.', async () => {
const source = ' print(\'Python is awesome,\')\n';
const sigHelp: SignatureHelp = await testSignatureReturns(source, 27);
expect(sigHelp).to.not.be.equal(undefined, 'Expected to get a blank signature item back - did the pattern change here?');
expect(sigHelp.signatures.length).to.equal(0, 'Signature provided for symbols within a string?');
});
test('Ensure no signature is given within a line comment.', async () => {
const source = '# print(\'Python is awesome,\')\n';
const sigHelp: SignatureHelp = await testSignatureReturns(source, 28);
expect(sigHelp).to.not.be.equal(undefined, 'Expected to get a blank signature item back - did the pattern change here?');
expect(sigHelp.signatures.length).to.equal(0, 'Signature provided for symbols within a full-line comment?');
});
test('Ensure no signature is given within a comment tailing a command.', async () => {
const source = ' print(\'Python\') # print(\'is awesome,\')\n';
const sigHelp: SignatureHelp = await testSignatureReturns(source, 38);
expect(sigHelp).to.not.be.equal(undefined, 'Expected to get a blank signature item back - did the pattern change here?');
expect(sigHelp.signatures.length).to.equal(0, 'Signature provided for symbols within a trailing comment?');
});
test('Ensure signature is given for built-in print command.', async () => {
const source = ' print(\'Python\',)\n';
let sigHelp: SignatureHelp;
try {
sigHelp = await testSignatureReturns(source, 17);
expect(sigHelp).to.not.equal(undefined, 'Expected to get a blank signature item back - did the pattern change here?');
expect(sigHelp.signatures.length).to.not.equal(0, 'Expected dummy argresult back from testing our print signature.');
expect(sigHelp.activeParameter).to.be.equal(0, 'Parameter for print should be the first member of the test argresult\'s params object.');
expect(sigHelp.activeSignature).to.be.equal(0, 'The signature for print should be the first member of the test argresult.');
expect(sigHelp.signatures[sigHelp.activeSignature].label).to.be.equal('print(param)', `Expected arg result calls for specific returned signature of \'print(param)\' but we got ${sigHelp.signatures[sigHelp.activeSignature].label}`);
} catch (error) {
assert(false, `Caught exception ${error}`);
}
});
});
1 change: 1 addition & 0 deletions src/test/vscode-mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ mockedVSCode.SnippetString = vscodeMocks.vscMockExtHostedTypes.SnippetString;
mockedVSCode.EventEmitter = vscodeMocks.vscMock.EventEmitter;
mockedVSCode.ConfigurationTarget = vscodeMocks.vscMockExtHostedTypes.ConfigurationTarget;
mockedVSCode.StatusBarAlignment = vscodeMocks.vscMockExtHostedTypes.StatusBarAlignment;
mockedVSCode.SignatureHelp = vscodeMocks.vscMockExtHostedTypes.SignatureHelp;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DonJayamanne you rock so hard. I love that I was able to add this mock so easily.


// This API is used in src/client/telemetry/telemetry.ts
const extensions = TypeMoq.Mock.ofType<typeof vscode.extensions>();
Expand Down