Search code examples
javascriptace-editor

determine language (mode) by filepath in ace


I'm using the Ace-Editor to modify several different files. In order to determine the correct mode, I wrote a simple switch, which looks like this:

function getMode(path){
    switch( getFileExtension(path) ){
        case "html": return "html";
        case "htm":  return "html";
        case "css":  return "css";
        case "bat":  return "batchfile";
        ...
        default: return "text";
    }
}

However, to cover all supported languages in ace, I would have to frequently update this list.

It would be nice, if Ace could tell me which mode is the right one, and so I found function getModeForPath(path) in ace: https://github.com/ajaxorg/ace/blob/master/lib/ace/ext/modelist.js#L11

How can I call and use this function in my own code? Or how is this function supposed to be used?

Edit:

What I'm trying to do is, to make a small dropdown-box where the user can select the language. And the default-language is determined by the filepath. (Similar to sublime)


Solution

  • If you are using prebuilt version from ace-builds repository, include src/ext-modelist.js file with ace, then do

    var modelist = require("ace/ext/modelist")
    modelist.getModeForPath(path)
    

    see also https://github.com/ajaxorg/ace/blob/v1.1.5/demo/kitchen-sink/demo.js#L306-L311