forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitemInfoSource.ts
More file actions
169 lines (151 loc) · 7.1 KB
/
itemInfoSource.ts
File metadata and controls
169 lines (151 loc) · 7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { EOL } from 'os';
import * as vscode from 'vscode';
import { RestTextConverter } from '../common/markdown/restTextConverter';
import { JediFactory } from '../languageServices/jediProxyFactory';
import * as proxy from './jediProxy';
export class LanguageItemInfo {
constructor(
public tooltip: vscode.MarkdownString,
public detail: string,
public signature: vscode.MarkdownString) { }
}
export interface IItemInfoSource {
getItemInfoFromText(documentUri: vscode.Uri, fileName: string,
range: vscode.Range, sourceText: string,
token: vscode.CancellationToken): Promise<LanguageItemInfo[] | undefined>;
getItemInfoFromDocument(document: vscode.TextDocument, position: vscode.Position,
token: vscode.CancellationToken): Promise<LanguageItemInfo[] | undefined>;
}
export class ItemInfoSource implements IItemInfoSource {
private textConverter = new RestTextConverter();
constructor(private jediFactory: JediFactory) { }
public async getItemInfoFromText(documentUri: vscode.Uri, fileName: string, range: vscode.Range, sourceText: string, token: vscode.CancellationToken)
: Promise<LanguageItemInfo[] | undefined> {
const result = await this.getHoverResultFromTextRange(documentUri, fileName, range, sourceText, token);
if (!result || !result.items.length) {
return;
}
return this.getItemInfoFromHoverResult(result, '');
}
public async getItemInfoFromDocument(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken)
: Promise<LanguageItemInfo[] | undefined> {
const range = document.getWordRangeAtPosition(position);
if (!range || range.isEmpty) {
return;
}
const result = await this.getHoverResultFromDocument(document, position, token);
if (!result || !result.items.length) {
return;
}
const word = document.getText(range);
return this.getItemInfoFromHoverResult(result, word);
}
private async getHoverResultFromDocument(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken)
: Promise<proxy.IHoverResult | undefined> {
if (position.character <= 0 || document.lineAt(position.line).text.match(/^\s*\/\//)) {
return;
}
const range = document.getWordRangeAtPosition(position);
if (!range || range.isEmpty) {
return;
}
return this.getHoverResultFromDocumentRange(document, range, token);
}
private async getHoverResultFromDocumentRange(document: vscode.TextDocument, range: vscode.Range, token: vscode.CancellationToken)
: Promise<proxy.IHoverResult | undefined> {
const cmd: proxy.ICommand = {
command: proxy.CommandType.Hover,
fileName: document.fileName,
columnIndex: range.end.character,
lineIndex: range.end.line
};
if (document.isDirty) {
cmd.source = document.getText();
}
return this.jediFactory.getJediProxyHandler<proxy.IHoverResult>(document.uri).sendCommand(cmd, token);
}
private async getHoverResultFromTextRange(documentUri: vscode.Uri, fileName: string, range: vscode.Range, sourceText: string, token: vscode.CancellationToken)
: Promise<proxy.IHoverResult | undefined> {
const cmd: proxy.ICommand = {
command: proxy.CommandType.Hover,
fileName: fileName,
columnIndex: range.end.character,
lineIndex: range.end.line,
source: sourceText
};
return this.jediFactory.getJediProxyHandler<proxy.IHoverResult>(documentUri).sendCommand(cmd, token);
}
private getItemInfoFromHoverResult(data: proxy.IHoverResult, currentWord: string): LanguageItemInfo[] {
const infos: LanguageItemInfo[] = [];
data.items.forEach(item => {
const signature = this.getSignature(item, currentWord);
let tooltip = new vscode.MarkdownString();
if (item.docstring) {
let lines = item.docstring.split(/\r?\n/);
// If the docstring starts with the signature, then remove those lines from the docstring.
if (lines.length > 0 && item.signature.indexOf(lines[0]) === 0) {
lines.shift();
const endIndex = lines.findIndex(line => item.signature.endsWith(line));
if (endIndex >= 0) {
lines = lines.filter((_line, index) => index > endIndex);
}
}
if (lines.length > 0 && currentWord.length > 0 && item.signature.startsWith(currentWord) && lines[0].startsWith(currentWord) && lines[0].endsWith(')')) {
lines.shift();
}
if (signature.length > 0) {
tooltip = tooltip.appendMarkdown(['```python', signature, '```', ''].join(EOL));
}
const description = this.textConverter.toMarkdown(lines.join(EOL));
tooltip = tooltip.appendMarkdown(description);
infos.push(new LanguageItemInfo(tooltip, item.description, new vscode.MarkdownString(signature)));
return;
}
if (item.description) {
if (signature.length > 0) {
tooltip.appendMarkdown(['```python', signature, '```', ''].join(EOL));
}
const description = this.textConverter.toMarkdown(item.description);
tooltip.appendMarkdown(description);
infos.push(new LanguageItemInfo(tooltip, item.description, new vscode.MarkdownString(signature)));
return;
}
if (item.text) { // Most probably variable type
const code = currentWord && currentWord.length > 0
? `${currentWord}: ${item.text}`
: item.text;
tooltip.appendMarkdown(['```python', code, '```', ''].join(EOL));
infos.push(new LanguageItemInfo(tooltip, '', new vscode.MarkdownString()));
}
});
return infos;
}
private getSignature(item: proxy.IHoverItem, currentWord: string): string {
let { signature } = item;
switch (item.kind) {
case vscode.SymbolKind.Constructor:
case vscode.SymbolKind.Function:
case vscode.SymbolKind.Method: {
signature = `def ${signature}`;
break;
}
case vscode.SymbolKind.Class: {
signature = `class ${signature}`;
break;
}
case vscode.SymbolKind.Module: {
if (signature.length > 0) {
signature = `module ${signature}`;
}
break;
}
default: {
signature = typeof item.text === 'string' && item.text.length > 0 ? item.text : currentWord;
}
}
return signature;
}
}