// 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 System; using System.Collections.Generic; using System.Text; using Rauchy.Regionerate.Shared.Components; namespace Rauchy.Regionerate.ServiceLayer.Components.Tools { /// /// TODO: comment /// public abstract class TypeCodeTextRemover { #region Methods (3) /// /// Removes text from the specified code. /// /// The code. /// The same code blocks, without the unwanted text. /// /// Thrown when code is null. /// /// /// Handles type-level text only, does not remove method-level, property-level or region-level text. /// public string Remove( string code ) { if ( code == null ) { throw new ArgumentNullException( "code" ); } IList lines = code.GetLines(); StringBuilder filteredCode = new StringBuilder(); int currentIndentationlevel = 0; bool insideRegion = false; foreach ( string line in lines ) { if ( line.Trim().StartsWith( "#region" ) ) { insideRegion = true; } else if ( line.Trim().StartsWith( "#endregion" ) ) { insideRegion = false; } if ( insideRegion ) { // Keep lines inside existing regions. filteredCode.Append( line ); } else { // Not inside region, check if inside method or inside property. currentIndentationlevel += CountInstances( line, Language.BeginBlock.ToCharArray() ); currentIndentationlevel -= CountInstances( line, Language.EndBlock.ToCharArray() ); if ( currentIndentationlevel >= 2 ) { // Indentation level is above the safe level (usually meaning we are // dealing with a property or method), there is no need to remove regions here. filteredCode.Append( line ); } else { // Indentation level is below the safe level (usually meaning we are // dealing with a class or struct), remove this line if it whitespace. bool isRegionDeclaration = ShouldRemove( line ); if ( !isRegionDeclaration ) { filteredCode.Append( line ); } } } } return filteredCode.ToString(); } /// /// Determines wether a line, when not placed in a method or property (or such), should be removed. /// /// The line. protected abstract bool ShouldRemove( string line ); /// /// Counts the instances of a specific characters in a string. /// /// The line. /// The characters. /// private static int CountInstances( string line, params char[] characters ) { return line.Split( characters ).Length - 1; } #endregion } }