CodeMirror - automatic language detection

I use standalone selection (not an editor), something like this: http://codemirror.net/demo/runmode.html

How can I automatically determine the language that will be used when working with backlight?

There is a mod-autostart demo here: http://codemirror.net/demo/changemode.html

But I do not know how I could adapt this to work with Codemirror.runMode (). I want to highlight the entire block of code using an automatically detected circuit.

+6
source share
1 answer

The "changemode" demonstration can only distinguish between the programming language "Schema" and "everything else", see implementation

function looksLikeScheme(code) { return !/^\s*\(\s*function\b/.test(code) && /^\s*[;\(]/.test(code); } 

Thus, this will not work for automatic detection of other programming languages, and, as demo pages show, even for Scheme detection it is very rude.

Unfortunately, it is not easy to automatically detect a wide range of programming languages ​​from a source code fragment (for small fragments, the same syntax can even be used in different programming languages). However, a reasonable approach is suggested in Detecting a programming language from a fragment .

+3
source

Source: https://habr.com/ru/post/906491/


All Articles