Dentaku is a parser and evaluator of formulas for Ruby. This package is language support of this formula language for CodeMirror.
npm install --save codemirror codemirror-lang-dentaku
import { EditorView } from "codemirror";
import { dentaku } from "codemirror-lang-dentaku";
new EditorView({
extensions: [dentaku()],
});
- Syntax highlighting (except
case
statements) - Autocompletion for known variables and built-in functions
- 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.
The main export, dentaku
, comes packaged with linting and autocompletion that you can configure:
dentaku({ completionOptions, linterOptions });
export interface DentakuLanguageCompletionOptions {
/**
* CodeMirror `Completion` objects for known variables.
*
* @example
* ["a", "b"].map(name => ({ label: name, type: "variable" }))
*/
variableEntries?: Array<Completion>;
/**
* Converter of built-in Dentaku functions into `Completion` objects.
* Allows you to add additional information to completions like descriptions.
*/
makeEntryForBuiltInFunctions?: (
name: (typeof builtInFunctions)[number]
) => Completion | null | false;
/**
* Converter of built-in Dentaku operators into `Completion` objects.
* Allows you to add additional information to completions like descriptions.
*/
makeEntryForBuiltInOperators?: (
name: (typeof builtInOperators)[number]
) => Completion | null | false;
}
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)
).
export interface DentakuLinterOptions {
/** List of variable names to not treat as undefined. */
knownVariables?: Array<string>;
/** Custom error messages. */
messages?: ErrorMessages;
}
/** Message codes that can be overridden with custom messages. */
export interface ErrorMessages {
/** Generic "invalid syntax" message when no better message can be inferred. */
invalidSyntax: string;
/** Missing a closing bracket in a list of function arguments. */
closingBracketMissing: string;
/** Expected a comma before this function argument. */
expectedCommaBefore: string;
/** Expected an operator before this expression. */
expectedOperatorBefore: string;
/** The function name needs parentheses to be called. */
callParenthesesMissing: string;
/** This variable is not defined. */
undefinedVariable: string;
/** Expected at least one parameter in a function call. */
expectedAtLeastOneParameter: string;
/** Expected that many parameters, found this many. */
parameterCountMismatch: (thatMany: number, thisMany: number) => string;
}
These features aren't on our roadmap because we don't use them currently, but contributions are highly welcome!
- Hexadecimal number literals (e.g.,
0xFF
) - Logic operators as operators (e.g.,
a AND b
), functions are supported though (e.g,and(a, b)
) - Syntax highlighting of
case
statements - Autocompletion for custom functions
- Linting argument counts of custom functions (including functions that take no arguments)
- Linting argument counts of functions with optional arguments
- Customizing the severity of lint errors
- Auto-fixing for fixable lint errors