Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve tooltip display #725

Merged
merged 64 commits into from
Feb 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
7675901
Basic tokenizer
Dec 1, 2017
eb42669
Fixed property names
Dec 1, 2017
2756974
Tests, round I
Dec 1, 2017
c2c1ced
Tests, round II
Dec 2, 2017
a108c96
merge master
Dec 3, 2017
14864a5
tokenizer test
Dec 4, 2017
0ed51d6
Remove temorary change
Dec 4, 2017
51b544c
Fix merge issue
Dec 4, 2017
3cd11e6
Merge conflict
Dec 4, 2017
82e0ad1
Merge conflict
Dec 4, 2017
9295c1a
Completion test
Dec 4, 2017
06eb1a5
Fix last line
Dec 4, 2017
e9db8e0
Fix javascript math
Dec 4, 2017
d12ca03
Merge master
Dec 5, 2017
d8ab041
Make test await for results
Dec 5, 2017
db75cd0
Add license headers
Dec 5, 2017
9ab2c47
Rename definitions to types
Dec 5, 2017
d587485
License headers
Dec 5, 2017
1da5e0a
Merge branch 'master' of https://github.com/Microsoft/vscode-python
Dec 5, 2017
7668cee
Merge branch 'master' of https://github.com/Microsoft/vscode-python
Dec 11, 2017
1ac4932
Fix typo in completion details (typo)
Dec 11, 2017
2aa5a6c
Fix hover test
Dec 12, 2017
5db31bd
Merge branch 'master' of https://github.com/Microsoft/vscode-python
Dec 12, 2017
560d2af
Russian translations
Dec 13, 2017
c71024d
Merge branch 'master' of https://github.com/Microsoft/vscode-python
Dec 13, 2017
31aa087
Update to better translation
Dec 13, 2017
593ae05
Fix typo
Dec 13, 2017
e6d69bb
#70 How to get all parameter info when filling in a function param list
Dec 13, 2017
b5a23d3
Fix #70 How to get all parameter info when filling in a function para…
Dec 14, 2017
cd200f7
Clean up
Dec 14, 2017
7c33228
Clean imports
Dec 14, 2017
c4a6b90
CR feedback
Dec 14, 2017
f85b848
Trim whitespace for test stability
Dec 14, 2017
37c210b
More tests
Dec 15, 2017
61a5650
Better handle no-parameters documentation
Dec 15, 2017
a10305e
Better handle ellipsis and Python3
Dec 15, 2017
bfcae78
Merge branch 'master' of https://github.com/Microsoft/vscode-python
Dec 15, 2017
42a5f79
Merge branch 'master' of https://github.com/Microsoft/vscode-python
Dec 18, 2017
e4ba322
Merge branch 'master' of https://github.com/Microsoft/vscode-python
Jan 8, 2018
7baec1a
Merge branch 'master' of https://github.com/Microsoft/vscode-python
Jan 9, 2018
9cb43e7
#385 Auto-Indentation doesn't work after comment
Jan 9, 2018
5a9c3fd
#141 Auto indentation broken when return keyword involved
Jan 9, 2018
9800c4a
Undo changes
Jan 9, 2018
3205d33
Merge branch 'master' of https://github.com/Microsoft/vscode-python
Jan 11, 2018
c1150d4
Merge branch 'master' of https://github.com/Microsoft/vscode-python
Feb 1, 2018
30519c7
#627 Docstrings for builtin methods are not parsed correctly
Feb 5, 2018
96511cb
reStructuredText converter
Feb 5, 2018
c8670b9
Fix: period is not an operator
Feb 5, 2018
97f232f
Minor fixes
Feb 6, 2018
768bffe
Restructure
Feb 6, 2018
825f16b
Tests
Feb 6, 2018
eb36eef
Tests
Feb 6, 2018
bab4239
Code heuristics
Feb 6, 2018
2a30201
Baselines
Feb 6, 2018
e430ef8
HTML handling
Feb 6, 2018
1afa841
Lists
Feb 7, 2018
6bffb07
State machine
Feb 7, 2018
e436fde
Baselines
Feb 7, 2018
c03e619
Merge branch 'master' of https://github.com/Microsoft/vscode-python
Feb 7, 2018
e52bcff
Squash
Feb 7, 2018
e6b8196
Merge branch 'master' of https://github.com/MikhailArkhipov/vscode-py…
Feb 7, 2018
3a0cfb1
no message
Feb 7, 2018
4616996
Merge branch 'master' of https://github.com/MikhailArkhipov/vscode-py…
Feb 7, 2018
35838b9
Whitespace difference
Feb 7, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
250 changes: 250 additions & 0 deletions src/client/common/markdown/restTextConverter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import { EOL } from 'os';
// tslint:disable-next-line:import-name
import Char from 'typescript-char';
import { isDecimal, isWhiteSpace } from '../../language/characters';

enum State {
Default,
Preformatted,
Code
}

export class RestTextConverter {
private state: State = State.Default;
private md: string[] = [];

// tslint:disable-next-line:cyclomatic-complexity
public toMarkdown(docstring: string): string {
// Translates reStructruredText (Python doc syntax) to markdown.
// It only translates as much as needed to display tooltips
// and documentation in the completion list.
// See https://en.wikipedia.org/wiki/ReStructuredText

const result = this.transformLines(docstring);
this.state = State.Default;
this.md = [];

return result;
}

public escapeMarkdown(text: string): string {
// Not complete escape list so it does not interfere
// with subsequent code highlighting (see above).
return text
.replace(/\#/g, '\\#')
.replace(/\*/g, '\\*')
.replace(/\_/g, '\\_');
}

private transformLines(docstring: string): string {
const lines = docstring.split(/\r?\n/);
for (let i = 0; i < lines.length; i += 1) {
const line = lines[i];
// Avoid leading empty lines
if (this.md.length === 0 && line.length === 0) {
continue;
}

switch (this.state) {
case State.Default:
i += this.inDefaultState(lines, i);
break;
case State.Preformatted:
i += this.inPreformattedState(lines, i);
break;
case State.Code:
this.inCodeState(line);
break;
default:
break;
}
}

this.endCodeBlock();
this.endPreformattedBlock();

return this.md.join(EOL).trim();
}

private inDefaultState(lines: string[], i: number): number {
let line = lines[i];
if (line.startsWith('```')) {
this.startCodeBlock();
return 0;
}

if (line.startsWith('===') || line.startsWith('---')) {
return 0; // Eat standalone === or --- lines.
}
if (this.handleDoubleColon(line)) {
return 0;
}
if (this.isIgnorable(line)) {
return 0;
}

if (this.handleSectionHeader(lines, i)) {
return 1; // Eat line with === or ---
}

const result = this.checkPreContent(lines, i);
if (this.state !== State.Default) {
return result; // Handle line in the new state
}

line = this.cleanup(line);
line = line.replace(/``/g, '`'); // Convert double backticks to single.
line = this.escapeMarkdown(line);
this.md.push(line);

return 0;
}

private inPreformattedState(lines: string[], i: number): number {
let line = lines[i];
if (this.isIgnorable(line)) {
return 0;
}
// Preformatted block terminates by a line without leading whitespace.
if (line.length > 0 && !isWhiteSpace(line.charCodeAt(0)) && !this.isListItem(line)) {
this.endPreformattedBlock();
return -1;
}

const prevLine = this.md.length > 0 ? this.md[this.md.length - 1] : undefined;
if (line.length === 0 && prevLine && (prevLine.length === 0 || prevLine.startsWith('```'))) {
return 0; // Avoid more than one empty line in a row.
}

// Since we use HTML blocks as preformatted text
// make sure we drop angle brackets since otherwise
// they will render as tags and attributes
line = line.replace(/</g, ' ').replace(/>/g, ' ');
line = line.replace(/``/g, '`'); // Convert double backticks to single.
// Keep hard line breaks for the preformatted content
this.md.push(`${line} `);
return 0;
}

private inCodeState(line: string): void {
const prevLine = this.md.length > 0 ? this.md[this.md.length - 1] : undefined;
if (line.length === 0 && prevLine && (prevLine.length === 0 || prevLine.startsWith('```'))) {
return; // Avoid more than one empty line in a row.
}

if (line.startsWith('```')) {
this.endCodeBlock();
} else {
this.md.push(line);
}
}

private isIgnorable(line: string): boolean {
if (line.indexOf('generated/') >= 0) {
return true; // Drop generated content.
}
const trimmed = line.trim();
if (trimmed.startsWith('..') && trimmed.indexOf('::') > 0) {
// Ignore lines likes .. sectionauthor:: John Doe.
return true;
}
return false;
}

private checkPreContent(lines: string[], i: number): number {
const line = lines[i];
if (i === 0 || line.trim().length === 0) {
return 0;
}

if (!isWhiteSpace(line.charCodeAt(0)) && !this.isListItem(line)) {
return 0; // regular line, nothing to do here.
}
// Indented content is considered to be preformatted.
this.startPreformattedBlock();
return -1;
}

private handleSectionHeader(lines: string[], i: number): boolean {
const line = lines[i];
if (i < lines.length - 1 && (lines[i + 1].startsWith('==='))) {
// Section title -> heading level 3.
this.md.push(`### ${this.cleanup(line)}`);
return true;
}
if (i < lines.length - 1 && (lines[i + 1].startsWith('---'))) {
// Subsection title -> heading level 4.
this.md.push(`#### ${this.cleanup(line)}`);
return true;
}
return false;
}

private handleDoubleColon(line: string): boolean {
if (!line.endsWith('::')) {
return false;
}
// Literal blocks begin with `::`. Such as sequence like
// '... as shown below::' that is followed by a preformatted text.
if (line.length > 2 && !line.startsWith('..')) {
// Ignore lines likes .. autosummary:: John Doe.
// Trim trailing : so :: turns into :.
this.md.push(line.substring(0, line.length - 1));
}

this.startPreformattedBlock();
return true;
}

private startPreformattedBlock(): void {
// Remove previous empty line so we avoid double empties.
this.tryRemovePrecedingEmptyLines();
// Lie about the language since we don't want preformatted text
// to be colorized as Python. HTML is more 'appropriate' as it does
// not colorize -- or + or keywords like 'from'.
this.md.push('```html');
this.state = State.Preformatted;
}

private endPreformattedBlock(): void {
if (this.state === State.Preformatted) {
this.tryRemovePrecedingEmptyLines();
this.md.push('```');
this.state = State.Default;
}
}

private startCodeBlock(): void {
// Remove previous empty line so we avoid double empties.
this.tryRemovePrecedingEmptyLines();
this.md.push('```python');
this.state = State.Code;
}

private endCodeBlock(): void {
if (this.state === State.Code) {
this.tryRemovePrecedingEmptyLines();
this.md.push('```');
this.state = State.Default;
}
}

private tryRemovePrecedingEmptyLines(): void {
while (this.md.length > 0 && this.md[this.md.length - 1].trim().length === 0) {
this.md.pop();
}
}

private isListItem(line: string): boolean {
const trimmed = line.trim();
const ch = trimmed.length > 0 ? trimmed.charCodeAt(0) : 0;
return ch === Char.Asterisk || ch === Char.Hyphen || isDecimal(ch);
}

private cleanup(line: string): string {
return line.replace(/:mod:/g, 'module:');
}
}
2 changes: 1 addition & 1 deletion src/client/language/tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ export class Tokenizer implements ITokenizer {
break;

default:
break;
return false;
}
this.tokens.push(new Token(TokenType.Operator, this.cs.position, length));
this.cs.advance(length);
Expand Down
Loading