Reputation: 33
I have read CodeMirror User Manual, but I couldn't find how to set mode for JAVA, could you help me?
CodeMirror.fromTextArea(document.getElementById("code1"), {
lineNumbers: true,
mode: "text/x-csrc",
matchBrackets: true
});
Upvotes: 3
Views: 5718
Reputation: 2701
In the latest version of codemirror you can't find the path for java mode, instead you can use c-like mode js file for your purpose.
CodeMirror.fromTextArea(document.getElementById("codepane"), {
mode: "text/x-java",
indentWithTabs: true,
smartIndent: true,
lineNumbers: true,
lineWrapping: true,
matchBrackets: true,
autofocus: true,
theme: "ambiance",
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.58.3/codemirror.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.58.3/theme/ambiance.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.58.3/codemirror.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.58.3/mode/clike/clike.min.js"></script>
<textarea id="codepane">
private class InnerClass {
public int zero() {
return 0;
}
}
</textarea>
Upvotes: 0
Reputation: 2187
(I know this is answered but I wanted to leave this here for anyone else with the same question we had!)
Each mode lives in a subdirectory of the mode/ directory, and typically defines a single JavaScript file that implements the mode. Loading such file will make the language available to CodeMirror through the mode option, which you declare while creating your CodeMirror instance:
CodeMirror.fromTextArea(document.getElementById("code1"), {
lineNumbers: true,
mode: "text/x-java",
matchBrackets: true
});
You'll need to ensure your different mode files are added to a mode folder in your library. In your case the java.js file needs to be in a new folder called lib/mode, with a filepath of lib/mode/java.js.
You can inspect each mode's demo page to see what string you must pass to the mode:
option in order for it to be called. Here's the java demo which also defines all of the "MIME types defined" at the bottom (basically the strings you can use for different java syntaxes).
Upvotes: 2