From 5a55b05ad98267b8b898c6e5c3436d3123704c5e Mon Sep 17 00:00:00 2001 From: Vadim Macagon Date: Sun, 5 Jul 2015 13:27:50 +0700 Subject: [PATCH] Add typings for Atom's first-mate library --- first-mate/.editorconfig | 3 + first-mate/first-mate-tests.ts | 15 +++++ first-mate/first-mate.d.ts | 108 +++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 first-mate/.editorconfig create mode 100644 first-mate/first-mate-tests.ts create mode 100644 first-mate/first-mate.d.ts diff --git a/first-mate/.editorconfig b/first-mate/.editorconfig new file mode 100644 index 000000000..570211f89 --- /dev/null +++ b/first-mate/.editorconfig @@ -0,0 +1,3 @@ +[*.ts] +indent_style = tab +indent_size = 4 diff --git a/first-mate/first-mate-tests.ts b/first-mate/first-mate-tests.ts new file mode 100644 index 000000000..1a181de4a --- /dev/null +++ b/first-mate/first-mate-tests.ts @@ -0,0 +1,15 @@ +/// + +import firstMate = require('first-mate'); +var GrammarRegistry = firstMate.GrammarRegistry; +var Grammar = firstMate.GrammarRegistry; +type IToken = AtomFirstMate.IToken; +// The import and type aliasing above can be done more concisely in TypeScript 1.5+: +//import { GrammarRegistry, Grammar, IToken } from "first-mate"; + +var registry = new GrammarRegistry({ maxTokensPerLine: 100 }); +var grammar = registry.loadGrammarSync('javascript.json'); +var result = grammar.tokenizeLine('var text = "hello world";'); +result.tokens.forEach((token) => { + console.log("Token text: '" + token.value + "' with scopes: " + token.scopes); +}); diff --git a/first-mate/first-mate.d.ts b/first-mate/first-mate.d.ts new file mode 100644 index 000000000..d780116c2 --- /dev/null +++ b/first-mate/first-mate.d.ts @@ -0,0 +1,108 @@ +// Type definitions for first-mate v4.1.7 +// Project: https://github.com/atom/first-mate/ +// Definitions by: Vadim Macagon +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +declare module AtomFirstMate { + type Disposable = AtomEventKit.Disposable; + + interface IToken { + value: string; + scopes: string[]; + } + + /** Result returned by `Grammar.tokenizeLine`. */ + interface TokenizeLineResult { + /** Text that was tokenized. */ + line: string; + tags: any[]; + /** + * This is a dynamic property that will only be available if `Grammar.tokenizeLine` was called + * with `compatibilityMode` set to `true` (the default). + */ + tokens?: IToken[]; + /** + * The tokenized state at the end of the line. This should be passed back into `tokenizeLine` + * when tokenizing the next line in the file/buffer. + */ + ruleStack: Rule[] + } + + /** Instance side of Rule class. */ + interface Rule { + } + + /** Static side of Grammar class. */ + interface GrammarStatic { + prototype: Grammar; + new (registry: GrammarRegistry, options?: any): Grammar; + } + + /** Instance side of Grammar class. */ + interface Grammar { + constructor: GrammarStatic; + onDidUpdate(callback: Function): Disposable; + /** + * Tokenizes all lines in a string. + * + * @param text A string containing one or more lines. + * @return An array of token arrays, one token array per line. + */ + tokenizeLines(text: string): Array>; + /** + * Tokenizes a line of text. + * + * @param line Text to be tokenized. + * @param firstLine Indicates whether `line` is the first line in the file/buffer, + * defaults to `false`. + * @param compatibilityMode `true` by default. + * @return An object containing tokens for the given line. + */ + tokenizeLine( + line: string, ruleStack?: Rule[], firstLine?: boolean, compatibilityMode?: boolean + ): TokenizeLineResult; + } + + /** Grammar that tokenizes lines of text. */ + var Grammar: GrammarStatic; + + /** Static side of GrammarRegistry class. */ + interface GrammarRegistryStatic { + prototype: GrammarRegistry; + new (options?: { maxTokensPerLine: number }): GrammarRegistry; + } + + /** Instance side of GrammarRegistry class. */ + interface GrammarRegistry { + constructor: GrammarRegistryStatic; + + // Event Subscription + + onDidAddGrammar(callback: (grammar: Grammar) => void): Disposable; + onDidUpdateGrammar(callback: (grammar: Grammar) => void): Disposable; + + // Managing Grammars + + getGrammars(): Grammar[]; + grammarForScopeName(scopeName: string): Grammar; + addGrammar(grammar: Grammar): Disposable; + removeGrammarForScopeName(scopeName: string): Grammar; + readGrammarSync(grammarPath: string): Grammar; + readGrammar(grammarPath: string, callback: (error: Error, grammar: Grammar) => void): void; + loadGrammarSync(grammarPath: string): Grammar; + loadGrammar(grammarPath: string, callback: (error: Error, grammar: Grammar) => void): void; + grammarOverrideForPath(filePath: string): Grammar; + setGrammarOverrideForPath(filePath: string, scopeName: string): Grammar; + clearGrammarOverrides(): void; + selectGrammar(filePath: string, fileContents: string): Grammar; + } + + /** Registry containing one or more grammars. */ + var GrammarRegistry: GrammarRegistryStatic; +} + +declare module 'first-mate' { + export = AtomFirstMate; +}