Monaco Editor 获取插入符号的源代码索引

2024-01-31

有没有办法将插入符号位置作为源代码字符串中的索引? 我知道我可以获取位置,这将为我提供一个包含行和列的对象,但是有没有办法获取或将行+列转换为字符串字符索引?

例如,如果我有:

using System;
using System.Data;

我将插入符号放在“.Data”之前,我知道如何获取行+列坐标(第1行,第13行),但如何获取字符数组索引(应该类似于25)?


您可以在以下示例代码中使用摩纳哥游乐场 https://microsoft.github.io/monaco-editor/playground.html尝试一下。

您正在寻找的功能是文本模型 https://microsoft.github.io/monaco-editor/api/interfaces/monaco.editor.itextmodel.html's 获取偏移量(IPosition) https://microsoft.github.io/monaco-editor/api/interfaces/monaco.editor.itextmodel.html#getoffsetat功能。

var model = monaco.editor.createModel(
    "using System;\n" +
    "using System.Data;",
    "csharp"
); 

var editor = monaco.editor.create(document.getElementById("container"), {
    model
});

var offset = model.getOffsetAt({ lineNumber: 2, column: 13 });
alert(offset);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Monaco Editor 获取插入符号的源代码索引 的相关文章

随机推荐