FreakySources.CodeDataGenerator.InsertWithIndents C# (CSharp) Method

InsertWithIndents() private static method

private static InsertWithIndents ( StringBuilder source, int ind, string code ) : StringBuilder
source StringBuilder
ind int
code string
return StringBuilder
        private static StringBuilder InsertWithIndents(StringBuilder source, int ind, string code)
        {
            string sourceIndents;
            if (ind > 0)
            {
                int i = ind - 1;
                while (i >= 0 && char.IsWhiteSpace(source[i]) && source[i] != '\n' && source[i] != '\r')
                    i--;
                sourceIndents = source.GetSubstring(i + 1, ind - (i + 1)).ToString();
            }
            else
                sourceIndents = "";
            bool tabs = sourceIndents.Contains('\t');

            string[] codeLines = code.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

            int removedIndentsInd = 0;
            while (char.IsWhiteSpace(codeLines[0][removedIndentsInd]))
                removedIndentsInd++;

            for (int j = 0; j < codeLines.Length; j++)
            {
                if (codeLines[j] != "")
                {
                    codeLines[j] = codeLines[j].Substring(removedIndentsInd);
                    var codeLine = codeLines[j];
                    int i = 0;
                    while (i < codeLine.Length && char.IsWhiteSpace(codeLine[i]))
                        i++;
                    if (i > 0)
                    {
                        string codeIndents = codeLine.Remove(i);
                        if (tabs)
                            codeIndents = codeIndents.Replace(TabSpaces, "\t");
                        else
                            codeIndents = codeIndents.Replace("\t", TabSpaces);

                        codeLines[j] = codeIndents + codeLine.Substring(i);
                    }
                }
            }
            string indentedCode = string.Join(Environment.NewLine + sourceIndents, codeLines);

            return source.Insert(ind, indentedCode);
        }