VSCode API 从 editor.document 获取格式化文本

VSCode API get formatted text from editor.document

我正在尝试制作自定义 VSCode 扩展,将我当前选择的文件内容导出为 PDF。 (我知道有 PrintCode,但它不适合我的用例)

我当前代码的相关部分是:

const editor = vscode.window.activeTextEditor;
if (editor !== undefined) {
    let cursorPosition = editor.selection.start;
    let wordRange = editor.document.getWordRangeAtPosition(cursorPosition);
    let highlight = editor.document.getText(wordRange);
    console.log(highlight);
}

这可以很好地输出选定的文本,但只提供纯文本。我希望将带有语法高亮显示的格式化代码放入我的 PDF 文件中。获取格式化文本的最佳方式是通过 rtf 或更好的方式 html.

甚至可以通过 Visual Studio 代码 API 实现吗?

感谢您的帮助!

您好, 法比奥

在撰写本文时这是不可能的。但是,VSCode 使用 Textmate 语法,因此您可以使用 this npm 包轻松解析要突出显示的代码片段,并在导出为 PDF 之前自行完成。