Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically pick a "mode" for Ace Editor, given a file extension

I'm working on a project that uses a java/scala backend (Lift, to be precise, though that shouldn't affect this question), and as part of the frontend we use Ace Editor. I've been googling for a while and have yet to find an answer to this question:

Given a file extension (e.g. js, c, cpp, h, java, rb, etc), how can I automatically pick an Ace "mode" for the appropriate language?

I'm hoping to avoid manually creating a map, a la js -> javascript, c -> c_cpp, java -> java. Is there a java/scala library available for this? Or better yet, does Ace have this functionality built in somehow?

like image 937
Dylan Avatar asked Apr 09 '13 13:04

Dylan


People also ask

How do I change the mode in Ace editor?

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

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

Ace now provides modelist extension to do this.

var modelist = ace.require("ace/ext/modelist")
var filePath = "blahblah/weee/some.js"
var mode = modelist.getModeForPath(filePath).mode
editor.session.setMode(mode) // mode now contains "ace/mode/javascript".

Note that if you are using prebuilt version of ace you need to include ace.js and ext-modelist.js files in your page.
With source version, you need to replace ace.require with require and require.js will load all dependencies automatically.

See https://github.com/ajaxorg/ace/blob/master/demo/modelist.html and https://github.com/ajaxorg/ace-builds/blob/master/demo/modelist.html for examples of how to use it

like image 188
a user Avatar answered Sep 22 '22 14:09

a user