获取 CodeMirror 中特定行中使用的缩进

Get indentation used in specific line in CodeMirror

本文关键字:缩进 CodeMirror 获取      更新时间:2023-09-26

我正在使用CodeMirror,我想提供一些简单的代码转换功能。

我需要知道我所在的行的放置缩进,例如:

function test() {
  var x = 0; //I need to get that this line has 2 spaces.
  var y = function() {
    return true; //And that this one has 4 spaces -or a tab.
  }
}

是否有通过CodeMirror API或任何相关黑客来获取它的标准方法?

由于 CodeMirror 主要处理语法分析(标记等),我尝试分析行标记并将其与光标数据相结合,但我想要求更彻底和清晰的东西。

令牌

的状态包含 indented 属性,该属性为令牌的缩进提供以下信息:

var token = editor.getTokenAt(editor.getCursor());
console.log(token.state.indented);