npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

codemirror-lang-dentaku

v2.0.0

Published

CodeMirror support for the Dentaku formula language

Downloads

876

Readme

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 {
  /**
   * CodeMirror `Completion` objects for known variables.
   *
   * @example
   * ["a", "b"].map(name => ({ label: name, type: "variable" }))
   */
  variableEntries?: Array<Completion>;
  /**
   * CodeMirror `Completion` objects for custom functions.
   *
   * @example
   * ["fnA", "fnB"].map(name => ({ label: name, type: "function" }))
   */
  customFunctionEntries?: 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;
}

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 {
  /** List of variable names to not treat as undefined. */
  knownVariables?: Array<string>;
  /** Custom functions. */
  customFunctions?: Record<string, Arity>;
  /** 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 exactly that many parameters, found this many. */
  expectedExactArgumentCount: (
    actualCount: number,
    expectedCount: number
  ) => string;
  /** Expected at most that many parameters, found this many. */
  expectedMaximumArgumentCount: (
    actualCount: number,
    maxCount: number
  ) => string;
  /** Expected at least that many parameters, found this many. */
  expectedMinimumArgumentCount: (
    actualCount: number,
    minCount: number
  ) => string;
  /** Expected between that and that many parameters, found this many. */
  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.

/** Arity config for built-in and custom functions  */
export type Arity = {
  /** @default 0 */
  minArgs?: number;
  /** @default Infinity */
  maxArgs?: number;
};

// Example of custom functions configuration
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!

  • [ ] 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
  • [ ] Customizing the severity of lint errors
  • [ ] Auto-fixing for fixable lint errors