Skip to content

modular/mojo-vscode

Mojo for Visual Studio Code

Build extension

This VS Code extension from the Modular team adds support for the Mojo programming language.

Features

  • Syntax highlighting for .mojo and .🔥 files
  • Code completion
  • Code diagnostics and quick fixes
  • Full LSP experience for doc string code blocks
  • Go to symbol
  • API docs on hover
  • Code formatting
  • Run Mojo file

Get started

  1. Install the MAX SDK.
  2. Open the folder you installed MAX in.
  3. Open any .mojo or .🔥 file.

Mojo SDK Resolution

The extension relies on the Python extension for locating your Python environment. In some cases, this appears to default to your globally-installed environment, even when a virtual environment exists. If the Mojo extension cannot find your SDK installation, try invoking the Python: Set Project Environment command and selecting your virtual environment.

Debugger

A fully featured LLDB debugger is included with Mojo. You can press the down arrow next to the ▶️ button in the top right of a Mojo file, and select Debug Mojo File:

debugging

The default key is F5, and you can rebind the related hotkeys in Preferences: Open Keyboard Shortcuts > Debug: Start Debugging

Code Completion

To trigger a completion press ctrl + space, pressing ctrl + space again will bring up doc hints:

completion

Rebind the hotkey in Preferences: Open Keyboard Shortcuts > Trigger Suggest

Hover and Doc Hints

Hover over a symbol with your cursor for doc hints. The default hotkey to trigger it in macOS is ⌘ + k, ⌘ + i or ctrl + k, ctrl + i in Linux and Windows:

hover

Rebind the hotkey in Preferences: Open Keyboard Shortcuts > Show or Focus Hover

Signature Help

Mojo provides function overloading, so you need a way to scroll through the multiple signatures available. You can bring this up with the hotkey ⌘ + shift + space in macOS or ctrl + shift + space in Linux or Windows.

signature-help

Rebind related hotkeys in Preferences: Open Keyboard Shortcuts > Trigger Parameter Hints

Code Diagnostics

Code diagnostics are indicated with an underline on the code and details appear when you hover. You can also see them in the PROBLEMS tab and use Go to Next Problem in Files to quickly cycle through them:

diagnostics2

Rebind related hotkeys in Preferences: Open Keyboard Shortcuts > Go to Next Problem...

Tip: Also try the Error Lens extension (not associated with Modular), which will display the first line of the diagnostic inline, making it easier to quickly fix problems.

Doc String Code Blocks

Unique to Mojo, you get a full LSP experience for code blocks inside doc strings, with all the features mentioned here including completions and diagnostics:

doc-lsp

Go to Symbol

You can quickly jump to a symbol in the file with ⌘ + shift + o in macOS or ctrl + shift + o in Linux and Windows.

go-to-symbol

This also enables the outline view in the explorer window.

Rebind the hotkey in Preferences: Open Keyboard Shortcuts > Go to Symbol in Editor

Quick Fix

If there is an available quick fix with the code diagnostic, click the lightbulb icon or use the default hotkey ctrl + . for a list of options:

quick-fix

Rebind the hotkey in Preferences: Open Keyboard Shortcuts > Quick Fix...

Run Mojo File

The extension provides a set of actions on the top-right of a Mojo file to run the active file, which by default are under a small ▶️ button up the top-right of the editor:

run-file

These actions are also available in the command palette and under the Mojo submenu in the File Explorer when right-clicking on Mojo files:

right-click-menu

You may bind hotkeys to any of the actions listed here. For example, to bind a hotkey for the "Run Mojo File" action, open preferences, then select Keyboard Shortcuts > Mojo: Run Mojo File.

Run Mojo File

This executes the current Mojo file in a terminal that is reused by other invocations of this same action, even if they run a different file.

Run Mojo File in Dedicated Terminal

This executes the current Mojo file in a dedicated terminal that is reused only by subsequent runs of this very same file.

Code Formatting

From the command palette run Format Document or tick the setting Format on Save:

format

Restarting Mojo Extension

The extension may crash and produce incorrect results periodically, to fix this from the command palette search for Mojo: Restart the extension

restart

Bind a hotkey in Preferences: Open Keyboard Shortcuts > Mojo: Restart the extension

About

Mojo support for VS Code

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages