VS Code: check whether the file explorer is shown?

Viewed 168

I'm using an extension that calls commands.executeCommand('revealInExplorer');. This switches the left sidebar to the file explorer and then reveals the current file in the file tree. However, I want this to happen only when the explorer is currently visible. E.g. when I'm in the search view, I don't want the extension to call commands.executeCommand('revealInExplorer');. How can I do this?

The extension is "Auto-Collapse Explorer":

const { window, commands } = require('vscode');

const COLLAPSE = 'workbench.files.action.collapseExplorerFolders';
const REVEAL = 'revealInExplorer';

function activate(context) {
  const subscription = window.onDidChangeActiveTextEditor(showOnlyCurrentFile);
  context.subscriptions.push(subscription);
  showOnlyCurrentFile();
}

async function showOnlyCurrentFile() {
  await commands.executeCommand(COLLAPSE);
  await commands.executeCommand(REVEAL);
  if (!window.activeTextEditor) return;
  window.showTextDocument(window.activeTextEditor.document);
}

function deactivate() {}

module.exports = {
  activate,
  deactivate
};
1 Answers

I found that in the settings.json there is an openEditors constant that when set to 0 it means that the editors should not be displayed in the file explorer. Hence, you can create a task to extract the variable value of openEditors and in your command add an if statement for openEditors != 0 and then you add the commands.executeCommand('revealInExplorer');

https://code.visualstudio.com/docs/editor/variables-reference#_configuration-variables

You can reference VS Code settings ("configurations") through ${config:Name} syntax (for example, ${config:editor.fontSize})

Not sure if this if what your going for, hope it helps.

Related