forked from fannheyward/coc-pyright
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
150 lines (136 loc) · 5.13 KB
/
middleware.ts
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
import {
CancellationToken,
CompletionContext,
CompletionItem,
CompletionItemKind,
ConfigurationParams,
Diagnostic,
HandleDiagnosticsSignature,
InsertTextFormat,
LinesTextDocument,
Position,
ProvideCompletionItemsSignature,
ProvideHoverSignature,
ProvideSignatureHelpSignature,
ResolveCompletionItemSignature,
SignatureHelpContext,
workspace,
} from 'coc.nvim';
import { PythonSettings } from './configSettings';
function toJSONObject(obj: any): any {
if (obj) {
if (Array.isArray(obj)) {
return obj.map(toJSONObject);
} else if (typeof obj === 'object') {
const res = Object.create(null);
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
res[key] = toJSONObject(obj[key]);
}
}
return res;
}
}
return obj;
}
export function configuration(params: ConfigurationParams, token: CancellationToken, next: any) {
const pythonItem = params.items.find((x) => x.section === 'python');
if (pythonItem) {
const custom = () => {
const config = toJSONObject(workspace.getConfiguration(pythonItem.section, pythonItem.scopeUri));
config['pythonPath'] = PythonSettings.getInstance().pythonPath;
// expand relative path
const analysis = config['analysis'];
analysis['stubPath'] = workspace.expand(analysis['stubPath'] as string);
const extraPaths = analysis['extraPaths'] as string[];
if (extraPaths?.length) {
analysis['extraPaths'] = extraPaths.map((p) => workspace.expand(p));
}
const typeshedPaths = analysis['typeshedPaths'] as string[];
if (typeshedPaths?.length) {
analysis['typeshedPaths'] = typeshedPaths.map((p) => workspace.expand(p));
}
config['analysis'] = analysis;
return [config];
};
return custom();
}
const analysisItem = params.items.find((x) => x.section === 'python.analysis');
if (analysisItem) {
const custom = () => {
const analysis = toJSONObject(workspace.getConfiguration(analysisItem.section, analysisItem.scopeUri));
analysis['stubPath'] = workspace.expand(analysis['stubPath'] as string);
const extraPaths = analysis['extraPaths'] as string[];
if (extraPaths?.length) {
analysis['extraPaths'] = extraPaths.map((p) => workspace.expand(p));
}
const typeshedPaths = analysis['typeshedPaths'] as string[];
if (typeshedPaths?.length) {
analysis['typeshedPaths'] = typeshedPaths.map((p) => workspace.expand(p));
}
return [analysis];
};
return custom();
}
return next(params, token);
}
export async function provideCompletionItem(
document: LinesTextDocument,
position: Position,
context: CompletionContext,
token: CancellationToken,
next: ProvideCompletionItemsSignature
) {
const result = await next(document, position, context, token);
if (!result) return;
const items = Array.isArray(result) ? result : result.items;
items.map((x) => (x.sortText ? (x.sortText = x.sortText.toLowerCase()) : (x.sortText = x.label.toLowerCase())));
const snippetSupport = workspace.getConfiguration('pyright').get<boolean>('completion.snippetSupport');
if (snippetSupport) {
for (const item of items) {
if (item.data?.funcParensDisabled) continue;
if (item.kind === CompletionItemKind.Method || item.kind === CompletionItemKind.Function) {
item.insertText = `${item.label}($1)$0`;
item.insertTextFormat = InsertTextFormat.Snippet;
}
}
}
return Array.isArray(result) ? items : { items, isIncomplete: result.isIncomplete };
}
export async function resolveCompletionItem(item: CompletionItem, token: CancellationToken, next: ResolveCompletionItemSignature) {
const result = await next(item, token);
if (result && typeof result.documentation === 'object' && 'kind' in result.documentation && result.documentation.kind === 'markdown') {
result.documentation.value = result.documentation.value.replace(/ /g, ' ');
}
return result;
}
export async function provideHover(document: LinesTextDocument, position: Position, token: CancellationToken, next: ProvideHoverSignature) {
const hover = await next(document, position, token);
if (hover && typeof hover.contents === 'object' && 'kind' in hover.contents && hover.contents.kind === 'markdown') {
hover.contents.value = hover.contents.value.replace(/ /g, ' ');
}
return hover;
}
export async function provideSignatureHelp(
document: LinesTextDocument,
position: Position,
context: SignatureHelpContext,
token: CancellationToken,
next: ProvideSignatureHelpSignature
) {
const sign = await next(document, position, context, token);
if (sign && sign.signatures.length) {
sign.signatures.forEach((info) => {
if (info.documentation && typeof info.documentation === 'object' && info.documentation.kind === 'markdown') {
info.documentation.value = info.documentation.value.replace(/ /g, ' ');
}
});
}
return sign;
}
export async function handleDiagnostics(uri: string, diagnostics: Diagnostic[], next: HandleDiagnosticsSignature) {
next(
uri,
diagnostics.filter((d) => d.message !== '"__" is not accessed')
);
}