MoonBit Thrift
A VS Code extension for Thrift, with its core language logic written in MoonBit. The current version provides syntax highlighting, live diagnostics, documentation on hover, go to definition, intelligent completion, a document outline, and common code snippets.
Features
- Parses
include, cpp_include, namespace, typedef, const, enum, struct, union, exception, and service declarations.
- Supports primitive types, nested containers, field defaults, method parameters,
throws, extends, oneway, and traditional annotations.
- Highlights service method names, including signatures with qualified return types, nested containers, multiline declarations, and RPC annotations.
- Uses consistent custom type highlighting for return types, parameters, record fields, and nested container types.
- Handles all three comment styles, single- and double-quoted strings, floating-point numbers, hexadecimal integers, and legacy
cpp_type / XSD syntax.
- Recovers from incomplete code to preserve subsequent fields and declarations in the outline where possible.
- Flags duplicates, missing includes, unresolved type and service references, and non-exception types in
throws as you edit.
- Uses UTF-16 source ranges, with support for Chinese text, emoji, BOM, and CRLF.
- Displays declarations, fields, enum members, methods, parameters, and exception parameters in the outline.
- Navigates include paths and type references, including service request and response structs, typedefs, nested container types, exception types, and service inheritance.
- Resolves types in included files and follows unsaved changes in open documents.
- Parses declaration and field comments and displays their documentation on hover, including through references to types in included files.
- Completes keywords, primitive and container types, local and included types, service base names, and include paths.
The parser and highlighter support legacy senum, slist, php_namespace and xsd_namespace, as well as structured @Annotation and @module.Annotation{key = value} before declarations and members. Traditional parenthesized annotations are unchanged. Structured annotation values are parsed but not type-checked.
Installation
Install MoonBit Thrift in VS Code and open a .thrift file to enable syntax highlighting, diagnostics, snippets, and the document outline. If you have a VSIX package, use Extensions: Install from VSIX... in the Command Palette to install it.
The extension bundles the MoonBit core compiled to JavaScript. Users do not need to install MoonBit or a separate Node.js runtime.
Documentation on Hover
Hover over a struct name, a field name, or a referenced type to see its declaration and documentation:
/**
* A user record.
*/
struct User {
// Display name.
1: optional string name,
}
The parser supports /** ... */, /* ... */, //, ///, and # comments. Adjacent leading comment groups belong to the following declaration or field; a blank line separates a comment group from that declaration. Comments on the same line after a declaration belong to the preceding declaration or field. Paragraphs, Markdown emphasis, and code indentation are preserved while comment markers and common indentation are removed.
Hovering over a type reference shows documentation from its definition, including definitions in included files. Changes to comments in open, unsaved files are reflected immediately.
Go to Definition
Press F12 or use Go to Definition on an include path or a referenced type. You can also use Ctrl+click (Cmd+click on macOS).
include "types.thrift"
service UserService {
types.GetUserResponse getUser(1: types.GetUserRequest req)
}
The include path opens the referenced file. In types.GetUserRequest, use Go to Definition on types to open types.thrift at the top, or on GetUserRequest to jump to that declaration. The types prefix comes from the included filename without .thrift. Unqualified names resolve in the current file. Typedef references jump to the alias declaration; navigating its underlying type continues to that type's definition.
Includes are resolved relative to the including file first. For shared IDL directories, configure additional search paths in VS Code settings:
{
"moonbitThrift.includePaths": ["idl", "../shared/idl"]
}
Relative search paths are resolved against the workspace folder containing the current file. Absolute paths are also supported. An unresolved type name has no definition target, but its prefix can still open the included file if it exists.
Intelligent Completion
Use Trigger Suggest to see keywords and types valid at the cursor. For example, in list<...> the extension suggests types, and after extends it suggests only services. Type types. to see declarations from an included types.thrift, including unsaved edits.
Inside an include string, the opening quote or / triggers folder and .thrift file suggestions. Completion searches relative to the current file and in moonbitThrift.includePaths. It does not automatically add includes for unimported types.
Diagnostics
The Problems panel reports duplicate declarations, field IDs and names, enum member names and explicit values, and RPC method names. It also flags missing includes, unknown types and base services, and throws entries whose type does not resolve to an exception. Typedef aliases to exceptions are accepted.
Include diagnostics use the same search paths as navigation and refresh when an included file changes on disk or in an unsaved editor. If an include cannot be found, its path is flagged without repeating the error on every reference through that include.
Workspace Symbols
Use Go to Symbol in Workspace to find top-level Thrift declarations, even in files you have not opened. The search includes all workspace folders and open Thrift buffers. Open buffers take precedence over disk content, and new searches pick up file additions, edits, and deletions.
Find References and Rename
Use Find All References or Rename Symbol on a type or service declaration or reference. The extension checks workspace files and open Thrift buffers, resolving each reference before using it. Renaming does not change include prefixes, comments, strings, or references to same-named declarations in other files.
Code Folding
Fold multiline declarations, RPC parameters, throws lists, annotations, constant containers, and block comments using the editor gutter. Delimiters inside strings or comments do not create fold regions.
Use Format Document to normalize indentation, spacing, and declaration and member line breaks. The formatter respects VS Code’s tabs or spaces setting, preserves comments, string literals, and existing commas and semicolons, and leaves documents with syntax errors unchanged.
Quick Fixes
On an unresolved type or service, open Quick Fix to create the declaration or add an include for a matching workspace declaration. The extension qualifies an unqualified reference when needed, can reuse an existing include, and creates an exception instead of a struct for a missing type used directly in throws.
Roadmap
Full type checking is planned.
Contributing
See CONTRIBUTING.md for local setup, testing, packaging, and architecture details.
License
MIT