// Copyright (c) 2007 Omer Rauchwerger (a.k.a rauchy) (omer@rauchy.net) // All rights reserved. // // This file is part of Regionerate. // // This program is free software; you can redistribute it and/or modify it // under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 3 of the License, // or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see . using EnvDTE; using EnvDTE80; namespace Rauchy.Regionerate.Presentation.Addins.VisualStudio2005 { /// /// Provides basic operations on . /// internal class TextOperations { #region Fields private readonly DTE2 _applicationObject; private readonly TextSelection _selection; #endregion #region Constructors /// /// Initializes a new instance of the class. /// /// The application object. public TextOperations( DTE2 applicationObject ) { _applicationObject = applicationObject; _selection = ( TextSelection )_applicationObject.ActiveDocument.Selection; } #endregion #region Methods /// /// Disables editing of the document. /// public void LockEditing() { _applicationObject.ActiveDocument.ReadOnly = true; } /// /// Enables editing of the document. /// public void UnlockEditing() { _applicationObject.ActiveDocument.ReadOnly = false; } /// /// Collapses all regions. /// public void CollapseAllRegions() { // Oh boy, seems like this is a known issue for VS 2010 // See: http://connect.microsoft.com/VisualStudio/feedback/details/499569/collapse-to-definition-not-collapsing-all // I think this justifies a hack. No region collapsing if running VS 2010. if (_applicationObject.Version != "10.0") { _applicationObject.ExecuteCommand("Edit.StopOutlining", ""); _applicationObject.ExecuteCommand("Edit.StartAutomaticOutlining", ""); _selection.StartOfDocument(true); while (_selection.FindText("#region", (int) vsFindOptions.vsFindOptionsNone)) { _selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn, true); _applicationObject.ExecuteCommand("Edit.ToggleOutliningExpansion", ""); _selection.StartOfDocument(true); } _selection.Cancel(); } } /// /// Reads all text from the active document. /// /// public string ReadAll() { _selection.SelectAll(); string text = _selection.Text; _selection.Cancel(); return text; } public void GoToText( string text ) { _selection.FindText( text, ( int )vsFindOptions.vsFindOptionsMatchInHiddenText ); _selection.Cancel(); } public string GetCurrentLineText() { _selection.SelectLine(); return _selection.Text.Trim(); } /// /// Writes the text to the active document, replacing existing text. /// /// The text. public void WriteText( string text ) { _selection.SelectAll(); _selection.Insert( text, ( int )vsInsertFlags.vsInsertFlagsContainNewText ); _selection.Cancel(); } #endregion } }