Testing and tools, 5 of 5
The VS Code extension
Install the FutureBlitz extension, and write, run, test, and debug a package without leaving the editor.
What you’ll build
A small package, opened in VS Code with the FutureBlitz extension, and worked on there: errors as you type, hovers and completion, renaming, formatting on save, and the program run, tested, and debugged from the editor.
Installing it
Install FutureBlitz from the Extensions view: search for FutureBlitz, by futureblitz, on the VS Code Marketplace or Open VSX. Or install it from a terminal:
code --install-extension futureblitz.futureblitzVS Code installs CodeLLDB with it, which the debugger uses. The extension finds fb on your PATH,
or else in ~/.futureblitz/bin, and the language server, fb-lsp, beside fb. If either is
somewhere else, set futureblitz.path or futureblitz.lspPath in your settings.
A package to open
Make a package and open its folder:
fb new greeter
code greeterOpen main.fb and replace it with this. The extension highlights it: keywords, strings and what’s
interpolated in them, comments, and numbers each in their own color.
Import std.core
Import std.collections
Function Greeting(name: String, hour: Int): String
If hour < 12 Then Return "Good morning, {name}"
If hour < 18 Then Return "Good afternoon, {name}"
Return "Good evening, {name}"
End Function
For name In ["Ada", "Grace"]
Print(Greeting(name, 9))
End ForGood morning, Ada Good morning, Grace
The language server
As you type, the language server checks the code and underlines each error, with the same
message fb check gives, in the editor and in the Problems view. Change hour < 12 to
hour < "12", and the error shows at once, as fb would report it:
Import std.core
Function Greeting(name: String, hour: Int): String
If hour < "12" Then Return "Good morning, {name}"
Return "Good evening, {name}"
End Function
Print(Greeting("Ada", 9)) Compiling main.fb
main.fb:4:8: error: '<' can't combine Int32 and String
|
4 | If hour < "12" Then Return "Good morning, {name}"
| ^^^^^^^^^^^
error: couldn't build main.fb because of 1 errorA quick fix appears where fb has one. The server does more as you work:
- Hover a name for its type and its doc comment, std’s included.
- Completion offers members after a
., names in scope, and std’s modules afterImport. - Go to Definition (F12) and Find All References (Shift+F12) work across the package and into std.
- Rename Symbol (F2) renames a declaration everywhere it’s used, Methods’ overrides too.
- Signature help shows a call’s parameters as you type its arguments.
The language server works with any editor that speaks the Language Server Protocol, too: start
fb-lsp from the editor’s LSP client, over stdin and stdout, for .fb files.
Formatting on save
Saving a .fb file formats it, as fb fmt would, with the package’s [format] width. Type a
line with its spacing all wrong, like For name In[ "Ada","Grace" ], and save: it’s laid out in
the standard style.
Import std.core
For name In ["Ada", "Grace"]
Print(name)
End ForAda Grace
Running and testing
Run and Debug buttons are in the editor’s title bar on a .fb file. Run builds the open
file’s package, or the file alone when it’s in none, and runs it in the terminal. The Command
Palette has FutureBlitz: Run, FutureBlitz: Debug, and FutureBlitz: Run Tests.
Terminal > Run Task lists fb’s tasks: build, run, test, fmt, and check for the package, and all
but test for the open file. Their errors, a program’s panics, and a test’s failed Expects go to
the Problems view, linked to their lines. A task in tasks.json can pass options, like a release
build and the program’s arguments:
{
"type": "futureblitz",
"command": "run",
"release": true,
"programArgs": ["--verbose"]
}Debugging
Press F5 on a .fb file to build it in debug and run it under the debugger. Set breakpoints by
clicking left of a line’s number; the Variables view shows Strings, arrays, Maps, and objects as
their values. A launch.json configuration of type futureblitz sets the program’s arguments,
working folder, and environment:
{
"type": "futureblitz",
"request": "launch",
"name": "Debug greeter",
"program": "${workspaceFolder}",
"args": []
}What you learned
- The extension gives highlighting, the language server’s checks, and fb’s tasks in VS Code.
- Hover, completion, go to definition, references, and rename come from the language server.
- Saving formats the file in the standard style.
- Run, Debug, and Run Tests work on the open file’s package, and F5 starts the debugger.
The rules are in its page in Tools.
Report an issue with this page or with FutureBlitz on GitHub.