Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I highlight code with ACE editor?

I'd like to syntax highlight more than a dozen small snippets of code and then make them editable with ACE Editor by clicking on them, since I think it would be much faster than setting up the full editor for each. I see there's a simple command for setting up an ACE editor:

<div id="editor">some text</div>
<script src="src/ace.js" type="text/javascript" charset="utf-8"></script>
<script>
window.onload = function() {
    var editor = ace.edit("editor");
};
</script>

Is there a simple way to call into the API to just highlight the text without setting up the editor? The ideal API would take in some text and return HTML with tags that could be used for highlighting. I'm aware there are specialized highlighting libraries for JavaScript, but I would like to try using the same highlighter both for text that's being displayed and text that's being edited.

like image 394
Benjamin Atkin Avatar asked Jan 12 '12 08:01

Benjamin Atkin


People also ask

How do I highlight code in notepad?

Open Notepad++ once again, go back to the language menu, and at the bottom between "Define your language" and "User-Defined" you should find "choicescript". Select it and and your code should automatically be highlighted.

How do I get an ace editor value?

To get the entered value from the ACE editor, we can use the getValue method. And to set the value of the editor, we can use the setValue method. Then we can write the following JavaScript code to get and set the editor value: const editor = ace.

What languages does ACE Editor support?

Editing documents with millions of lines as fast as small docs. Syntax highlighting in 45 languages including JavaScript, Java, C#, C, C++, Clojure, Go, Groovy, JSON, Scala, Ruby, XML, and others. Emacs, Vi key bindings. Support for TextMate themes.


1 Answers

Highlight the word:

var range = new Range(rowStart, columnStart, rowEnd, columnEnd);
var marker = editor.getSession().addMarker(range,"ace_selected_word", "text");

Remove the highlighted word:

editor.getSession().removeMarker(marker);

Highlight the line:

editor.getSession().addMarker(range,"ace_active_line","background");
like image 121
megas Avatar answered Sep 20 '22 18:09

megas