-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
Comments
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. editor.session.bgTokenizer.start(0) The reason |
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? |
I meant something like this https://jsbin.com/taniqikagi/edit?html,js,output |
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? |
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? |
I'd need syntax aware highlighting. |
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. |
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: So the refined question is: |
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 |
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! |
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?
The text was updated successfully, but these errors were encountered: