VScode API 为什么我无法获取当前行? [英] VScode API why can't I get the current line?
问题描述
我正在使用 typescript 编写 vs 代码扩展,但由于某种原因我无法获取当前行.
I am using typescript to write a vs code extension and for some reason I am unable to get the current line.
我正在尝试制作的功能是:
The function I am trying to make is:
function makeFrame()
{
vscode.window.activeTextEditor.selection.active.line;
}
失败并出现错误:对象可能未定义导入语句是:
Which fails with error: Object is possibly undefined The import statement is:
import {window, commands, Disposable, ExtensionContext, StatusBarAlignment, StatusBarItem, TextDocument} from 'vscode';
我做错了什么?
(我既是 TypeScript 的新手,也是为 VS 代码编写扩展)
(I am both new to TypeScript and writing extensions for VS code)
推荐答案
activeTextEditor
可能是 undefined
.这表示没有活动编辑器,例如当您第一次打开新工作区或关闭所有编辑器时会发生
activeTextEditor
may be undefined
. This indicates that there is no active editor and will happen for example when you first open a new workspace or when you close all editors
要修复,只需添加一个快速检查:
To fix, just add a quick check:
function makeFrame()
{
const activeEditor = vscode.window.activeTextEditor;
if (activeEditor) {
activeEditor.selection.active.line;
}
}
这篇关于VScode API 为什么我无法获取当前行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!