如何通知编辑有关文档更改的信息 [英] How to notify the editor about document changes

查看:67
本文介绍了如何通知编辑有关文档更改的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在自定义项目系统上工作,但在重命名项目时遇到问题.项目系统实现基于 MPF 并通过解决方案资源管理器重命名项目或多或少没有任何问题,但是...

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?

推荐答案

假设它是一个文本文件而不是一些自定义设计器,请使用 IVsRunningDocumentTable 接口来获取当前打开的文件的文本缓冲区.界面的第四次迭代是从托管代码中最容易使用的一种.您可以调用 IsMonikerValid(其中moniker"是文件名)来查看文件是否打开,如果打开,则调用 GetDocumentData 以获取 IVsTextBuffer 文件.使用这种类型本身就很烦人,因此如果您只支持 Visual Studio 2010 或更高版本,请将其传递给 此函数 获取更新的编辑器 API 版本.

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.

这篇关于如何通知编辑有关文档更改的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆