Skip to content

Latest commit

 

History

History
115 lines (84 loc) · 4.82 KB

README.md

File metadata and controls

115 lines (84 loc) · 4.82 KB


logo image for the rust test highlight vscode extension depicting a cartoon crab working at a computer terminal with some of the code it is typing displayed in the background, partially highlighted so that the bottom half of it stands out more than the top half
Rust Test Highlight

Highlight your inline Rust tests with a customizable background color so they stand out in your editor.

Visual Studio Marketplace Downloads GitHub License


Demo

Screenshot Demo

Demonstration of code highlighting for an inline "tests" module in a rust file. There is source code displayed, and the code for the inline tests has a lighter background color than the rest of the file.

Video Demo

demo.mp4

Motivation and How it Works

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.

Examples Of Highlighting Scenarios

// 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() {}

Installation

Install using Command Palette

  1. Go to View -> Command Palette or press Ctrl+Shift+P
  2. Then enter "Install Extension
  3. Enter "Rust Test Highlight"
  4. Select it or press Enter to install

Customizing Highlight Color

// 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.

License

MIT License