我正在开发一个 vscode 扩展。我想获取整个文档的光标位置引用,例如,如果我有以下 html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<|body>
<div>
<p>Hello World</p>
</div>
</body>
</html>
并且 cursur 在 body 标记内(上面代码中 |
符号的位置),我想要的结果是 178,如果你开始计算所有字符,它是字符的索引在文档中(包括空格)从头开始。
我试过这段代码
const position = editor.selection.active;
console.log(position);
但它给出了行和字符在该行中的位置,这不是我需要的。
有什么办法可以得到想要的输出吗?
最佳答案
使用下面的调用
editor.document.offsetAt(editor.selection.active)
Selection
有 4 个 Position
属性:
start
,end
: 相对于文件开头的位置anchor
,active
: 光标开始和停止的位置进行选择
关于javascript - vscode : how to get position of cursor in the document?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65261663/