Skip to content

Commit 4fb4988

Browse files
committed
addressed review comments
1 parent 6a77ab6 commit 4fb4988

File tree

6 files changed

+96
-69
lines changed

6 files changed

+96
-69
lines changed

Rubberduck.CodeAnalysis/Inspections/Concrete/UnreachableCaseEvaluation/UnreachableCaseInspector.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,23 +98,24 @@ public UnreachableCaseInspector(
9898

9999
private UnreachableCaseInspection.CaseInspectionResultType? InvalidRangeExpressionsType(ICollection<IRangeClauseExpression> rangeClauseExpressions)
100100
{
101-
if (rangeClauseExpressions.Any(expr => expr?.IsMismatch == true))
101+
var usableClauses = rangeClauseExpressions.Where(expr => expr != null).ToList();
102+
if (usableClauses.Any(expr => expr.IsMismatch))
102103
{
103104
return UnreachableCaseInspection.CaseInspectionResultType.MismatchType;
104105
}
105106

106-
if (rangeClauseExpressions.Any(expr => expr?.IsOverflow == true))
107+
if (usableClauses.Any(expr => expr.IsOverflow))
107108
{
108109
return UnreachableCaseInspection.CaseInspectionResultType.Overflow;
109110
}
110111

111-
if (rangeClauseExpressions.All(expr => expr?.IsInherentlyUnreachable == true))
112+
if (usableClauses.All(expr => expr.IsInherentlyUnreachable))
112113
{
113114
return UnreachableCaseInspection.CaseInspectionResultType.InherentlyUnreachable;
114115
}
115116

116-
if (rangeClauseExpressions.All(expr =>
117-
expr != null && (expr.IsUnreachable || expr.IsMismatch|| expr.IsOverflow || expr.IsInherentlyUnreachable)))
117+
if (usableClauses.All(expr =>
118+
expr.IsUnreachable || expr.IsMismatch|| expr.IsOverflow || expr.IsInherentlyUnreachable))
118119
{
119120
return UnreachableCaseInspection.CaseInspectionResultType.Unreachable;
120121
}

Rubberduck.Core/UI/CodeExplorer/Commands/CodeExplorerFindAllReferencesCommand.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,26 @@ private bool SpecialEvaluateCanExecute(object parameter)
4444

4545
protected override void OnExecute(object parameter)
4646
{
47-
if (_state.Status != ParserState.Ready ||
48-
!(parameter is ICodeExplorerNode node) ||
49-
node.Declaration == null)
47+
if (_state.Status != ParserState.Ready
48+
|| !(parameter is ICodeExplorerNode node)
49+
|| node.Declaration == null)
5050
{
5151
return;
5252
}
5353

54+
if (!(node.Parent.Declaration is ProjectDeclaration projectDeclaration))
55+
{
56+
Logger.Error($"The specified ICodeExplorerNode expected to be a direct child of a node whose declaration is a ProjectDeclaration.");
57+
return;
58+
}
59+
5460
if (parameter is CodeExplorerReferenceViewModel reference)
5561
{
5662
if (!(reference.Reference is ReferenceModel model))
5763
{
5864
return;
5965
}
60-
_finder.FindAllReferences((ProjectDeclaration)node.Parent.Declaration, model.ToReferenceInfo());
66+
_finder.FindAllReferences(projectDeclaration, model.ToReferenceInfo());
6167
return;
6268
}
6369

Rubberduck.Core/UI/Controls/FindAllReferencesAction.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void FindAllReferences(Declaration target)
5454
{
5555
if (_state.Status != ParserState.Ready)
5656
{
57-
_logger.Info($"ParserState is {_state.Status}. This action requires a Ready state.");
57+
_logger.Debug($"ParserState is {_state.Status}. This action requires a Ready state.");
5858
return;
5959
}
6060

@@ -71,7 +71,7 @@ public void FindAllReferences(ProjectDeclaration project, ReferenceInfo referenc
7171
{
7272
if (_state.Status != ParserState.Ready)
7373
{
74-
_logger.Info($"ParserState is {_state.Status}. This action requires a Ready state.");
74+
_logger.Debug($"ParserState is {_state.Status}. This action requires a Ready state.");
7575
return;
7676
}
7777

@@ -153,13 +153,18 @@ private SearchResultsViewModel CreateViewModel(Declaration declaration, string i
153153
foreach (var qualifiedModuleName in modules)
154154
{
155155
var component = _state.ProjectsProvider.Component(qualifiedModuleName);
156+
if (component == null)
157+
{
158+
_logger.Warn($"Could not retrieve the IVBComponent for module '{qualifiedModuleName}'.");
159+
continue;
160+
}
156161
var module = component.CodeModule;
157162

158163
if (nameRefs.TryGetValue(qualifiedModuleName, out var identifierReferences))
159164
{
160165
foreach (var identifierReference in identifierReferences)
161166
{
162-
var (context, selection) = identifierReference.HighligthSelection(module);
167+
var (context, selection) = identifierReference.HighlightSelection(module);
163168
var result = new SearchResultItem(
164169
identifierReference.ParentNonScoping,
165170
new NavigateCodeEventArgs(qualifiedModuleName, identifierReference.Selection),
@@ -172,7 +177,7 @@ private SearchResultsViewModel CreateViewModel(Declaration declaration, string i
172177
{
173178
foreach (var argumentReference in argReferences)
174179
{
175-
var (context, selection) = argumentReference.HighligthSelection(module);
180+
var (context, selection) = argumentReference.HighlightSelection(module);
176181
var result = new SearchResultItem(
177182
argumentReference.ParentNonScoping,
178183
new NavigateCodeEventArgs(qualifiedModuleName, argumentReference.Selection),
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Rubberduck.Parsing.Symbols;
7+
using Rubberduck.VBEditor;
8+
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
9+
10+
namespace Rubberduck.UI.Controls
11+
{
12+
static class IdentifierReferenceExtensions
13+
{
14+
public static (string context, Selection highlight) HighlightSelection(this IdentifierReference reference, ICodeModule module)
15+
{
16+
const int maxLength = 255;
17+
var selection = reference.Selection;
18+
19+
var lines = module.GetLines(selection.StartLine, selection.LineCount).Split('\n');
20+
21+
var line = lines[0];
22+
var indent = line.TakeWhile(c => c.Equals(' ')).Count();
23+
24+
var highlight = new Selection(
25+
1, Math.Max(selection.StartColumn - indent, 1),
26+
1, Math.Max(selection.EndColumn - indent, 1))
27+
.ToZeroBased();
28+
29+
var trimmed = line.Trim();
30+
if (trimmed.Length > maxLength || lines.Length > 1)
31+
{
32+
trimmed = trimmed.Substring(0, maxLength) + " …";
33+
}
34+
return (trimmed, highlight);
35+
}
36+
37+
public static (string context, Selection highlight) HighlightSelection(this ArgumentReference reference, ICodeModule module)
38+
{
39+
const int maxLength = 255;
40+
var selection = reference.Selection;
41+
42+
var lines = module.GetLines(selection.StartLine, selection.LineCount).Split('\n');
43+
44+
var line = lines[0];
45+
var indent = line.TakeWhile(c => c.Equals(' ')).Count();
46+
47+
var highlight = new Selection(
48+
1, Math.Max(selection.StartColumn - indent, 1),
49+
1, Math.Max(selection.EndColumn - indent, 1))
50+
.ToZeroBased();
51+
52+
var trimmed = line.Trim();
53+
if (trimmed.Length > maxLength || lines.Length > 1)
54+
{
55+
trimmed = trimmed.Substring(0, Math.Min(trimmed.Length, maxLength)) + " …";
56+
highlight = new Selection(1, highlight.StartColumn, 1, trimmed.Length);
57+
}
58+
59+
if (highlight.IsSingleCharacter && highlight.StartColumn == 0)
60+
{
61+
trimmed = " " + trimmed;
62+
highlight = new Selection(0, 0, 0, 1);
63+
}
64+
else if (highlight.IsSingleCharacter)
65+
{
66+
highlight = new Selection(0, selection.StartColumn - 1 - indent - 1, 0, selection.StartColumn - 1 - indent);
67+
}
68+
return (trimmed, highlight);
69+
}
70+
}
71+
}

Rubberduck.Parsing/Symbols/ArgumentReference.cs

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -49,39 +49,5 @@ internal ArgumentReference(
4949
public int NumberOfArguments { get; }
5050
public VBAParser.ArgumentListContext ArgumentListContext { get; }
5151
public Selection ArgumentListSelection { get; }
52-
53-
public override (string context, Selection highlight) HighligthSelection(ICodeModule module)
54-
{
55-
const int maxLength = 255;
56-
57-
var lines = module.GetLines(Selection.StartLine, Selection.LineCount).Split('\n');
58-
59-
var line = lines[0];
60-
var indent = line.TakeWhile(c => c.Equals(' ')).Count();
61-
62-
var highlight = new Selection(
63-
1, Math.Max(Selection.StartColumn - indent, 1),
64-
1, Math.Max(Selection.EndColumn - indent, 1))
65-
.ToZeroBased();
66-
67-
var trimmed = line.Trim();
68-
if (trimmed.Length > maxLength || lines.Length > 1)
69-
{
70-
trimmed = trimmed.Substring(0, Math.Min(trimmed.Length, maxLength)) + "...";
71-
highlight = new Selection(1, highlight.StartColumn, 1, trimmed.Length);
72-
}
73-
74-
if (highlight.IsSingleCharacter && highlight.StartColumn == 0)
75-
{
76-
trimmed = " " + trimmed;
77-
highlight = new Selection(0, 0, 0, 1);
78-
}
79-
else if (highlight.IsSingleCharacter)
80-
{
81-
highlight = new Selection(0, Selection.StartColumn-1 - indent - 1, 0, Selection.StartColumn-1 - indent);
82-
}
83-
return (trimmed, highlight);
84-
}
85-
8652
}
8753
}

Rubberduck.Parsing/Symbols/IdentifierReference.cs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -133,28 +133,6 @@ public bool IsSelected(QualifiedSelection selection)
133133
Selection.ContainsFirstCharacter(selection.Selection);
134134
}
135135

136-
public virtual (string context, Selection highlight) HighligthSelection(ICodeModule module)
137-
{
138-
const int maxLength = 255;
139-
140-
var lines = module.GetLines(Selection.StartLine, Selection.LineCount).Split('\n');
141-
142-
var line = lines[0];
143-
var indent = line.TakeWhile(c => c.Equals(' ')).Count();
144-
145-
var highlight = new Selection(
146-
1, Math.Max(Selection.StartColumn - indent, 1),
147-
1, Math.Max(Selection.EndColumn - indent, 1))
148-
.ToZeroBased();
149-
150-
var trimmed = line.Trim();
151-
if (trimmed.Length > maxLength || lines.Length > 1)
152-
{
153-
trimmed = trimmed.Substring(0, maxLength) + "...";
154-
}
155-
return (trimmed, highlight);
156-
}
157-
158136
public bool Equals(IdentifierReference other)
159137
{
160138
return other != null

0 commit comments

Comments
 (0)