Help with udl
-
I believe this has been already posted, but I cannot find what I’m looking for.
I want to be able to have a second C++ language, (autocomplete and everything like C++) but that has an extra styler with specific new keywords. Can I add this directly to langs.xml or do I create a file in the UDL folder? Whatever I do, how do I go about formatting it?
Thanks! -
Can I add this directly to langs.xml or do I create a file in the UDL folder?
Neither.
You cannot add a new style category to a built-in lexer. However, the C++ lexer has user-defined-keywords available to both INSTRUCTION WORD and TYPE WORD (which you can set in the Style Configurator).
And for a UDL, you would have to start from scratch, so to have an “extra” category, you’d have to manually create a new UDL (Language > User Defined Language > Define Your Language > Create New…), then enter the keywords and styling information for each of the different keyword types yourself (it cannot inherit those from the C++ lexer), and try to define the commenting and folding requirements. However, if you go down the UDL path, you should know that the UDL system is not as powerful as the built-in lexers, so you will not be able to 100% replicate the behavior of the C++ in the UDL system, so you are likely to be disappointed. And since you also wanted autocomplete (as you explicitly noted), and probably FunctionList definition (as your “everything like C++” implied), you would also have to read the user manual and figure out how to replicate those to your UDL (that’s more doable than some aspects, but it would take effort on your part).
Given your description, I think your best bet is to use the EnhanceAnyLexer plugin: with it, you can open a C++ file, use Plugins > EnhanceAnyLexer > Enhance current language, it will create a
[c++]
section in its configuration file that will start something like this:[c++] ; Each 3-digit number is styled with the color 0xff0050. ; Matches styled by IDs 3 and 6 are recolored. 0xff0050[3, 6] = \d{3} ; Each word "test" is styled with the color 0x00bb00 ; but only if the matches are not already styled by one of the IDs in excluded_styles. 0x00bb00 = test ; check in the respective styler xml if the following IDs are valid excluded_styles = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,20,21,22,23
You could then define a new foreground color (maybe bright pink
#FF00FF
) for your list of keywords, and use a regex to give the list of keywords. For example, if you had keywordsmykwSpecialType
andmykwSuperFunction
, you could create a regex that finds text that matches either (with a word boundary on each side, so thatmykwSpecialType
would not matchTommykwSpecialTypeHere
):[c++] ; Each word mykwSpecialType and mykwSuperFunction are styled with the color 0xFF00FF, ; but only if the matches are not already styled by one of the IDs in excluded_styles. 0xFF00FF = \b(mykwSpecialType|mykwSuperFunction)\b ; since C++ uses style#11 for "default" text (see name="cpp" section of stylers.xml) ; you would have to remove 11 from the list of excluded_styles excluded_styles = 1,2,3,4,5,6,7,8,9,10,12,13,14,15,16,20,21,22,23
If you have more keywords, just use more
|anotherKeyword
in the alternation list in that regex.My guess is that this will come the closest to what you want: it will keep all the benefits of the C++ lexer, but allow you to define one or more new style categories, where you get to define the desired extra keywords and their foreground color.
-
@PeterJones Sounds like the only way to do it, using the plugin. Thank you. Perhaps would you know where I can find a comprehensive regex guide? Would be useful for other things too.
-
@Monospace-V said in Help with udl:
Perhaps would you know where I can find a comprehensive regex guide
We’ve got a whole FAQ on that: “Where to find REGular EXpressions (RegEx) documentation ?”