我的 Visual Studio 插件如何在以与“删除和排序使用”相同的方式构建之前检测编译器错误?

发布于 2024-08-22 06:04:37 字数 489 浏览 5 评论 0原文

我正在制作一个重构工具,可以自动处理一些处理 StyleCop 的更琐碎的代码样式事务。我希望我的外接程序能够执行的一件事(作为开发人员可以打开/关闭的可选功能)是自动调用“组织使用 -> 删除和排序”功能。这是一个简单的宏调用。

然而,我的问题是,当我在解决方案中的每个 .cs 非设计器文件上递归调用宏时,这个特定的宏在执行之前检查错误。如果存在语法错误并且我的加载项开始调用该功能,它会为每个文件弹出一个对话框。

我想做的是使用与函数用于动态检查错误相同的方法,然后如果存在导致弹出对话框的错误,只需弹出一次性通知并跳过调用在每个文件上。

我知道我的加载项可以检查错误列表,但是我多次发现触发“删除和排序”中问题的错误并不总是出现在列表中。我将列表设为空,然后尝试“删除并排序”,它告诉我存在构建问题。然后我构建解决方案,果然它失败了并且错误被填充。我看到的唯一解决方案是使用与“删除和排序”相同的功能来预先检查。

有谁知道我如何在以与“删除和排序使用”相同的方式构建之前检测编译器错误?

I'm making a refactoring tool that automates a few of the more trivial code styling things dealing with StyleCop. One of the things I'd like my add-in to be able to do (as an optional feature the developer can turn on/off) is automatically call the "Organize Usings -> Remove and Sort" functionality. This is a simple macro call.

However, my problem is that as I go through and recursively call the macro on every .cs non-designer file in the solution this particular macro checks for errors before execution. If there's a syntax error and my add-in starts calling that functionality, it throws up a dialog for each and every file.

What I'd like to do, is use the same method that function uses for checking for errors on the fly and then if there's an error that would cause the dialog to pop up, simply pop up a one-time notification and skip the calls on each file.

I know my add-in could check the error list, however I've found many times that errors that trigger problems in "Remove and Sort" don't always appear in the list. I've had the list be empty, then I try "Remove and Sort" and it tells me there's build problems. I then build the solution and sure enough it fails and the errors are populated. The only solution I'm seeing is to use the same functionality as "Remove and Sort" to check beforehand.

Does anybody know how I can detect compiler errors before building in the same manner as "Remove and Sort Usings"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时常饿 2024-08-29 06:04:37

我不太确定您在谈论哪种错误,我假设它们是 IntelliSense 解析器生成的错误。是的,那个解析器不是很可靠。它并不是一个成熟的 C# 解析器,它经过优化以完成一种非常不同的工作:即使代码不完整且无法编译,也提供上下文相关的帮助。除了等待下一个 VS 版本之外,您无法采取任何措施来使其更加可靠。

但我觉得你可能试图解决错误的问题。关键问题是您的加载项似乎正在删除不应删除的 using 指令。真正的解决方法是改进代码分析引擎,以便它可靠地检测真正的命名空间依赖关系。试图猜测哪些是重要的,希望 IntelliSense 会抱怨,只会让您的客户感到沮丧。

I'm not exactly sure what kind of errors you are talking about, I'll assume they are the ones that the IntelliSense parser generates. Yes, that parser isn't very reliable. It is not meant to be a full-blown C# parser, it was optimized to do a very different kind of job: providing context-sensitive help even if the code is incomplete and cannot compile. There isn't anything you can do to make it more reliable, other than waiting for the next VS release perhaps.

But it strikes me that you might be trying to fix the wrong problem. The key issue is that your add-in appears to be removing using directives that should not be removed. A real fix is to improve your code analysis engine so it reliably detects true namespace dependencies. Trying to guess which ones matter from hoping that IntelliSense is going to complain is only going to frustrate your customer.

眼中杀气 2024-08-29 06:04:37

这看起来很老套,但除非有人能想出更好的解决方案,否则我将使用错误列表检查,因为这比什么都不做要好。

另外,我想我可以尝试在对“Edit.RemoveAndSort”的调用周围放置一个计时器,如果它花费了一定的时间,则弹出一个一次性对话框,提及RemoveAndSort似乎遇到了问题,并询问是否用户想要取消,或者至少不调用删除和排序。

It seems hacky, but unless somebody can come up with a better solution I'm gonna use a check on the errors list because it's better than nothing.

Additionally I think I may experiment with putting a timer around the call to "Edit.RemoveAndSort" and if it takes over a certain amount of time, pop up a one-time dialog mentioning that it looks like RemoveAndSort is having problems and asking if the user wants to cancel, or at least not call remove and sort.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文