Highlight Dangerous Code


Every now and then, I forget some dangerous code added for debugging which leads to trouble later on. It would be great if you could maintain a pattern (regexp) for such dangerous code which would be highlighted strikingly in the editor no matter what the current scope is.

This could of course be fitted in the grammar of each language, but people are using different tools and techniques, so “one size fits all” doesn’t seem right.

Here’s an example to illustrate this:

In order to work on an individual test of a Ruby test suite (say Minitest or RSpec), you can temporarily add focus immediately before that test:

If you forget to remove it after debugging, the test suite runs without error and gives you green even if other tests fail.

A option such as “Preferences -> Editor” to define a pattern (regexp) which attaches an additional class e.g. .dangerous:

Highlight Dangerous Code
Code which matches this pattern is highlighted with class .dangerous.

/focus|binding\.pry|binding\.irb|# DANGEROUS/

(The .dangerous class can be decorated with colors fitting your template in styles.less.)

A possible result:

There is a package highlight-registered-keyword which does something similar, but it’s unmaintained since 2017, quite an overkill in terms of configuration and I didn’t get it to mark the defined keywords automatically at all.

In order to be useful, I guess such a feature has to be very easy to use: One setting in the editor preferences along with one style (either already part of your template or added by editing styles.less).

What do you think, could this be a useful feature worth of creating an issue for? Or is there maybe another way to define such custom highlighting easily without having to drill open core packages?

Cheers, -sven

Why not use todo-show package and add a pattern //DANGEROUS.

1 Like

It is a very good idea to build on the idea of the mentioned package.

@d_l The todo-show package searches for (configurable) TODO markers, but it does not handle the highlighting as stated in it’s README:

Attention: This package searches for todos. For todo word highlighting see language-todo.

The language-todo core package on the other hand cannot be customized at all. So unless I’m missing something here, this is not a feasible approach.

Consider making a copy of the package and modifying it to your taste.

Alternatively consider scripting for your init file.
As example a message can jump up if you save your code and some trigger words are in the code. You could possible also mark the code with decorations.

Install the package you found an see how/if it works.

All valid as plan B, however, I’m still wondering if others would find such a “highlight dangerous code” feature useful.

What @d_l suggests could look like this:

  • modified TODO language (more words & expanded style)
  • TODO show with small configuration (added words)

Instead of HACK you could use DANGEROUS, but it relies on you adding some word in the comment. If we would agree that it is a plan B, let us agree it is a B++.

It would be a nice plan, if todo-show handled the highlighting as well. But since that’s done by language-todo, you’d have to configure in two different places. Sorry, that’s a no for me, I’d rather create a new package from scratch than that.

Intuitively, this would be my approach (but then I would not add “dangerous” code myself).

[1] Install todo-show package (notwithstanding issues about lack of syntax highlighting)

[2] Ensure that all “dangerous” blocks of code are in a dedicated library directory.

[3] Apply separate highlight style to these imported blocks if required.

[4] These blocks of dangerous code to be imported/included/embedded into the main code block with each single line import statement having //DANGEROUS comment line preceding.

[5] There be a global toggle to enable/disable such imports/includes/embeds either individually or all. For this I would look to clone package-list package for ideas to list all such imports/includes/embeds. That is, create a custom dangerous-list package based on package-list.