PageRenderTime 15ms CodeModel.GetById 7ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/UiException/CodeFormatters/PlainTextCodeFormatter.cs

#
C# | 100 lines | 60 code | 18 blank | 22 comment | 5 complexity | 133a00732dadddd26485d811845509d4 MD5 | raw file
  1// ****************************************************************
  2// This is free software licensed under the NUnit license. You may
  3// obtain a copy of the license at http://nunit.org
  4// ****************************************************************
  5
  6using System;
  7using System.Collections.Generic;
  8using System.Text;
  9using NUnit.UiException.CodeFormatters;
 10
 11namespace NUnit.UiException.CodeFormatters
 12{
 13    /// <summary>
 14    /// Create a default FormattedCode for any string value.
 15    /// This should be used as a last resort when there is not ICodeFormatter
 16    /// that fit source code for an ErrorItem.
 17    /// </summary>
 18    public class PlainTextCodeFormatter :
 19        ICodeFormatter
 20    {
 21        #region ICodeFormatter Membres
 22
 23        /// <summary>
 24        /// Returns "Plain text"
 25        /// </summary>
 26        public string Language
 27        {
 28            get { return ("Plain text"); }
 29        }
 30
 31        public FormattedCode Format(string sourceCode)
 32        {
 33            DefaultTextManager text;
 34            byte[] byteArray;
 35            int[] strArray;
 36            int[] lineArray;
 37            int index;
 38            int posLineStart;
 39            int posChar;
 40
 41            UiExceptionHelper.CheckNotNull(sourceCode, "sourceCode");
 42
 43            sourceCode = PreProcess(sourceCode);
 44
 45            text = new DefaultTextManager();
 46            text.Text = sourceCode;
 47
 48            strArray = new int[text.LineCount];
 49            lineArray = new int[text.LineCount];
 50
 51            index = 0;
 52            posLineStart = 0;
 53            posChar = 0;
 54
 55            foreach (char c in sourceCode)
 56            {
 57                if (c == '\n')
 58                {
 59                    strArray[index] = posLineStart;
 60                    lineArray[index] = index;
 61                    posLineStart = posChar + 1;
 62                    index++;
 63                }
 64
 65                posChar++;
 66            }
 67
 68            if (index <= text.LineCount - 1)
 69            {
 70                strArray[index] = posLineStart;
 71                lineArray[index] = index;
 72            }
 73
 74            byteArray = new byte[text.LineCount];
 75
 76            return (new FormattedCode(sourceCode, strArray, byteArray, lineArray));
 77        }
 78
 79        #endregion
 80
 81        /// <summary>
 82        /// Prepare input text for the parsing stage.
 83        /// </summary>
 84        /// <param name="text">The text to be pre-processed.</param>
 85        /// <returns>A string ready to be parsed.</returns>
 86        public string PreProcess(string text)
 87        {
 88            if (text == null)
 89                return (null);
 90
 91            // this replace tabulation by space sequences. The reason is
 92            // that the technique used to measure strings at rendering time
 93            // fail to measure '\t' correctly and lines of text containing those
 94            // characters are badly aligned.
 95            //  The simplest thing to fix that is to remove tabs altogether.
 96
 97            return (text.Replace("\t", "    "));
 98        }
 99    }
100}