Code Monkey home page Code Monkey logo

rust-test-highlight's Introduction


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

rust-test-highlight's People

Contributors

andrewbrey avatar

Stargazers

 avatar

Watchers

 avatar

rust-test-highlight's Issues

Highlight is retained for range after test module no longer occupies it

If you open a file that has a test module which gets highlighted, then remove the test module and instead put different code in the same editor range that was previously just highlighted, the editor decorations (highlights) are retained.

Workaround: Closing and re-opening the file clears the highlight

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.