简体   繁体   English

Codemirror内部模式自动缩进问题

[英]codemirror inner mode auto indentation problems

I'm having some trouble getting codemirror to apply the correct autoindentation to inner modes in a mixed mode. 我在使Codemirror在混合模式下将正确的自动缩进应用于内部模式时遇到了一些麻烦。

You can see a live version of the mode (and how it's not working) here: https://extremely-alpha.iodide.io/notebooks/216/ but in short the idea is to be able to use matlab style block delimiters to switch between languages like this: 您可以在此处查看该模式的实时版本(及其运行方式): https : //extremely-alpha.iodide.io/notebooks/216/,但总之,我们的想法是能够使用matlab样式块定界符来在这样的语言之间切换:

%% js
[1,2,3].forEach(i => {
  console.log(i)
})

%% py
for i in range(5):
    for j in range(10):
        print i+j

%% css
div#foo {
    border: 1px solid pink
}

As you can see from my example link, the syntax highlighting works ok, but you'll also notice that the indentation is not working as desired. 从我的示例链接可以看到,语法突出显示可以正常工作,但是您还会注意到缩进不能按需工作。

The code for this codemirror mode is here on github . 此代码镜像模式的代码在github上 It is very much based on codemirror's html mixed mode . 它非常基于codemirror的html混合模式

I tried adding copyState to my code, again following the html mixed mode -- 我尝试按照HTML混合模式再次将copyState添加到代码中-

copyState: state => {
    let local;
    if (state.localState) {
      console.log("state.localState copied");
      local = CodeMirror.copyState(state.localMode, state.localState);
    }
    return {
      token: state.token,
      localMode: state.localMode,
      localState: local
    };
  },

-- but this results in a different kind of weird indentation behavior, and doesn't end up working. -但这会导致另一种奇怪的压痕行为,并且最终无法正常工作。

I've been banging my head against this for quite some time, and I haven't been able to piece it together via google, api docs and forums, so any help would be greatly appreciated! 我已经为此花费了很长时间,而且我还无法通过google,api文档和论坛将其拼凑起来,因此,任何帮助将不胜感激! Thank you! 谢谢!

in case anyone comes across this problem in the future: it turns out codemirror modes do not typically come with sensible defaults built in, or at least they are not loaded by default when you use CodeMirror.getMode(...) . 以防将来有人遇到此问题:事实证明Codemirror模式通常没有内置明智的默认值,或者至少在使用CodeMirror.getMode(...)时默认情况下未加载它们。 In my case, I had to change from 就我而言,我不得不从

const innerModes = {
  js: CodeMirror.getMode({}, { name: "javascript" }),
  py: CodeMirror.getMode({}, { name: "python" }),
  md: CodeMirror.getMode({}, { name: "markdown" }),
  css: CodeMirror.getMode({}, { name: "css" }),
  raw: CodeMirror.getMode({}, { name: "text/plain" }),
  fetch: CodeMirror.getMode({}, { name: "fetch" })
};

to: 至:

const innerModes = {
  js: CodeMirror.getMode(
    { indentUnit: 2, statementIndent: 2 },
    { name: "javascript" }
  ),
  py: CodeMirror.getMode(
    { indentUnit: 4, hangingIndent: 4 },
    { name: "python" }
  ),
  md: CodeMirror.getMode({}, { name: "markdown" }),
  css: CodeMirror.getMode({ indentUnit: 2 }, { name: "css" }),
  raw: CodeMirror.getMode({}, { name: "text/plain" }),
  fetch: CodeMirror.getMode({}, { name: "fetch" })
};

This prevented NaN s from getting passed out of the indent function of the sub-modes. 这样可以防止NaN从子模式的缩进函数中传递出去。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM