Skip to content

TypeScript Plugin

a-calc provides a TypeScript language service plugin that gives intelligent autocomplete for expression strings in calc, fmt, and other functions.

Features

  • Variable completion — suggests variable names from the second argument
  • Format syntax completion — suggests all formatting options after |
  • Preset completion — suggests preset names after !
  • Unit completion — suggests available units based on project config
  • Deep property support — supports nested property completion like user.name

Setup

Add the plugin to your tsconfig.json:

json
{
  "compilerOptions": {
    "plugins": [{ "name": "a-calc/ts-plugin" }]
  }
}

VS Code Configuration

VS Code uses its built-in TypeScript version by default. You need to switch to the workspace version to load plugins.

Option 1: Manual Switch

  1. Open any .ts file
  2. Press Ctrl+Shift+P (Mac: Cmd+Shift+P)
  3. Type TypeScript: Select TypeScript Version
  4. Select Use Workspace Version

Option 2: Auto Switch

Add to .vscode/settings.json:

json
{
  "typescript.tsdk": "node_modules/typescript/lib"
}

Completion Examples

Variable Completion

Type a variable name prefix in the expression area:

typescript
const data = { price: 100, quantity: 3, user: { name: "test" } };

calc("pri", data); // typing 'pri' suggests 'price'
calc("user.", data); // typing 'user.' suggests 'name'

Format Syntax Completion

Trigger completion after | to see all formatting options:

typescript
calc("100 | "); // suggests =N, <=N, %, ,, !t, !c, etc.

Preset Completion

Type ! to see preset types:

typescript
calc("100 | !"); // suggests t (thousands), c (compact), u (unit), etc.
calc("100 | !t:"); // suggests en, indian, wan, etc.

Supported Functions

The plugin provides completion for these functions:

  • calc
  • fmt
  • calc_sum
  • calc_wrap
  • calc_space
  • calc_avg
  • calc_max
  • calc_min
  • calc_count

Notes

  1. Restart VS Code or reload the window after modifying tsconfig.json
  2. Ensure typescript is installed as a project dependency
  3. The plugin only works in TypeScript files

Released under the MIT License