检查所选最后一行的值 - vscode-api
Check value of last line selected - vscode-api
我想知道如何查看活动编辑器的最后一行是否包含任何 characters/text,但不包括空格。这样做的目的是确定是否有突出显示的文本,如果没有突出显示,则在编辑器中返回一个数字而不包括该行。
我知道你可以做到
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
}
我想知道如何查看活动编辑器的最后一行是否包含任何 characters/text,但不包括空格。这样做的目的是确定是否有突出显示的文本,如果没有突出显示,则在编辑器中返回一个数字而不包括该行。
我知道你可以做到
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
}