-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master'
* upstream/master: Fetch tooltip details on-demand for auto-completions (#368) Fix interpreter display (#391)
- Loading branch information
Showing
17 changed files
with
844 additions
and
1,672 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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 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 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 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,39 @@ | ||
'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) { | ||
item.detail = itemInfos[0].detail; | ||
item.documentation = itemInfos[0].documentation; | ||
} | ||
} | ||
return item; | ||
} | ||
} |
Oops, something went wrong.