从 Visual Studio 在浏览器中加载搜索 URL

发布于 2024-08-03 04:25:26 字数 378 浏览 3 评论 0原文

我发现内置的 Visual Studio Document Explorer 不太相关,特别是当我使用的更多 SDK 都具有最新的在线内容时。按 F1 会启动文档资源管理器,通常会显示一些无用的信息,并且对我来说不再可用。

有没有什么方法可以在按下 Visual Studio 中的组合键时:

  • 默认浏览器打开到搜索引擎查询的 URL,
  • 使用的是当前光标位置下的关键字,
  • 添加过滤器,例如site:msdn.microsoft.com

我对 VS 中的宏一无所知,但大概这就是我需要的。有谁知道如何进行设置? 代码会很好!

I'm finding the built-in Visual Studio Document Explorer less relevant, especially as more of the SDKs I work with have the most up-to-date content on-line. Pressing F1 starts Document Explorer usually with something unhelpful and it's not usable any more for me.

Is there any way that on the press of a key combination in Visual Studio:

  • the default browser opens to the URL of a search engine
  • query used is the keyword under the current cursor position
  • a filter is added such as site:msdn.microsoft.com

I don't know anything about macros in VS but presumably that's what I need. Does anyone know how to go about setting this up? teh codez would be nice!

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

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

发布评论

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

评论(3

轮廓§ 2024-08-10 04:25:26

这是另一个版本(基于亚历克斯的答案),它也会选择您当前所在的单词。更像是典型的 F1 帮助。

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module Search
    Sub GoogleSearch()
        AnySearch("http://www.google.com/search?q=.net+")
    End Sub

    Sub BingSearch()
        AnySearch("http://www.bing.com/search?q=")
    End Sub

    Private Sub AnySearch(ByVal searchUrl)
        Dim strUrl As String
        Dim selection As String = GetSelection()
        If selection <> "" Then
            strUrl = searchUrl + selection
            DTE.ExecuteCommand("nav", strUrl & " /ext")
        Else
            MsgBox("Select text to search for.")
        End If
    End Sub

    Private Function GetSelection() As String
        Dim selection As TextSelection = DTE.ActiveDocument.Selection()
        If selection.Text <> "" Then
            Return selection.Text
        Else
            DTE.ExecuteCommand("Edit.SelectCurrentWord")
            selection = DTE.ActiveDocument.Selection()
            Return selection.Text
        End If
    End Function
End Module

Here is another version (based on Alex's answer) that will also select the current word you are on. More like the typical F1 help.

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module Search
    Sub GoogleSearch()
        AnySearch("http://www.google.com/search?q=.net+")
    End Sub

    Sub BingSearch()
        AnySearch("http://www.bing.com/search?q=")
    End Sub

    Private Sub AnySearch(ByVal searchUrl)
        Dim strUrl As String
        Dim selection As String = GetSelection()
        If selection <> "" Then
            strUrl = searchUrl + selection
            DTE.ExecuteCommand("nav", strUrl & " /ext")
        Else
            MsgBox("Select text to search for.")
        End If
    End Sub

    Private Function GetSelection() As String
        Dim selection As TextSelection = DTE.ActiveDocument.Selection()
        If selection.Text <> "" Then
            Return selection.Text
        Else
            DTE.ExecuteCommand("Edit.SelectCurrentWord")
            selection = DTE.ActiveDocument.Selection()
            Return selection.Text
        End If
    End Function
End Module
青芜 2024-08-10 04:25:26

使用链接Preet提供我出现了这样就可以启动默认浏览器:

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module Search
    Sub GoogleSearch()
        AnySearch("http://www.google.com/search?q=")
    End Sub

    Sub BingSearch()
        AnySearch("http://www.bing.com/search?q=")
    End Sub

    Private Sub AnySearch(ByVal searchUrl)
        Dim strUrl As String
        Dim selection As TextSelection = DTE.ActiveDocument.Selection()
        If selection.Text <> "" Then
            strUrl = searchUrl + selection.Text
            DTE.ExecuteCommand("nav", strUrl & " /ext")
        Else
            MsgBox("Select text to search for.")
        End If
    End Sub
End Module

Using the link Preet provided I came up with this which starts the default browser:

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module Search
    Sub GoogleSearch()
        AnySearch("http://www.google.com/search?q=")
    End Sub

    Sub BingSearch()
        AnySearch("http://www.bing.com/search?q=")
    End Sub

    Private Sub AnySearch(ByVal searchUrl)
        Dim strUrl As String
        Dim selection As TextSelection = DTE.ActiveDocument.Selection()
        If selection.Text <> "" Then
            strUrl = searchUrl + selection.Text
            DTE.ExecuteCommand("nav", strUrl & " /ext")
        Else
            MsgBox("Select text to search for.")
        End If
    End Sub
End Module
池予 2024-08-10 04:25:26

我想出了这个。正如它所说,您需要为 HttpUtility.UrlEncode 添加对“System.Web”的引用。此外,代码中还有一些被注释掉的“选项”。部分内容取自 http://www.codinghorror.com/ blog/2005/10/google-search-vsnet-macro.html ,但有很大改进(恕我直言)。

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module HelpFindInBrowser
    Sub HelpFindInBrowser()
        Dim s As String = ActiveWindowSelection().Trim()
        If s.Length > 0 Then
            OpenURL("http://www.google.com/search?q=" & _
                Web.HttpUtility.UrlEncode(s))
            '--------------------------------------------------------------------------

            'You will need to add a reference to 'System.Web' for HttpUtility.UrlEncode !!!

            '--------------------------------------------------------------------------
        End If
    End Sub

    Private Sub OpenURL(ByVal inURL As String)
        'specify a non default browser
        'DTE.ExecuteCommand("Tools.Shell", "notepad.exe " & inURL)

        'use the default browser:
        DTE.ExecuteCommand("nav", inURL & " /ext")

        'to have it in a new visual studio window:
        'DTE.ItemOperations.Navigate(inURL, EnvDTE.vsNavigateOptions.vsNavigateOptionsNewWindow)

        'to have it in the default visual studio browser window:
        'DTE.ItemOperations.Navigate(inURL, EnvDTE.vsNavigateOptions.vsNavigateOptionsDefault)
    End Sub

    'large part taken from http://www.codinghorror.com/blog/2005/10/google-search-vsnet-macro.html
    Private Function ActiveWindowSelection() As String
        If DTE.ActiveWindow.ObjectKind = EnvDTE.Constants.vsWindowKindOutput Then
            Return OutputWindowSelection()
        End If
        If DTE.ActiveWindow.ObjectKind = "{57312C73-6202-49E9-B1E1-40EA1A6DC1F6}" Then
            Return HTMLEditorSelection()
        End If
        Return SelectionText(DTE.ActiveWindow.Selection)
    End Function

    Private Function HTMLEditorSelection() As String
        Dim hw As HTMLWindow = ActiveDocument.ActiveWindow.Object
        Dim tw As TextWindow = hw.CurrentTabObject
        Return SelectionText(tw.Selection)
    End Function

    Private Function OutputWindowSelection() As String
        Dim w As Window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
        Dim ow As OutputWindow = w.Object
        Dim owp As OutputWindowPane = ow.OutputWindowPanes.Item(ow.ActivePane.Name)
        Return SelectionText(owp.TextDocument.Selection)
    End Function

    Private Function SelectionText(ByVal sel As EnvDTE.TextSelection) As String
        If sel Is Nothing Then
            Return ""
        End If

        Dim s As String

        If sel.Text.Length = 0 Then
            s = GetWordUnderCursor(sel)
        Else
            s = sel.Text
        End If
        'We could limit the text to some minimal size
        'If sel.Text.Length <= 2 Then
        'Return ""
        'End If
        Return s
    End Function

    Private Function GetWordUnderCursor(ByVal sel As EnvDTE.TextSelection) As String
        Dim ptStart As EnvDTE.EditPoint = sel.ActivePoint.CreateEditPoint()

        Dim theSelectToRight As Boolean = False

        If (ptStart.AtStartOfLine) Then
            theSelectToRight = True
        Else
            'See if there's a space to the left of ptStart
            'not at start of line, so moving one left is safe
            ptStart.CharLeft()

            If (ptStart.GetText(1).Trim() = "") Then
                theSelectToRight = True
            End If

            'Back to original position
            ptStart.CharRight()
        End If


        If (Not theSelectToRight) Then
            ptStart.WordLeft(1)
        End If

        Dim ptEnd As EnvDTE.EditPoint = ptStart.CreateEditPoint()
        ptEnd.WordRight(1)

        Return ptStart.GetText(ptEnd)
    End Function

End Module

I've come up with this one. As it says, you will need to add a reference to 'System.Web' for HttpUtility.UrlEncode. Also, there's a few 'options' in the code commented out. Parts taken from http://www.codinghorror.com/blog/2005/10/google-search-vsnet-macro.html , but much improved (IMHO).

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module HelpFindInBrowser
    Sub HelpFindInBrowser()
        Dim s As String = ActiveWindowSelection().Trim()
        If s.Length > 0 Then
            OpenURL("http://www.google.com/search?q=" & _
                Web.HttpUtility.UrlEncode(s))
            '--------------------------------------------------------------------------

            'You will need to add a reference to 'System.Web' for HttpUtility.UrlEncode !!!

            '--------------------------------------------------------------------------
        End If
    End Sub

    Private Sub OpenURL(ByVal inURL As String)
        'specify a non default browser
        'DTE.ExecuteCommand("Tools.Shell", "notepad.exe " & inURL)

        'use the default browser:
        DTE.ExecuteCommand("nav", inURL & " /ext")

        'to have it in a new visual studio window:
        'DTE.ItemOperations.Navigate(inURL, EnvDTE.vsNavigateOptions.vsNavigateOptionsNewWindow)

        'to have it in the default visual studio browser window:
        'DTE.ItemOperations.Navigate(inURL, EnvDTE.vsNavigateOptions.vsNavigateOptionsDefault)
    End Sub

    'large part taken from http://www.codinghorror.com/blog/2005/10/google-search-vsnet-macro.html
    Private Function ActiveWindowSelection() As String
        If DTE.ActiveWindow.ObjectKind = EnvDTE.Constants.vsWindowKindOutput Then
            Return OutputWindowSelection()
        End If
        If DTE.ActiveWindow.ObjectKind = "{57312C73-6202-49E9-B1E1-40EA1A6DC1F6}" Then
            Return HTMLEditorSelection()
        End If
        Return SelectionText(DTE.ActiveWindow.Selection)
    End Function

    Private Function HTMLEditorSelection() As String
        Dim hw As HTMLWindow = ActiveDocument.ActiveWindow.Object
        Dim tw As TextWindow = hw.CurrentTabObject
        Return SelectionText(tw.Selection)
    End Function

    Private Function OutputWindowSelection() As String
        Dim w As Window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
        Dim ow As OutputWindow = w.Object
        Dim owp As OutputWindowPane = ow.OutputWindowPanes.Item(ow.ActivePane.Name)
        Return SelectionText(owp.TextDocument.Selection)
    End Function

    Private Function SelectionText(ByVal sel As EnvDTE.TextSelection) As String
        If sel Is Nothing Then
            Return ""
        End If

        Dim s As String

        If sel.Text.Length = 0 Then
            s = GetWordUnderCursor(sel)
        Else
            s = sel.Text
        End If
        'We could limit the text to some minimal size
        'If sel.Text.Length <= 2 Then
        'Return ""
        'End If
        Return s
    End Function

    Private Function GetWordUnderCursor(ByVal sel As EnvDTE.TextSelection) As String
        Dim ptStart As EnvDTE.EditPoint = sel.ActivePoint.CreateEditPoint()

        Dim theSelectToRight As Boolean = False

        If (ptStart.AtStartOfLine) Then
            theSelectToRight = True
        Else
            'See if there's a space to the left of ptStart
            'not at start of line, so moving one left is safe
            ptStart.CharLeft()

            If (ptStart.GetText(1).Trim() = "") Then
                theSelectToRight = True
            End If

            'Back to original position
            ptStart.CharRight()
        End If


        If (Not theSelectToRight) Then
            ptStart.WordLeft(1)
        End If

        Dim ptEnd As EnvDTE.EditPoint = ptStart.CreateEditPoint()
        ptEnd.WordRight(1)

        Return ptStart.GetText(ptEnd)
    End Function

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