finalizing type definitions.

workstream:
This commit is contained in:
Nikolaj Kappler
2018-12-05 16:16:40 +01:00
parent 9c72aa7b89
commit dbf3b71982
3 changed files with 60 additions and 61 deletions

View File

@@ -1,52 +1,32 @@
//MUCH TODO
// Type definitions for CodeMirror
// Project: https://github.com/marijnh/CodeMirror
// Definitions by: Nikolaj Kappler <https://github.com/nkappler>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// See docs https://codemirror.net/doc/manual.html#addon_tern
// TypeScript Version: 2.2
import * as CodeMirror from "codemirror";
import * as Tern from "tern";
declare module "codemiror" {
interface Editor {
server: TernServer;
}
declare module "codemirror" {
interface TernServer {
addDoc: (
name: string,
doc: CodeMirror.Doc
) => {
doc: CodeMirror.Doc,
name: string,
changed: { from: number, to: number } | null
};
delDoc: (id: string | CodeMirror.Editor | CodeMirror.Doc) => void;
hideDoc: (id: string | CodeMirror.Editor | CodeMirror.Doc) => void;
complete: (cm: CodeMirror.Editor) => void;
showType: (cm: CodeMirror.Editor, pos?: CodeMirror.Position, callback?: Function) => void;
showDocs: (cm: CodeMirror.Editor, pos?: CodeMirror.Position, callback?: Function) => void;
updateArgHints: (cm: CodeMirror.Editor) => void;
jumpToDef: (cm: CodeMirror.Editor) => void;
jumpBack: (cm: CodeMirror.Editor) => void;
rename: (cm: CodeMirror.Editor) => void;
selectName: (cm: CodeMirror.Editor) => void;
request: (cm: CodeMirror.Editor, query: string | Tern.QueryRegistry[keyof Tern.QueryRegistry]["query"], callback: (error: any, data?: any) => void, pos?: CodeMirror.Position) => void;
destroy: () => void;
addDoc(name: string, doc: CodeMirror.Doc): { doc: CodeMirror.Doc, name: string, changed: { from: number, to: number } | null };
delDoc(id: string | CodeMirror.Editor | CodeMirror.Doc): void;
hideDoc(id: string | CodeMirror.Editor | CodeMirror.Doc): void;
complete(cm: CodeMirror.Doc): void;
showType(cm: CodeMirror.Doc, pos?: CodeMirror.Position | number, callback?: Function): void;
showDocs(cm: CodeMirror.Doc, pos?: CodeMirror.Position | number, callback?: Function): void;
updateArgHints(cm: CodeMirror.Doc): void;
jumpToDef(cm: CodeMirror.Doc): void;
jumpBack(cm: CodeMirror.Doc): void;
rename(cm: CodeMirror.Doc): void;
selectName(cm: CodeMirror.Doc): void;
request<Q extends Tern.Query>(cm: CodeMirror.Doc, query: Q, callback: (error?: Error, data?: Tern.QueryRegistry[Q["type"]]["result"]) => void, pos?: CodeMirror.Position): void;
request<Q extends Tern.Query["type"]>(cm: CodeMirror.Doc, query: Q, callback: (error?: Error, data?: Tern.QueryRegistry[Q]["result"]) => void, pos?: CodeMirror.Position): void;
destroy(): void;
}
interface TernConstructor {
@@ -58,39 +38,33 @@ declare module "codemiror" {
/** An object mapping plugin names to configuration options. */
plugins?: Tern.ConstructorOptions["plugins"];
/** An array of JSON definition data structures. */
defs?: Tern.ConstructorOptions["defs"];
defs?: Tern.Def[];
/**
* Can be used to access files in
* the project that haven't been loaded yet. Simply do callback(null) to
* indicate that a file is not available.
*/
getFile?: (name: string, callback: (doc: CodeMirror.Doc | null) => void) => void;
getFile?(name: string, callback: (doc: CodeMirror.Doc | null) => any): any;
/**
* This function will be applied
* to documents before passing them on to Tern.
*/
fileFilter?: (value: any, docName: string, doc: CodeMirror.Doc) => any;
fileFilter?(value: string, docName: string, doc: CodeMirror.Doc): string;
/** This function should, when providing a multi-file view, switch the view or focus to the named file. */
switchToDoc?: (name: string, doc: CodeMirror.Doc) => any;
switchToDoc?(name: string, doc: CodeMirror.Doc): any;
/** Can be used to override the way errors are displayed. */
showError?: (editor: CodeMirror.Editor, message: any) => void;
showError?(editor: CodeMirror.Editor, message: Error): any;
/**
* Customize the content in tooltips for completions.
* Is passed a single argumentthe completion's data as returned by
* Ternand may return a string, DOM node, or null to indicate that
* Is passed a single argumentthe completion's data as returned by
* Ternand may return a string, DOM node, or null to indicate that
* no tip should be shown. By default the docstring is shown.
*/
completionTip?: (data: Tern.QueryRegistry["completions"]["result"]) => string | HTMLElement | null;
completionTip?(data: Tern.CompletionsQueryResult): string | HTMLElement | null;
/** Like completionTip, but for the tooltips shown for type queries. */
typeTip?: (data: Tern.QueryRegistry["type"]["result"]) => string | HTMLElement | null;
typeTip?(data: Tern.TypeQueryResult): string | HTMLElement | null;
/** This function will be applied to the Tern responses before treating them */
responseFilter?: (
doc: CodeMirror.Doc,
query: Tern.QueryRegistry[keyof Tern.QueryRegistry]["query"],
request: any,
error: Error,
data: any
) => any;
responseFilter?(doc: CodeMirror.Doc, query: Tern.Query, request: Tern.Document, error: Error | undefined, data: Tern.QueryRegistry[Tern.Query["type"]]["result"] | undefined): any;
/**
* Set to true to enable web worker mode. You'll probably
* want to feature detect the actual value you use here, for example
@@ -98,7 +72,7 @@ declare module "codemiror" {
*/
useWorker?: boolean;
/** The main script of the worker. Point this to wherever you are hosting worker.js from this directory. */
workerScript?: any;
workerScript?: string;
/**
* An array of paths pointing (relative to workerScript)
* to the Acorn and Tern libraries and any Tern plugins you want to

View File

@@ -0,0 +1,24 @@
const options: CodeMirror.TernOptions = {
completionTip: (data) => {
const d = data.completions;
return "";
},
showError: (editor, message) => {
alert(message);
}
};
const ts = new CodeMirror.TernServer(options);
ts.request(cm.getDoc(), "completions", (_e, d) => {
if (d) {
const c = d.completions;
}
}, { ch: 0, line: 0 });
ts.complete(cm.getDoc());
ts.showType(cm.getDoc());

View File

@@ -19,19 +19,20 @@
},
"files": [
"index.d.ts",
"codemirror-comment.d.ts",
"codemirror-matchbrackets.d.ts",
"codemirror-panel.d.ts",
"codemirror-runmode.d.ts",
"codemirror-showhint.d.ts",
"codemirror-comment.d.ts",
"codemirror-tern.d.ts",
"searchcursor.d.ts",
"test/comment.ts",
"test/index.ts",
"test/matchbrackets.ts",
"test/panel.ts",
"test/runmode.ts",
"test/searchcursor.ts",
"test/showhint.ts",
"test/comment.ts"
"test/tern.ts"
]
}