Skip to content

Commit 4c7503f

Browse files
committed
fixed #447
1 parent 7087f8f commit 4c7503f

3 files changed

Lines changed: 55 additions & 42 deletions

File tree

‎src/client/providers/completionProvider.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import * as vscode from 'vscode';
44
import * as proxy from './jediProxy';
55
import * as telemetryContracts from '../common/telemetryContracts';
6+
import { extractSignatureAndDocumentation } from './jediHelpers';
7+
import { EOL } from 'os';
68

79
export class PythonCompletionItemProvider implements vscode.CompletionItemProvider {
810
private jediProxyHandler: proxy.JediProxyHandler<proxy.ICompletionResult, vscode.CompletionItem[]>;
@@ -13,9 +15,12 @@ export class PythonCompletionItemProvider implements vscode.CompletionItemProvid
1315
private static parseData(data: proxy.ICompletionResult): vscode.CompletionItem[] {
1416
if (data && data.items.length > 0) {
1517
return data.items.map(item => {
18+
const sigAndDocs = extractSignatureAndDocumentation(item);
1619
let completionItem = new vscode.CompletionItem(item.text);
1720
completionItem.kind = item.type;
18-
completionItem.documentation = item.description;
21+
completionItem.documentation = sigAndDocs[1].length === 0 ? item.description : sigAndDocs[1];
22+
completionItem.detail = sigAndDocs[0].split(EOL).join('');
23+
1924
// ensure the built in memebers are at the bottom
2025
completionItem.sortText = (completionItem.label.startsWith('__') ? 'z' : (completionItem.label.startsWith('_') ? 'y' : '__')) + completionItem.label;
2126
return completionItem;

‎src/client/providers/hoverProvider.ts‎

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import * as vscode from 'vscode';
44
import * as proxy from './jediProxy';
55
import * as telemetryContracts from "../common/telemetryContracts";
6-
import { EOL } from 'os';
6+
import { extractHoverInfo} from './jediHelpers';
77

88
export class PythonHoverProvider implements vscode.HoverProvider {
99
private jediProxyHandler: proxy.JediProxyHandler<proxy.ICompletionResult, vscode.Hover>;
@@ -59,43 +59,3 @@ export class PythonHoverProvider implements vscode.HoverProvider {
5959
});
6060
}
6161
}
62-
63-
function extractHoverInfo(definition: proxy.IAutoCompleteItem): vscode.Hover {
64-
// Somtimes the signature of the function, class (whatever) is broken into multiple lines
65-
// Here's an example
66-
// ```python
67-
// def __init__(self, group=None, name=None,
68-
// args=(), kwargs=None, verbose=None):
69-
// """This constructor should always be called with keyword arguments. Arguments are:
70-
71-
// *group* should be None; reserved for future extension when a ThreadGroup
72-
// class is implemented.
73-
/// """
74-
/// ```
75-
const txt = definition.description || definition.text;
76-
const rawDocString = typeof definition.raw_docstring === 'string' ? definition.raw_docstring.trim() : '';
77-
const firstLineOfRawDocString = rawDocString.length > 0 ? rawDocString.split(EOL)[0] : '';
78-
const lines = txt.split(EOL);
79-
const startIndexOfDocString = firstLineOfRawDocString === '' ? -1 : lines.findIndex(line => line.indexOf(firstLineOfRawDocString) === 0);
80-
81-
let signatureLines = startIndexOfDocString === -1 ? [lines.shift()] : lines.splice(0, startIndexOfDocString);
82-
let signature = signatureLines.filter(line => line.trim().length > 0).join(EOL);
83-
84-
switch (definition.type) {
85-
case vscode.CompletionItemKind.Constructor:
86-
case vscode.CompletionItemKind.Function:
87-
case vscode.CompletionItemKind.Method: {
88-
signature = 'def ' + signature;
89-
break;
90-
}
91-
case vscode.CompletionItemKind.Class: {
92-
signature = 'class ' + signature;
93-
break;
94-
}
95-
}
96-
const hoverInfo: vscode.MarkedString[] = [{ language: 'python', value: signature }];
97-
if (lines.some(line => line.trim().length > 0)) {
98-
hoverInfo.push(lines.join(EOL).trim().replace(/^\s+|\s+$/g, '').trim());
99-
}
100-
return new vscode.Hover(hoverInfo);
101-
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import * as proxy from './jediProxy';
2+
import { EOL } from 'os';
3+
import * as vscode from 'vscode';
4+
5+
export function extractSignatureAndDocumentation(definition: proxy.IAutoCompleteItem): [string, string] {
6+
// Somtimes the signature of the function, class (whatever) is broken into multiple lines
7+
// Here's an example
8+
// ```python
9+
// def __init__(self, group=None, name=None,
10+
// args=(), kwargs=None, verbose=None):
11+
// """This constructor should always be called with keyword arguments. Arguments are:
12+
13+
// *group* should be None; reserved for future extension when a ThreadGroup
14+
// class is implemented.
15+
/// """
16+
/// ```
17+
const txt = definition.description || definition.text;
18+
const rawDocString = typeof definition.raw_docstring === 'string' ? definition.raw_docstring.trim() : '';
19+
const firstLineOfRawDocString = rawDocString.length > 0 ? rawDocString.split(EOL)[0] : '';
20+
const lines = txt.split(EOL);
21+
const startIndexOfDocString = firstLineOfRawDocString === '' ? -1 : lines.findIndex(line => line.indexOf(firstLineOfRawDocString) === 0);
22+
23+
let signatureLines = startIndexOfDocString === -1 ? [lines.shift()] : lines.splice(0, startIndexOfDocString);
24+
let signature = signatureLines.filter(line => line.trim().length > 0).join(EOL);
25+
26+
switch (definition.type) {
27+
case vscode.CompletionItemKind.Constructor:
28+
case vscode.CompletionItemKind.Function:
29+
case vscode.CompletionItemKind.Method: {
30+
signature = 'def ' + signature;
31+
break;
32+
}
33+
case vscode.CompletionItemKind.Class: {
34+
signature = 'class ' + signature;
35+
break;
36+
}
37+
}
38+
return [signature, lines.join(EOL).trim().replace(/^\s+|\s+$/g, '').trim()];
39+
}
40+
41+
export function extractHoverInfo(definition: proxy.IAutoCompleteItem): vscode.Hover {
42+
const parts = extractSignatureAndDocumentation(definition);
43+
const hoverInfo: vscode.MarkedString[] = [{ language: 'python', value: parts[0] }];
44+
if (parts[1].length > 0) {
45+
hoverInfo.push(parts[1]);
46+
}
47+
return new vscode.Hover(hoverInfo);
48+
}

0 commit comments

Comments
 (0)