forked from jupyter-lsp/jupyterlab-lsp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokens.ts
189 lines (161 loc) · 5.3 KB
/
tokens.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import { ISessionContext } from '@jupyterlab/apputils';
import { CodeEditor } from '@jupyterlab/codeeditor';
import { Completer, CompletionHandler } from '@jupyterlab/completer';
import { IDocumentWidget } from '@jupyterlab/docregistry';
import { LabIcon } from '@jupyterlab/ui-components';
import { Token } from '@lumino/coreutils';
import { GenericCompleterModel } from './model';
export const PLUGIN_ID = '@krassowski/completion-manager';
/**
* Source of the completions (e.g. kernel, lsp, kite, snippets-extension, etc.)
*/
export interface ICompletionsSource {
/**
* The name displayed in the GUI
*/
name: string;
/**
* The higher the number the higher the priority
*/
priority: number;
/**
* The icon to be displayed if no type icon is present
*/
fallbackIcon?: LabIcon;
}
export interface ICompletionProviderSettings {
timeout: number;
enabled: boolean;
}
/**
* Source-aware and improved completion item
*/
export interface IExtendedCompletionItem
extends CompletionHandler.ICompletionItem {
insertText: string;
sortText: string;
source?: ICompletionsSource;
/**
* Provider will be added automatically.
*/
provider?: ICompletionProvider;
/**
* Adding self-reference ensures that the original completion object can be accessed from the renderer.
* It is recommended for providers to set self if objects are storing additional dynamic state,
* e.g. by downloading documentation text asynchronously.
*/
self?: IExtendedCompletionItem;
}
/**
* Completion items reply from a specific source
*/
export interface ICompletionsReply<
T extends IExtendedCompletionItem = IExtendedCompletionItem
> extends CompletionHandler.ICompletionItemsReply {
// TODO: it is not clear when the source is set here and when on IExtendedCompletionItem.
// it might be good to separate the two stages for both interfaces
/**
* Source of the completions. A provider can be the source of completions,
* but in general a provider can provide completions from multiple sources,
* for example:
* - LSP can provide completions from multiple language servers for the same document.
* - A machine-learning-based completion provider may provide completions based on algorithm A and algorithm B
*/
source: ICompletionsSource;
items: T[];
}
export interface ICompletionContext {
editor: CodeEditor.IEditor;
widget: IDocumentWidget;
// extracted from notebook widget as convenience:
sessionContext?: ISessionContext;
}
export interface IIconSource {
iconFor(completionType: string): LabIcon;
}
export interface ICompletionRequest extends CompletionHandler.IRequest {
triggerKind: CompletionTriggerKind;
}
export interface ICompleterRenderer<T extends IExtendedCompletionItem>
extends Completer.IRenderer {
createCompletionItemNode(item: T, orderedTypes: string[]): HTMLLIElement;
createDocumentationNode(item: T): HTMLElement;
}
export interface ICompletionProvider<
T extends IExtendedCompletionItem = IExtendedCompletionItem
> {
/**
* Unique identifier of the provider
*/
identifier: string;
/**
* Is completion provider applicable to specified context?
* @param request - useful because we may want to disable specific sources in some parts of document (like sql code cell in a Python notebook)
* @param context
*/
isApplicable(
request: ICompletionRequest,
context: ICompletionContext
): Promise<boolean>;
/**
* Renderer for provider's completions (optional).
*/
renderer?: ICompleterRenderer<T>;
/**
* Fetch completion requests.
*
* @param request - the completion request text and details
* @param context - additional information about context of completion request
*/
fetch(
request: ICompletionRequest,
context: ICompletionContext
): Promise<ICompletionsReply<T>>;
// TODO: not functional yet
/**
* Given an incomplete (unresolved) completion item, resolve it by adding all missing details,
* such as lazy-fetched documentation.
*
* @param completion - the completion item to resolve
*/
resolve?(completion: T): Promise<T>;
}
enum LSPCompletionTriggerKind {
Invoked = 1,
TriggerCharacter = 2,
TriggerForIncompleteCompletions = 3
}
enum AdditionalCompletionTriggerKinds {
AutoInvoked = 9999
}
export const CompletionTriggerKind = {
...LSPCompletionTriggerKind,
...AdditionalCompletionTriggerKinds
};
export type CompletionTriggerKind =
| LSPCompletionTriggerKind
| AdditionalCompletionTriggerKinds;
export interface ICompletionProviderManager {
registerProvider(provider: ICompletionProvider): void;
getProvider(identifier: string): ICompletionProvider;
overrideProvider(provider: ICompletionProvider): void;
setIconSource(iconSource: IIconSource): void;
invoke(trigger: CompletionTriggerKind): Promise<any>;
// TODO?
// unregister(provider: ICompletionProvider): void;
connect(
context: ICompletionContext,
model: GenericCompleterModel<CompletionHandler.ICompletionItem>
): void;
configure(settings: ICompletionSettings): void;
}
export const ICompletionProviderManager = new Token<ICompletionProviderManager>(
PLUGIN_ID + ':ICompletionProviderManager'
);
export interface ICompletionSettings {
providers: {
[identifier: string]: ICompletionProviderSettings;
};
suppressContinuousHintingIn: string[];
suppressTriggerCharacterIn: string[];
}