forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fetch tooltip details on-demand for auto-completions #368
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 30 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
7675901
Basic tokenizer
eb42669
Fixed property names
2756974
Tests, round I
c2c1ced
Tests, round II
a108c96
merge master
14864a5
tokenizer test
0ed51d6
Remove temorary change
51b544c
Fix merge issue
3cd11e6
Merge conflict
82e0ad1
Merge conflict
9295c1a
Completion test
06eb1a5
Fix last line
e9db8e0
Fix javascript math
d12ca03
Merge master
d8ab041
Make test await for results
db75cd0
Add license headers
9ab2c47
Rename definitions to types
d9c95d0
Round I
d587485
License headers
1da5e0a
Merge branch 'master' of https://github.com/Microsoft/vscode-python
3ac66b6
Merge branch 'master' into 152
94a5a7e
Separate completion and doc fetching
df5af0e
Test fixes
af5c648
Merge master
fdb0e57
Undo temp change
c67ac49
Merge branch 'master' into 152
3f27b3b
Merge branch 'master' of https://github.com/Microsoft/vscode-python i…
7163a1e
Merge branch '152' of https://github.com/MikhailArkhipov/vscode-pytho…
2c8b179
CR feedback
3689667
Restore signature, make info colorized
e193805
Provide details string
ed48871
Merge branch 'master' of https://github.com/Microsoft/vscode-python i…
92655ec
Fix tests
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,76 +1,41 @@ | ||
'use strict'; | ||
|
||
import * as vscode from 'vscode'; | ||
import { Position, ProviderResult, SnippetString, Uri } from 'vscode'; | ||
import { PythonSettings } from '../common/configSettings'; | ||
import { Tokenizer } from '../language/tokenizer'; | ||
import { TokenType } from '../language/types'; | ||
import { isTestExecution } from '../common/configSettings'; | ||
import { JediFactory } from '../languageServices/jediProxyFactory'; | ||
import { captureTelemetry } from '../telemetry'; | ||
import { COMPLETION } from '../telemetry/constants'; | ||
import { extractSignatureAndDocumentation } from './jediHelpers'; | ||
import * as proxy from './jediProxy'; | ||
import { CompletionSource } from './completionSource'; | ||
|
||
export class PythonCompletionItemProvider implements vscode.CompletionItemProvider { | ||
private completionSource: CompletionSource; | ||
|
||
public constructor(private jediFactory: JediFactory) { } | ||
private static parseData(data: proxy.ICompletionResult, resource: Uri): vscode.CompletionItem[] { | ||
if (data && data.items.length > 0) { | ||
return data.items.map(item => { | ||
const sigAndDocs = extractSignatureAndDocumentation(item); | ||
const completionItem = new vscode.CompletionItem(item.text); | ||
completionItem.kind = item.type; | ||
completionItem.documentation = sigAndDocs[1].length === 0 ? item.description : sigAndDocs[1]; | ||
completionItem.detail = sigAndDocs[0].split(/\r?\n/).join(''); | ||
if (PythonSettings.getInstance(resource).autoComplete.addBrackets === true && | ||
(item.kind === vscode.SymbolKind.Function || item.kind === vscode.SymbolKind.Method)) { | ||
completionItem.insertText = new SnippetString(item.text).appendText('(').appendTabstop().appendText(')'); | ||
} | ||
|
||
// ensure the built in memebers are at the bottom | ||
completionItem.sortText = (completionItem.label.startsWith('__') ? 'z' : (completionItem.label.startsWith('_') ? 'y' : '__')) + completionItem.label; | ||
return completionItem; | ||
}); | ||
} | ||
return []; | ||
constructor(jediFactory: JediFactory) { | ||
this.completionSource = new CompletionSource(jediFactory); | ||
} | ||
|
||
@captureTelemetry(COMPLETION) | ||
public provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken): ProviderResult<vscode.CompletionItem[]> { | ||
if (position.character <= 0) { | ||
return Promise.resolve([]); | ||
} | ||
const filename = document.fileName; | ||
const lineText = document.lineAt(position.line).text; | ||
if (lineText.match(/^\s*\/\//)) { | ||
return Promise.resolve([]); | ||
public async provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken): | ||
Promise<vscode.CompletionItem[]> { | ||
const items = await this.completionSource.getVsCodeCompletionItems(document, position, token); | ||
if (isTestExecution()) { | ||
for (let i = 0; i < Math.min(3, items.length); i += 1) { | ||
items[i] = await this.resolveCompletionItem(items[i], token); | ||
} | ||
} | ||
// Suppress completion inside string and comments | ||
if (this.isPositionInsideStringOrComment(document, position)) { | ||
return Promise.resolve([]); | ||
} | ||
const type = proxy.CommandType.Completions; | ||
const columnIndex = position.character; | ||
|
||
const source = document.getText(); | ||
const cmd: proxy.ICommand<proxy.ICommandResult> = { | ||
command: type, | ||
fileName: filename, | ||
columnIndex: columnIndex, | ||
lineIndex: position.line, | ||
source: source | ||
}; | ||
|
||
return this.jediFactory.getJediProxyHandler<proxy.ICompletionResult>(document.uri).sendCommand(cmd, token).then(data => { | ||
return PythonCompletionItemProvider.parseData(data, document.uri); | ||
}); | ||
return items; | ||
} | ||
|
||
private isPositionInsideStringOrComment(document: vscode.TextDocument, position: vscode.Position): boolean { | ||
const tokenizeTo = position.translate(1, 0); | ||
const text = document.getText(new vscode.Range(new Position(0, 0), tokenizeTo)); | ||
const t = new Tokenizer(); | ||
const tokens = t.Tokenize(text); | ||
const index = tokens.getItemContaining(document.offsetAt(position)); | ||
return index >= 0 && (tokens[index].TokenType === TokenType.String || tokens[index].TokenType === TokenType.Comment); | ||
public async resolveCompletionItem(item: vscode.CompletionItem, token: vscode.CancellationToken): Promise<vscode.CompletionItem> { | ||
if (!item.documentation) { | ||
const itemInfos = await this.completionSource.getDocumentation(item, token); | ||
if (itemInfos && itemInfos.length > 0) { | ||
// Only filling documentation since tooltip already contains | ||
// all the necessary information and is colorized via markdown. | ||
item.documentation = itemInfos[0].tooltip; | ||
item.detail = ''; | ||
} | ||
} | ||
return item; | ||
} | ||
} |
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.
@MikhailArkhipov
I think we'll need the details, lets talk on Monday.
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.
Without detail, you loose the signature displayed for every item (see below).

This is something that was requested some time ago in the extension (in some issue).
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 sure which one is which (
documentation
vsdetail
).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.
I get

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.
We'll chat Monday