UNPKG

4.61 kBTypeScriptView Raw
1import '../../';
2
3declare module '../../' {
4 /**
5 * Provides a framework for showing autocompletion hints. Defines editor.showHint, which takes an optional
6 * options object, and pops up a widget that allows the user to select a completion. Finding hints is done with
7 * a hinting functions (the hint option), which is a function that take an editor instance and options object,
8 * and return a {list, from, to} object, where list is an array of strings or objects (the completions), and
9 * from and to give the start and end of the token that is being completed as {line, ch} objects. An optional
10 * selectedHint property (an integer) can be added to the completion object to control the initially selected hint.
11 */
12 function showHint(cm: Editor, hinter?: HintFunction, options?: ShowHintOptions): void;
13
14 function on<T extends keyof CompletionEventMap>(hints: Hints, eventName: T, handler: CompletionEventMap[T]): void;
15 function off<T extends keyof CompletionEventMap>(hints: Hints, eventName: T, handler: CompletionEventMap[T]): void;
16 function signal<T extends keyof CompletionEventMap>(hints: Hints, eventName: T, ...args: Parameters<CompletionEventMap[T]>): void;
17
18 interface CompletionEventMap {
19 shown: () => void;
20 select: (completion: Hint | string, element: Element) => void;
21 pick: (completion: Hint | string) => void;
22 close: () => void;
23 }
24
25 interface Hints {
26 from: Position;
27 to: Position;
28 list: Array<Hint | string>;
29 }
30
31 /**
32 * Interface used by showHint.js Codemirror add-on
33 * When completions aren't simple strings, they should be objects with the following properties:
34 */
35 interface Hint {
36 text: string;
37 className?: string | undefined;
38 displayText?: string | undefined;
39 from?: Position | undefined;
40 /** Called if a completion is picked. If provided *you* are responsible for applying the completion */
41 hint?: ((cm: Editor, data: Hints, cur: Hint) => void) | undefined;
42 render?: ((element: HTMLLIElement, data: Hints, cur: Hint) => void) | undefined;
43 to?: Position | undefined;
44 }
45
46 interface EditorEventMap {
47 startCompletion: (instance: Editor) => void;
48 endCompletion: (instance: Editor) => void;
49 }
50
51 interface Editor {
52 showHint(options?: ShowHintOptions): void;
53 closeHint(): void;
54 }
55
56 interface CommandActions {
57 /* An extension of the existing CodeMirror typings for the autocomplete command */
58 autocomplete: typeof showHint;
59 }
60
61 interface HintFunction {
62 (cm: Editor, options: ShowHintOptions): Hints | null | undefined | PromiseLike<Hints | null | undefined>;
63 }
64
65 interface AsyncHintFunction {
66 (cm: Editor, callback: (hints: Hints | null | undefined) => void, options: ShowHintOptions): void;
67 async: true;
68 }
69
70 interface HintFunctionResolver {
71 resolve(cm: Editor, post: Position): HintFunction | AsyncHintFunction;
72 }
73
74 interface ShowHintOptions {
75 completeSingle?: boolean | undefined;
76 hint?: HintFunction | AsyncHintFunction | HintFunctionResolver | undefined;
77 alignWithWord?: boolean | undefined;
78 closeCharacters?: RegExp | undefined;
79 closeOnPick?: boolean | undefined;
80 closeOnUnfocus?: boolean | undefined;
81 updateOnCursorActivity?: boolean | undefined;
82 completeOnSingleClick?: boolean | undefined;
83 container?: HTMLElement | null | undefined;
84 customKeys?: { [key: string]: ((editor: Editor, handle: CompletionHandle) => void) | string } | null | undefined;
85 extraKeys?: { [key: string]: ((editor: Editor, handle: CompletionHandle) => void) | string } | null | undefined;
86 scrollMargin?: number | undefined;
87 paddingForScrollbar?: boolean | undefined;
88 moveOnOverlap?: boolean | undefined;
89 words?: ReadonlyArray<string> | undefined; // used by fromList
90 }
91
92 /** The Handle used to interact with the autocomplete dialog box. */
93 interface CompletionHandle {
94 moveFocus(n: number, avoidWrap: boolean): void;
95 setFocus(n: number): void;
96 menuSize(): number;
97 length: number;
98 close(): void;
99 pick(): void;
100 data: any;
101 }
102
103 interface EditorConfiguration {
104 showHint?: boolean | undefined;
105 hintOptions?: ShowHintOptions | undefined;
106 }
107
108 interface HintHelpers {
109 auto: HintFunctionResolver;
110 fromList: HintFunction;
111 }
112
113 const hint: HintHelpers;
114}
115
\No newline at end of file