Possible to determine syntax highlighting token type at current editor position?

Is is possible, in a JavaScript macro in Komodo, to determine the syntax token type (e.g., M_DEFAULT, M_STRING or the SCE_ equivalents) at the current editor position? If so, how?

Hi, you can use something like this:

var scimoz = require('ko/editor').scimoz();
if (scimoz.getStyleAt(pos) == scimoz.SCE_UDL_M_STRING) {
  // Do something

Note the SCE_UDL_ prefix before a style constant name.

Ah, thanks @mitchell. That looks like what I’m after.

Is the scimoz object in that example the same scimoz object that is described in the current Komodo help? I haven’t seen a reference to the getStyleAt() method before; is it documented somewhere? It may be and I’ve probably just missed it.

Seems like not. Searching on Komodo Docs gives me only 2 matches, so I think it’s not documented atm.

Is that version of the style constants documented somewhere? I ask because they don’t seem to match the values in KomodoEdit/src/udl/ludditelib/constants.py.

I threw together a quick macro to play with this:

try {
	var e = require('ko/editor');
	var s = e.getSelection();
	var p = e.getCursorPosition('absolute');
	var t = e.scimoz().getStyleAt(p);
	alert('[selection]@position(type): [' + s + ']@' + p + '(' + t + ')');
catch(e) {
	ko.dialogs.internalError(e, "Error: " + e);

For instance, if I have the word “absolute” selected and run the macro on its own source, it alerts with the appropriate selection and position, but it returns a 7 for the style. In the above-linked version of the SCE_UDL_ constants, 7 is SCE_UDL_M_EMP_TAGC.

Just to quickly interject, this addon may be of use to you:


While not documented, Komodo uses the Scintilla editing component behind the scenes. There is a logical mapping between Scintilla API functions (http://scintilla.org/ScintillaDoc.html) and Komodo’s scimoz object functions and properties. We recommend sticking with the 'ko/editor' API, but when it’s not powerful enough, you can fall back on the scimoz() object.

When editing macros in the editor, the Javascript lexer is used, which is not a UDL-based lexer. It is based on Scintilla’s C++ lexer whose constants start wit the SCE_C_ prefix. In your case, 7 matches scimoz.SCE_C_CHARACTER (https://github.com/Komodo/KomodoEdit/blob/master/contrib/scintilla/include/SciLexer.h#L159), due to the single quote characters. If absolute were in double quotes, style 6 would be returned (SCE_C_STRING).

For a given lexer you can usually find it in Komodo’s src/languages/*.py files. Javascript for example is here: https://github.com/Komodo/KomodoEdit/blob/master/src/languages/koJavaScriptLanguage.py#L53. That is how we know it uses the CPP lexer and not the UDL lexer.

I hope this helps.

@mitchell Thanks! That does help; makes much more sense, given that background.