vscode API:获取行最后一个字符的位置 [英] vscode API: get Position of last character of line

查看:67
本文介绍了vscode API:获取行最后一个字符的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

跟进这个仍未解决的问题 关于使用 VS Code API 的 VS Code 扩展.我没有回答它,因为它特别要求使用 Position 对象的 with 方法的解决方案.我无法完成这项工作,也无法遍历对象以获取最后一个字符.尝试使用 vscode.commands.executeCommand 操作选择也不起作用,因为 vscode.window.activeTextEditor 似乎没有反映窗口中的实际选择一旦执行开发主机开始运行.我能找到的唯一解决方案是下面的跳圈练习,它获取一行的第一个字符和下一行的第一个字符,设置一个范围,获取该范围的文本,然后减少该文本字符串的长度按 1 获取上一行的最后一个字符.

Following up on this still unanswered question regarding VS Code Extensions with the VS Code API. I didn't answer it because it specifically asked for a solution using the with method of the Position object. I couldn't make that work, nor was I able to loop through the object to get the last character. Trying to manipulate the selection with vscode.commands.executeCommand didn't work either, because vscode.window.activeTextEditor doesn't appear to reflect the actual selection in the window as soon as the Execution Development Host starts running. The only solution I could find was the hoop-jumping exercise below, which gets the first character of one line and the first character of the next line, sets a Range, gets the text of that Range, then reduces the length of that text string by 1 to get the last character of the previous line.

function getCursorPosition() {
  const position = editor.selection.active;
  curPos = selection.start;
  return curPos;
}
curPos = getCursorPosition();
var curLineStart =  new vscode.Position(curPos.line, 0);
var nextLineStart = new vscode.Position(curPos.line + 1, 0);
var rangeWithFirstCharOfNextLine = new vscode.Range( curLineStart, nextLineStart);
var contentWithFirstCharOfNextLine = editor.document.getText(rangeWithFirstCharOfNextLine);
var firstLineLength = contentWithFirstCharOfNextLine.length - 1;
var curLinePos = new vscode.Position(curPos.line, firstLineLength);
var curLineEndPos = curLinePos.character;
console.log('curLineEndPos :>> ' + curLineEndPos);

我显然遗漏了一些东西 - 使用 VSCode API 获取一行的最后一个字符是不可能的,而不会像这样用鼠标鼠标移动这样的东西.所以问题很简单,正确的方法是什么?

I'm obviously missing something - it can't be impossible to get the last character of a line using the VSCode API without mickey-mousing the thing like this. So the question is simply, what is the right way to do this?

推荐答案

一旦你有了光标 Position TextDocument.lineAt() 函数返回一个 TextLine.从中你可以得到它的 range 并且 range.end.character 会给你最后一个字符的字符号.- 不包括换行符,如果您想包括该换行符,请参见 TextLine.rangeInducingLineBreak().

Once you have the cursor Position the TextDocument.lineAt() function returns a TextLine. From which you can get its range and that range.end.character will give you the character number of the last character. - not including the linebreak which if you want to include that see TextLine.rangeIncludingLineBreak().

const editor = vscode.window.activeTextEditor;
const document = editor.document;          
const cursorPos = editor.selection.active;

document.lineAt(cursorPos).range.end.character;

注意(来自 [TextDocument.lineAt 文档][1] ):

返回由位置表示的文本行.注意返回的对象未生效,并且不会反映对文档的更改.

Returns a text line denoted by the position. Note that the returned object is not live and changes to the document are not reflected.

这篇关于vscode API:获取行最后一个字符的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆