From 4acd5f8775c27137d54bf925c19c009c79c16472 Mon Sep 17 00:00:00 2001 From: Andrew Audibert Date: Fri, 20 Mar 2015 14:20:19 -0700 Subject: [PATCH] Add typings for dealing with modes in codemirror --- codemirror/codemirror.d.ts | 87 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/codemirror/codemirror.d.ts b/codemirror/codemirror.d.ts index a187d8ec18..65692cf1c1 100644 --- a/codemirror/codemirror.d.ts +++ b/codemirror/codemirror.d.ts @@ -919,4 +919,91 @@ declare module CodeMirror { */ current(): string; } + + /** + * A Mode is, in the simplest case, a lexer (tokenizer) for your language — a function that takes a character stream as input, + * advances it past a token, and returns a style for that token. More advanced modes can also handle indentation for the language. + */ + interface Mode { + /** + * This function should read one token from the stream it is given as an argument, optionally update its state, + * and return a style string, or null for tokens that do not have to be styled. Multiple styles can be returned, separated by spaces. + */ + token(stream: StringStream, state: T): string; + + /** + * A function that produces a state object to be used at the start of a document. + */ + startState?: () => T; + /** + * For languages that have significant blank lines, you can define a blankLine(state) method on your mode that will get called + * whenever a blank line is passed over, so that it can update the parser state. + */ + blankLine?: (state: T) => void; + /** + * Given a state returns a safe copy of that state. + */ + copyState?: (state: T) => T; + + /** + * The indentation method should inspect the given state object, and optionally the textAfter string, which contains the text on + * the line that is being indented, and return an integer, the amount of spaces to indent. + */ + indent?: (state: T, textAfter: string) => number; + + /** The four below strings are used for working with the commenting addon. */ + /** + * String that starts a line comment. + */ + lineComment?: string; + /** + * String that starts a block comment. + */ + blockCommentStart?: string; + /** + * String that ends a block comment. + */ + blockCommentEnd?: string; + /** + * String to put at the start of continued lines in a block comment. + */ + blockCommentLead?: string; + + /** + * Trigger a reindent whenever one of the characters in the string is typed. + */ + electricChars?: string + /** + * Trigger a reindent whenever the regex matches the part of the line before the cursor. + */ + electricinput?: RegExp + } + + /** + * A function that, given a CodeMirror configuration object and an optional mode configuration object, returns a mode object. + */ + interface ModeFactory { + (config: CodeMirror.EditorConfiguration, modeOptions?: any): Mode + } + + /** + * id will be the id for the defined mode. Typically, you should use this second argument to defineMode as your module scope function + * (modes should not leak anything into the global scope!), i.e. write your whole mode inside this function. + */ + function defineMode(id: string, modefactory: ModeFactory): void; + + /** + * The first argument is a configuration object as passed to the mode constructor function, and the second argument + * is a mode specification as in the EditorConfiguration mode option. + */ + function getMode(config: CodeMirror.EditorConfiguration, mode: any): Mode; + + /** + * Utility function from the overlay.js addon that allows modes to be combined. The mode given as the base argument takes care of + * most of the normal mode functionality, but a second (typically simple) mode is used, which can override the style of text. + * Both modes get to parse all of the text, but when both assign a non-null style to a piece of code, the overlay wins, unless + * the combine argument was true and not overridden, or state.overlay.combineTokens was true, in which case the styles are combined. + */ + function overlayMode(base: Mode, overlay: Mode, combine?: boolean): Mode + }