Highlight your inline Rust tests with a customizable background color so they stand out in your editor.
Screenshot Demo
Video Demo
demo.mp4
Inline test modules in Rust are usually pretty great, but when working in large files it is pretty easy to lose track of where your test code starts and your application code ends. It's not that hard to scan around for the nearest #[test]
annotation, but the fact is that it does often take some scanning to re-obtain your bearings as you fly around your code.
That's why I created the Rust Test Highlight
extension to automatically apply a customizable background color to inline test modules. That way, as you zip through your file you'll be able to quickly distinguish between your test code and your code under test.
Once installed, the extension will automatically activate whenever you open a file with the rust
language mode, and apply your chosen highlight color (or the defaults if you don't care to customize it) to the inline tests
module in your open editors.
As of now, this is the extent of the test code that is highlighted, and free #[test]
functions that are not part of a mod tests
are not highlighted. This is for the simple reason that using a tests
module is recommended by the Rust documentation. Further, integration tests (or in general, tests which aren't part of an inline module and are in a file that is already well-identified as being test code because the file is called, for example test.rs
) are not highlighted. In short, the goal of this extension is to make it easier to spot inline tests, and it encourages the use of the tests
module convention when doing so.
Note - your Rust code is transformed into an AST by the syn crate compiled to WebAssembly. This AST is then introspected to find the
tests
module and determine the editor range that should be highlighted. This isn't need to know information, I just think it's neat.
// add.rs
pub fn add() {}
// ✅ the following code would be highlighted
#[cfg(test)]
mod tests {
#[test]
fn test_add() {}
}
// add.rs
pub fn add() {}
// ❌ the following code **would not** be highlighted
// (wrong test module name)
#[cfg(test)]
mod unit_tests {
#[test]
fn test_add() {}
}
// add.rs
pub fn add() {}
// ❌ the following code **would not** be highlighted
// (test not part of a "tests" module)
#[test]
fn test_add() {}
// test.rs
use crate::add;
// ❌ the following code **would not** be highlighted
// (test not part of a "tests" module and well
// identified as a test by the file it's in)
#[test]
fn test_add() {}
Install using Command Palette
- Go to View -> Command Palette or press Ctrl+Shift+P
- Then enter "Install Extension
- Enter "Rust Test Highlight"
- Select it or press Enter to install
// user `settings.json` (or in the workspace .vscode/settings.json)
"workbench.colorCustomizations": {
"rustTestHighlight.backgroundColor": "#2813491c"
}
Note that any color recognized as a color by VS Code's settings parsing will work, so rgb
, rgba
, hex
and hex with alpha
all will work just fine, and it's recommended that you do include an alpha value (ideally, a pretty low one) so that the highlight doesn't interfere with other decorations, color themes, or syntax highlighting.