Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic syntax highlighting #3905

Closed
Woofthecat opened this issue Mar 12, 2019 · 10 comments
Closed

Dynamic syntax highlighting #3905

Woofthecat opened this issue Mar 12, 2019 · 10 comments

Comments

@Woofthecat
Copy link

Hi,

I'd like to allow the users to add a list of keywords of their own if it's needed (as an extra set of keywords next to the available set of keywords of the selected mode) - therefore I have no idea what the user defined keywords are going to be. I've already figured out how to add custom values to the autocompletion (via creating a completer and using langTools.addCompleter()), and that's working fine. The problem is that this way those certain keywords remain without syntax highlighting.

I've been searching a lot but I cannot find a solution. Is this something that's supported? If not, is it possible to introduce a dynamic syntax highlighting feature?

@nightwing
Copy link
Member

nightwing commented Mar 12, 2019

This can be achieved for most of the modes by modifying createKeywordMapper before any session is created https://github.com/ajaxorg/ace/blob/master/lib/ace/mode/text_highlight_rules.js#L210.

var TextHighlightRules = ace.require("ace/mode/text_highlight_rules").TextHighlightRules
TextHighlightRules.prototype.createKeywordMapper = function(
     map, defaultToken, ignoreCase, splitChar
) {
    var keywords  = this.$keywords = Object.create(null);

After this new keywords can be added like this:

editor.session.$mode.$highlightRules.$keywords["foo"] = "variable.language"

The modification will be applied to all editors in the page using that mode.
To trigger redraw of specific editor after the mode is changed use

editor.session.bgTokenizer.start(0)

The reason this.$keywords is not assigned by default is that on old browsers, keeping it in the closure was making the function noticeably faster (maybe this is fixed on modern browsers).

@Woofthecat
Copy link
Author

Thanks for your reply! I've tried to get it working but I think I misunderstood you. Is this something I'm supposed to modify in the source code itself?

@nightwing
Copy link
Member

I meant something like this https://jsbin.com/taniqikagi/edit?html,js,output
For which language mode do you want to use this?

@Woofthecat
Copy link
Author

Ah okay, I was testing it against text and pascal modes, but it won't work with them.

The ideal situation would be if I could add syntax highlighting to a set of keywords, no matter what the language mode is. Would that be possible?

@nightwing
Copy link
Member

Do you want syntax aware highlighting, that is to not highlight the word if it is inside a string or a comment, or highlighting all instances of the word?

@Woofthecat
Copy link
Author

I'd need syntax aware highlighting.

@YellowAfterlife
Copy link
Contributor

The above advice is correct - if you insert a keywordMapper (or a custom rule that maps keywords through a function), you can later add/remove key-value pairs to it to register/unregister keywords.

I'm actively using this in GMEdit for context-specific highlighting (local definitions > project-specific definitions > native definitions).

If you need row/column data, see #3500 for what you'd need to tweak.

@Woofthecat
Copy link
Author

But wouldn't that require me to modify the source code and update every single mode highlight rule that does not have a keywordMapper? Based on the small jsbin demo that @nightwing provided and taking a quick look at the source code, the only difference between for example Pascal and Java is that java_highlight_rules has a keywordMapper that's used in the rules: var keywordMapper = this.createKeywordMapper(...);

So the refined question is:
Without modifying the source code, can I achieve syntax aware highlighting for a set of keywords that are user defined (meaning I don't know them beforehand)? I'd like this to work no matter what language mode is selected.

@nightwing
Copy link
Member

Unfortunately i do not see a universal way to do this for all modes, but most of the modes already use keywordMapper so a possible solution is to manually update the ones that are not using it yet #3911

@Woofthecat
Copy link
Author

Sorry I had many projects going on so I forgot to reply. I see how that could be a solution, but I assume it also depends on how many modes are missing the keywordMapper.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants