다음을 통해 공유


EditPoint 사용 예제

업데이트: 2007년 11월

다음 예제에서는 TextDocument 개체를 만들고 이 개체에 텍스트를 추가한 다음 몇 가지 EditPoint 개체 기능을 보여 줍니다.

예제

설명

이러한 기능에서는 1부터 시작하는 문자 오프셋을 사용하지만 IVsTextView 및 유사 인터페이스에서는 0부터 시작하는 문자 오프셋을 사용합니다. EditPoint는 VB 자동화를 지원하도록 디자인되어 있으므로 1부터 시작합니다.

코드

Public Sub OnConnection(ByVal application As Object, ByVal connectMode_
 As Extensibility.ext_ConnectMode, ByVal addInInst As Object,_
 ByRef custom As System.Array) Implements_
 Extensibility.IDTExtensibility2.OnConnection

    applicationObject = CType(application, EnvDTE.DTE)
    addInInstance = CType(addInInst, EnvDTE.AddIn)
    EditPointExample1(applicationObject)
End Sub
Sub EditPointExample1(ByVal dte As DTE)
    Dim objTextDoc As TextDocument
    Dim objEditPt As EditPoint, iCtr As Integer

    ' Create a new text file.
    applicationObject.ItemOperations.NewFile("General\Text File")

    ' Get a handle to the new document and create an EditPoint.
    objTextDoc = applicationObject.ActiveDocument.Object("TextDocument")
    objEditPt = objTextDoc.StartPoint.CreateEditPoint

    ' Insert ten lines of text.
    For iCtr = 1 To 10
        objEditPt.Insert("This is a test." & Chr(13))
    Next iCtr

    ' Replace the eighth word on the third line with a new word.
    objEditPt.StartOfDocument()
    objEditPt.LineDown(3)
    objEditPt.WordRight(3)
    objEditPt.Delete(4)
    objEditPt.Insert("raid")

    ' Indent a line.
    objEditPt.LineDown(2)
    objEditPt.StartOfLine()
    objEditPt.Indent()

    ' Set some bookmarks.
    objEditPt.LineDown(1)
    ' Set the first bookmark.
    objEditPt.SetBookmark()
    ' Set the second bookmark on the next line.
    objEditPt.LineDown(1)
    objEditPt.SetBookmark()
    ' Go back to the first bookmark.
    objEditPt.PreviousBookmark()
    ' Add some text there.
    objEditPt.Insert("BOOKMARK 1: ")
    ' Do the same thing at the next bookmark.
    objEditPt.NextBookmark()
    objEditPt.Insert("BOOKMARK 2: ")

    ' Print the current line and character offset in the buffer. 
    MsgBox("Line and offset: " & objEditPt.Line, _
    objEditPt.LineCharOffset())

    ' Capitalize the third word of the second line.
    objEditPt.StartOfDocument()
    objEditPt.LineDown(2)
    objEditPt.WordRight(3)
    objEditPt.ChangeCase(4, vsCaseOptions.vsCaseOptionsCapitalize)

    ' Remove the spaces between the words.
    objEditPt.DeleteWhitespace(vsWhitespaceOptions. _
    vsWhitespaceOptionsHorizontal)
End Sub

코드

Using System.Windows.Forms;
public void OnConnection(object application,
 Extensibility.ext_ConnectMode connectMode, object addInInst,
 ref System.Array custom)
{
    applicationObject = (_DTE)application;
    addInInstance = (AddIn)addInInst;
    EditPointExample1((DTE)applicationObject);
}
public void EditPointExample1( DTE dte ) 
{ 
    TextDocument objTextDoc = null; 
    EditPoint objEditPt = null; int iCtr = 0; 

    // Create a new text file.
    applicationObject.ItemOperations.NewFile( @"General\Text File",
 "", "{00000000-0000-0000-0000-000000000000}" ); 

    // Get a handle to the new document and create an EditPoint.
    objTextDoc = ( ( EnvDTE.TextDocument )(
 applicationObject.ActiveDocument.Object( "TextDocument" ) ) ); 
    objEditPt = objTextDoc.StartPoint.CreateEditPoint(); 

    // Insert ten lines of text.
    for ( iCtr=1; iCtr<=10; iCtr++ ) 
    { 
        objEditPt.Insert( "This is a test." + "\n"); 
    } 

    // Replace the eighth word on the third line with a new word.
    objEditPt.StartOfDocument(); 
    objEditPt.LineDown( 3 ); 
    objEditPt.WordRight( 3 ); 
    objEditPt.Delete( 4 ); 
    objEditPt.Insert( "raid" ); 
    
    // Indent a line.
    objEditPt.LineDown( 2 ); 
    objEditPt.StartOfLine(); 
    objEditPt.Indent( null, 1 ); 

    // Set some bookmarks.
    objEditPt.LineDown( 1 ); 
    // Set the first bookmark.
    objEditPt.SetBookmark(); 
    // Set the second bookmark on the next line.
    objEditPt.LineDown( 1 ); 
    objEditPt.SetBookmark(); 
    // Go back to the first bookmark.
    objEditPt.PreviousBookmark(); 
    // Add some text there.
    objEditPt.Insert( "BOOKMARK 1: " ); 
    // Do the same thing at the next bookmark.
    objEditPt.NextBookmark(); 
    objEditPt.Insert( "BOOKMARK 2: " ); 
    // Print the current line and character offset in the buffer. 
    MessageBox.Show( "Line and offset: " + objEditPt.Line); 

    // Capitalize the third word of the second line.
   objEditPt.StartOfDocument(); 
    objEditPt.LineDown( 2 ); 
    objEditPt.WordRight( 3 ); 
    objEditPt.ChangeCase( 4, vsCaseOptions.vsCaseOptionsCapitalize ); 

    // Remove the spaces between the words.
    objEditPt.DeleteWhitespace( vsWhitespaceOptions.vsWhitespaceOptionsHorizontal ); 
}

참고 항목

참조

EditPoint

EditPoint2

TextPoint

TextDocument