You can run a tool after the file is saved like this: https://vlasovstudio.com/visual-commander/extensions.html#OnDocumentSaved
There is no similar event before the file is saved and the tool wouldn't be able to access the file content as it is not yet saved.
You may also look at single-file generators that can transform a single input file into a single output file whenever the file is saved: https://learn.microsoft.com/en-us/visualstudio/extensibility/internals/custom-tools?view=vs-2019