Skip to content

Commit d21e64f

Browse files
Yu ZhangDonJayamanne
authored andcommitted
Better formatting of docstring (#919)
* πŸ› split docstring with /\r?\n/ rather than os.EOL * πŸ’„ 'translate' RST to Markdown * ✨ better formatting * ✨ support grid table * 🎨 add leading/trailing blank lines to avoid corner cases in regex * πŸ› handle setext headings which use `-`
1 parent 6eb1f26 commit d21e64f

2 files changed

Lines changed: 46 additions & 10 deletions

File tree

β€Žsrc/client/providers/hoverProvider.tsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class PythonHoverProvider implements vscode.HoverProvider {
4747
lines.shift();
4848
}
4949
let descriptionWithHighlightedCode = highlightCode(lines.join(EOL));
50-
let hoverInfo = '```python' + EOL + signature + EOL + '```' + EOL + descriptionWithHighlightedCode;
50+
let hoverInfo = ['```python', signature, '```', descriptionWithHighlightedCode].join(EOL);
5151
let key = signature + lines.join('');
5252
// Sometimes we have duplicate documentation, one with a period at the end
5353
if (capturedInfo.indexOf(key) >= 0 || capturedInfo.indexOf(key + '.') >= 0) {

β€Žsrc/client/providers/jediHelpers.tsβ€Ž

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,50 @@ export function extractSignatureAndDocumentation(definition: proxy.IAutoComplete
4848
return [signature, lines.join(EOL).trim().replace(/^\s+|\s+$/g, '').trim()];
4949
}
5050

51-
export function highlightCode(documentation: string): string {
52-
let lines = documentation.split(/\r?\n/);
53-
lines = lines.map(line => {
54-
if (line.trim().startsWith('>>> ')) {
55-
return '```python\n' + line.substring(4).trim() + '\n```';
51+
export function highlightCode(docstring: string): string {
52+
// Add blank line
53+
docstring = EOL + EOL + docstring + EOL + EOL;
54+
// Section title -> heading level 2
55+
docstring = docstring.replace(/(.+\r?\n)[-=]+\r?\n/g, '## $1' + EOL);
56+
// Directives: '.. directive::' -> '**directive**'
57+
docstring = docstring.replace(/\.\. (.*)::/g, '**$1**');
58+
// Field lists: ':field:' -> '**field**'
59+
docstring = docstring.replace(/:(.+?):/g, '**$1** ');
60+
// Pattern of 'var : description'
61+
let paramLinePattern = '[\\*\\w_]+ ?:[^:\r\n]+';
62+
// Add new line after and before param line
63+
docstring = docstring.replace(new RegExp(`(${EOL + paramLinePattern})`, 'g'), `$1${EOL}`);
64+
docstring = docstring.replace(new RegExp(`(${EOL + paramLinePattern + EOL})`, 'g'), `${EOL}$1`);
65+
// 'var : description' -> '`var` description'
66+
docstring = docstring.replace(/\r?\n([\*\w]+) ?: ?([^:\r\n]+\r?\n)/g, `${EOL}\`$1\` $2`);
67+
// Doctest blocks: begin with `>>>` and end with blank line
68+
docstring = docstring.replace(/(>>>[\w\W]+?\r?\n)\r?\n/g, `${'```python' + EOL}$1${'```' + EOL + EOL}`);
69+
// Literal blocks: begin with `::` (literal blocks are indented or quoted; for simplicity, we end literal blocks with blank line)
70+
docstring = docstring.replace(/(\r?\n[^\.]*)::\r?\n\r?\n([\w\W]+?\r?\n)\r?\n/g, `$1${EOL + '```' + EOL}$2${'```' + EOL + EOL}`);
71+
// Remove indentation in Field lists and Literal blocks
72+
let inCodeBlock = false;
73+
let codeIndentation = 0;
74+
let lines = docstring.split(/\r?\n/);
75+
for (let i = 0; i < lines.length; i++) {
76+
let line = lines[i];
77+
if (line.startsWith('```')) {
78+
inCodeBlock = !inCodeBlock;
79+
if (inCodeBlock) {
80+
codeIndentation = lines[i + 1].match(/^ */)[0].length;
81+
}
82+
continue;
5683
}
57-
return line;
58-
});
59-
60-
return lines.join(EOL).trim().replace(/^\s+|\s+$/g, '').trim();
84+
if (!inCodeBlock) {
85+
lines[i] = line.replace(/^ {4,8}/, '');
86+
} else {
87+
if (codeIndentation != 0) {
88+
lines[i] = line.substring(codeIndentation);
89+
}
90+
}
91+
}
92+
docstring = lines.join(EOL);
93+
// Grid Tables
94+
docstring = docstring.replace(/\r?\n[\+-]+\r?\n/g, EOL);
95+
docstring = docstring.replace(/\r?\n[\+=]+\r?\n/g, s => s.replace(/\+/g, '|').replace(/=/g, '-'));
96+
return docstring;
6197
}

0 commit comments

Comments
Β (0)