Esempio n. 1
0
        private static void SpaceHeader(IList <LogicalCodeLine> header, IIndenterSettings settings)
        {
            var commentSkipped = false;
            var commentLines   = 0;

            for (var i = header.Count - 2; i >= 0; i--)
            {
                if (!commentSkipped && header[i].IsCommentBlock)
                {
                    commentLines++;
                    continue;
                }

                commentSkipped = true;
                if (header[i].IsEmpty)
                {
                    header.RemoveAt(i);
                }
                else
                {
                    header.Insert(header.Count - 1 - commentLines,
                                  new LogicalCodeLine(
                                      Enumerable.Repeat(new AbsoluteCodeLine(string.Empty, settings),
                                                        settings.LinesBetweenProcedures), settings));
                    return;
                }
            }
        }
Esempio n. 2
0
        public AbsoluteCodeLine(string code, IIndenterSettings settings, AbsoluteCodeLine previous)
        {
            _settings = settings;
            Previous  = previous;

            if (code.EndsWith(StupidLineEnding))
            {
                _code             = code.Substring(0, code.Length - StupidLineEnding.Length);
                _stupidLineEnding = true;
            }
            else
            {
                _code = code;
            }

            Original = code;

            _escaper = new StringLiteralAndBracketEscaper(_code);
            _code    = _escaper.EscapedString;

            ExtractLineNumber();
            ExtractEndOfLineComment();

            _segments = _code.Split(new[] { ": " }, StringSplitOptions.None);
        }
Esempio n. 3
0
        public AbsoluteCodeLine(string code, IIndenterSettings settings)
        {
            _settings = settings;
            _code     = code;
            Original  = code;

            ExtractStringLiterals();
            ExtractLineNumber();
            ExtractEndOfLineComment();

            _segments = _code.Split(new[] { ": " }, StringSplitOptions.None);
        }
Esempio n. 4
0
 private void TransferSettingsToView(IIndenterSettings toLoad)
 {
     AlignCommentsWithCode = toLoad.AlignCommentsWithCode;
     AlignContinuations    = toLoad.AlignContinuations;
     AlignDimColumn        = toLoad.AlignDimColumn;
     AlignDims             = toLoad.AlignDims;
     EndOfLineCommentColumnSpaceAlignment = toLoad.EndOfLineCommentColumnSpaceAlignment;
     EndOfLineCommentStyle            = toLoad.EndOfLineCommentStyle;
     ForceCompilerDirectivesInColumn1 = toLoad.ForceCompilerDirectivesInColumn1;
     ForceDebugStatementsInColumn1    = toLoad.ForceDebugStatementsInColumn1;
     IgnoreOperatorsInContinuations   = toLoad.IgnoreOperatorsInContinuations;
     IndentCase = toLoad.IndentCase;
     IndentEnumTypeAsProcedure   = toLoad.IndentEnumTypeAsProcedure;
     IndentCompilerDirectives    = toLoad.IndentCompilerDirectives;
     IndentEntireProcedureBody   = toLoad.IndentEntireProcedureBody;
     IndentFirstCommentBlock     = toLoad.IndentFirstCommentBlock;
     IndentFirstDeclarationBlock = toLoad.IndentFirstDeclarationBlock;
     IndentSpaces = toLoad.IndentSpaces;
     VerticallySpaceProcedures = toLoad.VerticallySpaceProcedures;
     LinesBetweenProcedures    = toLoad.LinesBetweenProcedures;
 }
Esempio n. 5
0
 public LogicalCodeLine(IEnumerable <AbsoluteCodeLine> lines, IIndenterSettings settings)
 {
     _lines    = lines.ToList();
     _settings = settings;
 }
Esempio n. 6
0
 public LogicalCodeLine(AbsoluteCodeLine first, IIndenterSettings settings)
 {
     _lines.Add(first);
     _settings = settings;
 }
Esempio n. 7
0
        private IEnumerable <LogicalCodeLine> BuildLogicalCodeLines(IEnumerable <string> lines, out IIndenterSettings settings)
        {
            settings = _settings.Invoke();
            var              logical  = new List <LogicalCodeLine>();
            LogicalCodeLine  current  = null;
            AbsoluteCodeLine previous = null;

            foreach (var line in lines)
            {
                var absolute = new AbsoluteCodeLine(line, settings, previous);
                if (current == null)
                {
                    current = new LogicalCodeLine(absolute, settings);
                    logical.Add(current);
                }
                else
                {
                    current.AddContinuationLine(absolute);
                }

                if (!absolute.HasContinuation)
                {
                    current = null;
                }
                previous = absolute;
            }
            return(logical);
        }
Esempio n. 8
0
 public AbsoluteCodeLine(string code, IIndenterSettings settings) : this(code, settings, null)
 {
 }
Esempio n. 9
0
        private int FunctionAlign(IIndenterSettings settings, string line, bool firstLine, out int paramOffset)
        {
            if (firstLine)
            {
                CurrentAlignment.Clear();
            }

            //Convert and numbers at the start of the line to spaces
            int testToken;
            var leftPadding = 0;
            var space       = line.IndexOf(' ');

            if (space > 0 && int.TryParse(line, out testToken))
            {
                line        = line.Substring(space);
                leftPadding = space + 1;
            }

            leftPadding += (line.Length - line.TrimStart().Length);
            var iFirstThisLine = CurrentAlignment.Count;

            line = line.Trim();
            //Skip over stuff that we don't want to locate the start off
            var skip = SkipWhenFindingFunctionStart.Where(sMatch => line.StartsWith(sMatch)).Sum(sMatch => sMatch.Length) + 2;

            for (var charIndex = skip; charIndex <= line.Length; charIndex++)
            {
                var character = line.Substring(charIndex - 1, 1);
                switch (character)
                {
                case "\"":
                    //A String => jump to the end of it
                    charIndex = line.IndexOf("\"", charIndex + 1, StringComparison.InvariantCulture);
                    break;

                case "(":
                    //Start of another function => remember this position
                    CurrentAlignment.Push(new Tuple <string, int>("(", charIndex + leftPadding + 2));
                    CurrentAlignment.Push(new Tuple <string, int>(",", charIndex + leftPadding + 3));
                    break;

                case ")":
                    //Function finished => Remove back to the previous open bracket
                    while (CurrentAlignment.Any() && (!CurrentAlignment.Peek().Item1.Equals("(") || CurrentAlignment.Count == iFirstThisLine))
                    {
                        CurrentAlignment.Pop();
                    }
                    break;

                case " ":
                    if (charIndex + 3 < line.Length && line.Substring(charIndex - 1, 3).Equals(" = "))
                    {
                        //Space before an = sign => remember it to align to later
                        if (!CurrentAlignment.Any(align => align.Item1.Equals("=") || align.Item1.Equals(" ")))
                        {
                            CurrentAlignment.Push(new Tuple <string, int>("=", charIndex + leftPadding + 2));
                        }
                    }
                    else if (!CurrentAlignment.Any() && charIndex < line.Length - 2)
                    {
                        //Space after a name before the end of the line => remember it for later
                        CurrentAlignment.Push(new Tuple <string, int>(" ", charIndex + leftPadding));
                    }
                    else if (charIndex > 5 && line.Substring(charIndex - 5, 6).Equals(" Then "))
                    {
                        //Clear the collection if we find a Then in an If...Then and set the
                        //indenting to align with the bit after the "If "
                        while (CurrentAlignment.Count > 1)
                        {
                            CurrentAlignment.Pop();
                        }
                    }
                    break;

                case ",":
                    //Start of a new parameter => remember it to align to
                    CurrentAlignment.Push(new Tuple <string, int>(",", charIndex + leftPadding + 2));
                    break;

                case ":":
                    if (line.Substring(charIndex, 2).Equals(":="))
                    {
                        //A named paremeter => remember to align to after the name
                        CurrentAlignment.Push(new Tuple <string, int>(",", charIndex + leftPadding + 2));
                    }
                    else if (line.Substring(charIndex, 2).Equals(": "))
                    {
                        //A new line section, so clear the brackets
                        CurrentAlignment.Clear();
                        charIndex++;
                    }
                    break;
                }
            }
            //If we end with a comma or a named parameter, get rid of all other comma alignments
            if (line.Substring(line.Length - 3).Equals(", _") || line.Substring(line.Length - 3).Equals(", _"))
            {
                while (CurrentAlignment.Any() && CurrentAlignment.Peek().Item1.Equals(","))
                {
                    CurrentAlignment.Pop();
                }
            }

            //If we end with a "( _", remove it and the space alignment after it
            if (line.Substring(line.Length - 3).Equals("( _"))
            {
                CurrentAlignment.Pop();
                CurrentAlignment.Pop();
            }

            paramOffset = 0;
            //Get the position of the unmatched bracket and align to that
            foreach (var align in CurrentAlignment)
            {
                if (align.Item1.Equals(","))
                {
                    paramOffset = align.Item2;
                }
                else if (align.Item1.Equals("("))
                {
                    paramOffset = align.Item2 + 1;
                }
                else
                {
                    skip = align.Item2;
                }
            }

            if (skip == 1 || skip >= line.Length + leftPadding - 1)
            {
                if (!CurrentAlignment.Any() && firstLine)
                {
                    skip = settings.IndentSpaces * 2 + leftPadding;
                }
                else
                {
                    skip = leftPadding;
                }
            }

            if (paramOffset == 0)
            {
                paramOffset = skip + 1;
            }
            return(skip + 1);
        }
Esempio n. 10
0
        private void CheckLine(IIndenterSettings settings, string code, ref bool noIndent, out int ins, out int outs, ref bool atProcedureStart, ref bool atFirstProcLine, ref bool insideIf)
        {
            ins  = 0;
            outs = 0;
            var line = code.Trim() + " ";

            if (!noIndent)
            {
                ins  += _inCode.Count(value => line.StartsWith(value) && (line.Substring(value.Length, 1) == " " || line.Substring(value.Length, 1) == ":"));
                outs += _outCode.Count(value => line.StartsWith(value) && (line.Substring(value.Length, 1) == " " || (line.Substring(value.Length, 1) == ":" && line.Substring(value.Length + 1, 1) != "=")));
            }


            foreach (var value in _inProcedure.Where(value => line.StartsWith(value) && (line.Substring(value.Length, 1) == " " || (line.Substring(value.Length, 1) == ":" && line.Substring(value.Length + 1, 1) != "="))))
            {
                atProcedureStart = true;
                atFirstProcLine  = true;

                // don't indent within type or enum constructs
                if (value.EndsWith("Type ") || value.EndsWith("Enum "))
                {
                    ins++;
                    noIndent = true;
                }
                else if (!noIndent && settings.IndentEntireProcedureBody)
                {
                    ins++;
                }
            }

            var outMatches = ProcedureLevelOutdentingMatch.Where(value => line.StartsWith(value) &&
                                                                 (line.Substring(value.Length, 1) == " " ||
                                                                  (line.Substring(value.Length, 1) == ":" &&
                                                                   line.Substring(value.Length + 1, 1) != "=")
                                                                 )).Count(value => !value.EndsWith("Type ") && !value.EndsWith("Enum "));

            outs += outMatches;
            if (outMatches > 0)
            {
            }
            // special-case handle 'If'; if 'Then' is followed by anything other than a comment, we don't indent.
            if (noIndent || (!insideIf && !code.StartsWith("If ") && !code.StartsWith("#If ")))
            {
                return;
            }

            if (insideIf)
            {
                ins = 1;
            }

            // strip strings
            var i = code.IndexOf('"');

            while (i >= 0)
            {
                var j = code.IndexOf('"', i + 1);
                if (j == -1)
                {
                    j = code.Length;
                }
                code = code.Substring(0, i - 1) + code.Substring(j + 1);
                i    = code.IndexOf('"');
            }

            // strip comments
            i = code.IndexOf('\'');
            if (i >= 1)
            {
                code = code.Substring(0, i - 1);
            }

            // allow lines continuations inside the 'If'
            insideIf = code.Trim().EndsWith(" _");

            // if we have a 'Then' in the line, adding space before & after
            // enables testing for the 'Then' being both within or at the end of the line.
            code = ' ' + code + ' ';
            i    = code.IndexOf(" Then ", StringComparison.InvariantCulture);

            if (i >= 0)
            {
                if (code.Substring(i + 5).Trim() != string.Empty)
                {
                    // there's something after the 'Then', we don't indent the 'If'
                    ins = 0;
                }
                // no need to check next time around
                insideIf = false;
            }
        }
Esempio n. 11
0
        private int FunctionAlign(IIndenterSettings settings, string line, bool firstLine, out int paramOffset)
        {
            if (firstLine)
            {
                CurrentAlignment.Clear();
            }

            //Convert and numbers at the start of the line to spaces
            int testToken;
            var leftPadding = 0;
            var space = line.IndexOf(' ');
            if (space > 0 && int.TryParse(line, out testToken))
            {
                line = line.Substring(space);
                leftPadding = space + 1;
            }

            leftPadding += (line.Length - line.TrimStart().Length);
            var iFirstThisLine = CurrentAlignment.Count;

            line = line.Trim();
            //Skip over stuff that we don't want to locate the start off
            var skip = SkipWhenFindingFunctionStart.Where(sMatch => line.StartsWith(sMatch)).Sum(sMatch => sMatch.Length) + 2;

            for (var charIndex = skip; charIndex <= line.Length; charIndex++)
            {
                var character = line.Substring(charIndex - 1, 1);
                switch (character)
                {                    
                    case "\"":
                        //A String => jump to the end of it
                        charIndex = line.IndexOf("\"", charIndex + 1, StringComparison.InvariantCulture);
                        break;
                    case "(":
                        //Start of another function => remember this position
                        CurrentAlignment.Push(new Tuple<string, int>("(", charIndex + leftPadding + 2));
                        CurrentAlignment.Push(new Tuple<string, int>(",", charIndex + leftPadding + 3));
                        break;
                    case ")":
                        //Function finished => Remove back to the previous open bracket
                        while (CurrentAlignment.Any() && (!CurrentAlignment.Peek().Item1.Equals("(") || CurrentAlignment.Count == iFirstThisLine))
                        {
                            CurrentAlignment.Pop();
                        }
                        break;
                    case " ":                        
                        if (charIndex + 3 < line.Length && line.Substring(charIndex - 1, 3).Equals(" = "))
                        {
                            //Space before an = sign => remember it to align to later
                            if (!CurrentAlignment.Any(align => align.Item1.Equals("=") || align.Item1.Equals(" ")))
                            {
                                CurrentAlignment.Push(new Tuple<string, int>("=", charIndex + leftPadding + 2));
                            }
                        }
                        else if (!CurrentAlignment.Any() && charIndex < line.Length - 2)
                        {
                            //Space after a name before the end of the line => remember it for later
                            CurrentAlignment.Push(new Tuple<string, int>(" ", charIndex + leftPadding));
                        }
                        else if (charIndex > 5 && line.Substring(charIndex - 5, 6).Equals(" Then "))
                        {
                            //Clear the collection if we find a Then in an If...Then and set the
                            //indenting to align with the bit after the "If "
                            while (CurrentAlignment.Count > 1)
                            {
                                CurrentAlignment.Pop();
                            }
                        }
                        break;
                    case ",":
                        //Start of a new parameter => remember it to align to
                        CurrentAlignment.Push(new Tuple<string, int>(",", charIndex + leftPadding + 2));
                        break;
                    case ":":
                        if (line.Substring(charIndex, 2).Equals(":="))
                        {
                            //A named paremeter => remember to align to after the name
                            CurrentAlignment.Push(new Tuple<string, int>(",", charIndex + leftPadding + 2));
                        }
                        else if (line.Substring(charIndex, 2).Equals(": "))
                        {
                            //A new line section, so clear the brackets
                            CurrentAlignment.Clear();
                            charIndex++;
                        }
                        break;
                }
            }
            //If we end with a comma or a named parameter, get rid of all other comma alignments
            if (line.Substring(line.Length - 3).Equals(", _") || line.Substring(line.Length - 3).Equals(", _"))
            {
                while (CurrentAlignment.Any() && CurrentAlignment.Peek().Item1.Equals(","))
                {                    
                    CurrentAlignment.Pop();
                }
            }

            //If we end with a "( _", remove it and the space alignment after it
            if (line.Substring(line.Length - 3).Equals("( _"))
            {
                CurrentAlignment.Pop(); 
                CurrentAlignment.Pop();
            }

            paramOffset = 0;
            //Get the position of the unmatched bracket and align to that
            foreach (var align in CurrentAlignment)
            {
                if (align.Item1.Equals(","))
                {
                    paramOffset = align.Item2;
                }
                else if (align.Item1.Equals("("))
                {
                    paramOffset = align.Item2 + 1;
                }
                else
                {
                    skip = align.Item2;
                }
            }

            if (skip == 1 || skip >= line.Length + leftPadding - 1)
            {
                if (!CurrentAlignment.Any() && firstLine)
                {
                    skip = settings.IndentSpaces * 2 + leftPadding;
                }
                else
                {
                    skip = leftPadding;
                }
            }

            if (paramOffset == 0)
            {
                paramOffset = skip + 1;
            }
            return skip + 1;
        }
Esempio n. 12
0
        private void CheckLine(IIndenterSettings settings, string code, ref bool noIndent, out int ins, out int outs, ref bool atProcedureStart, ref bool atFirstProcLine, ref bool insideIf)
        {

            ins = 0;
            outs = 0;
            var line = code.Trim() + " ";
            if (!noIndent)
            {
                ins += _inCode.Count(value => line.StartsWith(value) && (line.Substring(value.Length, 1) == " " || line.Substring(value.Length, 1) == ":"));
                outs += _outCode.Count(value => line.StartsWith(value) && (line.Substring(value.Length, 1) == " " || (line.Substring(value.Length, 1) == ":" && line.Substring(value.Length + 1, 1) != "=")));
            }

            
            foreach (var value in _inProcedure.Where(value => line.StartsWith(value) && (line.Substring(value.Length, 1) == " " || (line.Substring(value.Length, 1) == ":" && line.Substring(value.Length + 1, 1) != "="))))
            {
                atProcedureStart = true;
                atFirstProcLine = true;

                // don't indent within type or enum constructs
                if (value.EndsWith("Type ") || value.EndsWith("Enum "))
                {
                    ins++;
                    noIndent = true;
                }
                else if (!noIndent && settings.IndentEntireProcedureBody)
                {
                    ins++;
                }
            }

            var outMatches = ProcedureLevelOutdentingMatch.Where(value => line.StartsWith(value) && 
                                                                 (line.Substring(value.Length, 1) == " " || 
                                                                  (line.Substring(value.Length, 1) == ":" && 
                                                                   line.Substring(value.Length + 1, 1) != "=")
                                                                 )).Count(value => !value.EndsWith("Type ") && !value.EndsWith("Enum "));

            outs += outMatches;
            if (outMatches > 0)
            {
                
            }
            // special-case handle 'If'; if 'Then' is followed by anything other than a comment, we don't indent.
            if (noIndent || (!insideIf && !code.StartsWith("If ") && !code.StartsWith("#If ")))
            {
                return;
            }

            if (insideIf)
            {
                ins = 1;
            }

            // strip strings
            var i = code.IndexOf('"');
            while (i >= 0)
            {
                var j = code.IndexOf('"', i + 1);
                if (j == -1)
                {
                    j = code.Length;
                }
                code = code.Substring(0, i - 1) + code.Substring(j + 1);
                i = code.IndexOf('"');
            }

            // strip comments
            i = code.IndexOf('\'');
            if (i >= 1)
            {
                code = code.Substring(0, i - 1);
            }

            // allow lines continuations inside the 'If' 
            insideIf = code.Trim().EndsWith(" _");

            // if we have a 'Then' in the line, adding space before & after
            // enables testing for the 'Then' being both within or at the end of the line.
            code = ' ' + code + ' ';
            i = code.IndexOf(" Then ", StringComparison.InvariantCulture);

            if (i >= 0)
            {
                if (code.Substring(i + 5).Trim() != string.Empty)
                {
                    // there's something after the 'Then', we don't indent the 'If'
                    ins = 0;
                }
                // no need to check next time around
                insideIf = false;
            }
        }
Esempio n. 13
0
 public CodeMetricsListener(DeclarationFinder finder, IIndenterSettings indenterSettings)
 {
     _finder           = finder;
     _indenterSettings = indenterSettings;
 }
Esempio n. 14
0
 public CodeMetricsAnalyst(IIndenterSettings indenterSettings)
 {
     _indenterSettings = indenterSettings;
 }