VSCode:如何在编辑器中确定光标的列位置?



我正在寻找一种简单的方法来确定VSCode编辑器的文本插入光标/插入符号的列位置,可以在选择要复制的文本区域之前,也可以立即使用鼠标开始选择。然后,在执行进一步的剪贴板操作之前,列编号将存储在剪贴板中。

我尝试过搜索AutoHotkey方法来实现这一点,但我能找到的唯一解决方案是使用ImageSearch,这不适合我的目的。

编辑:我找到了这个API引用,我是否可以使用它来确定光标位置,最好使用windows cmd/powershell?

您可以进行vscode扩展,并将命令绑定到键盘快捷键。

如何获取光标的行和列(字符(:

const activeEditor = vscode.window.activeTextEditor
if (activeEditor) {
console.log(activeEditor.selection.active.line)
console.log(activeEditor.selection.active.character) //column
}

api:https://code.visualstudio.com/api/references/vscode-api#details-159

activeEditor.selection为您提供一个包含4个对象的对象

start:Object
line:4
character:8
end:Object
line:6
character:8
active:Object
line:4
character:8
anchor:Object
line:6
character:8

activeEditor.selection.active是您的光标。

activeEditor.selection.anchor是

选择开始的位置。此位置可能在活动之前或之后。

激活的锚点可以反转,但起始端将始终为上下。

注意:在我发现如何从这里获取当前行之后,我找到了api:VScode API为什么可以';我不知道现在的线路吗?

编辑:对于columnNum(参见Eric的评论(:

const activeEditor = vscode.window.activeTextEditor
if (activeEditor) {
const lineOffset = activeEditor.selection.active.line
const charOffset = activeEditor.selection.active.character
console.log(`line: ${lineOffset + 1}`)
console.log(`character: ${charOffset + 1}`)
console.log(`column: ${getColumn(activeEditor.document.lineAt(lineOffset).text,charOffset) + 1}`) //column
function getColumn(str, sumCharacter) {
const arr = [...str]
let whichCharacter = 0
for (let whichColumn = 0; whichColumn < arr.length; whichColumn++) {
if (whichCharacter===sumCharacter) {
return whichColumn
}
whichCharacter+=arr[whichColumn].length
}
return arr.length
}
}

最新更新