Skip to content

Commit 2f2f093

Browse files
committed
Create VBESettings class to access the VBE's settings stored in the registry.
Create tests for VBESettings class (incomplete; need SystemWrappers or something) Update the GeneralSettingsViewModel so that it checks the Compile On Demand settings and offer to update Add resource strings for messages.
1 parent b20a7eb commit 2f2f093

File tree

8 files changed

+246
-4
lines changed

8 files changed

+246
-4
lines changed

RetailCoder.VBE/Rubberduck.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,8 @@
501501
<Compile Include="UnitTesting\Stubs\Beep.cs" />
502502
<Compile Include="UnitTesting\IStub.cs" />
503503
<Compile Include="UnitTesting\StubBase.cs" />
504+
<Compile Include="VBERuntime\IVBESettings.cs" />
505+
<Compile Include="VBERuntime\VBESettings.cs" />
504506
<Compile Include="VersionCheck\IVersionCheck.cs" />
505507
<Compile Include="UI\Command\MenuItems\CommandBars\AppCommandBarBase.cs" />
506508
<Compile Include="UI\Command\MenuItems\CommandBars\ContextSelectionLabelMenuItem.cs" />

RetailCoder.VBE/UI/RubberduckUI.Designer.cs

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

RetailCoder.VBE/UI/RubberduckUI.resx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,4 +1795,16 @@ Would you like to import them to Rubberduck?</value>
17951795
<data name="ParserState_UnexpectedError" xml:space="preserve">
17961796
<value>Unexpected Error</value>
17971797
</data>
1798+
<data name="Command_Reparse_CompileOnDemandEnabled" xml:space="preserve">
1799+
<value>The VBEsetting "Compile On Demand" is currently enabled. This is not recommended as this may hide compilation errors and cause problems with parsing. Do you want to parse anyway?</value>
1800+
</data>
1801+
<data name="Command_Reparse_CompileOnDemandEnabled_Caption" xml:space="preserve">
1802+
<value>Compile On Demand Setting</value>
1803+
</data>
1804+
<data name="GeneralSettings_CompileBeforeParse_WarnCompileOnDemandEnabled" xml:space="preserve">
1805+
<value>The VBE setting "Compile On Demand" is currently enabled. It is strongly recommended that you disable the setting via the VBE Properties dialog under General tab in order to use the Compile Before Parse feature. Do you want to disable it now?\r\n\r\nNOTE: Restart may be required for setting to take effect.</value>
1806+
</data>
1807+
<data name="GeneralSettings_CompileBeforeParse_WarnCompileOnDemandEnabled_Caption" xml:space="preserve">
1808+
<value>Compile On Demand currently enabled</value>
1809+
</data>
17981810
</root>

RetailCoder.VBE/UI/Settings/GeneralSettingsViewModel.cs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
using System.Collections.Generic;
33
using System.Collections.ObjectModel;
44
using System.Linq;
5+
using System.Windows.Forms;
56
using Rubberduck.Settings;
67
using Rubberduck.Common;
78
using NLog;
89
using Rubberduck.Parsing.Common;
910
using Rubberduck.Root;
1011
using Rubberduck.SettingsProvider;
1112
using Rubberduck.UI.Command;
13+
using Rubberduck.VBERuntime;
1214

1315
namespace Rubberduck.UI.Settings
1416
{
@@ -21,11 +23,17 @@ public enum DelimiterOptions
2123
public class GeneralSettingsViewModel : SettingsViewModelBase, ISettingsViewModel
2224
{
2325
private readonly IOperatingSystem _operatingSystem;
26+
private readonly IMessageBox _messageBox;
27+
private readonly IVBESettings _vbeSettings;
28+
2429
private bool _indenterPrompted;
2530

26-
public GeneralSettingsViewModel(Configuration config, IOperatingSystem operatingSystem)
31+
public GeneralSettingsViewModel(Configuration config, IOperatingSystem operatingSystem, IMessageBox messageBox, IVBESettings vbeSettings)
2732
{
2833
_operatingSystem = operatingSystem;
34+
_messageBox = messageBox;
35+
_vbeSettings = vbeSettings;
36+
2937
Languages = new ObservableCollection<DisplayLanguageSetting>(
3038
new[]
3139
{
@@ -131,11 +139,26 @@ public bool CompileBeforeParse
131139
get => _compileBeforeParse;
132140
set
133141
{
134-
if (_compileBeforeParse != value)
142+
if (_compileBeforeParse == value)
135143
{
136-
_compileBeforeParse = value;
137-
OnPropertyChanged();
144+
return;
145+
}
146+
147+
if (value && _vbeSettings.CompileOnDemand)
148+
{
149+
var result = _messageBox.Show(RubberduckUI.GeneralSettings_CompileBeforeParse_WarnCompileOnDemandEnabled,
150+
RubberduckUI.GeneralSettings_CompileBeforeParse_WarnCompileOnDemandEnabled_Caption, MessageBoxButtons.YesNo,
151+
MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
152+
if(result == DialogResult.No)
153+
{
154+
return;
155+
}
156+
157+
_vbeSettings.CompileOnDemand = false;
138158
}
159+
160+
_compileBeforeParse = value;
161+
OnPropertyChanged();
139162
}
140163
}
141164

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Rubberduck.VBERuntime
2+
{
3+
public interface IVBESettings
4+
{
5+
VBESettings.DllVersion Version { get; }
6+
bool CompileOnDemand { get; set; }
7+
bool BackGroundCompile { get; set; }
8+
}
9+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System;
2+
using Microsoft.Win32;
3+
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
4+
5+
namespace Rubberduck.VBERuntime
6+
{
7+
public class VBESettings : IVBESettings
8+
{
9+
private const string Vbe7SettingPath = @"HKEY_CURRENT_USER\Software\Microsoft\VBA\7.0\Common";
10+
private const string Vbe6SettingPath = @"HKEY_CURRENT_USER\Software\Microsoft\VBA\6.0\Common";
11+
12+
public enum DllVersion
13+
{
14+
Unknown,
15+
Vbe6,
16+
Vbe7
17+
}
18+
19+
private readonly string _registryRootPath;
20+
21+
public VBESettings(IVBE vbe)
22+
{
23+
try
24+
{
25+
switch (Convert.ToInt32(decimal.Parse(vbe.Version)))
26+
{
27+
case 6:
28+
Version = DllVersion.Vbe6;
29+
_registryRootPath = Vbe6SettingPath;
30+
break;
31+
case 7:
32+
Version = DllVersion.Vbe7;
33+
_registryRootPath = Vbe7SettingPath;
34+
break;
35+
default:
36+
Version = DllVersion.Unknown;
37+
break;
38+
}
39+
}
40+
catch
41+
{
42+
Version = DllVersion.Unknown;
43+
_registryRootPath = null;
44+
}
45+
}
46+
47+
public DllVersion Version { get; }
48+
49+
public bool CompileOnDemand
50+
{
51+
get => DWordToBooleanConverter(nameof(CompileOnDemand));
52+
set => BooleanToDWordConverter(nameof(CompileOnDemand), value);
53+
}
54+
55+
public bool BackGroundCompile
56+
{
57+
get => DWordToBooleanConverter(nameof(BackGroundCompile));
58+
set => BooleanToDWordConverter(nameof(BackGroundCompile), value);
59+
}
60+
61+
private const int DWordTrueValue = 1;
62+
private const int DWordFalseValue = 0;
63+
64+
private bool DWordToBooleanConverter(string keyName)
65+
{
66+
var result = Registry.GetValue(_registryRootPath, keyName, DWordFalseValue) as int?;
67+
return Convert.ToBoolean(result ?? DWordFalseValue);
68+
}
69+
70+
private void BooleanToDWordConverter(string keyName, bool value)
71+
{
72+
Registry.SetValue(_registryRootPath, keyName, value ? DWordTrueValue : DWordFalseValue, RegistryValueKind.DWord);
73+
}
74+
}
75+
}

RubberduckTests/RubberduckTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@
305305
<Compile Include="UnitTesting\EngineTests.cs" />
306306
<Compile Include="UnitTesting\DiscoveryTests.cs" />
307307
<Compile Include="UnitTesting\ViewModelTests.cs" />
308+
<Compile Include="VBE\VBESettingsTests.cs" />
308309
</ItemGroup>
309310
<ItemGroup>
310311
<None Include="app.config" />
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using NUnit.Framework;
2+
using Rubberduck.VBERuntime;
3+
using RubberduckTests.Mocks;
4+
5+
namespace RubberduckTests.VBE
6+
{
7+
[TestFixture]
8+
public class VBESettingsTests
9+
{
10+
[Category("VBE")]
11+
[Test]
12+
public void DllVersion_MustBe6()
13+
{
14+
var vbe = new MockVbeBuilder().Build();
15+
vbe.SetupGet(s => s.Version).Returns("6.00");
16+
var settings = new VBESettings(vbe.Object);
17+
18+
Assert.IsTrue(settings.Version == VBESettings.DllVersion.Vbe6);
19+
}
20+
21+
[Category("VBE")]
22+
[Test]
23+
public void DllVersion_MustBe7()
24+
{
25+
var vbe = new MockVbeBuilder().Build();
26+
vbe.SetupGet(s => s.Version).Returns("7.00");
27+
var settings = new VBESettings(vbe.Object);
28+
29+
Assert.IsTrue(settings.Version == VBESettings.DllVersion.Vbe7);
30+
}
31+
32+
[Category("VBE")]
33+
[Test]
34+
public void DllVersion_IsBogus()
35+
{
36+
var vbe = new MockVbeBuilder().Build();
37+
vbe.SetupGet(s => s.Version).Returns("foo");
38+
var settings = new VBESettings(vbe.Object);
39+
40+
Assert.IsTrue(settings.Version == VBESettings.DllVersion.Unknown);
41+
}
42+
43+
[Category("VBE")]
44+
[Test]
45+
public void DllVersion_IsNull()
46+
{
47+
var vbe = new MockVbeBuilder().Build();
48+
vbe.SetupGet(s => s.Version).Returns((string)null);
49+
var settings = new VBESettings(vbe.Object);
50+
51+
Assert.IsTrue(settings.Version == VBESettings.DllVersion.Unknown);
52+
}
53+
54+
[Category("VBE")]
55+
[Test]
56+
public void CompileOnDemand_WriteRead()
57+
{
58+
var vbe = new MockVbeBuilder().Build();
59+
vbe.SetupGet(s => s.Version).Returns("7.00");
60+
var settings = new VBESettings(vbe.Object);
61+
62+
settings.CompileOnDemand = true;
63+
Assert.IsTrue(settings.CompileOnDemand);
64+
65+
settings.CompileOnDemand = false;
66+
Assert.IsTrue(settings.CompileOnDemand == false);
67+
}
68+
69+
[Category("VBE")]
70+
[Test]
71+
public void BackGroundCompile_WriteRead()
72+
{
73+
var vbe = new MockVbeBuilder().Build();
74+
vbe.SetupGet(s => s.Version).Returns("7.00");
75+
var settings = new VBESettings(vbe.Object);
76+
77+
settings.BackGroundCompile = true;
78+
Assert.IsTrue(settings.BackGroundCompile);
79+
80+
settings.BackGroundCompile = false;
81+
Assert.IsTrue(settings.BackGroundCompile == false);
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)