📜 ⬆️ ⬇️

Syntax highlighting for own language

image Syntax highlighting - the task is simple and has been solved many times. But it has one unpleasant feature - if we want to highlight the syntax of a new language (for example, the bb-tags language of a habra editor, or which program's log), then most of the solutions include creating a grammar, a parser, and then embedding it all somewhere. And what to do if you get a highlight for the logs, the desire to eat, but to waste three hours of desire is not?


Suitable text editor


Before coloring the text you need to select a text editor in which we will do it. Text editors we have the sea, for every taste and color. What do we need? Something simple, of course free, so that it works under any operating system so that the text can be conveniently edited. And of course, so that embedding your syntax highlighting doesn’t turn into recompiling the hardcore C code with PEG grammar embedded there :). It is not strange, even with such a requirement of text editors a lot. Choose scite, because I know him best :). Under windows, download from here , under MacOS and * NIX, you can directly from the repository. Install, run, we get about the following:

editor
')

Connect to the internal editor


As we agreed at the beginning, we will not compile anything - this is a long, dreary business, and the “paint in 15 minutes” paradigm does not fit. How to get to the guts of a text editor without recompiling it? To do this, the editor must either support the scripts, or give out the interface of interprocess communication like COM. Scite scripts. They are written in the lua language, which is simple as three rubles in rubles and is mastered in ten minutes. Almost basic :). And connect it to scite too:
  1. Create where it is convenient for us text file scite.lua (the name can be any).
  2. In scite, select options -> 'open global options file'.
  3. In the settings file that opens, write the full path to script.lua as follows:
    editor
    Note that there are no spaces before and after the '=', this is important.
  4. Reboot scite.

Hello world


Now every time you run scite, it will automatically load and execute the specified script. Let's check that everything works in the classic way - we will add a command to scite.lua, which displays the well-known text in scite:

hello
If we now run scite, we will see the result:

result
In the special window that appears, which is proudly referred to as the 'output window', the text that we specified in the script will be displayed. Using the print () commands in the script and the output window, you can quite easily debug the scripts and output the information we need - for example, the number of emoticons in our article :).

Preparing for painting


Making sure to successfully connect to the inside of a text editor, you can proceed to painting. For a successful painting, we will need to use the ioc principle and the script to specify scite so that it consults with us every time the text is updated. To do this, simply set a function with a special name:

function OnUpdateUI()
print( "text update" )
end


This function will be called whenever the text in the text editor window changes. The following picture shows what happens if you run the test editor and enter 'a' - we are provided with mass calls to the handler function:
update log

We paint


In general, everything is ready: there is a function that will be automatically called every time the text changes. You can paint. The most valuable thing in scite is that it is an editor demonstrating the work of scintilla, a library for creating text editing windows. And the library, by the way, is popular - Komodo Edit, wxWidgets, GTK use just her for text editing. And this library has a regular mechanism for interacting with everything and everyone, SCT_ messages. Detailed documentation on the website will lucidly explain exactly which messages need to be sent and in what sequence, so that the text editor can get the text out of it, colorize the text, emphasize the words we need and place beautiful marks on the margins. In particular, in order to color the text, it is enough to send two messages:

The following code will color every second character in some color:

function OnUpdateUI()
text = editor:textrange( 0, editor.Length )
for i = 1, string.len( text ), 2 do
scite.SendEditor( SCI_STARTSTYLING, i, 31 )
scite.SendEditor( SCI_SETSTYLING, 1, 5 )
end
end


Pay attention to a couple of nuances: '31' is a mask for a style number, just some kind of magic number that you should always pass to this function. '5' is the color number. By default, scite matches each number to some color. For our example, the result will look ... strange:

update log

Nuances


As you can see, nothing complicated. Spending 10 minutes on a brief introduction to lua and armed with a list of messages to send scite you can do a lot of unnecessary and useless things with the text :). What else can you pay attention to:

All the holiday!

Source: https://habr.com/ru/post/59317/


All Articles