Dentaku formula language support for CodeMirror
Dentaku is a parser and evaluator of formulas for Ruby. This package is language support of this formula language for CodeMirror.
Installation & usage
npm install --save codemirror codemirror-lang-dentaku
import { EditorView } from "codemirror";
import { dentaku } from "codemirror-lang-dentaku";
new EditorView({
extensions: [dentaku()],
});
Features
- Syntax highlighting (except
case
statements) - Autocompletion for built-in and custom functions and known variables
- Linting for syntax errors and undefined variables
This package doesn't offer support for everything that Dentaku offers, see the list of missing features below.
Configuration
The main export, dentaku
, comes packaged with linting and autocompletion that you can configure:
dentaku({ completionOptions, linterOptions });
Autocompletion (completionOptions
)
export interface DentakuLanguageCompletionOptions {
variableEntries?: Array<Completion>;
customFunctionEntries?: Array<Completion>;
makeEntryForBuiltInFunctions?: (
name: (typeof builtInFunctions)[number]
) => Completion | null | false;
makeEntryForBuiltInOperators?: (
name: (typeof builtInOperators)[number]
) => Completion | null | false;
}
Linting (linterOptions
)
The linterOptions
object is of type DentakuLinterOptions & { codeMirrorConfig?: Parameters<typeof linter>[1] }
(see linter
).
This means that, apart from allowing fields from DentakuLinterOptions
described below, the linterOptions
also allows an optional codeMirrorConfig
field that lets you specify options for the CodeMirror linter function (linter(lintSource, options)
).
The customFunctions
property is an object with the custom functions names as keys and Arity
object as values (see Custom Functions below).
export interface DentakuLinterOptions {
knownVariables?: Array<string>;
customFunctions?: Record<string, Arity>;
messages?: ErrorMessages;
}
export interface ErrorMessages {
invalidSyntax: string;
closingBracketMissing: string;
expectedCommaBefore: string;
expectedOperatorBefore: string;
callParenthesesMissing: string;
undefinedVariable: string;
expectedExactArgumentCount: (
actualCount: number,
expectedCount: number
) => string;
expectedMaximumArgumentCount: (
actualCount: number,
maxCount: number
) => string;
expectedMinimumArgumentCount: (
actualCount: number,
minCount: number
) => string;
expectedArgumentCountRange: (
actualCount: number,
minCount: number,
maxCount: number
) => string;
}
Custom Functions
The configuration of custom functions allows defining the minimum and maximum number of arguments that can be passed to each function. When a function supports an unknown number of maximum arguments, Infinity
should be provided. If an empty object ({}
) is provided as value, by default, the minimum will be 0
and the maximum Infinity
. When a is provided, both minArgs
and maxArgs
are required.
export type Arity = {
minArgs?: number;
maxArgs?: number;
};
const customFunctions: Record<string, Arity> = {
fnWithOneArg: {
minArgs: 1,
maxArgs: 1,
},
fnWithTwoOrMoreArgs: {
minArgs: 2,
maxArgs: Infinity,
},
fnWithDefaultConfig: {},
};
Missing features
These features aren't on our roadmap because we don't use them currently, but contributions are highly welcome!