Skip to content

Commit 459600f

Browse files
committed
completed signature parser with cleanup
1 parent af6d5b2 commit 459600f

4 files changed

Lines changed: 95 additions & 8 deletions

File tree

src/client/jedi/parsers/HoverParser.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import { highlightCode } from '../../providers/jediHelpers';
44
import { EOL } from 'os';
55
export class HoverParser {
66
public static parse(data: proxy.IHoverResult, currentWord: string): Hover {
7+
if (!data || !Array.isArray(data.items) || data.items.length === 0) {
8+
return new Hover([]);
9+
}
10+
711
let results = [];
812
let capturedInfo: string[] = [];
913
data.items.forEach(item => {
Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
1-
import { Location } from 'vscode';
1+
import { Location, Range, Uri } from 'vscode';
2+
import * as proxy from '../../providers/jediProxy';
23
export class LocationParser {
3-
public static parse(data: Object): Location[] {
4-
return [];
4+
public static parse(data: proxy.IReferenceResult): Location[] {
5+
if (!data || !Array(data.references) || data.references.length === 0) {
6+
return [];
7+
}
8+
var references = data.references.filter(ref => {
9+
if (!ref || typeof ref.columnIndex !== 'number' || typeof ref.lineIndex !== 'number'
10+
|| typeof ref.fileName !== 'string' || ref.columnIndex === -1 || ref.lineIndex === -1 || ref.fileName.length === 0) {
11+
return false;
12+
}
13+
return true;
14+
}).map(ref => {
15+
var definitionResource = Uri.file(ref.fileName);
16+
17+
var range = new Range(ref.lineIndex, ref.columnIndex, ref.lineIndex, ref.columnIndex);
18+
return new Location(definitionResource, range);
19+
});
20+
21+
return references;
522
}
6-
}
23+
}
Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,73 @@
11
import { SignatureHelp } from 'vscode';
2+
import * as vscode from "vscode";
3+
import * as proxy from "../../providers/jediProxy";
4+
5+
const DOCSTRING_PARAM_PATTERNS = [
6+
"\\s*:type\\s*PARAMNAME:\\s*([^\\n, ]+)", // Sphinx
7+
"\\s*:param\\s*(\\w?)\\s*PARAMNAME:[^\\n]+", // Sphinx param with type
8+
"\\s*@type\\s*PARAMNAME:\\s*([^\\n, ]+)" // Epydoc
9+
];
10+
11+
/**
12+
* Extrct the documentation for parameters from a given docstring
13+
*
14+
* @param {string} paramName Name of the parameter
15+
* @param {string} docString The docstring for the function
16+
* @returns {string} Docstring for the parameter
17+
*/
18+
function extractParamDocString(paramName: string, docString: string): string {
19+
let paramDocString = "";
20+
// In docstring the '*' is escaped with a backslash
21+
paramName = paramName.replace(new RegExp("\\*", "g"), "\\\\\\*");
22+
23+
DOCSTRING_PARAM_PATTERNS.forEach(pattern => {
24+
if (paramDocString.length > 0) {
25+
return;
26+
}
27+
pattern = pattern.replace("PARAMNAME", paramName);
28+
let regExp = new RegExp(pattern);
29+
let matches = regExp.exec(docString);
30+
if (matches && matches.length > 0) {
31+
paramDocString = matches[0];
32+
if (paramDocString.indexOf(":") >= 0) {
33+
paramDocString = paramDocString.substring(paramDocString.indexOf(":") + 1);
34+
}
35+
if (paramDocString.indexOf(":") >= 0) {
36+
paramDocString = paramDocString.substring(paramDocString.indexOf(":") + 1);
37+
}
38+
}
39+
});
40+
41+
return paramDocString.trim();
42+
}
243
export class SignatureHelpParser {
3-
public static parse(data: Object): SignatureHelp {
4-
return null;
44+
public static parse(data: proxy.IArgumentsResult): SignatureHelp {
45+
if (!data || !Array.isArray(data.definitions) || data.definitions.length === 0) {
46+
return new SignatureHelp();
47+
}
48+
let signature = new SignatureHelp();
49+
signature.activeSignature = 0;
50+
51+
data.definitions.forEach(def => {
52+
signature.activeParameter = def.paramindex;
53+
// Don't display the documentation, as vs code doesn't format the docmentation
54+
// i.e. line feeds are not respected, long content is stripped
55+
let sig = <vscode.SignatureInformation>{
56+
// documentation: def.docstring,
57+
label: def.description,
58+
parameters: []
59+
};
60+
sig.parameters = def.params.map(arg => {
61+
if (arg.docstring.length === 0) {
62+
arg.docstring = extractParamDocString(arg.name, def.docstring);
63+
}
64+
return <vscode.ParameterInformation>{
65+
documentation: arg.docstring.length > 0 ? arg.docstring : arg.description,
66+
label: arg.description.length > 0 ? arg.description : arg.name
67+
};
68+
});
69+
signature.signatures.push(sig);
70+
});
71+
return signature;
572
}
673
}

src/client/providers/signatureProvider.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
"use strict";
22

33
import * as vscode from "vscode";
4-
import { TextDocument, Position, CancellationToken, SignatureHelp, ExtensionContext } from "vscode";
4+
import { TextDocument, Position, CancellationToken, SignatureHelp } from "vscode";
55
import * as proxy from "./jediProxy";
6-
import * as telemetryContracts from "../common/telemetryContracts";
76

87
const DOCSTRING_PARAM_PATTERNS = [
98
"\\s*:type\\s*PARAMNAME:\\s*([^\\n, ]+)", // Sphinx

0 commit comments

Comments
 (0)