Mishkin Faustini
Mishkin Faustini

Reputation: 626

Watching for File renaming in vscode extension

Is there a way to watch file name changes in vscode extensions. I am looking to know the old name -> new name for a file rename or when a user saves a file that was previously unsaved (Untitled-1 -> someName.txt for example).

I noticed that there is a FileSystemWatcher but it only seems to track onDidChange, onDidCreate and onDidDelete and each of those callbacks only passes a single URI.

Is there some better way to do this?

Upvotes: 5

Views: 1739

Answers (2)

Mauricio Gracia Gutierrez
Mauricio Gracia Gutierrez

Reputation: 10844

Create a FileSystemWatcher with the desired glob path that is relevant to you

var watcher = vscode.workspace.createFileSystemWatcher("**/*.ts"); //glob search string
watcher.ignoreChangeEvents = false;

watcher.onDidChange(() => {
 vscode.window.showInformationMessage("change applied!"); 
});

Taken from - Using FileSystemWatcher in Typescript (Visual Studio Code extension)

Upvotes: 2

Matt Bierner
Matt Bierner

Reputation: 65223

As of VS Code 1.29, a file rename api is tracked here.

You can try this today using the proposed APIs:

export namespace workspace {
    export const onWillRenameFile: Event<FileWillRenameEvent>;
    export const onDidRenameFile: Event<FileRenameEvent>;
}

Upvotes: 0

Related Questions