Skip to content

Commit c340b36

Browse files
author
Anthony Guo
committed
Added typings for the CodeMirror showhint addon
1 parent ad21a10 commit c340b36

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

codemirror/showhint-tests.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/// <reference path="codemirror.d.ts" />
2+
/// <reference path="showhint.d.ts" />
3+
var doc = new CodeMirror.Doc('text');
4+
var pos = new CodeMirror.Pos(2, 3);
5+
CodeMirror.showHint(doc);
6+
CodeMirror.showHint(doc, function (cm) {
7+
return {
8+
from: pos,
9+
list: ["one", "two"],
10+
to: pos
11+
};
12+
});
13+
CodeMirror.showHint(doc, function (cm) {
14+
return {
15+
from: pos,
16+
list: [
17+
{
18+
text: "disp1",
19+
render: function (el, self, data) {
20+
;
21+
}
22+
},
23+
{
24+
className: "class2",
25+
displayText: "disp2",
26+
from: pos,
27+
to: pos,
28+
text: "sometext"
29+
}
30+
],
31+
to: pos
32+
};
33+
});

codemirror/showhint.d.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Type definitions for CodeMirror
2+
// Project: https://github.com/marijnh/CodeMirror
3+
// Definitions by: jacqt <https://github.com/jacqt>
4+
// Definitions: https://github.com/borisyankov/DefinitelyTyped
5+
6+
declare module CodeMirror {
7+
var commands : any;
8+
9+
/** Provides a framework for showing autocompletion hints. Defines editor.showHint, which takes an optional
10+
options object, and pops up a widget that allows the user to select a completion. Finding hints is done with
11+
a hinting functions (the hint option), which is a function that take an editor instance and options object,
12+
and return a {list, from, to} object, where list is an array of strings or objects (the completions), and
13+
from and to give the start and end of the token that is being completed as {line, ch} objects. An optional
14+
selectedHint property (an integer) can be added to the completion object to control the initially selected hint. */
15+
function showHint (cm: CodeMirror.Doc, hinter?: (doc : CodeMirror.Doc) => Hints, options?: IShowHintOptions) : void;
16+
17+
18+
interface Hints {
19+
from: Position;
20+
to: Position;
21+
list: Hint[] | string[];
22+
}
23+
24+
/** Interface used by showHint.js Codemirror add-on
25+
When completions aren't simple strings, they should be objects with the following properties: */
26+
interface Hint {
27+
text: string;
28+
className?: string;
29+
displayText?: string;
30+
from?: Position;
31+
render?: (element: any, self: any, data: any) => void;
32+
to?: Position;
33+
}
34+
35+
interface Editor {
36+
/** An extension of the existing CodeMirror typings for the Editor.on("keyup", func) syntax */
37+
on(eventName: string, handler: (doc: CodeMirror.Doc, event : any ) => void ): void;
38+
off(eventName: string, handler: (doc: CodeMirror.Doc, event : any) => void ): void;
39+
}
40+
41+
/** Extend CodeMirror.Doc with a state object, so that the Doc.state.completionActive property is reachable*/
42+
interface Doc {
43+
state: any;
44+
showHint: (options: IShowHintOptions) => void;
45+
}
46+
47+
interface IShowHintOptions {
48+
completeSingle: boolean;
49+
hint: (doc : CodeMirror.Doc) => Hints;
50+
}
51+
52+
/** The Handle used to interact with the autocomplete dialog box.*/
53+
interface Handle {
54+
moveFocus(n: number, avoidWrap: boolean): void;
55+
setFocus(n: number): void;
56+
menuSize(): number;
57+
length: number;
58+
close(): void;
59+
pick(): void;
60+
data: any;
61+
}
62+
}

0 commit comments

Comments
 (0)