UNPKG

4.67 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>(
17 hints: Hints,
18 eventName: T,
19 ...args: Parameters<CompletionEventMap[T]>
20 ): void;
21
22 interface CompletionEventMap {
23 shown: () => void;
24 select: (completion: Hint | string, element: Element) => void;
25 pick: (completion: Hint | string) => void;
26 close: () => void;
27 }
28
29 interface Hints {
30 from: Position;
31 to: Position;
32 list: Array<Hint | string>;
33 }
34
35 /**
36 * Interface used by showHint.js Codemirror add-on
37 * When completions aren't simple strings, they should be objects with the following properties:
38 */
39 interface Hint {
40 text: string;
41 className?: string | undefined;
42 displayText?: string | undefined;
43 from?: Position | undefined;
44 /** Called if a completion is picked. If provided *you* are responsible for applying the completion */
45 hint?: ((cm: Editor, data: Hints, cur: Hint) => void) | undefined;
46 render?: ((element: HTMLLIElement, data: Hints, cur: Hint) => void) | undefined;
47 to?: Position | undefined;
48 }
49
50 interface EditorEventMap {
51 startCompletion: (instance: Editor) => void;
52 endCompletion: (instance: Editor) => void;
53 }
54
55 interface Editor {
56 showHint(options?: ShowHintOptions): void;
57 closeHint(): void;
58 }
59
60 interface CommandActions {
61 /* An extension of the existing CodeMirror typings for the autocomplete command */
62 autocomplete: typeof showHint;
63 }
64
65 interface HintFunction {
66 (cm: Editor, options: ShowHintOptions): Hints | null | undefined | PromiseLike<Hints | null | undefined>;
67 }
68
69 interface AsyncHintFunction {
70 (cm: Editor, callback: (hints: Hints | null | undefined) => void, options: ShowHintOptions): void;
71 async: true;
72 }
73
74 interface HintFunctionResolver {
75 resolve(cm: Editor, post: Position): HintFunction | AsyncHintFunction;
76 }
77
78 interface ShowHintOptions {
79 completeSingle?: boolean | undefined;
80 hint?: HintFunction | AsyncHintFunction | HintFunctionResolver | undefined;
81 alignWithWord?: boolean | undefined;
82 closeCharacters?: RegExp | undefined;
83 closeOnPick?: boolean | undefined;
84 closeOnUnfocus?: boolean | undefined;
85 updateOnCursorActivity?: boolean | undefined;
86 completeOnSingleClick?: boolean | undefined;
87 container?: HTMLElement | null | undefined;
88 customKeys?:
89 | { [key: string]: ((editor: Editor, handle: CompletionHandle) => void) | string }
90 | null
91 | undefined;
92 extraKeys?: { [key: string]: ((editor: Editor, handle: CompletionHandle) => void) | string } | null | undefined;
93 scrollMargin?: number | undefined;
94 paddingForScrollbar?: boolean | undefined;
95 moveOnOverlap?: boolean | undefined;
96 words?: readonly string[] | undefined; // used by fromList
97 }
98
99 /** The Handle used to interact with the autocomplete dialog box. */
100 interface CompletionHandle {
101 moveFocus(n: number, avoidWrap: boolean): void;
102 setFocus(n: number): void;
103 menuSize(): number;
104 length: number;
105 close(): void;
106 pick(): void;
107 data: any;
108 }
109
110 interface EditorConfiguration {
111 showHint?: boolean | undefined;
112 hintOptions?: ShowHintOptions | undefined;
113 }
114
115 interface HintHelpers {
116 auto: HintFunctionResolver;
117 fromList: HintFunction;
118 }
119
120 const hint: HintHelpers;
121}
122
\No newline at end of file