Skip to content

Commit 612b9ed

Browse files
authored
Merge pull request #2838 from comintern/next
Enforce maxes, mins in IndenterSettings. Add null check for DeclarationFinder.
2 parents f499deb + 3144a43 commit 612b9ed

File tree

3 files changed

+60
-31
lines changed

3 files changed

+60
-31
lines changed

RetailCoder.VBE/UI/Settings/IndenterSettings.xaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@
153153
</TextBlock>
154154
<controls:NumberPicker Margin="0,-6,0,0"
155155
MinNumber="0"
156+
MaxNumber="{x:Static smartIndenter:IndenterSettings.MaximumIndentSpaces}"
156157
NumValue="{Binding IndentSpaces,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
157158
</StackPanel>
158159
</StackPanel>
@@ -202,6 +203,7 @@
202203
<controls:NumberPicker Margin="0,-6,0,0"
203204
HorizontalAlignment="Left"
204205
MinNumber="0"
206+
MaxNumber="{x:Static smartIndenter:IndenterSettings.MaximumAlignDimColumn}"
205207
IsEnabled="{Binding ElementName=AlignDimColumn,Path=IsChecked}"
206208
NumValue="{Binding AlignDimColumn,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
207209
</WrapPanel>
@@ -227,6 +229,7 @@
227229
<controls:NumberPicker Margin="0,-4,0,0"
228230
HorizontalAlignment="Left"
229231
MinNumber="0"
232+
MaxNumber="{x:Static smartIndenter:IndenterSettings.MaximumEndOfLineCommentColumnSpaceAlignment}"
230233
NumValue="{Binding EndOfLineCommentColumnSpaceAlignment,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
231234
Visibility="{Binding EndOfLineCommentStyle,Converter={StaticResource EndOfLineCommentStyleToVisibility}}"/>
232235
</StackPanel>
@@ -289,7 +292,7 @@
289292
<controls:NumberPicker Margin="0,-6,0,0"
290293
HorizontalAlignment="Left"
291294
MinNumber="0"
292-
MaxNumber="2"
295+
MaxNumber="{x:Static smartIndenter:IndenterSettings.MaximumVerticalSpacing}"
293296
IsEnabled="{Binding ElementName=SpaceProcedures,Path=IsChecked}"
294297
NumValue="{Binding LinesBetweenProcedures,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
295298
</WrapPanel>

Rubberduck.Parsing/VBA/RubberduckParserState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,7 @@ public void RebuildSelectionCache()
10051005

10061006
public Declaration FindSelectedDeclaration(ICodePane activeCodePane, bool procedureLevelOnly = false)
10071007
{
1008-
return DeclarationFinder.FindSelectedDeclaration(activeCodePane);
1008+
return DeclarationFinder?.FindSelectedDeclaration(activeCodePane);
10091009
}
10101010

10111011
public void RemoveBuiltInDeclarations(IReference reference)

Rubberduck.SmartIndenter/IndenterSettings.cs

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ namespace Rubberduck.SmartIndenter
77
[XmlType(AnonymousType = true)]
88
public class IndenterSettings : IIndenterSettings, IEquatable<IndenterSettings>
99
{
10-
public const int MinimumVerticalSpacing = 0;
11-
public const int MaximumVerticalSpacing = 2;
10+
// These have to be int to allow the settings UI to bind them.
11+
public const int MaximumAlignDimColumn = 100;
12+
public const int MaximumEndOfLineCommentColumnSpaceAlignment = 100;
13+
public const int MaximumIndentSpaces = 32;
14+
public const int MaximumVerticalSpacing = 2;
1215

1316
public virtual bool IndentEntireProcedureBody { get; set; }
1417
public virtual bool IndentFirstCommentBlock { get; set; }
@@ -22,31 +25,45 @@ public class IndenterSettings : IIndenterSettings, IEquatable<IndenterSettings>
2225
public virtual bool ForceCompilerDirectivesInColumn1 { get; set; }
2326
public virtual bool IndentCompilerDirectives { get; set; }
2427
public virtual bool AlignDims { get; set; }
25-
public virtual int AlignDimColumn { get; set; }
28+
29+
private int _dimAlignment;
30+
public virtual int AlignDimColumn
31+
{
32+
get { return _dimAlignment; }
33+
set
34+
{
35+
_dimAlignment = value > MaximumAlignDimColumn ? MaximumAlignDimColumn : Math.Max(value, 0);
36+
}
37+
}
38+
2639
public virtual EndOfLineCommentStyle EndOfLineCommentStyle { get; set; }
27-
public virtual int EndOfLineCommentColumnSpaceAlignment { get; set; }
28-
public virtual int IndentSpaces { get; set; }
40+
41+
private int _commentAlignment;
42+
public virtual int EndOfLineCommentColumnSpaceAlignment
43+
{
44+
get { return _commentAlignment; }
45+
set
46+
{
47+
_commentAlignment = value > MaximumEndOfLineCommentColumnSpaceAlignment
48+
? MaximumEndOfLineCommentColumnSpaceAlignment
49+
: value;
50+
}
51+
}
52+
53+
private int _indentSpaces;
54+
public virtual int IndentSpaces
55+
{
56+
get { return _indentSpaces; }
57+
set { _indentSpaces = value > MaximumIndentSpaces ? MaximumIndentSpaces : Math.Max(value, 0); }
58+
}
59+
2960
public virtual bool VerticallySpaceProcedures { get; set; }
3061

3162
private int _procedureSpacing;
3263
public virtual int LinesBetweenProcedures
3364
{
3465
get { return _procedureSpacing; }
35-
set
36-
{
37-
if (value < MinimumVerticalSpacing)
38-
{
39-
_procedureSpacing = MinimumVerticalSpacing;
40-
}
41-
else if (value > MaximumVerticalSpacing)
42-
{
43-
_procedureSpacing = MaximumVerticalSpacing;
44-
}
45-
else
46-
{
47-
_procedureSpacing = value;
48-
}
49-
}
66+
set { _procedureSpacing = value > MaximumVerticalSpacing ? MaximumVerticalSpacing : Math.Max(value, 0); }
5067
}
5168

5269
public IndenterSettings()
@@ -120,8 +137,7 @@ public bool LegacySettingsExist()
120137
catch
121138
{
122139
return false;
123-
}
124-
140+
}
125141
}
126142

127143
public void LoadLegacyFromRegistry()
@@ -135,15 +151,13 @@ public void LoadLegacyFromRegistry()
135151
IndentFirstDeclarationBlock = GetSmartIndenterBoolean(reg, "IndentDim", IndentFirstDeclarationBlock);
136152
AlignCommentsWithCode = GetSmartIndenterBoolean(reg, "IndentCmt", AlignCommentsWithCode);
137153
AlignContinuations = GetSmartIndenterBoolean(reg, "AlignContinued", AlignContinuations);
138-
IgnoreOperatorsInContinuations = GetSmartIndenterBoolean(reg, "AlignIgnoreOps",
139-
IgnoreOperatorsInContinuations);
154+
IgnoreOperatorsInContinuations = GetSmartIndenterBoolean(reg, "AlignIgnoreOps", IgnoreOperatorsInContinuations);
140155
IndentCase = GetSmartIndenterBoolean(reg, "IndentCase", IndentCase);
141156
ForceDebugStatementsInColumn1 = GetSmartIndenterBoolean(reg, "DebugCol1", ForceDebugStatementsInColumn1);
142-
ForceCompilerDirectivesInColumn1 = GetSmartIndenterBoolean(reg, "CompilerCol1",
143-
ForceCompilerDirectivesInColumn1);
157+
ForceCompilerDirectivesInColumn1 = GetSmartIndenterBoolean(reg, "CompilerCol1", ForceCompilerDirectivesInColumn1);
144158
IndentCompilerDirectives = GetSmartIndenterBoolean(reg, "IndentCompiler", IndentCompilerDirectives);
145159
AlignDims = GetSmartIndenterBoolean(reg, "AlignDim", AlignDims);
146-
AlignDimColumn = Convert.ToInt32(reg.GetValue("AlignDimCol") ?? AlignDimColumn);
160+
AlignDimColumn = GetSmartIndenterNumeric(reg, "AlignDimCol", AlignDimColumn, MaximumAlignDimColumn);
147161

148162
var eolSytle = reg.GetValue("EOLComments") as string;
149163
if (!string.IsNullOrEmpty(eolSytle))
@@ -164,8 +178,8 @@ public void LoadLegacyFromRegistry()
164178
break;
165179
}
166180
}
167-
EndOfLineCommentColumnSpaceAlignment =
168-
Convert.ToInt32(reg.GetValue("EOLAlignCol") ?? EndOfLineCommentColumnSpaceAlignment);
181+
EndOfLineCommentColumnSpaceAlignment = GetSmartIndenterNumeric(reg, "EOLAlignCol",
182+
EndOfLineCommentColumnSpaceAlignment, MaximumEndOfLineCommentColumnSpaceAlignment);
169183
}
170184
// ReSharper disable once EmptyGeneralCatchClause
171185
catch { }
@@ -176,5 +190,17 @@ private static bool GetSmartIndenterBoolean(RegistryKey key, string name, bool c
176190
var value = key.GetValue(name) as string;
177191
return string.IsNullOrEmpty(value) ? current : value.Trim().Equals("Y");
178192
}
193+
194+
private static int GetSmartIndenterNumeric(RegistryKey key, string name, int current, int max)
195+
{
196+
try
197+
{
198+
var value = (int)key.GetValue(name);
199+
return value < 0 ? current : Math.Min(value, max);
200+
}
201+
// ReSharper disable once EmptyGeneralCatchClause
202+
catch { }
203+
return current;
204+
}
179205
}
180206
}

0 commit comments

Comments
 (0)