Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 402
Ilia Avatar asked Jun 28 '26 21:06

Ilia


1 Answers

It seems that this is a known open issue in VS code. The following Github issue discusses it.

like image 74
Ilia Avatar answered Jun 30 '26 10:06

Ilia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!