Visual studio code 检查所选最后一行的值-vscode api
我试图找出如何查看活动编辑器的最后一行所选内容是否包含除空格以外的任何字符/文本。这样做的目的是确定是否有突出显示的文本,如果没有,则在编辑器中返回一个数字,不包括该行 我知道你能做到Visual studio code 检查所选最后一行的值-vscode api,visual-studio-code,vscode-extensions,Visual Studio Code,Vscode Extensions,我试图找出如何查看活动编辑器的最后一行所选内容是否包含除空格以外的任何字符/文本。这样做的目的是确定是否有突出显示的文本,如果没有,则在编辑器中返回一个数字,不包括该行 我知道你能做到 const editor = vscode.window.activeTextEditor const lastLineSelected = editor.selection.end 为了获取最后一行编号,但如何计算该行中突出显示的文本?如果我理解正确,您希望获取所选内容最后一行
const editor = vscode.window.activeTextEditor
const lastLineSelected = editor.selection.end
为了获取最后一行编号,但如何计算该行中突出显示的文本?如果我理解正确,您希望获取所选内容最后一行的文本,该文本可能是多行的。下面是这样做的,如果只选择了部分行,则会进行检查
const editor = vscode.window.activeTextEditor;
const selection = editor.selection;
if (selection && !selection?.isEmpty) {
// is more than one line selected, if so use 0 as the start of the selection on the last line
// otherwise use the actual first character selected on that line
const firstSelectedCharacterOnLastLine = (selection.start.line < selection.end.line) ? 0 : selection.start.character;
// make a Range to use with getText(<some Range>)
const lastLineOfSelectionRange = new vscode.Range(selection.end.line, firstSelectedCharacterOnLastLine, selection.end.line, selection.end.character);
const text = editor.document.getText(lastLineOfSelectionRange);
// do your testing on the text using javascript String methods
// like text.search(/[^\s]/) search for any non-whitespace character
}
const editor=vscode.window.activeTextEditor;
const selection=editor.selection;
if(selection&!selection?.isEmpty){
//是否选择了多行,如果是,请使用0作为最后一行选择的开始
//否则,请使用该行上实际选定的第一个字符
const firstSelectedCharacterOnLastLine=(selection.start.line
谢谢。那太有用了!