CHAPTER 11
Each source file being edited is represented in Visual Studio as a document object, which has the necessary properties to access the file content, save it, etc. In this chapter, we will look at how to use the document object.
The document object can be obtained from any source window or by asking Visual Studio for the active document. Any “Document” type window will include a reference to the document object associated with it. The following code sample illustrates a few ways to get the document object.
|
Document ActiveDoc = _applicationObject.ActiveDocument; foreach (Document CurrentDoc in _applicationObject.Documents) { } foreach(Window CurWindow in _applicationObject.Windows) { if (CurWindow.Kind == "Document") { Document CurDoc = CurWindow.Document; } } |
The document object has some basic properties to allow you to determine the file and path name, which windows the document is loaded in, which project item it is associated with, etc.:
With these basic properties, you can navigate from the document to windows or to the project item within the solution. You could back up a copy of the document before you make any changes, etc.
In addition, there are a few methods you can use with the document object to save the file, activate its window, close the document, etc.:
You can use the basic properties and methods to perform operations on the document as a whole. In later chapters, we will explore getting the code and text from the document and manipulating it as well.
Each document object has an object method which provides access to the text content of the document and allows you to do some basic edit operations in the document. You can get the associated text document with the following code sample.
|
Document theDoc = _applicationObject.ActiveDocument; TextDocument theTextDoc = (TextDocument)theDoc.Object("TextDocument"); |
The text document object has two properties of interest to help editing the text in the document window. The start and end points are objects representing the first and last points in the file. You can create an edit point from either of these objects if you want to do some basic editing. We cover how to edit with edit points in the next chapter. An edit point is the programmatic equivalent of the user’s cursor location while editing. Edit operations take place from the edit point in the document.
The text document has some basic manipulation methods for the document. These include:
These methods allow you to easily manipulate the text content.
There is a great number of websites that will convert your C# code to VB.NET, but let’s assume we wanted to tackle such a beast ourselves (which is way beyond the scope of this book). The following code sample shows how we could use the MarkText and ReplacePattern methods to get started.
|
Document ActiveDoc = _applicationObject.ActiveDocument; TextDocument TextDoc = (TextDocument)ActiveDoc.Object("TextDocument"); if (TextDoc != null) { TextDoc.MarkText("public void"); // Bookmark all the lines we are going to tweak. TextDoc.ReplacePattern("public void", "sub"); } |
In this simple example, we’ve searched for all C# void methods and converted them to sub calls (the VB equivalent). We’ve also marked the line we’ve changed so the user can navigate to the bookmarked lines to review the code changes.
The document and text document objects can perform some basic file manipulations and global text updates, but in the next two chapters we explore code modification in more detail, including the built-in Visual Studio code-parser class, the code model.