-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Changes from 5 commits
997c7b9
609ae44
57bb883
5dd6e76
677f7e3
d86ed09
29afb84
c5c192e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. |
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
...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; | ||
|
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}`); | ||
} | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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>(); | ||
|
There was a problem hiding this comment.
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?).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is possible
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.