vscode.py icon indicating copy to clipboard operation
vscode.py copied to clipboard

Get current open file/project

Open TTitcombe opened this issue 2 years ago • 3 comments

Is there a way through the extension to get the filename and path to the currently open file or project in the editor? Something like pathlib.Path.cwd doesn't work if the user navigates to a different project within VSCode

TTitcombe avatar Dec 01 '21 21:12 TTitcombe

I believe there's ActiveTextEditor.document to get the currently opened text document

Makiyu-py avatar Dec 01 '21 23:12 Makiyu-py

Currently, the Python script you write has the default file path set to the home directory. In #25 I mentioned I have created a fox for this, and I plan on opening a PR for it. Maybe @CodeWithSwastik can comment on this. My approach to implementing this is to grab the vscode workspace directory in JavaScript and pass it in as an input argument to the Python script. From there, the Python script sets the working directory to the vscode workspace directory.

Nicholas-Schaub avatar Dec 22 '21 16:12 Nicholas-Schaub

active_text_editor = await ctx.window.active_text_editor
file_name = active_text_editor.document.fileName

SmartManoj avatar Dec 16 '23 13:12 SmartManoj