CodeMirror electricInput 不匹配具有前导空格的表达式
CodeMirror electricInput does not match expression with leading whitespace
在我的 CodeMirror 自定义模式中,我希望当用户键入以单词 bank
开头的行(带有可选的前导空格)时触发 electricInput 事件。
我的 electricInput 是这样设置的:electricInput: /\s*bank$/i
当用户在行首键入 bank
时,事件确实会触发。当单词 bank
前有空格时,它不会触发。为什么?
(RegEx 似乎没问题。我在该模式下有一个语法规则,具有相同的 RegEx,它与预期的标记匹配,不管前导空格如何:
CodeMirror.defineSimpleMode("myMode", {
start: [
{regex: /\s*bank$/i, token: 'bank', sol: true, indent: true}
感谢 Marijn 在 CodeMirror 讨论论坛上的热心帮助,我得以解决这个问题:自定义 indent
函数需要传递给 defineSimpleMode
。然后,我们仍然需要设置 electricInput
(否则在键入 bank
时不会调用缩进函数)。但不需要 onElectricInput
的事件处理程序。
CodeMirror.defineSimpleMode("myMode", {
start: [
...
],
meta: {
electricInput: /\s*bank$/i,
indent: function (state, textAfter, line) {
if (textAfter.substring(0, 4).toLowerCase() === 'bank') return 0
return 2;
}
}
});
在我的 CodeMirror 自定义模式中,我希望当用户键入以单词 bank
开头的行(带有可选的前导空格)时触发 electricInput 事件。
我的 electricInput 是这样设置的:electricInput: /\s*bank$/i
当用户在行首键入 bank
时,事件确实会触发。当单词 bank
前有空格时,它不会触发。为什么?
(RegEx 似乎没问题。我在该模式下有一个语法规则,具有相同的 RegEx,它与预期的标记匹配,不管前导空格如何:
CodeMirror.defineSimpleMode("myMode", {
start: [
{regex: /\s*bank$/i, token: 'bank', sol: true, indent: true}
感谢 Marijn 在 CodeMirror 讨论论坛上的热心帮助,我得以解决这个问题:自定义 indent
函数需要传递给 defineSimpleMode
。然后,我们仍然需要设置 electricInput
(否则在键入 bank
时不会调用缩进函数)。但不需要 onElectricInput
的事件处理程序。
CodeMirror.defineSimpleMode("myMode", {
start: [
...
],
meta: {
electricInput: /\s*bank$/i,
indent: function (state, textAfter, line) {
if (textAfter.substring(0, 4).toLowerCase() === 'bank') return 0
return 2;
}
}
});