|
3 | 3 | import * as vscode from 'vscode'; |
4 | 4 | import * as proxy from './jediProxy'; |
5 | 5 | import * as telemetryContracts from "../common/telemetryContracts"; |
6 | | - |
| 6 | +import { EOL } from 'os'; |
7 | 7 |
|
8 | 8 | export class PythonHoverProvider implements vscode.HoverProvider { |
9 | 9 | private jediProxyHandler: proxy.JediProxyHandler<proxy.ICompletionResult, vscode.Hover>; |
10 | 10 |
|
11 | 11 | public constructor(context: vscode.ExtensionContext) { |
12 | | - this.jediProxyHandler = new proxy.JediProxyHandler(context, null, PythonHoverProvider.parseData); |
| 12 | + this.jediProxyHandler = new proxy.JediProxyHandler(context); |
13 | 13 | } |
14 | | - private static parseData(data: proxy.ICompletionResult): vscode.Hover { |
15 | | - if (data && data.items.length > 0) { |
16 | | - var definition = data.items[0]; |
| 14 | + public provideHover(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken): Thenable<vscode.Hover> { |
| 15 | + var filename = document.fileName; |
| 16 | + if (document.lineAt(position.line).text.match(/^\s*\/\//)) { |
| 17 | + return Promise.resolve(); |
| 18 | + } |
| 19 | + if (position.character <= 0) { |
| 20 | + return Promise.resolve(); |
| 21 | + } |
17 | 22 |
|
18 | | - var txt = definition.description || definition.text; |
19 | | - return new vscode.Hover({ language: "python", value: txt }); |
| 23 | + var range = document.getWordRangeAtPosition(position); |
| 24 | + if (!range || range.isEmpty) { |
| 25 | + return Promise.resolve(); |
20 | 26 | } |
21 | | - return null; |
22 | | - } |
23 | | - public provideHover(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken): Thenable<vscode.Hover> { |
24 | | - return new Promise<vscode.Hover>((resolve, reject) => { |
25 | | - var filename = document.fileName; |
26 | | - if (document.lineAt(position.line).text.match(/^\s*\/\//)) { |
27 | | - return resolve(); |
| 27 | + var columnIndex = range.start.character < range.end.character ? range.start.character + 2 : range.end.character; |
| 28 | + var cmd: proxy.ICommand<proxy.ICompletionResult> = { |
| 29 | + telemetryEvent: telemetryContracts.IDE.HoverDefinition, |
| 30 | + command: proxy.CommandType.Completions, |
| 31 | + fileName: filename, |
| 32 | + columnIndex: columnIndex, |
| 33 | + lineIndex: position.line |
| 34 | + }; |
| 35 | + if (document.isDirty) { |
| 36 | + cmd.source = document.getText(); |
| 37 | + } |
| 38 | + |
| 39 | + return this.jediProxyHandler.sendCommand(cmd, token).then(data => { |
| 40 | + if (!data || !Array.isArray(data.items) || data.items.length === 0) { |
| 41 | + return; |
28 | 42 | } |
29 | | - if (position.character <= 0) { |
30 | | - return resolve(); |
| 43 | + // Find the right items |
| 44 | + const wordUnderCursor = document.getText(range); |
| 45 | + const completionItem = data.items.filter(item => item.text === wordUnderCursor); |
| 46 | + if (completionItem.length === 0) { |
| 47 | + return; |
31 | 48 | } |
32 | | - |
33 | | - var range = document.getWordRangeAtPosition(position); |
34 | | - if (range == undefined || range.isEmpty) { |
35 | | - return resolve(); |
| 49 | + var definition = completionItem[0]; |
| 50 | + var txt = definition.description || definition.text; |
| 51 | + if (typeof txt !== 'string' || txt.length === 0) { |
| 52 | + return; |
| 53 | + } |
| 54 | + if (wordUnderCursor === txt) { |
| 55 | + return; |
36 | 56 | } |
37 | | - var columnIndex = range.end.character; |
38 | | - var cmd: proxy.ICommand<proxy.ICompletionResult> = { |
39 | | - telemetryEvent: telemetryContracts.IDE.HoverDefinition, |
40 | | - command: proxy.CommandType.Completions, |
41 | | - fileName: filename, |
42 | | - columnIndex: columnIndex, |
43 | | - lineIndex: position.line |
44 | | - }; |
45 | | - if (document.isDirty){ |
46 | | - cmd.source = document.getText(); |
| 57 | + const lines = txt.split(EOL); |
| 58 | + if (lines.length > 2 && lines[1].trim().length === 0) { |
| 59 | + const line1 = lines[0]; |
| 60 | + lines.shift(); |
| 61 | + return new vscode.Hover([{ language: 'python', value: line1 }, lines.join(EOL)]); |
47 | 62 | } |
48 | | - this.jediProxyHandler.sendCommand(cmd, resolve, token); |
| 63 | + return new vscode.Hover(txt); |
49 | 64 | }); |
50 | 65 | } |
51 | 66 | } |
0 commit comments