Sublime Forum

Hiding Characters with Syntax Highlighting

#1

I would like to use custom syntax highlighting to hide/remove certain characters, is this possible?

So:
Foo[something]: bar

Becomes:
Foo: bar

0 Likes

#2

As the name says highlighting is about colors. It can’t remove text. The only thing you could do was to apply a special scope to [something] and create your own color scheme which addresses this scope to apply background color to it.

0 Likes

#3

Thank you for your fast reply!
Do you have any idea how I would go about solving my problem? I’m thinking about a automatic replace-programme that runs in the background and deletes the unwanted code. Is something like that more realistic?

0 Likes

#4

If you remove the code from the buffer, your syntax has to be able to still highlight everything else correctly without it being there, which means that your syntax will highlight things as valid that might not be. That may or may not be an issue though, depending on what you’re trying to do.\

Either way you probably want your syntax to give some sort of explicit scope to the parts that you want to remove away (if it doesn’t already have such). That will allow you to use view.find_by_selector() to quickly get a list of all of the bits that you want to do something with.

Aside of that, if you want to do something like this on the fly you would also need something like an event handler for on_modified so that you can detect when the buffer is changing and check if you need to remove anything that was just added.

You may want to investigate view.fold() as a potential solution. With that you could fold away the parts that you want to hide, which may be enough. You would still have a fold character in that location though,

0 Likes

#5

As an example of the view.fold() approach, you can see this in action: Open an HTML file, and select the Edit/Code Folding/Fold Tag Attributes menu item

0 Likes