<html>
<head>
    <base href="https://wiki.asterisk.org/wiki">
            <link rel="stylesheet" href="/wiki/s/2036/1/7/_/styles/combined.css?spaceKey=TOP&amp;forWysiwyg=true" type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://wiki.asterisk.org/wiki/display/TOP/Visual+Studio+Macro+for+Ensuring+Blank+Line+Added+To+Source+File">Visual Studio Macro for Ensuring Blank Line Added To Source File</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://wiki.asterisk.org/wiki/display/~khunt">Ken Hunt</a>
    </h4>
        <br/>
                         <h4>Changes (1)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >{noformat} <br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">#h6 Save  <br>Select the Save icon or File &gt; Save MyMacros. <br> <br></td></tr>
            <tr><td class="diff-unchanged" >h5. Random Thoughts <br># This could have been a bit cleaner if there were an event sent prior to the save. As it is, the only useful event to trigger off of in the Visual Studio Automation API is the *Saved* event, which is sent right after the document has been saved. While there is a *Closing* event (which is called, handily, prior to the close), it is of no value when the user is saving and keeping the document open. So the approach I chose is to handle *Saved*, check to see if you need to modify the file, and, if we do modify it, save it again. At least this means *Save As...&quot; works OK, since the file will have been renamed prior to getting into our handler. And it seems to work fine when the save occurs as a result of closing a modified file and the user replies OK to the Save Modified File prompt.  <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <p>This page describes how to edit your Visual Studio environment to insure a blank is at the end of every source file edited. This is useful when editing code that has to be compiled by gcc as well as Visual C++. </p>

<h5><a name="VisualStudioMacroforEnsuringBlankLineAddedToSourceFile-VisualStudio2008andVisualStudio2010"></a>Visual Studio 2008 and Visual Studio 2010</h5>

<ol>
        <li><h6><a name="VisualStudioMacroforEnsuringBlankLineAddedToSourceFile-OpenthemacrosIDE."></a>Open the macros IDE.</h6>
<p>Select menu item Tools &gt; Macros &gt; Macros IDE. </p></li>
        <li><h6><a name="VisualStudioMacroforEnsuringBlankLineAddedToSourceFile-EdittheEnvironmentEventsmacro."></a>Edit the EnvironmentEvents macro.</h6>
<p>Select the Class View tab of the Macros IDE. Under My Macros, double-click the EnvironmentEvents node. This will open the EnvironementEvents macro in the editor.</p></li>
        <li><h6><a name="VisualStudioMacroforEnsuringBlankLineAddedToSourceFile-AddahandlerfortheDocumentSavedevent."></a>Add a handler for the DocumentSaved event.</h6>
<p>In the EnvironmentEvents macro editor, select DocumentEvents from the combo box at the top-left of the editor. Then, from the Declarations combo box to the right, select DocumentSaved. This will result in an empty handler being inserted into the macro code named DocumentEvents_DocumentSaved.</p></li>
        <li><h6><a name="VisualStudioMacroforEnsuringBlankLineAddedToSourceFile-Inthenewhandler%2Centerthiscode%3A"></a>In the new handler, enter this code:</h6>
<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent">
<pre>    Private Sub DocumentEvents_DocumentSaved(ByVal Document As EnvDTE.Document) Handles DocumentEvents.DocumentSaved
        '
        ' This handler will add a newline to end of file if it doesn't already have one.
        '
        Dim textSelection As EnvDTE.TextSelection
        ' Get a TextSelection object for the doc we are editing. 
        textSelection = CType(DTE.ActiveDocument.Selection(), EnvDTE.TextSelection)
        ' Get some information about insertion point and top visible line so 
        ' we can restore it later. 
        Dim originalLine As Integer
        originalLine = textSelection.AnchorPoint.Line
        Dim originalOffset As Integer
        originalOffset = textSelection.AnchorPoint.LineCharOffset
        Dim viewTop As EnvDTE.TextPoint
        viewTop = textSelection.TextPane.StartPoint

        ' Move to the end of the document.
        textSelection.EndOfDocument(False)
        Dim pt As VirtualPoint
        pt = textSelection.BottomPoint()
        ' Select last line of text in the file. 
        textSelection.GotoLine(pt.Line, True)
        If textSelection.Text.Length &lt;&gt; 0 Then
            textSelection.Text = textSelection.Text &amp; vbCrLf
            ' We just altered the document, so we need to save again. 
            DTE.ActiveDocument.Save()
        End If
        ' Restore the insertion point and top line. 
        textSelection.MoveToLineAndOffset(originalLine, originalOffset, False)
        textSelection.TextPane.TryToShow(viewTop, vsPaneShowHow.vsPaneShowTop)
    End Sub
</pre>
</div></div></li>
</ol>


<p>#h6 Save <br/>
Select the Save icon or File &gt; Save MyMacros.</p>

<h5><a name="VisualStudioMacroforEnsuringBlankLineAddedToSourceFile-RandomThoughts"></a>Random Thoughts</h5>
<ol>
        <li>This could have been a bit cleaner if there were an event sent prior to the save. As it is, the only useful event to trigger off of in the Visual Studio Automation API is the <b>Saved</b> event, which is sent right after the document has been saved. While there is a <b>Closing</b> event (which is called, handily, prior to the close), it is of no value when the user is saving and keeping the document open. So the approach I chose is to handle <b>Saved</b>, check to see if you need to modify the file, and, if we do modify it, save it again. At least this means *Save As..." works OK, since the file will have been renamed prior to getting into our handler. And it seems to work fine when the save occurs as a result of closing a modified file and the user replies OK to the Save Modified File prompt.</li>
        <li>This page will be hit a lot once it goes public by people who have no interest in Asterisk SCF. C'est la vie.</li>
</ol>

    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://wiki.asterisk.org/wiki/users/viewnotifications.action" class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://wiki.asterisk.org/wiki/display/TOP/Visual+Studio+Macro+for+Ensuring+Blank+Line+Added+To+Source+File">View Online</a>
        |
        <a href="https://wiki.asterisk.org/wiki/pages/diffpagesbyversion.action?pageId=5767698&revisedVersion=17&originalVersion=16">View Changes</a>
                |
        <a href="https://wiki.asterisk.org/wiki/display/TOP/Visual+Studio+Macro+for+Ensuring+Blank+Line+Added+To+Source+File?showComments=true&amp;showCommentArea=true#addcomment">Add Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>