简体   繁体   中英

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?

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>;
}

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM