|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | +using Rubberduck.Parsing.VBA; |
| 4 | +using Antlr4.Runtime.Tree; |
| 5 | +using Rubberduck.Parsing.Grammar; |
| 6 | +using Rubberduck.VBEditor; |
| 7 | +using Antlr4.Runtime.Misc; |
| 8 | +using Rubberduck.Parsing.Symbols; |
| 9 | +using Rubberduck.SmartIndenter; |
| 10 | + |
| 11 | +namespace Rubberduck.Navigation.CodeMetrics |
| 12 | +{ |
| 13 | + public class CodeMetricsAnalyst : ICodeMetricsAnalyst |
| 14 | + { |
| 15 | + private readonly IIndenterSettings _indenterSettings; |
| 16 | + |
| 17 | + public CodeMetricsAnalyst(IIndenterSettings indenterSettings) |
| 18 | + { |
| 19 | + _indenterSettings = indenterSettings; |
| 20 | + } |
| 21 | + |
| 22 | + public IEnumerable<ModuleMetricsResult> ModuleMetrics(RubberduckParserState state) |
| 23 | + { |
| 24 | + if (state == null || !state.AllUserDeclarations.Any()) |
| 25 | + { |
| 26 | + // must not return Enumerable.Empty |
| 27 | + yield break; |
| 28 | + } |
| 29 | + |
| 30 | + var trees = state.ParseTrees; |
| 31 | + |
| 32 | + foreach (var moduleTree in trees) |
| 33 | + { |
| 34 | + yield return GetModuleResult(moduleTree.Key, moduleTree.Value, state.DeclarationFinder); |
| 35 | + }; |
| 36 | + } |
| 37 | + |
| 38 | + public ModuleMetricsResult GetModuleResult(RubberduckParserState state, QualifiedModuleName qmn) |
| 39 | + { |
| 40 | + return GetModuleResult(qmn, state.GetParseTree(qmn), state.DeclarationFinder); |
| 41 | + } |
| 42 | + |
| 43 | + private ModuleMetricsResult GetModuleResult(QualifiedModuleName qmn, IParseTree moduleTree, DeclarationFinder declarationFinder) |
| 44 | + { |
| 45 | + // Consider rewrite as visitor? That should make subtrees easier and allow us to expand metrics |
| 46 | + var cmListener = new CodeMetricsListener(declarationFinder, _indenterSettings); |
| 47 | + ParseTreeWalker.Default.Walk(cmListener, moduleTree); |
| 48 | + return cmListener.GetMetricsResult(qmn); |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + private class CodeMetricsListener : VBAParserBaseListener |
| 53 | + { |
| 54 | + private readonly DeclarationFinder _finder; |
| 55 | + private readonly IIndenterSettings _indenterSettings; |
| 56 | + |
| 57 | + private Declaration _currentMember; |
| 58 | + private List<CodeMetricsResult> _results = new List<CodeMetricsResult>(); |
| 59 | + private List<CodeMetricsResult> _moduleResults = new List<CodeMetricsResult>(); |
| 60 | + |
| 61 | + private List<MemberMetricsResult> _memberResults = new List<MemberMetricsResult>(); |
| 62 | + |
| 63 | + public CodeMetricsListener(DeclarationFinder finder, IIndenterSettings indenterSettings) |
| 64 | + { |
| 65 | + _finder = finder; |
| 66 | + _indenterSettings = indenterSettings; |
| 67 | + } |
| 68 | + |
| 69 | + public override void EnterEndOfLine([NotNull] VBAParser.EndOfLineContext context) |
| 70 | + { |
| 71 | + int followingIndentationLevel = 0; |
| 72 | + // we have a proper newline |
| 73 | + if (context.NEWLINE() != null) |
| 74 | + { |
| 75 | + // the last whitespace, which is the one in front of the next line's contents |
| 76 | + var followingWhitespace = context.whiteSpace().LastOrDefault(); |
| 77 | + followingIndentationLevel = IndentationLevelFromWhitespace(followingWhitespace); |
| 78 | + } |
| 79 | + (_currentMember == null ? _moduleResults : _results).Add(new CodeMetricsResult(1, 0, followingIndentationLevel)); |
| 80 | + } |
| 81 | + |
| 82 | + public override void EnterIfStmt([NotNull] VBAParser.IfStmtContext context) |
| 83 | + { |
| 84 | + _results.Add(new CodeMetricsResult(0, 1, 0)); |
| 85 | + } |
| 86 | + |
| 87 | + public override void EnterElseIfBlock([NotNull] VBAParser.ElseIfBlockContext context) |
| 88 | + { |
| 89 | + _results.Add(new CodeMetricsResult(0, 1, 0)); |
| 90 | + } |
| 91 | + |
| 92 | + // notably: NO additional complexity for an Else-Block |
| 93 | + |
| 94 | + public override void EnterForEachStmt([NotNull] VBAParser.ForEachStmtContext context) |
| 95 | + { |
| 96 | + _results.Add(new CodeMetricsResult(0, 1, 0)); |
| 97 | + } |
| 98 | + |
| 99 | + public override void EnterForNextStmt([NotNull] VBAParser.ForNextStmtContext context) |
| 100 | + { |
| 101 | + _results.Add(new CodeMetricsResult(0, 1, 0)); |
| 102 | + } |
| 103 | + |
| 104 | + public override void EnterCaseClause([NotNull] VBAParser.CaseClauseContext context) |
| 105 | + { |
| 106 | + _results.Add(new CodeMetricsResult(0, 1, 0)); |
| 107 | + } |
| 108 | + |
| 109 | + public override void EnterSubStmt([NotNull] VBAParser.SubStmtContext context) |
| 110 | + { |
| 111 | + _results.Add(new CodeMetricsResult(0, 1, 0)); |
| 112 | + _currentMember = _finder.UserDeclarations(DeclarationType.Procedure).Where(d => d.Context == context).First(); |
| 113 | + } |
| 114 | + |
| 115 | + public override void ExitSubStmt([NotNull] VBAParser.SubStmtContext context) |
| 116 | + { |
| 117 | + ExitMeasurableMember(); |
| 118 | + } |
| 119 | + |
| 120 | + public override void EnterFunctionStmt([NotNull] VBAParser.FunctionStmtContext context) |
| 121 | + { |
| 122 | + _results.Add(new CodeMetricsResult(0, 1, 0)); |
| 123 | + _currentMember = _finder.UserDeclarations(DeclarationType.Function).Where(d => d.Context == context).First(); |
| 124 | + } |
| 125 | + |
| 126 | + public override void ExitFunctionStmt([NotNull] VBAParser.FunctionStmtContext context) |
| 127 | + { |
| 128 | + ExitMeasurableMember(); |
| 129 | + } |
| 130 | + |
| 131 | + public override void EnterPropertyGetStmt([NotNull] VBAParser.PropertyGetStmtContext context) |
| 132 | + { |
| 133 | + _results.Add(new CodeMetricsResult(0, 1, 0)); |
| 134 | + _currentMember = _finder.UserDeclarations(DeclarationType.PropertyGet).Where(d => d.Context == context).First(); |
| 135 | + } |
| 136 | + |
| 137 | + public override void ExitPropertyGetStmt([NotNull] VBAParser.PropertyGetStmtContext context) |
| 138 | + { |
| 139 | + ExitMeasurableMember(); |
| 140 | + } |
| 141 | + |
| 142 | + public override void EnterPropertyLetStmt([NotNull] VBAParser.PropertyLetStmtContext context) |
| 143 | + { |
| 144 | + _results.Add(new CodeMetricsResult(0, 1, 0)); |
| 145 | + _currentMember = _finder.UserDeclarations(DeclarationType.PropertyLet).Where(d => d.Context == context).First(); |
| 146 | + } |
| 147 | + |
| 148 | + public override void ExitPropertyLetStmt([NotNull] VBAParser.PropertyLetStmtContext context) |
| 149 | + { |
| 150 | + ExitMeasurableMember(); |
| 151 | + } |
| 152 | + |
| 153 | + public override void EnterPropertySetStmt([NotNull] VBAParser.PropertySetStmtContext context) |
| 154 | + { |
| 155 | + _results.Add(new CodeMetricsResult(0, 1, 0)); |
| 156 | + _currentMember = _finder.UserDeclarations(DeclarationType.PropertySet).Where(d => d.Context == context).First(); |
| 157 | + } |
| 158 | + |
| 159 | + public override void ExitPropertySetStmt([NotNull] VBAParser.PropertySetStmtContext context) |
| 160 | + { |
| 161 | + ExitMeasurableMember(); |
| 162 | + } |
| 163 | + |
| 164 | + public override void EnterBlockStmt([NotNull] VBAParser.BlockStmtContext context) |
| 165 | + { |
| 166 | + // there is a whitespace context here after the option of a statementLabel. |
| 167 | + // we need to account for that |
| 168 | + _results.Add(new CodeMetricsResult(0, 0, IndentationLevelFromWhitespace(context.whiteSpace()))); |
| 169 | + } |
| 170 | + |
| 171 | + private int IndentationLevelFromWhitespace(VBAParser.WhiteSpaceContext wsContext) |
| 172 | + { |
| 173 | + if (wsContext == null) return 0; |
| 174 | + // the only thing that contains underscores is the line-continuation at this point |
| 175 | + var lineContinuation = wsContext.children.LastOrDefault((tree) => tree.GetText().Contains("_")); |
| 176 | + var index = lineContinuation != null ? wsContext.children.IndexOf(lineContinuation) : 0; |
| 177 | + return (wsContext?.ChildCount ?? 0 - index) / _indenterSettings.IndentSpaces; |
| 178 | + } |
| 179 | + |
| 180 | + private void ExitMeasurableMember() |
| 181 | + { |
| 182 | + _memberResults.Add(new MemberMetricsResult(_currentMember, _results)); |
| 183 | + _results = new List<CodeMetricsResult>(); // reinitialize to drop results |
| 184 | + _currentMember = null; |
| 185 | + } |
| 186 | + |
| 187 | + internal ModuleMetricsResult GetMetricsResult(QualifiedModuleName qmn) |
| 188 | + { |
| 189 | + return new ModuleMetricsResult(qmn, _memberResults, _moduleResults); |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | +} |
0 commit comments