Skip to content

Commit e22bd5c

Browse files
committedApr 25, 2023
fix: add mode types, improve type definitions
This change adds a new ace-modes.d.ts file (references in the main ace.d.ts), which declares types for everything in the `ace/src/mode` folder. It's been done semi-automatically, and every mode or highlight rules have the same base type, even though technically different modes can have different sub-types (so currently we are omitting some information). Type definitions are excluded in the ace-builds package. Also included small fixes for existing types, like EditSession or Config.
1 parent 990745a commit e22bd5c

File tree

3 files changed

+1229
-13
lines changed

3 files changed

+1229
-13
lines changed
 

‎Makefile.dryice.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,10 @@ function ace() {
171171
}
172172

173173
function buildTypes() {
174-
var definitions = fs.readFileSync(ACE_HOME + '/ace.d.ts', 'utf8');
174+
var aceCodeModeDefinitions = '/// <reference path="./ace-modes.d.ts" />';
175+
// ace-builds package has different structure and can't use mode types defined for the ace-code.
176+
// ace-builds modes are declared along with other modules in the ace-modules.d.ts file below.
177+
var definitions = fs.readFileSync(ACE_HOME + '/ace.d.ts', 'utf8').replace(aceCodeModeDefinitions, '');
175178
var paths = fs.readdirSync(BUILD_DIR + '/src-noconflict');
176179
var moduleRef = '/// <reference path="./ace-modules.d.ts" />';
177180

@@ -193,21 +196,20 @@ function buildTypes() {
193196

194197
fs.writeFileSync(BUILD_DIR + '/ace.d.ts', moduleRef + '\n' + definitions);
195198
fs.writeFileSync(BUILD_DIR + '/ace-modules.d.ts', pathModules);
196-
197199
var esmUrls = [];
200+
198201
var loader = paths.map(function(path) {
199202
if (/\.js$/.test(path) && !/^ace\.js$/.test(path)) {
200203
var moduleName = path.split('.')[0].replace(/-/, "/");
201204
if (/^worker/.test(moduleName))
202205
moduleName = "mode" + moduleName.slice(6) + "_worker";
203206
moduleName = moduleName.replace(/keybinding/, "keyboard");
204-
205207
esmUrls.push("ace.config.setModuleLoader('ace/" + moduleName + "', () => import('./src-noconflict/" + path + "'));");
206208
return "ace.config.setModuleUrl('ace/" + moduleName + "', require('file-loader?esModule=false!./src-noconflict/" + path + "'));";
207209
}
208210
}).join('\n');
209211
var esmLoader = esmUrls.join('\n');
210-
212+
211213
fs.writeFileSync(BUILD_DIR + '/webpack-resolver.js', loader, "utf8");
212214
fs.writeFileSync(BUILD_DIR + '/esm-resolver.js', esmLoader, "utf8");
213215
}

‎ace-modes.d.ts

+1,152
Large diffs are not rendered by default.

‎ace.d.ts

+71-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/// <reference path="./ace-modes.d.ts" />
2+
13
export namespace Ace {
24
export type NewLineMode = 'auto' | 'unix' | 'windows';
35

@@ -364,7 +366,66 @@ export namespace Ace {
364366
stepForward(): Token;
365367
}
366368

369+
export type HighlightRule = {defaultToken: string} | {include: string} | {todo: string} | {
370+
token: string | string[] | ((value: string) => string);
371+
regex: string | RegExp;
372+
next?: string;
373+
push?: string;
374+
comment?: string;
375+
caseInsensitive?: boolean;
376+
}
377+
378+
export type HighlightRulesMap = Record<string, HighlightRule[]>;
379+
380+
export type KeywordMapper = (keyword: string) => string;
381+
382+
export interface HighlightRules {
383+
$rules: HighlightRulesMap;
384+
$embeds?: string[];
385+
$keywordList?: string[];
386+
$keywords?: KeywordMapper;
387+
addRules(rules: HighlightRulesMap, prefix?: string): void;
388+
getRules(): HighlightRulesMap;
389+
embedRules(rules: (new () => HighlightRules) | HighlightRulesMap, prefix: string, escapeRules?: boolean, append?: boolean): void;
390+
getEmbeds(): string[];
391+
normalizeRules(): void;
392+
createKeywordMapper(map: Record<string, string>, defaultToken?: string, ignoreCase?: boolean, splitChar?: string): KeywordMapper;
393+
}
394+
395+
export interface FoldMode {
396+
foldingStartMarker: RegExp;
397+
foldingStopMarker?: RegExp;
398+
getFoldWidget(session: EditSession, foldStyle: string, row: number): string;
399+
getFoldWidgetRange(session: EditSession, foldStyle: string, row: number, forceMultiline?: boolean): Range | undefined;
400+
indentationBlock(session: EditSession, row: number, column?: number): Range | undefined;
401+
openingBracketBlock(session: EditSession, bracket: string, row: number, column: number, typeRe?: RegExp): Range | undefined;
402+
closingBracketBlock(session: EditSession, bracket: string, row: number, column: number, typeRe?: RegExp): Range | undefined;
403+
}
404+
405+
type BehaviorAction = (state: string, action: string, editor: Editor, session: EditSession, text: string) => {text: string, selection: number[]} | Range | undefined;
406+
type BehaviorMap = Record<string, Record<string, BehaviorAction>>;
407+
408+
export interface Behaviour {
409+
$behaviours: BehaviorMap;
410+
add(name: string, action: string, callback: BehaviorAction): void;
411+
addBehaviours(behaviours: BehaviorMap): void;
412+
remove(name: string): void;
413+
inherit(mode: SyntaxMode | (new () => SyntaxMode), filter: string[]): void;
414+
getBehaviours(filter: string[]): BehaviorMap;
415+
}
416+
417+
export interface Outdent {
418+
checkOutdent(line: string, input: string): boolean;
419+
autoOutdent(doc: Document, row: number): number | undefined;
420+
$getIndent(line: string): string;
421+
}
422+
367423
export interface SyntaxMode {
424+
HighlightRules: new () => HighlightRules;
425+
foldingRules?: FoldMode;
426+
$behaviour?: Behaviour;
427+
$defaultBehaviour?: Behaviour;
428+
lineCommentStart?: string;
368429
getTokenizer(): Tokenizer;
369430
toggleCommentLines(state: any,
370431
session: EditSession,
@@ -377,28 +438,28 @@ export namespace Ace {
377438
getNextLineIndent(state: any, line: string, tab: string): string;
378439
checkOutdent(state: any, line: string, input: string): boolean;
379440
autoOutdent(state: any, doc: Document, row: number): void;
441+
$getIndent(line: string): string;
380442
// TODO implement WorkerClient types
381443
createWorker(session: EditSession): any;
382444
createModeDelegates(mapping: { [key: string]: string }): void;
383-
transformAction(state: string,
384-
action: string,
385-
editor: Editor,
386-
session: EditSession,
387-
text: string): any;
445+
transformAction: BehaviorAction;
388446
getKeywords(append?: boolean): Array<string | RegExp>;
389447
getCompletions(state: string,
390448
session: EditSession,
391449
pos: Point,
392450
prefix: string): Completion[];
393451
}
394452

453+
type AfterLoadCallback = (err: Error | null, module: unknown) => void;
454+
type LoaderFunction = (moduleName: string, afterLoad: AfterLoadCallback) => void;
455+
395456
export interface Config {
396457
get(key: string): any;
397458
set(key: string, value: any): void;
398459
all(): { [key: string]: any };
399460
moduleUrl(name: string, component?: string): string;
400461
setModuleUrl(name: string, subst: string): string;
401-
setLoader(cb: Function): void;
462+
setLoader(cb: LoaderFunction): void;
402463
setModuleLoader(name: string, onLoad: Function): void;
403464
loadModule(moduleName: string | [string, string],
404465
onLoad?: (module: any) => void): void;
@@ -455,11 +516,12 @@ export namespace Ace {
455516
on(name: 'tokenizerUpdate',
456517
callback: (obj: { data: { first: number, last: number } }) => void): Function;
457518
on(name: 'change', callback: () => void): Function;
519+
on(name: 'changeTabSize', callback: () => void): Function;
458520

459521

460522
setOption<T extends keyof EditSessionOptions>(name: T, value: EditSessionOptions[T]): void;
461523
getOption<T extends keyof EditSessionOptions>(name: T): EditSessionOptions[T];
462-
524+
463525
readonly doc: Document;
464526

465527
setDocument(doc: Document): void;
@@ -817,7 +879,7 @@ export namespace Ace {
817879
setKeyboardHandler(keyboardHandler: string, callback?: () => void): void;
818880
setKeyboardHandler(keyboardHandler: KeyboardHandler|null): void;
819881
getKeyboardHandler(): string;
820-
setSession(session: EditSession): void;
882+
setSession(session: EditSession | undefined): void;
821883
getSession(): EditSession;
822884
setValue(val: string, cursorPos?: number): string;
823885
getValue(): string;
@@ -1032,7 +1094,7 @@ export const VirtualRenderer: {
10321094
new(container: HTMLElement, theme?: string): Ace.VirtualRenderer;
10331095
};
10341096
export const EditSession: {
1035-
new(text: string | Document, mode?: Ace.SyntaxMode): Ace.EditSession;
1097+
new(text: string | Ace.Document, mode?: Ace.SyntaxMode): Ace.EditSession;
10361098
};
10371099
export const UndoManager: {
10381100
new(): Ace.UndoManager;

0 commit comments

Comments
 (0)
Please sign in to comment.