如何在 Visual Studio 插件中阻止 OpenFile

发布于 2024-09-09 07:30:53 字数 695 浏览 5 评论 0原文

我有一个 Visual Studio 2008 插件,当我按下某个热键时,它会打开一个特定文件(根据热键的上下文而有所不同),然后在该文件中搜索特定字符串(同样,依赖于上下文)。大多数情况下这工作完美,但有时如果它打开的文件太大,搜索将会失败。

这是一个代码片段:

Window xmlWindow = Commands.Application.ItemOperations.OpenFile(objectFilename, EnvDTE.Constants.vsViewKindPrimary);
Find find = xmlWindow.Document.DTE.Find;
find.Action = vsFindAction.vsFindActionFind;
find.FindWhat = String.Format("Name=\"{0}\"", objectLocalName);
if (find.Execute() == vsFindResult.vsFindResultFound) {
     MessageBox.Show("Found!");
}



1. 有没有办法让它始终工作(例如通过阻塞 OpenFile)?

2. 一个不太重要的问题是,有没有一种方法可以像这样进行搜索,而不会让结果出现在“查找结果”窗格中(这会导致我的旧结果被此搜索清除,该搜索仅用于将光标向下移动到文件的那部分)?


I have a Visual Studio 2008 addin that when I press a certain hotkey, it opens a specific file (different based on the context of the hotkey) and then searches that file for a specific string (again, context dependent.) Most of the time this works flawlessly, but sometimes if the file it's opening is too big, the search will fail.

Here is a code snippet:

Window xmlWindow = Commands.Application.ItemOperations.OpenFile(objectFilename, EnvDTE.Constants.vsViewKindPrimary);
Find find = xmlWindow.Document.DTE.Find;
find.Action = vsFindAction.vsFindActionFind;
find.FindWhat = String.Format("Name=\"{0}\"", objectLocalName);
if (find.Execute() == vsFindResult.vsFindResultFound) {
     MessageBox.Show("Found!");
}

1. Is there a way to get it to always work (for example by blocking on the OpenFile)?

2. On a less important note, is there a way to search like this without having the results end up in the Find Results pane (this causes my old results to be cleared out by this search that is only used to get the cursor down to that part of the file)?

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

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

发布评论

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

评论(2

嘿哥们儿 2024-09-16 07:30:53

如果 OpenFile 行为异步,我建议您考虑更改逻辑以依赖于不同的事件,即依赖于被激活的文档的事件。

例如,您是否尝试过使用快捷键触发 OpenFile,然后对搜索进行排队,以便稍后由 VS 事件处理? (下面的代码取自 Visual Studio 2010 插件,但我相信事件是相同的。)

// 确保这些是类变量,否则它们可能会被错误地进行 GC 并破坏 COM 交互
私有 WindowEvents _winEvents = null;
私有DTE2_applicationObject;

然后

_events = _applicationObject.Events;
_winEvents = _events.get_WindowEvents();

_winEvents.WindowActivated += new _dispWindowEvents_WindowActivatedEventHandler(WindowActivated);

,您可以在 WindowActivated: 中放置一些代码,

void WindowActivated(Window GotFocus, Window LostFocus)
        {
            Document gotFocusDoc = GotFocus.Document;
            if (gotFocusDoc != null)
            {
                string fileExt = Path.GetExtension(gotFocusDoc.Name);

在那里您可以监视要扫描的文件(您可能需要保留一个列表等)。

对于第二个问题,一旦您按照我上面建议的方式获得了访问权限,您就可以自己扫描文档。

If OpenFile behaves asynchronously, I'd suggest you consider changing the logic to rely on a different event, one that relies on the document being activated.

For example, have you tried triggering the OpenFile with your shortcut key, then queuing the search so that it's later handled by a VS event? (The code below was taken from a Visual Studio 2010 addin, but I believe the events are the same.)

// make sure these are class variables, otherwise they may get GC'd incorrectly and break the COM interaction
private WindowEvents _winEvents = null;
private DTE2 _applicationObject;

in the connect:

_events = _applicationObject.Events;
_winEvents = _events.get_WindowEvents();

_winEvents.WindowActivated += new _dispWindowEvents_WindowActivatedEventHandler(WindowActivated);

Then, you'd put some code in the WindowActivated:

void WindowActivated(Window GotFocus, Window LostFocus)
        {
            Document gotFocusDoc = GotFocus.Document;
            if (gotFocusDoc != null)
            {
                string fileExt = Path.GetExtension(gotFocusDoc.Name);

There, you'd watch for the file that you wanted to scan (you'd maybe need to keep a list, etc.).

For the second issue, you could just scan through the document yourself once you've got access in the way I suggested above.

黯然#的苍凉 2024-09-16 07:30:53

我认为 DTE.ItemOperations.OpenFile() 方法是同步的。尝试在 VS2008 中使用以下代码片段。

using EnvDTE;

Window win = _applicationObject.ItemOperations.OpenFile(@"path-to-xml-file", Constants.vsViewKindPrimary);
TextDocument doc = win.Document.Object("TextDocument") as TextDocument;
if (doc != null)
{
    EditPoint searchStart = doc.StartPoint.CreateEditPoint();
    EditPoint endOfFoundText = null;
    TextRanges ranges = null;

    bool result = searchStart.FindPattern("Text-to-search", (int)vsFindOptions.vsFindOptionsNone, ref endOfFoundText, ref ranges);
    if (result)
    {
        // Result is bounded by searchStart and endOfFoundText points.
        System.Windows.Forms.MessageBox.Show("BINGO! Found at " + searchStart.Line.ToString());
    }
}

如果您在捕捉打开的窗口时遇到问题,我建议您检查我的扩展程序的源代码 WordLight :有一个 WindowWatcher 类,它跟踪文本视图的创建。

I think, that DTE.ItemOperations.OpenFile() method is synchronous. Try to use a following code snippet for VS2008.

using EnvDTE;

Window win = _applicationObject.ItemOperations.OpenFile(@"path-to-xml-file", Constants.vsViewKindPrimary);
TextDocument doc = win.Document.Object("TextDocument") as TextDocument;
if (doc != null)
{
    EditPoint searchStart = doc.StartPoint.CreateEditPoint();
    EditPoint endOfFoundText = null;
    TextRanges ranges = null;

    bool result = searchStart.FindPattern("Text-to-search", (int)vsFindOptions.vsFindOptionsNone, ref endOfFoundText, ref ranges);
    if (result)
    {
        // Result is bounded by searchStart and endOfFoundText points.
        System.Windows.Forms.MessageBox.Show("BINGO! Found at " + searchStart.Line.ToString());
    }
}

If you will have a trouble with catching an opened window, I suggest to check a source code of my extension WordLight: there is a WindowWatcher class, which tracks creation of text views.

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