使用simpleHint的Codemirror 3复用模式 [英] Codemirror 3 multiplexing modes using simpleHint
问题描述
因此,我正在尝试制作一个SCXML编辑器,该编辑器基本上是带有JavaScript块的XML(状态机)。我已经接近了,但是无法添加提示。似乎可以归结为我不知道要提示的编辑模式。我在CodeMirror对象中寻找了线索,但没有看到。我正在像这样进行多路复用:
So I'm trying to make an SCXML editor which is basically XML (state machine) with JavaScript blocks in it. I'm close, but I'm having trouble adding hints. It seems to boil down to I don't know the editing mode I'm in when it comes time to hint. I've looked in the CodeMirror object for clues but I'm not seeing it. I'm doing the multiplexing like so:
CodeMirror.defineMode("scxml", function (config) {
return CodeMirror.multiplexingMode(
CodeMirror.getMode(config, "text/xml"),
{
open: "<script>", close: "</script>",
mode: CodeMirror.getMode(config, "text/javascript"),
delimStyle: "delimit"
}
);
});
editorXml = CodeMirror.fromTextArea(document.getElementById("editXmlFile"), {
lineNumbers: true,
mode: 'scxml',
indentUnit: 4,
autoCloseTags: true,
matchBrackets: true,
extraKeys: {
"'>'": function (cm) { cm.closeTag(cm, '>'); },
"'/'": function (cm) { cm.closeTag(cm, '/'); },
"' '": function (cm) { CodeMirror.xmlHint(cm, ' '); },
"'<'": function (cm) { CodeMirror.xmlHint(cm, '<'); },
"Ctrl-Space": function (cm) { CodeMirror.xmlHint(cm, ''); }
}
});
请注意 extraKeys
中的XML提示正在工作,如何在其中获取JavaScript提示?从JavaScript提示帮助中,看来我可以执行以下操作:
Note in the extraKeys
where the XML hinting is working, how do I get the JavaScript hinting in there? From the JavaScript hinting help, it appears I'd do something along the lines of:
CodeMirror.commands.autocomplete = function(cm) {
CodeMirror.simpleHint(cm, CodeMirror.javascriptHint);
}
... extraKeys: {"Ctrl-Space": "autocomplete"} ...
但是无论哪种方式,我都需要知道我所处的模式(XML或JavaScript)才能知道使用 simpleHint
与 xmlHint
。有人知道怎么做吗?
But either way, I need to know the mode I'm in (XML or JavaScript) to know to use simpleHint
versus xmlHint
. Anyone know how this might be done?
编辑: cm.getMode()。name
和 cm.getOption('mode')
当我在任一部分中时,只返回 scxml
cm.getMode().name
and cm.getOption('mode')
just return scxml
when I'm in either section
谢谢!
推荐答案
我认为您应该可以在 CodeMirror.innerMode上调度(cm.getMode(),cm.getTokenAt(POS).state).mode.name
,其中 POS
是 {line,ch}
您感兴趣的职位。它将返回一个名称,例如 xml
或 javascript
,描述该位置的内部模式。
I think you should be able to dispatch on CodeMirror.innerMode(cm.getMode(), cm.getTokenAt(POS).state).mode.name
, where POS
is the {line, ch}
position that you're interested in. It will return a name like "xml"
or "javascript"
, describing the inner mode at that position.
这篇关于使用simpleHint的Codemirror 3复用模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!