[英]CodeMirror editor: show hint after specific key pattern like @@
I am using CodeMirror as editor with show-hint.js
and anyword-hint.js
as example.我使用CodeMirror作为编辑器,以show-hint.js
和anyword-hint.js
为例。 By default, hints appear after pressing Ctrl-Space
.默认情况下,按Ctrl-Space
后会出现提示。
Problem:问题:
I want CodeMirror to show hints after I type @@
along with Ctrl-Space
.我希望 CodeMirror 在我输入@@
和Ctrl-Space
后显示提示。
What I have tried already:我已经尝试过的:
I tried adding extraKeys
like "'@'": "autocomplete"
and "'@'-'@'": "autocomplete"
, but it does not work.我尝试添加像"'@'": "autocomplete"
和"'@'-'@'": "autocomplete"
这样的extraKeys
,但它不起作用。 It works with a single @
, but not with @@
.它适用于单个@
,但不适用于@@
。
HTML: (save as .html) HTML:(另存为 .html)
<!doctype html>
<title>CodeMirror: Any Word Completion Demo</title>
<meta charset="utf-8" />
<link rel=stylesheet href="https://codemirror.net/doc/docs.css">
<link rel="stylesheet" href="https://codemirror.net/lib/codemirror.css">
<link rel="stylesheet" href="https://codemirror.net/addon/hint/show-hint.css">
<script src="https://codemirror.net/lib/codemirror.js"></script>
<script src="https://codemirror.net/addon/hint/show-hint.js"></script>
<script src="https://codemirror.net/addon/hint/anyword-hint.js"></script>
<script src="https://codemirror.net/mode/javascript/javascript.js"></script>
<article>
<h2>Any Word Completion Demo</h2>
<form>
<textarea id="code" name="code">
function isInt(n) { return n % 1 === 0; }
</textarea>
</form>
<script>
CodeMirror.commands.autocomplete = function(cm) {
cm.showHint({
hint: CodeMirror.hint.anyword
});
};
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
lineNumbers: true,
extraKeys: {
"Ctrl-Space": "autocomplete",
"'@'": "autocomplete",
}
});
</script>
</article>
I solved it like this.If there is a better suggestion , please post.我是这样解决的。如果有更好的建议,请发表。
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
lineNumbers: true,
extraKeys: {
"Ctrl-Space": "autocomplete",
"'@'": function(cm) {
var charTomatch = '@';
var curretCursorPosition = cm.getCursor();
var backwardCursorPosition = {
line: curretCursorPosition.line,
ch: curretCursorPosition.ch - 1
};
var forwardCursorPosition = {
line: curretCursorPosition.line,
ch: curretCursorPosition.ch + 1
};
var backwardCharacter = cm.getRange(backwardCursorPosition, curretCursorPosition);
var forwardCharacter = cm.getRange(curretCursorPosition, forwardCursorPosition);
//update text anyway
cm.replaceRange(charTomatch, curretCursorPosition);
//
if (backwardCharacter === charTomatch || forwardCharacter === charTomatch) {
CodeMirror.commands.autocomplete(cm);
}
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.