Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ace Code Editor Set Language Dynamically [closed]

I'm attempting to implement Ace Code Editor with a drop down to select the language. My drop down has an id of mode. I have got the editor to work correctly, but I cannot change the language using the drop down as I would like it to do. My current code is

var editor = ace.edit("code");
var textarea = $('textarea[name="code"]').hide();
editor.setTheme("ace/theme/textmate");
editor.getSession().setMode("ace/mode/sql");
editor.getSession().setValue(textarea.val());
editor.getSession().on('change', function(){
textarea.val(editor.getSession().getValue());
});

$('#mode').on('change', function(){
var newMode = $("mode").val();
editor.session().setMode({
    path: "ace/mode/" + newMode,
    v: Date.now()});
});

As above, this successfully launches the editor, however I can't change the language from SQL, which is the initial language. I came across this question Dynamically update syntax highlighting mode rules for the Ace Editor which is why I've included

v: Date.now()

but still no luck.

like image 638
ejdickerson Avatar asked Apr 24 '14 19:04

ejdickerson


People also ask

What is Aceeditor?

Founded in 1950, American Cinema Editors (ACE) is an honorary society of film editors that are voted in based on the qualities of professional achievements, their education of others, and their dedication to editing. Members use the post-nominal letters "ACE".

Is Ace editor open source?

Get the Open-Source Code Ace is a community project. We actively encourage and support contributions! The Ace source code is hosted on GitHub and released under the BSD license ‐ very simple and friendly to all kinds of projects, whether open-source or not.

What is ace in coding?

The Adaptive Communication Environment (ACE) is an open source software framework used for network programming. It provides a set of object-oriented C++ classes designed to help address the inherent complexities and challenges in network programming by preventing common errors.


2 Answers

you have a typo in the editor.session().setMode({ line

use editor.session.setMode("ace/mode/" + newMode) instead of it.

v: Date.now() was needed to disable caching, you most likely do not need that.

like image 195
a user Avatar answered Nov 17 '22 02:11

a user


This is correct editor.getSession().setMode("ace/mode/" + newMode);

But instead you type editor.session instead of editor.getSession()

like image 27
Divyank Avatar answered Nov 17 '22 01:11

Divyank