forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Ensure method parameter tooltip/popup never occurs within strings or comments #2072
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
997c7b9
Discovered root cause & fixed issue with popups in comments
d3r3kk 609ae44
Add tests for signature provider
d3r3kk 57bb883
News file
d3r3kk 5dd6e76
Off-by-one problem in determination of tokens
d3r3kk 677f7e3
Commit the proper off-by-one fix...
d3r3kk d86ed09
Update index of cursor position for expected signature
d3r3kk 29afb84
Add further tests
d3r3kk c5c192e
Update unit tests for Singature (Jedi)
d3r3kk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,28 @@ | ||
| // 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); | ||
| const 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 > 0 && index >= 0) { | ||
| // In case position is at the every end of the comment or unterminated string | ||
| index = tokens.getItemContaining(offset - 1); | ||
| if (index >= 0) { | ||
| const token = tokens.getItemAt(index); | ||
| return token.end === offset && token.type === TokenType.Comment; | ||
| } | ||
| const token = tokens.getItemAt(index); | ||
| return token.end === offset && token.type === TokenType.Comment; | ||
| } | ||
| return false; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
248 changes: 248 additions & 0 deletions
248
src/test/providers/pythonSignatureProvider.unit.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,248 @@ | ||
| // 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 { isPositionInsideStringOrComment } from '../../client/providers/providerUtilities'; | ||
| 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); | ||
| } | ||
|
|
||
| function testIsInsideStringOrComment(sourceLine: string, sourcePos: number) : boolean { | ||
| const textLine: TypeMoq.IMock<TextLine> = TypeMoq.Mock.ofType<TextLine>(); | ||
| textLine.setup(t => t.text).returns(() => sourceLine); | ||
| const doc: TypeMoq.IMock<TextDocument> = TypeMoq.Mock.ofType<TextDocument>(); | ||
| const pos: Position = new Position(1, sourcePos); | ||
|
|
||
| doc.setup(d => d.fileName).returns(() => ''); | ||
| doc.setup(d => d.getText(TypeMoq.It.isAny())).returns(() => sourceLine); | ||
| doc.setup(d => d.lineAt(TypeMoq.It.isAny())).returns(() => textLine.object); | ||
| doc.setup(d => d.offsetAt(TypeMoq.It.isAny())).returns(() => sourcePos); | ||
|
|
||
| return isPositionInsideStringOrComment(doc.object, pos); | ||
| } | ||
|
|
||
| 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, 18); | ||
| 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}`); | ||
| } | ||
| }); | ||
| test('Ensure isPositionInsideStringOrComment is behaving as expected.', () => { | ||
| const sourceLine: string = ' print(\'Hello world!\')\n'; | ||
| const sourcePos: number = sourceLine.length - 1; | ||
| const isInsideStrComment: boolean = testIsInsideStringOrComment(sourceLine, sourcePos); | ||
|
|
||
| expect(isInsideStrComment).to.not.be.equal(true, [ | ||
| `Position set to the end of ${sourceLine} but `, | ||
| 'is reported as being within a string or comment.'].join('')); | ||
| }); | ||
| test('Ensure isPositionInsideStringOrComment is behaving as expected at end of source.', () => { | ||
| const sourceLine: string = ' print(\'Hello world!\')\n'; | ||
| const sourcePos: number = 0; | ||
| const isInsideStrComment: boolean = testIsInsideStringOrComment(sourceLine, sourcePos); | ||
|
|
||
| expect(isInsideStrComment).to.not.be.equal(true, [ | ||
| `Position set to the end of ${sourceLine} but `, | ||
| 'is reported as being within a string or comment.'].join('')); | ||
| }); | ||
| test('Ensure isPositionInsideStringOrComment is behaving as expected at beginning of source.', () => { | ||
| const sourceLine: string = ' print(\'Hello world!\')\n'; | ||
| const sourcePos: number = 0; | ||
| const isInsideStrComment: boolean = testIsInsideStringOrComment(sourceLine, sourcePos); | ||
|
|
||
| expect(isInsideStrComment).to.not.be.equal(true, [ | ||
| `Position set to the beginning of ${sourceLine} but `, | ||
| 'is reported as being within a string or comment.'].join('')); | ||
| }); | ||
| test('Ensure isPositionInsideStringOrComment is behaving as expected within a string.', () => { | ||
| const sourceLine: string = ' print(\'Hello world!\')\n'; | ||
| const sourcePos: number = 16; | ||
| const isInsideStrComment: boolean = testIsInsideStringOrComment(sourceLine, sourcePos); | ||
|
|
||
| expect(isInsideStrComment).to.be.equal(true, [ | ||
| `Position set within the string in ${sourceLine} (position ${sourcePos}) but `, | ||
| 'is reported as NOT being within a string or comment.'].join('')); | ||
| }); | ||
| test('Ensure isPositionInsideStringOrComment is behaving as expected immediately before a string.', () => { | ||
| const sourceLine: string = ' print(\'Hello world!\')\n'; | ||
| const sourcePos: number = 8; | ||
| const isInsideStrComment: boolean = testIsInsideStringOrComment(sourceLine, sourcePos); | ||
|
|
||
| expect(isInsideStrComment).to.be.equal(false, [ | ||
| `Position set to just before the string in ${sourceLine} (position ${sourcePos}) but `, | ||
| 'is reported as being within a string or comment.'].join('')); | ||
| }); | ||
| test('Ensure isPositionInsideStringOrComment is behaving as expected immediately in a string.', () => { | ||
| const sourceLine: string = ' print(\'Hello world!\')\n'; | ||
| const sourcePos: number = 9; | ||
| const isInsideStrComment: boolean = testIsInsideStringOrComment(sourceLine, sourcePos); | ||
|
|
||
| expect(isInsideStrComment).to.be.equal(true, [ | ||
| `Position set to the start of the string in ${sourceLine} (position ${sourcePos}) but `, | ||
| 'is reported as being within a string or comment.'].join('')); | ||
| }); | ||
| test('Ensure isPositionInsideStringOrComment is behaving as expected within a comment.', () => { | ||
| const sourceLine: string = '# print(\'Hello world!\')\n'; | ||
| const sourcePos: number = 16; | ||
| const isInsideStrComment: boolean = testIsInsideStringOrComment(sourceLine, sourcePos); | ||
|
|
||
| expect(isInsideStrComment).to.be.equal(true, [ | ||
| `Position set within a full line comment ${sourceLine} (position ${sourcePos}) but `, | ||
| 'is reported as NOT being within a string or comment.'].join('')); | ||
| }); | ||
| test('Ensure isPositionInsideStringOrComment is behaving as expected within a trailing comment.', () => { | ||
| const sourceLine: string = ' print(\'Hello world!\') # some comment...\n'; | ||
| const sourcePos: number = 34; | ||
| const isInsideStrComment: boolean = testIsInsideStringOrComment(sourceLine, sourcePos); | ||
|
|
||
| expect(isInsideStrComment).to.be.equal(true, [ | ||
| `Position set within a trailing line comment ${sourceLine} (position ${sourcePos}) but `, | ||
| 'is reported as NOT being within a string or comment.'].join('')); | ||
| }); | ||
| test('Ensure isPositionInsideStringOrComment is behaving as expected at the very end of a trailing comment.', () => { | ||
| const sourceLine: string = ' print(\'Hello world!\') # some comment...\n'; | ||
| const sourcePos: number = sourceLine.length - 1; | ||
| const isInsideStrComment: boolean = testIsInsideStringOrComment(sourceLine, sourcePos); | ||
|
|
||
| expect(isInsideStrComment).to.be.equal(true, [ | ||
| `Position set within a trailing line comment ${sourceLine} (position ${sourcePos}) but `, | ||
| 'is reported as NOT being within a string or comment.'].join('')); | ||
| }); | ||
| test('Ensure isPositionInsideStringOrComment is behaving as expected within a multiline string.', () => { | ||
| const sourceLine: string = ' stringVal = \'\'\'This is a multiline\nstring that you can use\nto test this stuff out with\neveryday!\'\'\'\n'; | ||
| const sourcePos: number = 48; | ||
| const isInsideStrComment: boolean = testIsInsideStringOrComment(sourceLine, sourcePos); | ||
|
|
||
| expect(isInsideStrComment).to.be.equal(true, [ | ||
| `Position set within a multi-line string ${sourceLine} (position ${sourcePos}) but `, | ||
| 'is reported as NOT being within a string or comment.'].join('')); | ||
| }); | ||
| test('Ensure isPositionInsideStringOrComment is behaving as expected at the very last quote on a multiline string.', () => { | ||
| const sourceLine: string = ' stringVal = \'\'\'This is a multiline\nstring that you can use\nto test this stuff out with\neveryday!\'\'\'\n'; | ||
| const sourcePos: number = sourceLine.length - 2; // just at the last ' | ||
| const isInsideStrComment: boolean = testIsInsideStringOrComment(sourceLine, sourcePos); | ||
|
|
||
| expect(isInsideStrComment).to.be.equal(true, [ | ||
| `Position set within a multi-line string ${sourceLine} (position ${sourcePos}) but `, | ||
| 'is reported as NOT being within a string or comment.'].join('')); | ||
| }); | ||
| test('Ensure isPositionInsideStringOrComment is behaving as expected within a multiline string (double-quoted).', () => { | ||
| const sourceLine: string = ' stringVal = """This is a multiline\nstring that you can use\nto test this stuff out with\neveryday!"""\n'; | ||
| const sourcePos: number = 48; | ||
| const isInsideStrComment: boolean = testIsInsideStringOrComment(sourceLine, sourcePos); | ||
|
|
||
| expect(isInsideStrComment).to.be.equal(true, [ | ||
| `Position set within a multi-line string ${sourceLine} (position ${sourcePos}) but `, | ||
| 'is reported as NOT being within a string or comment.'].join('')); | ||
| }); | ||
| test('Ensure isPositionInsideStringOrComment is behaving as expected at the very last quote on a multiline string (double-quoted).', () => { | ||
| const sourceLine: string = ' stringVal = """This is a multiline\nstring that you can use\nto test this stuff out with\neveryday!"""\n'; | ||
| const sourcePos: number = sourceLine.length - 2; // just at the last ' | ||
| const isInsideStrComment: boolean = testIsInsideStringOrComment(sourceLine, sourcePos); | ||
|
|
||
| expect(isInsideStrComment).to.be.equal(true, [ | ||
| `Position set within a multi-line string ${sourceLine} (position ${sourcePos}) but `, | ||
| 'is reported as NOT being within a string or comment.'].join('')); | ||
| }); | ||
| test('Ensure isPositionInsideStringOrComment is behaving as expected during construction of a multiline string (double-quoted).', () => { | ||
| const sourceLine: string = ' stringVal = """This is a multiline\nstring that you can use\nto test this stuff'; | ||
| const sourcePos: number = sourceLine.length - 1; // just at the last position in the string before it's termination | ||
| const isInsideStrComment: boolean = testIsInsideStringOrComment(sourceLine, sourcePos); | ||
|
|
||
| expect(isInsideStrComment).to.be.equal(true, [ | ||
| `Position set within a multi-line string ${sourceLine} (position ${sourcePos}) but `, | ||
| 'is reported as NOT being within a string or comment.'].join('')); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
Uh oh!
There was an error while loading. Please reload this page.
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.