简体   繁体   中英

VSCode API get formatted text from editor.document

I am trying to make a customized VSCode extension, that exports my current selected file contents as a PDF. (I know there is PrintCode, but it doesn't suit my use case)

The relevant part of my current code is:

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);
}

This outputs the selected text really well, but only gives me the plain text. I want the formatted code with syntax highlighting to put in my PDF file. The best way to get the formatted text is via rtf or even better html.

Is that even possible via the Visual Studio Code API?

Thank you for your help!

Greetings, Fabio

This is not possible as of writing this. However, VSCode uses Textmate grammars, so you can use this npm package to easily parse the snippet of code you want to highlight and do it yourself before exporting to PDF.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM