1

I'm working on Roslyn plugin to Visual Studio. I am trying to subscribe to an event that would be raised after a file is renamed. I am using Workspace.WorkspaceChanged but it doesn't raise DocumentRemoved.

  1. Shouldn't WorkspaceChanged (with Kind=DocumentRemoved) be raised after an item is renamed?
  2. Is there any other way to get notifications about solution item renames? I was trying to subscribe to DTE events but no luck either.

This is the way how I get a workspace:

var componentModel = (IComponentModel)serviceProvider.GetService(typeof(SComponentModel));

_myWorkspace = componentModel.GetService<VisualStudioWorkspace>();
Piotr
  • 11
  • 2
  • 1
    What about DocumentInfoChanged? http://source.roslyn.io/#Microsoft.CodeAnalysis.Workspaces/Workspace/WorkspaceChangeKind.cs,104 – JoshVarty May 23 '19 at 04:05
  • Maybe you can get some help from these two issues:[Handling File Add, Remove, and Rename Events](https://stackoverflow.com/questions/35095241/visual-studio-sdk-handling-file-add-remove-and-rename-events) and [How to notify the editor about document changes](https://stackoverflow.com/questions/23826485/how-to-notify-the-editor-about-document-changes). – LoLance May 23 '19 at 06:24
  • DocumentInfoChanged is not raised. – Piotr May 24 '19 at 11:01
  • @Piotr is anything raised? If you just print `WorkspaceChangeEventArgs.Kind` for all events, do you get anything when a document is renamed? – JoshVarty May 24 '19 at 17:11
  • @JoshVarty I get only: 1. Project changed - no Document Id included 2. DocumentChanged by old and new solution point to the new document. – Piotr May 25 '19 at 11:41
  • @Piotr It's weird that the proper event isn't raised. You could try posting on http://github.com/dotnet/roslyn/issues. – JoshVarty May 27 '19 at 18:38
  • do have exactly the same issue. I could fix it with a FileSystemWatcher, where I registered for the Renamed Event for FileNames. https://learn.microsoft.com/en-us/dotnet/api/system.io.filesystemwatcher?view=net-5.0 But I am not really happy with the solution, why should it not work properly with Roslyn. – Cedric Mendelin Oct 15 '21 at 12:56

1 Answers1

2

It has been a couple of years i did something with this. I once wrote a VS plugin that searched through files in a solution. It worked with rename as well if i remember(unfortunate i can't check because it was for VS 2015). I did it with DTE events back than. I created an DteEventHandler and added to the DocumentEvents.DocumentSaved my function.

dte = Package.GetGlobalService(typeof(DTE)) as DTE2;
events = dte.Events;
docEv = events.DocumentEvents;
docEv.DocumentSaved += ScanDocumentForFunction;

The other way is probably to go through the solution file. The solutionfile contains all csproj files and you can get all files from there.

dte = FillIndexListCommandPackage.GetGlobalService(typeof(DTE)) as DTE2;
var solutionnamearr = dte.Solution.FullName.Split('\\');
...

if you use git it would probably the easiest way to just call a git status in a command windows and pipe the result through to your plugin logic.

i hope i was some help or pointed you at least in the right direction.

weiky
  • 140
  • 7