Postupy: Řízení editoru kódu (Visual Basic)

Doplňky Visual Studio jsou ve verzi aplikace Visual Studio 2013 zastaralé.Měli byste upgradovat doplňky na rozšíření VSPackage.Další informace o upgradu viz Nejčastější dotazy: Převádění doplňků na rozšíření VSPackage.

Editor kód Visual Studio je textový editor, který přizpůsobuje jazykové služby jako Visual Basic, Visual C++ a Visual C#.Text je zapsán do vyrovnávací paměti, která se zobrazí v textovém dokumentu.Použitím objektů modelu automatizace editoru Visual Studio; můžete měnit text na pozadí ve vyrovnávací paměti textu nebo v zobrazení.

Čtyři hlavní objekty používané k řízení textu v editoru kódu jsou:

Název objektu

Description

TextSelection

Používá se pro práci s textem v zobrazení.Objekt TextSelection představuje bod vkladu (nebo stříšku) nebo vybraný text ve viditelném dokumentu.

TextPoint

Pevné umístění ve vyrovnávací paměti textu.

EditPoint2

Podobně jako objekt TextPoint, ale může být přesunut a může změnit text ve vyrovnávací paměti.

VirtualPoint

Podobně jako objekt TextPoint s tím rozdílem, že obsahuje další funkce k vyhledání pozice textu ve virtuálním prostoru.

Dva hlavní objekty, které slouží k manipulaci s editorem kódu, jsou objekty TextSelection a EditPoint2.Hlavní rozdíly mezi nimi jsou:

  • TextSelection představuje výběr viditelného textu.Změna polohy změní výběr v zobrazení.EditPoint2 není vázána k žádné komponentě uživatelského rozhraní (UI), takže změna polohy nic v zobrazení nezmění.

  • Protože TextSelection představuje viditelný výběr, je pouze jeden objekt TextSelection v dokumentu.I když můžete mít více objektů TextSelection v dokumentu, všechny odkazují na stejný viditelný výběr a všechny mají stejnou pozici.Můžete mít kolik EditPoint2 objektů chcete a každý může mít jinou pozici.

  • Metody objektu TextSelection jsou navrženy, aby přímo korespondovaly s uživatelskými akcemi, a metody objektu EditPoint2 nejsou.V důsledku toho některé metody EditPoint2 provádějí akce, které nemůže provést jedna metoda TextSelection, zatímco jiné metody EditPoint2 jsou přesnější ve funkci než metody TextSelection.To je také důvodem, proč je TextSelection bohatší na vlastnosti a metody než EditPoint2.

Pomocí těchto objektů můžete:

  • Vybírejte, přidávejte, odstraňujte a přesouvejte text ve vyrovnávací paměti nebo v zobrazení.

  • Přesuňte bod vložen kolem vyrovnávací paměti nebo zobrazení.

  • Odsazení textu ve vyrovnávací paměti nebo zobrazení.

  • Vložte, odeberte a přejděte do záložek.

  • Přidání nebo odstranění textu, včetně prázdných znaků.

  • Najděte nebo nahraďte text podle zadaného vzoru.

  • Vytvořte oddíl popisující kód a text.

  • Dotazované informace o textu, jako je například umístění textu, horní a dolní část dokumentu, vybrané oblasti textu a tak dále.

Následující příklady ukazují, jak odkazovat a využívat různé členy modelu automatizace editoru.Další informace o spuštění tohoto vzorového kódu naleznete v tématu Postupy: Kompilace a spuštění příkladů kódu objektu automatizace.

Další příklady znázorňující používání editoru modelu automatizace, viz příklady na webu Vzorky automatizace pro Visual Studio (https://msdn2.microsoft.com/vstudio/aa718336.aspx).

[!POZNÁMKA]

Dialogová okna a příkazy nabídek, které vidíte, se mohou lišit od těch popsaných v nápovědě v závislosti na aktivních nastaveních nebo edici.Tyto postupy byly vyvinuty s aktivním Obecným vývojovým nastavením.Chcete-li změnit nastavení, zvolte NastaveníImportu a Exportu v nabídce Nástroje.Další informace naleznete v tématu Přizpůsobení nastavení pro vývoj v sadě Visual Studio.

HTMLWindow3, vsHTMLPanes a vsHTMLViews byly přidány se zavedením rozděleného zobrazení v editoru HTML Visual Studio 2008.Rozdělené zobrazení odděluje prvky karty a zobrazení okna úprav jazyka HTML.Přepnutí zobrazení (na Návrh nebo Zdroj) nemusí nutně znamenat přepnutí kartu (Návrh/Rozdělit/Zdroj).Například po klepnutí na kartu Rozdělit přepínání zobrazení mezi Návrhem a Zdrojem nemění kartu, pouze aktivuje nebo deaktivuje části Návrh a Zdroj v rozděleném zobrazení.

Příklad

Například pro ActivePoint.Tento příklad také znázorňuje použití StartOfLine, DisplayColumn a EndOfLine.Před spuštěním tohoto příkladu otevřete soubor kódu nebo textový dokument vVisual Studio, přidejte nějaký text a vyberte část textu.

' Example for TextSelection.ActivePoint.
'
Sub ActivePointExample()
    ' Before running this example, open a text document.
    Dim objSel As TextSelection = DTE.ActiveDocument.Selection
    Dim objActive As VirtualPoint = objSel.ActivePoint
     ' Collapse the selection to the beginning of the line.
    objSel.StartOfLine()
     ' objActive is "live", tied to the position of the actual 
     ' selection, so it will reflect the new position.
    Dim iCol As Long = objActive.DisplayColumn
     ' Move the selection to the end of the line.
        objSel.EndOfLine()

    MsgBox("The length of the insertion point line is " & _
    (objActive.DisplayColumn - iCol) & " display characters.")
End Sub

Například pro AnchorPoint.Tento příklad také znázorňuje použití DisplayColumn, Line, StartOfDocument a EndOfDocument.Před spuštěním tohoto příkladu otevřete soubor kódu nebo textový dokument vVisual Studio, přidejte nějaký text a vyberte část textu.

' Example for TextSelection.AnchorPoint.
'
Sub AnchorPointExample()
    ' Before running this example, open a text document.
    Dim objSel As TextSelection = DTE.ActiveDocument.Selection
    Dim objAnchor As VirtualPoint = objSel.AnchorPoint
    ' objAnchor is "live", tied to the position of the actual 
    ' selection, so it will reflect changes. iCol and iRow are created 
    ' here to save a "snapshot" of the anchor point's position at this 
    ' time.
    Dim iCol As Long = objAnchor.DisplayColumn
    Dim iRow As Long = objAnchor.Line
    ' As the selection is extended, the active point moves but the 
    ' anchor point remains in place.
    objSel.StartOfDocument(True)
    objSel.EndOfDocument(True)

    If (iCol = objAnchor.DisplayColumn And iRow = objAnchor.Line) Then
        MsgBox("The anchor point has remained in place at row " & _
        iRow & ", display column " & iCol)
    End If
End Sub

Například pro Insert.Tento příklad také znázorňuje použití IsEmpty, WordLeft, WordRight, Text, Delete a MoveToPoint.Před spuštěním tohoto příkladu otevřete soubor kódu nebo textový dokument v Visual Studio a přidejte nějaký text.

' Example for TextSelection.Insert.
'
Sub InsertExample()
    ' Before running this example, open a text document.
    Dim objSel As TextSelection = DTE.ActiveDocument.Selection
    If objSel.IsEmpty Then
        ' If there is no text selected, swap the words before and after 
        ' the insertion point. We begin by selecting the word before 
        ' the insertion point.
        objSel.WordLeft(True)
        If Not objSel.IsEmpty Then
            ' We can continue only if the selection was not already at 
            ' the beginning of the document.
            Dim strBefore As String = objSel.Text

            ' The text is saved in strBefore; now delete it and move 
            ' past the following word.
            objSel.Delete()
            objSel.WordRight(True)
            If objSel.Text.StartsWith(" ") Or _
            objSel.Text.StartsWith(Microsoft.VisualBasic. _
            ControlChars.Tab) Then
                ' The previous call to WordRight may have skipped some 
                ' white space instead of an actual word. In that case, 
                 ' we should call it again.
                objSel.WordRight(True)
            End If

            ' Insert the new text at the end of the selection.
            objSel.Insert(strBefore, _
            vsInsertFlags.vsInsertFlagsInsertAtEnd)
        End If
    Else
        ' If some text is selected, replace the following word with the 
        ' selected text.
        Dim strSelected As String = objSel.Text

        objSel.MoveToPoint(objSel.BottomPoint)
        objSel.WordRight(True)
        If objSel.Text.StartsWith(" ") Or _
        objSel.Text.StartsWith(Microsoft.VisualBasic. _
        ControlChars.Tab) Then
            ' The previous call to WordRight may have skipped some 
            ' white space instead of an actual word. In that case, we 
            ' should call it again.
            objSel.WordRight(True)
        End If

        ' Insert the text, overwriting the existing text and leaving 
        ' the selection containing the inserted text.
        objSel.Insert(strSelected, _
        vsInsertFlags.vsInsertFlagsContainNewText)
    End If
End Sub

Například pro FindPattern.Tento příklad také znázorňuje použití SelectLine.Před spuštěním tohoto příkladu je třeba otevřít textový dokument nebo soubor kódu v Visual Studio a přidat nějaký text.

' Example for TextSelection.FindPattern.
'
Sub FindPatternExample()
    ' Before running this example, open a text document.
    Dim objSel As TextSelection = DTE.ActiveDocument.Selection

    ' Advance to the next Visual Basic function beginning or end by 
    ' searching for  "Sub" with white space before and after it.
    If objSel.FindPattern(":WhSub:Wh", _
    vsFindOptions.vsFindOptionsRegularExpression) Then
        ' Select the entire line.
        objSel.SelectLine()
    End If
End Sub

Například pro OutlineSection.Tento příklad také znázorňuje použití StartOfDocument, Line, LineCharOffset, FindPattern, SwapAnchor, MoveToLineAndOffset a LineDown.Před spuštěním tohoto příkladu otevřete dokument kódu v Visual Studio obsahující blok #if _DEBUG…#endif.

' Example for TextSelection.OutlineSection.
'
Sub OutlineSectionExample()
    ' Before running this example, open a code document
    ' containing a #if _DEBUG…#endif block.
    Dim objSel As TextSelection = DTE.ActiveDocument.Selection

    ' Move to the beginning of the document so we can iterate over the 
    ' whole thing.
    objSel.StartOfDocument()
    While objSel.FindPattern("#if _DEBUG")
        ' If we found the beginning of a debug-only section, save the 
        ' position.
        Dim lStartLine As Long = objSel.TopPoint.Line
        Dim lStartColumn As Long = objSel.TopPoint.LineCharOffset

        ' Look for the end.
        If objSel.FindPattern("#endif") Then
            ' Select the entire section and outline it.
            objSel.SwapAnchor()
            objSel.MoveToLineAndOffset(lStartLine, lStartColumn, True)
            objSel.OutlineSection()
            objSel.LineDown()
        End If
    End While
End Sub

Příklad otevře textový dokument a vytvoří seznam všech dostupných příkazů do tohoto dokumentu.

  ' This generates a text document listing all available command names.
Sub CommandNamesCollapseExample()
  Dim Cmd As Command
  Dim Commands As Commands = DTE.Commands 
  Dim PrjItem As ProjectItem
  Dim Doc As Document
  Dim TxtDoc As TextDocument
  DTE.ItemOperations.NewFile ("General\Text File")
  Set Doc = ActiveDocument
  Set TxtDoc = Doc.Object("TextDocument")
  For Each Cmd In Commands
  If (Cmd.Name <> "") Then
    TxtDoc.Selection.Text = Cmd.Name & vbLF
    TxtDoc.Selection.Collapse
  End If
  Next
End Sub

Příklad objektu HTMLWindow.Tento příklad také znázorňuje použití ActiveDocument, ActiveWindow, Window, CurrentTab, CurrentTabObject, ActivePane, StartPoint, CreateEditPoint, FindPattern a InsertFromFile.Před spuštěním tohoto příkladu otevřete dokument HTML v Visual Studio.

' Example for HTMLWindow object

Sub HTMLWindowExample()
   ' Open an HTML document before running this sample.
   If TypeOf ActiveDocument.ActiveWindow.Object Is HTMLWindow Then
      ' Ask the user for a file to insert into the body of the HTML 
      ' document. This file should be an HTML fragment.
      Dim strFile As String = InputBox("Enter the name of a file to _
      insert at the end of the HTML document:")
      ' Get the HTMLWindow object and determin which tab is currently 
      ' active.
      Dim objHTMLWin As HTMLWindow = ActiveDocument.ActiveWindow.Object
      Dim Tab As vsHTMLTabs = objHTMLWin.CurrentTab

      ' Switch to the "source" tab.
      objHTMLWin.CurrentTab = vsHTMLTabs.vsHTMLTabsSource

      ' Get an EditPoint at the start of the text.
      Dim objTextWin As TextWindow = objHTMLWin.CurrentTabObject
      Dim objEP As EditPoint = _
      objTextWin.ActivePane.StartPoint.CreateEditPoint

      ' Look for the end of the document body.
      If objEP.FindPattern("</body>") Then
         ' Insert the contents of the file.
         objEP.InsertFromFile(strFile)
      End If

      ' Switch back to the original view of the HTML file.
       objHTMLWin.CurrentTab = Tab
   Else
      MsgBox("You must open an HTML document.")
   End If
End Sub

Viz také

Úkoly

Postupy: Změna charakteristik okna

Postupy: Vytvoření doplňku

Návod: Vytvoření průvodce

Koncepty

Graf modelu objektů automatizace

Další zdroje

Vytváření a řízení oken prostředí

Vytváření doplňků a průvodců

Referenční dokumentace automatizace a rozšíření