RAS Playground 日本語 GitHub

VS Code and the CLI

VS Code extension

日本語 | English

RAS is a better VBA: a language with a TypeScript-like syntax that transpiles to VBA (.bas / .cls). This extension adds RAS (.ras / .d.ras) language support to Visual Studio Code.

The extension itself is deliberately thin. All it does is provide syntax highlighting and start the ras binary as a language server (ras lsp --stdio) and connect to it. Completion, quick info, parameter info, diagnostics, go to definition, rename, inlay hints, semantic highlighting, CodeLens, formatting and the rest are all provided by the language server.

Requirements

When ras is not found, an error message is shown; its [Open Settings] button lets you set ras.path. Syntax highlighting works without ras.

Features

Commands

These are run from the Command Palette. Each of them only asks the language server to do the work.

When an empty folder is open (including a folder that holds only hidden files such as .git and Excel workbooks), the Explorer's title bar shows a [Create RAS Project] icon button (the RAS logo) and the status bar shows [Create RAS Project]. The Explorer of a window without a folder shows the same button (you choose the folder to create the project in, and the folder is opened afterwards).

When a folder with a ras.toml is open, the Explorer's title bar (the row with the project folder's name) shows the [New Standard Module] and [New User Form] icon buttons. Both can also be run from a folder's context menu.

Command What it does
Create RAS Project Creates a RAS project (ras init: ras.toml, src/main.ras, AGENTS.md, .gitignore) and a workbook in a folder, writes the sample code into the workbook (ras pack, which also creates the files of the workbook's sheets and ThisWorkbook, objects/Sheet1.ras and objects/ThisWorkbook.ras), and opens src/main.ras and the workbook's sheet preview. The workbook can be a new empty macro-enabled workbook (named after the folder by default), a workbook in the folder, or a workbook from elsewhere (copied into the folder). An .xlsx is converted to an .xlsm of the same name (the original .xlsx is not changed). Hand-written VBA in an existing workbook is imported into the project before writing (the same as ras pack; the workbook as it was before is kept in build/backup/). The project goes to the folder right-clicked in the Explorer, else a workspace folder without a ras.toml, else a folder you choose. In a workspace without a ras.toml, it can also be run from a folder's context menu in the Explorer. With an English display language, the new project uses code page 1252 and English comments.
RAS: Show Generated VBA Shows the VBA generated from the RAS file being edited in a read-only editor beside it, and highlights the line that corresponds to the cursor. Can also be run from the editor's title bar and context menu.
RAS: Sync with Workbook Applies the VBA generated from the saved sources to the workbook open in Excel right away. When the workbook is not open in Excel (including on systems other than Windows), it writes straight to the workbook file like ras pack (offline writing; the output goes to RAS in the Output panel). When modules were edited in Excel (VBE), it asks whether to overwrite them. While a project is open, the [Sync with Workbook] icon button in the Explorer's title bar runs it too.
RAS: Open in Excel Opens the workbook in Excel (or brings it to the front when it is open) and syncs.
RAS: Run Macro Choose a macro from the list; it is synced and then run in Excel.
RAS: Run Form in Excel Choose a user form; it is synced and then shown in Excel (Windows only). Can also be run from the designer's title bar.
RAS: Debug Form Shows the user form open in the designer in a VS Code panel and debugs it. Run from the designer's title bar.
RAS: Show Workbook in Sheet Preview Shows a workbook (.xlsx / .xlsm) in an Excel-like sheet preview in a panel. Can also be run from a workbook's context menu in the Explorer. The workbook shown can be edited in the sheet preview (see "Editing in the sheet preview" below; the workbooks a run saved under build/ and the view while debugging are read-only).
RAS: Show Sheet Preview On the file of a sheet (a file in objects/ named after the sheet's code name, like objects/Sheet1.ras), shows the project's workbook (project.workbook of ras.toml) in the sheet preview, open at that sheet. Run from the editor's title bar (the table icon), the editor's context menu, or a file's context menu in the Explorer. It shows the saved workbook file, which can be edited in the grid.
RAS: Add Sheet / Copy Sheet / Delete Sheet / Rename Sheet Changes a sheet of the workbook and its file (objects/<CodeName>.ras) together. Run from the context menu of objects/ and its files in the Explorer, of the tab of a sheet's file, or of a sheet tab in the sheet preview. When the workbook is closed, the workbook file is rewritten; when it is open in Excel, Excel does it (Windows only). Deleting a sheet whose file has code or that other things refer to asks for confirmation, with the reasons. Changing the code name also renames the file and the references in the sources (Sheet1.…). When a file of objects/ is renamed or deleted in the Explorer, you are asked whether the sheet should follow.
RAS: Show the Failed Test's Workbook in Sheet Preview Shows the workbook of a failed test (build/test/workbooks/) in the sheet preview, with the cells changed since the test started marked. Can also be run from the button and the context menu of a failed test's message and from the context menu of the test in the Test Explorer (see "Running tests" below).
RAS: Generate Test Adds the skeleton of a test of the function at the cursor (an exported function of a module in src/) to test/<module>.test.ras and opens it (see "Generating tests" below). Can also be run from the editor's context menu.
RAS: Restart Language Server Restarts the language server.
RAS: Start Debugging Choose a function and debug it (the same as F5). Can also be run from the editor's title bar.
RAS: New Standard Module Creates and opens src/<name>.ras. The VBA module name follows from the file name (str_util → StrUtil). Run on a folder under src/ in the Explorer, it creates the file in that folder.
RAS: New User Form Creates forms/<name>.form.json and the RAS code-behind forms/<name>.ras, and opens the form in the designer.
RAS: Open in Designer Opens the FormSpec open in a text editor in the designer.
RAS: Open JSON Opens the FormSpec open in the designer in a JSON text editor beside it.

Editing in the sheet preview

A workbook shown with "RAS: Show Workbook in Sheet Preview" or "RAS: Show Sheet Preview" can be edited the way it is in Excel. At the top is the same ribbon as Excel's (the Home, Insert, Formulas, Data and View tabs; the "Insert" of Excel's Developer tab is among the controls of the Insert tab, and View Code is on the Shape Format tab), with the buttons where Excel has them. Double-clicking a tab collapses the ribbon. The edits are written to the workbook file by ras book edit (specification §14.14). Only the changed cells are written back, so the VBA project, shapes, controls and formatting stay as they are.

The list of operations (ribbon, keys, mouse), how the edits are written and what cannot be done yet are in Editing a workbook of the reference (the "Book" tab of the playground works the same way). What is particular to the sheet preview of VS Code:

Run mode of the sheet preview

In the sheet preview of the project's workbook, the switch at the right end of the ribbon's tabs switches between "Edit" and "Run" (specification §14.10.10). In run mode the project is built and run on the Excel stand-in, and what you do on the sheet raises events (as in Excel with design mode off). "▶ Run mode" is shown at the left of the status bar.

What you do Events raised
The workbook (run mode starts) Workbook.Open
Switching sheets Deactivate / Activate
Selecting cells SelectionChange
Entering into a cell, Delete Change (and Calculate when formulas were recalculated)
Double-clicking or right-clicking a cell BeforeDoubleClick / BeforeRightClick (cancel cancels the editing or the menu)
Clicking an ActiveX control GotFocus, MouseDown, MouseUp, Click (LostFocus when the focus moves on). Check boxes, option buttons and toggle buttons are turned on / off, spin buttons and scroll bars change their value, and list boxes select an item (raising Change too)
Clicking an ActiveX text box or combo box An input box (and the list of items for a combo box) is shown below it. Enter or choosing an item changes the value and raises Change
Clicking a shape or a form control After the value of a form control changes (check box, option button, list, drop-down, spin, scroll), the @on("<name>.Click") handler or the assigned macro

Switching the run target

The ▶ item in the status bar (or RAS: Switch Run Target) chooses where "Run" runs a function, out of three. The choice is saved in the workspace setting ras.run.target.

Target "Run" "Debug"
Excel stand-in The program built to JavaScript runs on RAS without Excel (it reads workbook of ras.toml; the changed workbook is saved in build/debug/workbooks/) The Excel stand-in too
Excel (COM) The program built to JavaScript works on the real Excel over COM (Windows only) Excel (COM) too
Excel (VBA) The generated VBA is synced to the workbook and runs inside Excel (Windows only; the former "Run in Excel") Follows the setting ras.debug.excel

The ▶ CodeLens above functions and the ▶ button of the editor title bar run on the chosen target. A launch.json configuration with excel keeps its own value.

Debugging

Put a breakpoint in a .ras file and press F5: the project is built, you choose the function to run, and debugging starts. The CodeLens shown above a function starts that function directly.

▶ Run in Excel | ⚙ Debug
export function rebuildReport() {

(The CodeLens titles come from the language server, in the display language.)

In the code of a user form (forms/<name>.ras), the same CodeLens is shown at the top of the file and shows and runs the form. In the designer, the buttons of the title bar (▶ and ⚙) do the same.

The CodeLens can be hidden with the setting ras.codeLens.run.

Generating tests

Above the exported functions of the modules in src/, a "Generate Test" CodeLens is shown too.

⚙ Debug | 🧪 Generate Test
export function sumTo(n: int): int {

Clicking it adds the skeleton of a test of that function to test/<module>.test.ras (test/report.test.ras for src/report.ras) and opens the line of the test. (The skeleton's names and comments come from the language server, in the display language.)

import { sumTo } from "src/main";

describe("sumTo", () => {
    test("return value of sumTo", () => {
        // Arrange: replace the values of the arguments
        const n: int = 0;

        const actual = sumTo(n);

        // Assert: replace with the expected value
        expect(actual).toBe(0);
    });
    test.todo("edge values (0, the empty string, the largest value, ...)");
});

With a launch.json, use the configuration type ras.

{
  "type": "ras",
  "request": "launch",
  "name": "RAS: rebuildReport",
  "entry": "Report.rebuildReport"
}
Attribute Description
entry The function to run (ModuleName.functionName or just the function name). When omitted, it is chosen at start.
project The folder with ras.toml. Defaults to the workspace folder.
stopOnEntry Stops at the first line of the entry point function (in its .ras file).
dialogs What happens when Excel shows a dialog while the program waits for Excel. notify (default: reports the dialog and brings it to the front) / dismiss (reports it and closes it with the button that amounts to Cancel; End for a VBA run-time error) / off.
excel The Excel the program works on. auto (default: Excel on Windows, the Excel stand-in on Mac and Linux) / excel (Excel only) / standIn (the Excel stand-in, on Windows too). Defaults to the setting ras.debug.excel.
runtimeExecutable / comAddon / bookAddon / mockHost Replace the Node.js that runs the program, the addon that connects to Excel, the addon of the Excel stand-in, or the Excel stand-in module (not normally needed).

Debugging does not run on VBA: the program is converted to JavaScript and run on the runtime that comes with VS Code. What it does to Excel is done to the real Excel, through the bundled addon (ras_com.node, Windows only).

On Mac and Linux, and on Windows without Excel, the program reads the workbook of workbook in ras.toml instead of using Excel, and runs reading and writing its cells (the bundled addon ras_book.node).

Running tests

The tests in test/**/*.test.ras ([test] include in ras.toml changes where) are shown in VS Code's Test Explorer (the flask icon in the Activity Bar), as a tree of test files → describe → test. The list comes from ras test --list, which reads it from the syntax without running the tests. Saving, adding or deleting a .ras file or ras.toml reads the list again.

Settings

Setting Default Description
ras.path "" Path of the ras executable. When empty, ras (ras.exe on Windows) is looked up on PATH. A relative path is resolved against the first workspace folder, and a leading ~ is expanded to the home directory. Changing it restarts the language server.
ras.server.extraArgs [] Arguments added after ras lsp --stdio when the language server starts. Changing them restarts the language server.
ras.trace.server "off" Logs the traffic between VS Code and the language server to "RAS Language Server" in the Output panel (off / messages / verbose).
ras.codeLens.run true Shows the "Run in Excel" (Windows only) and "Debug" CodeLens above the functions that can be started on their own.
ras.run.target auto Where "Run" runs. auto (excel on Windows, standIn elsewhere) / standIn (the Excel stand-in) / excelCom (Excel over COM) / excel (the VBA in Excel). With standIn and excelCom, debugging runs there too. Switched from the status bar.
ras.debug.runtimeExecutable "" Path of the Node.js used for debugging. When empty, the runtime that comes with VS Code is used.
ras.debug.comAddon "" Path of the addon that connects to Excel (ras_com.node). When empty, the one shipped with the extension is used, else the one in the folder of ras.
ras.debug.excel auto The Excel the program works on while debugging. auto (Excel on Windows, the Excel stand-in elsewhere) / excel / standIn (the stand-in, on Windows too).
ras.debug.bookAddon "" Path of the addon of the Excel stand-in (ras_book.node). When empty, the one shipped with the extension is used, else the one in the folder of ras.
ras.debug.dialogs notify What happens when Excel shows a dialog and waits for an answer (notify / dismiss / off). Can be overridden per configuration with dialogs in launch.json.

In RAS files, the semantic highlighting of the language server (editor.semanticHighlighting.enabled) is enabled by default.

Language server features

Feature How
Completion Members after ., named arguments (after ( and ,). The exports of other modules that are not imported yet are offered too; accepting one adds the import
Quick info, parameter info Hover. After a function's ( or ,, shows the signature and the argument being entered (with its @param description)
Go to definition, go to type definition F12 / context menu → [Go to Type Definition]
Find and highlight references Shift+F12 ([Find All References] is Shift+Alt+F12). The uses of the name at the cursor are highlighted in the file
Rename F2. Rewrites all references in the project (including imports, named arguments, type annotations, @param, and keys of object literals). Renaming an interface member also renames the members of the same name in the classes that implement it. Renaming to a VBA reserved word, to a name that differs only in case (VBA does not tell them apart), to a name already used in the same scope, or renaming a name declared in type definitions (.d.ras) or a control name of a form is refused, with the reason
Symbols Outline, [Go to Symbol] (Ctrl+Shift+O), [Go to Symbol in Workspace] (Ctrl+T)
Inlay hints Inferred types of variables and lambda parameters, inferred type arguments of generic functions, byref before arguments passed by reference (toggled with editor.inlayHints.enabled)
Semantic highlighting Colors arguments, local variables, properties, methods, Excel / VBA names and more differently. Arguments passed by reference are underlined, and @deprecated declarations are struck through (can be changed with *.byref:ras / *.extension:ras and so on in editor.semanticTokenColorCustomizations)
Quick fixes Change names spelled the VBA way to camelCase; add the import of a name that is not imported. With "editor.codeActionsOnSave": { "source.fixAll": "explicit" }, all names spelled the VBA way are fixed on save
Folding Blocks, multi-line comments, runs of imports, // #region … // #endregion
Formatting, CodeLens "Format Document" (also changes names spelled the VBA way (W0304) to camelCase before formatting; the same fix as ras check --fix), and "Run in Excel", "Debug" and how optimizations were applied above functions

Live sync with Excel (Windows)

While the workbook set in project.workbook of ras.toml is open in Excel, every time a .ras file or a user form file (forms/*.form.json, forms/*.ras, the pictures in forms/assets/) is saved, the generated VBA and user forms are applied to the VBA project of that workbook (disable with [sync] enabled = false). A workbook opened later is connected to and synced within a few seconds.

Status bar

The sync status is shown on the left. Clicking it runs "Sync with Workbook" (writing to the workbook file offline when not connected; restarting the language server when it is stopped). On Windows, an [Open in Excel] button is shown to its right while not connected.

Shown Meaning
RAS: Book.xlsm Connected to the workbook open in Excel.
RAS: Not connected The workbook is not open in Excel (always shown on systems other than Windows). The tooltip gives the reason.
An up arrow and a number The number of saves not yet applied to the workbook (build errors, Excel busy, and so on; the tooltip gives the reason).
A warning icon and a number (warning background) Drift warnings: modules managed by RAS were edited in Excel (VBE).
RAS: Stopped The language server is not running.

User form designer

The design of a user form goes in forms/<name>.form.json directly under the project (FormSpec v1: JSON based on the FormSpec of xlflow), and its code, such as the event handling, in RAS in forms/<name>.ras (this is the form and the controls are this.<name>; events are received by functions marked with @on("<control name>.<event>"); completion after this., type checking, hovers and so on work as in the files of src/). The *.form.json files directly in forms/ open in the designer by default (to edit the text, use [Open JSON] or "Reopen Editor With…"). The designer is a port of the UserForm Designer of xlflow.

{
  "schemaVersion": 1,
  "kind": "ras.userform",
  "basis": "designer",
  "form": { "name": "OrderForm", "caption": "Order", "width": 240, "height": 180 },
  "controls": [
    { "id": "ok", "name": "OkButton", "type": "CommandButton", "caption": "OK", "left": 160, "top": 144, "width": 72, "height": 24 }
  ]
}

Forms made in the VBE can be imported into forms/ with the CLI's ras form pull [name…] [--book <file>] [--force] (it writes forms/<name>.form.json and forms/<name>.ras, and the pictures to forms/assets/; the code is converted from VBA to RAS, and lines that could not be converted are left as // TODO(VBA): comments). FormSpecs in YAML are not supported.

Restricted Mode (untrusted workspaces)

In an untrusted workspace, only syntax highlighting is available. The language server and the Test Explorer become available when the workspace is trusted.

Development

npm ci
npm run compile   # type check (tsc --noEmit) and bundling with esbuild
npm test          # tests of the TextMate grammar and the user form designer
npm run package   # creates ras-vscode-<version>.vsix

Node.js 22 or later is needed. syntaxes/ras.tmLanguage.json is generated by scripts/build-grammar.mjs. The designer's UI (webview/userFormDesigner/) is bundled into dist/webview/ by esbuild. Do not edit generated files directly; change the script and run npm run build:grammar.

The texts of package.json (command titles, settings and so on) are in package.nls.json (English, the default) and package.nls.ja.json (Japanese). The messages of the extension are written as t("日本語", "English") (src/l10n.ts).

License

MIT License. For the third-party works this extension includes (the TextMate grammar of TypeScript, the user form designer of xlflow, and others), see THIRD_PARTY_NOTICES.md.