G-Code Language Support
A Visual Studio Code extension providing comprehensive G-Code language support with syntax highlighting, intelligent formatting, and Language Server Protocol (LSP) integration.
Features
- Syntax Highlighting: Full syntax highlighting for G-Code files with support for 50+ file extensions
- Document Formatting: Intelligent formatting with customizable options
- Robust Error Handling: Parser gracefully handles unsupported syntax
- Preserves original code when encountering parse errors
- Inserts error comments with diagnostic information
- Continues parsing after errors for maximum code preservation
- Hover Information: Intelligent tooltips showing:
- Variable values and declarations
- G/M command descriptions with parameters and examples
- Operator and function documentation
- Axis parameter meanings
- Symbol Navigation: Document outline showing all variables
- Variable Renaming: Rename variables across entire document
- Syntax Highlighting: Semantic token-based highlighting
- Document Highlights: Highlight all occurrences of a variable
- Custom Theme: Dedicated G-Code color theme for optimal readability
- Language Server: LSP-based architecture for fast and reliable language features
- Format on Save: Automatic formatting support
- Range Formatting: Format selected portions of code
Supported File Extensions
The extension supports a wide range of G-Code file extensions including:
.nc, .ngc - Numerical Control / Numerical G-Code
.g, .gc, .gcode - Standard G-Code
.tap - TAP files
.m, .mpf, .spf - Fanuc/Heidenhain formats
.sbp - ShopBot files
- And 40+ more extensions (see
package.json for complete list)
Dialect Support
The extension supports multiple G-code dialects/flavors to provide accurate completions and documentation specific to your CNC control system:
- LinuxCNC (default) - Extended G-code with named variables (#), conditional statements (IF/THEN/ELSE), and various control flow constructs
- Fanuc - Industry standard for mills and lathes, featuring G65 macro calls, numeric variables (#1-#999), M98/M99 subprograms, and standard canned cycles
- Haas - Mill-specific G-codes with VPS smoothing, G187 accuracy control, and M95-M99 jump labels
- Siemens/Sinumerik - Extended G-code range (beyond G99), R parameters, polar programming, and advanced interpolation modes
Changing the Dialect
Set your preferred dialect in VS Code settings:
{
"gcode.dialect": "fanuc"
}
Available values: "linuxcnc", "fanuc", "haas", "siemens"
The dialect setting affects:
- Completions: Shows commands and parameters specific to your dialect
- Hover documentation: Displays dialect-specific command descriptions and examples
- IntelliSense: Provides accurate parameter suggestions for commands
- Formatting: Applies dialect-specific control flow syntax (e.g., Siemens omits THEN/DO keywords)
Dialect changes take effect immediately without needing to reload VS Code.
The formatter automatically adjusts control flow syntax based on your selected dialect:
LinuxCNC/Siemens (no THEN/DO keywords):
IF [#<x> GT 0]
G01 X10
ENDIF
WHILE [#<counter> LT 10]
G01 X[#<counter>]
ENDWHILE
Fanuc/Haas (with THEN/DO keywords):
IF [#<x> GT 0] THEN
G01 X10
ENDIF
WHILE [#<counter> LT 10] DO
G01 X[#<counter>]
END
Key differences:
- LinuxCNC/Siemens: Omit
THEN and DO keywords, use ENDWHILE
- Fanuc/Haas: Use
IF...THEN, WHILE...DO, and END
- Siemens: Labels use colon suffix (e.g.,
O100: instead of O100)
Installation
From VS Code Marketplace
- Open VS Code
- Go to Extensions view (
Ctrl+Shift+X / Cmd+Shift+X)
- Search for "G-Code Language Support"
- Click Install
or
- Open VS Code
- Open Quick Open (
Ctrl+P / Cmd+P)
- Paste
ext install QuickBoyz.vscode-gcode-extension and press Enter
From VSIX Package
- Download the
.vsix file from the Releases page
- Open VS Code
- Go to Extensions view
- Click the
... menu and select "Install from VSIX..."
- Select the downloaded
.vsix file
Usage
Use the command palette (Ctrl+Shift+P / Cmd+Shift+P) and run:
- G-Code: Format G-Code Document
Or use the standard VS Code format document shortcut (Shift+Alt+F / Shift+Option+F).
Enable format on save in VS Code settings:
{
"editor.formatOnSave": true,
"[gcode]": {
"editor.formatOnSave": true
}
}
Extension Settings
This extension contributes the following settings:
| Setting |
Default |
Description |
gcode.dialect |
"linuxcnc" |
Select the G-code dialect/flavor for completions and documentation |
gcode.formatter.addLineNumbers |
false |
Add N-block line numbers to each line (N10, N20, etc.) |
gcode.formatter.lineNumberStart |
10 |
Starting line number when adding N-blocks |
gcode.formatter.lineNumberIncrement |
10 |
Line number increment when adding N-blocks |
gcode.formatter.prettyPrintCommands |
true |
Pretty-print G and M codes with two digits (G1 → G01, M3 → M03) |
gcode.formatter.prettyPrintNumbers |
true |
Pretty-print parameter numbers to always include a decimal point (X2 → X2.0) |
gcode.formatter.indent |
true |
Enable indentation for control structures (WHILE, IF, etc.) |
gcode.formatter.compactOutput |
false |
Compact output mode - removes all empty lines |
Architecture
This extension uses a Language Server Protocol (LSP) architecture:
┌─────────────────┐
│ VS Code Client │
│ (extension.ts) │
└────────┬────────┘
│ IPC
│
┌────────▼────────┐
│ Language Server │
│ (server.ts) │
└────────┬────────┘
│
┌────┴────┬──────────┬────────────┐
│ │ │ │
┌───▼───┐ ┌──▼───┐ ┌────▼────┐ ┌─────▼─────┐
│ Lexer │ │Parser│ │Formatter│ │ Types │
└───────┘ └──────┘ └──────────┘ └───────────┘
Project Structure
src/
├── client/ # VS Code extension client
│ ├── extension.ts # Extension entry point
│ └── index.ts
├── server/ # Language Server implementation
│ ├── server.ts # LSP server setup
│ └── index.ts
├── lexer/ # G-Code lexer (tokenizer)
│ └── GCodeLexer.ts
├── parser/ # G-Code parser (AST generation)
│ ├── GCodeParser.ts
│ ├── AstFactory.ts
│ ├── AstTraverser.ts
│ ├── AstVisitor.ts
│ └── nodes/ # AST node definitions
├── formatter/ # Code formatter
│ ├── BaseFormatter.ts
│ ├── FormatterFactory.ts
│ ├── ExpressionFormatter.ts
│ └── dialects/ # Dialect-specific formatters
│ ├── LinuxCNCFormatter.ts
│ ├── FanucFormatter.ts
│ ├── HaasFormatter.ts
│ └── SiemensFormatter.ts
├── databases/ # G-Code command databases
│ ├── GCodeCommandDatabase.ts
│ └── dialects/ # Dialect-specific databases
├── providers/ # Language feature providers
│ ├── DocumentFormattingProvider.ts
│ ├── DocumentSymbolProvider.ts
│ ├── DocumentHighlightProvider.ts
│ ├── HoverProvider.ts
│ ├── RenameProvider.ts
│ ├── SemanticTokensProvider.ts
│ ├── DataProviderFactory.ts
│ └── dialects/ # Dialect-specific providers
├── test/ # Unit tests (Jest)
│ └── fixtures/ # Test fixtures
└── e2e/ # E2E tests (VS Code)
├── suite/ # Test suites
└── fixtures/ # Test fixtures
Development
Prerequisites
- Node.js 18+ and npm
- Visual Studio Code
- TypeScript knowledge
Setup
- Clone the repository:
git clone https://github.com/QuickBoyz/vscode-gcode-extension.git
cd vscode-gcode-extension
- Install dependencies:
npm install
- Build the project:
npm run build
Development Workflow
- Make changes to the source files in
src/
- Build the project:
npm run build
- Test your changes:
npm test
- Debug the extension:
- Press
F5 in VS Code to launch a new Extension Development Host window
- Open a G-Code file in the new window to test your changes
- Package the extension:
npm run package
Available Scripts
npm run build - Compile TypeScript to JavaScript
npm run build:e2e - Compile test files
npm run typecheck - Type check without emitting files
npm test - Run unit tests (Jest)
npm run test:watch - Run unit tests in watch mode
npm run test:e2e - Run e2e tests (VS Code)
npm run test:all - Run both unit and e2e tests
npm run package - Build and package the extension as .vsix
npm run package:pre - Build and package as pre-release
Testing
The project uses two types of tests:
Unit Tests (Jest): Test individual components in isolation
- Located in
src/test/ directory
- Run with:
npm test
- Run in watch mode:
npm run test:watch
- Fast, isolated tests for parsers, formatters, and providers
E2E Tests (VS Code): Test features end-to-end in VS Code Extension Development Host
- Located in
src/e2e/ directory
- Run with:
npm run test:e2e
- Run all tests:
npm run test:all
- Tests actual VS Code integration (formatting, symbols, highlighting, rename, semantic tokens)
For detailed information about testing, see TESTING.md.
Debugging
- Open the project in VS Code
- Press
F5 to launch the Extension Development Host
- Set breakpoints in your code
- The debugger will attach automatically
You can also debug the Language Server by setting breakpoints in src/server/server.ts.
Contributing
We welcome contributions! Please see CONTRIBUTING.md for guidelines on:
- Code style and standards
- Submitting pull requests
- Reporting bugs
- Feature requests
- Development setup
Requirements
- VS Code 1.85.0 or higher
- No additional dependencies needed for end users
Known Issues
None at this time. Please report issues on GitHub Issues.
Release Notes
See CHANGELOG.md for detailed release notes.
Version 0.3.0
- LSP-based architecture
- Document and range formatting
- Comprehensive G-Code parser
- Customizable formatting options
License
MIT License - see LICENSE file for details.
Acknowledgments
Enjoy coding in G-Code!