问题
I work on a custom project system and I have an issue with renaming items. The project system implementation is based on MPF and renaming items via the solution explorer worked more or less without any problems, but...
When an item gets renamed, I also update information which are stored in the file itself. In case the document is opened by the code editor, the text in the editor doesn´t get refreshed (only the document window´s caption changes to the new filename). If I save the open document all changes applied by the rename operation are overriden, of course.
How can I force the editor to reload the document, so that the automatic changes will be shown?
回答1:
Assuming it's a text file versus some custom designer, use the IVsRunningDocumentTable
interfaces to get the text buffer for the file that's currently open. The fourth iteration of the interface is the easiest one to use from managed code. You can call IsMonikerValid
(where the "moniker" is the file name) to see if the file is open, and if so then call GetDocumentData
to get the IVsTextBuffer
for the file. That type itself is fairly annoying to work with, so if you're only supporting Visual Studio 2010 or later, then pass that to this function to get the newer editor APIs version of it.
As far as they "why" it's a good idea to do this: if you edit the file on disk and then try to force a reload, there are various problems you might run into. If the file wasn't saved before the rename, you might accidentally lose those unsaved edits. The reload might cause the undo history of the file to be lost, and any other extensions/features that were tracking points in the file with editor tracking spans or markers might lose the points they're tracking. Performance should be a bit better too if the file is a large file.
来源:https://stackoverflow.com/questions/23826485/how-to-notify-the-editor-about-document-changes