0

I am new to visual studio code API.

I would like to listen some event like onSave , onClose etc...

I am currently using this API.
But I think this is vary expensive like this. When you only need to listen event for only a specific file.

let { workspace } = require("vscode");

module.exports = {
    activate(ctx) {
        ctx.subscriptions.push(
            workspace.onDidSaveTextDocument(({ fileName, version }) => {
                
            })
        )
    },
    deactivate() {}
};

4
  • how many files a second do you save? Commented Nov 8, 2020 at 22:32
  • "I think this is vary expensive like this", you'd better give some proof. Commented Nov 9, 2020 at 1:11
  • Every time If I save any file then it will fire this function. then check something if true then update else do something...Its take some CPU computation right ? So its expensive Commented Nov 9, 2020 at 17:48
  • Its like watch all...check then do something...I want something like watch one! ...check then do something...so my code only run if a specific file change Commented Nov 9, 2020 at 17:52

1 Answer 1

1

Not a great solution .But worked for me. Still Though is there any other way,The better way?...

My solution is...

let onSaveCleaner = workspace.onDidSaveTextDocument((e) => {
    // watch for all file save event ... yuck!
});

let onCloseCleaner = workspace.onDidCloseTextDocument(({ fileName, languageId }) => {
    // some condition like once `specific` file closed 
    if (languageId == "python" && fileName == "...") {
        onSaveCleaner.dispose()
        onCloseCleaner.dispose()
    }
});

By doing that We only run expensive code when file is not closed! Once the file closed...Destroy everything!

Sign up to request clarification or add additional context in comments.

2 Comments

I think there is better API.Which I don't know...for watching specific file...not all file!
This does not work for notebooks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.