最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to set a Java mode in CodeMirror editor? - Stack Overflow

programmeradmin4浏览0评论

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
});

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
});
Share edited Dec 14, 2016 at 9:33 Narasimham431 asked Dec 14, 2016 at 5:55 Narasimham431Narasimham431 331 silver badge5 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

"text/x-java" is the MIME type used to get the Java mode.

(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).

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./ajax/libs/codemirror/5.58.3/codemirror.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare./ajax/libs/codemirror/5.58.3/theme/ambiance.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare./ajax/libs/codemirror/5.58.3/codemirror.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/codemirror/5.58.3/mode/clike/clike.min.js"></script>

<textarea id="codepane">
  private class InnerClass {
    public int zero() {
      return 0;
    }
  }
</textarea>

发布评论

评论列表(0)

  1. 暂无评论