Visual Studio code extension - Getting active tab data for non-textual files

Viewed 834

I have an extension in which I query the source path of the current active tab file.

For textual files, the following works great:

const activeEditor: TextEditor = window.activeTextEditor;
if (activeEditor && activeEditor.document && activeEditor.document.fileName) {
    return activeEditor.document.fileName;
}

Problem is, this doesn't work when the active file in the text editor is non-textual, for example an image file such as a .jpg.

For these types of files, window.activeTextEditor is undefined. Moreover, when I try running the following code:

const uri = Uri.file(<path_to_binary_file>);
workspace.openTextDocument(uri);

I receive the following error: "cannot open <path_to_file>. Detail: File seems to be binary and cannot be opened as text", and I don't see any similar API to openTextDocument that deals with non-textual files.

Is there any straightforward way of obtaining this data without using commands.executeCommand('workbench.action.files.copyPathOfActiveFile') and various clipboard manipulations?

1 Answers
Related