Skip to content

Commit b883804

Browse files
committed
Merge pull request #27 from Haacked/haacked/24-customize-encouragements
Add property page for encouragements
2 parents 5ff6ae5 + 1768316 commit b883804

13 files changed

+283
-85
lines changed

EncouragePackage/EncourageIntellisenseController.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using EnvDTE;
32
using Microsoft.VisualStudio.Language.Intellisense;
43
using Microsoft.VisualStudio.Text;
54
using Microsoft.VisualStudio.Text.Editor;
@@ -27,7 +26,7 @@ public EncourageIntellisenseController(
2726

2827
void OnDocumentDirtyStateChanged(object sender, EventArgs e)
2928
{
30-
if (!this.textDocument.IsDirty)
29+
if (!textDocument.IsDirty)
3130
{
3231
DisplayEncouragement();
3332
}
@@ -55,12 +54,10 @@ public void Detach(ITextView detacedTextView)
5554

5655
public void ConnectSubjectBuffer(ITextBuffer subjectBuffer)
5756
{
58-
5957
}
6058

6159
public void DisconnectSubjectBuffer(ITextBuffer subjectBuffer)
6260
{
63-
6461
}
6562
}
66-
}
63+
}

EncouragePackage/EncourageIntellisenseControllerProvider.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System.Collections.Generic;
22
using System.ComponentModel.Composition;
3-
using EnvDTE;
43
using Microsoft.VisualStudio.Language.Intellisense;
5-
using Microsoft.VisualStudio.Shell;
64
using Microsoft.VisualStudio.Text;
75
using Microsoft.VisualStudio.Text.Editor;
86
using Microsoft.VisualStudio.Utilities;

EncouragePackage/EncouragePackage.cs

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
using System;
2-
using System.Diagnostics;
3-
using System.Runtime.InteropServices;
1+
using System.Runtime.InteropServices;
2+
using Haack.Encourage.Options;
43
using Microsoft.VisualStudio.Shell;
54

65
namespace Haack.Encourage
76
{
87
/// <summary>
98
/// This is the class that implements the package exposed by this assembly.
10-
///
119
/// The minimum requirement for a class to be considered a valid package for Visual Studio
1210
/// is to implement the IVsPackage interface and register itself with the shell.
1311
/// This package uses the helper classes defined inside the Managed Package Framework (MPF)
14-
/// to do it: it derives from the Package class that provides the implementation of the
15-
/// IVsPackage interface and uses the registration attributes defined in the framework to
12+
/// to do it: it derives from the Package class that provides the implementation of the
13+
/// IVsPackage interface and uses the registration attributes defined in the framework to
1614
/// register itself and its components with the shell.
1715
/// </summary>
1816
// This attribute tells the PkgDef creation utility (CreatePkgDef.exe) that this class is
@@ -22,31 +20,8 @@ namespace Haack.Encourage
2220
// in the Help/About dialog of Visual Studio.
2321
[InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
2422
[Guid(GuidList.guidEncouragePackagePkgString)]
23+
[ProvideOptionPage(typeof(OptionsDialogPage), "Encourage", "Encouragement List", 0, 0, supportsAutomation: true)]
2524
public sealed class EncouragePackage : Package
2625
{
27-
/// <summary>
28-
/// Default constructor of the package.
29-
/// Inside this method you can place any initialization code that does not require
30-
/// any Visual Studio service because at this point the package object is created but
31-
/// not sited yet inside Visual Studio environment. The place to do all the other
32-
/// initialization is the Initialize method.
33-
/// </summary>
34-
public EncouragePackage()
35-
{
36-
Debug.WriteLine("Entering constructor for: {0}", this);
37-
}
38-
39-
/////////////////////////////////////////////////////////////////////////////
40-
// Overridden Package Implementation
41-
42-
/// <summary>
43-
/// Initialization of the package; this method is called right after the package is sited, so this is the place
44-
/// where you can put all the initialization code that rely on services provided by VisualStudio.
45-
/// </summary>
46-
protected override void Initialize()
47-
{
48-
Debug.WriteLine("Entering Initialize() of: {0}", this);
49-
base.Initialize();
50-
}
5126
}
5227
}

EncouragePackage/EncouragePackage.csproj

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@
4545
<RunCodeAnalysis>true</RunCodeAnalysis>
4646
</PropertyGroup>
4747
<ItemGroup>
48+
<Reference Include="Microsoft.VisualStudio.ComponentModelHost, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
49+
<SpecificVersion>False</SpecificVersion>
50+
<HintPath>..\References\Vs2012\Microsoft.VisualStudio.ComponentModelHost.dll</HintPath>
51+
</Reference>
4852
<Reference Include="Microsoft.VisualStudio.Text.Data, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
4953
<Reference Include="Microsoft.VisualStudio.Text.Logic, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
5054
<Reference Include="Microsoft.VisualStudio.Text.UI, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
@@ -71,6 +75,7 @@
7175
<Reference Include="System.Xaml" />
7276
<Reference Include="System.Xml" />
7377
<Reference Include="WindowsBase" />
78+
<Reference Include="WindowsFormsIntegration" />
7479
</ItemGroup>
7580
<ItemGroup>
7681
<COMReference Include="EnvDTE">
@@ -129,14 +134,20 @@
129134
</COMReference>
130135
</ItemGroup>
131136
<ItemGroup>
132-
<Compile Include="Encouragements.cs" />
133137
<Compile Include="EncouragePackage.cs" />
134138
<Compile Include="EncourageIntellisenseController.cs" />
135139
<Compile Include="EncourageIntellisenseControllerProvider.cs" />
136140
<Compile Include="EncourageSignatureHelpSource.cs" />
137141
<Compile Include="EncourageSignatureHelpSourceProvider.cs" />
142+
<Compile Include="Encouragments.cs" />
138143
<Compile Include="Guids.cs" />
139144
<Compile Include="IEncouragements.cs" />
145+
<Compile Include="Options\OptionsDialogPage.cs">
146+
<SubType>Component</SubType>
147+
</Compile>
148+
<Compile Include="Options\OptionsDialogPageControl.xaml.cs">
149+
<DependentUpon>OptionsDialogPageControl.xaml</DependentUpon>
150+
</Compile>
140151
<Compile Include="Resources.Designer.cs">
141152
<AutoGen>True</AutoGen>
142153
<DesignTime>True</DesignTime>
@@ -175,6 +186,12 @@
175186
<ItemGroup>
176187
<WCFMetadata Include="Service References\" />
177188
</ItemGroup>
189+
<ItemGroup>
190+
<Page Include="Options\OptionsDialogPageControl.xaml">
191+
<SubType>Designer</SubType>
192+
<Generator>MSBuild:Compile</Generator>
193+
</Page>
194+
</ItemGroup>
178195
<PropertyGroup>
179196
<UseCodebase>true</UseCodebase>
180197
</PropertyGroup>
@@ -187,4 +204,4 @@
187204
<Target Name="AfterBuild">
188205
</Target>
189206
-->
190-
</Project>
207+
</Project>

EncouragePackage/EncourageSignatureHelpSource.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ sealed class Signature : ISignature
1717

1818
public ITrackingSpan ApplicableToSpan
1919
{
20-
get { return this.trackingSpan; }
20+
get { return trackingSpan; }
2121
}
2222

2323
public string Content
2424
{
25-
get { return this.content; }
25+
get { return content; }
2626
}
2727

2828
public IParameter CurrentParameter
@@ -34,17 +34,17 @@ public IParameter CurrentParameter
3434

3535
public string Documentation
3636
{
37-
get { return this.documentation; }
37+
get { return documentation; }
3838
}
3939

4040
public ReadOnlyCollection<IParameter> Parameters
4141
{
42-
get { return new ReadOnlyCollection<IParameter>(new IParameter[] { }); }
42+
get { return new ReadOnlyCollection<IParameter>(new IParameter[] { }); }
4343
}
4444

4545
public string PrettyPrintedContent
4646
{
47-
get { return this.prettyPrintedContent; }
47+
get { return prettyPrintedContent; }
4848
}
4949

5050
internal Signature(ITrackingSpan trackingSpan, string content, string prettyPrintedContent, string documentation)
@@ -57,9 +57,9 @@ internal Signature(ITrackingSpan trackingSpan, string content, string prettyPrin
5757
}
5858

5959
/// <summary>
60-
/// This object needs to be added as a key to the property bag of an ITextView where
61-
/// encouragement should be applied. This prevents encouragement from being
62-
/// introduced in places like signature overload.
60+
/// This object needs to be added as a key to the property bag of an ITextView where
61+
/// encouragement should be applied. This prevents encouragement from being
62+
/// introduced in places like signature overload.
6363
/// </summary>
6464
internal static readonly object SessionKey = new object();
6565

@@ -112,8 +112,11 @@ public void AugmentSignatureHelpSession(ISignatureHelpSession session, IList<ISi
112112
SpanTrackingMode.EdgeInclusive);
113113

114114
string encouragement = encouragements.GetRandomEncouragement();
115-
var signature = new Signature(applicableToSpan, encouragement, "", "");
116-
signatures.Add(signature);
115+
if (encouragement != null)
116+
{
117+
var signature = new Signature(applicableToSpan, encouragement, "", "");
118+
signatures.Add(signature);
119+
}
117120
}
118121

119122
public ISignature GetBestMatch(ISignatureHelpSession session)

EncouragePackage/Encouragements.cs

Lines changed: 0 additions & 35 deletions
This file was deleted.

EncouragePackage/Encouragments.cs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using Microsoft.VisualStudio.Settings;
2+
using Microsoft.VisualStudio.Shell;
3+
using Microsoft.VisualStudio.Shell.Settings;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.ComponentModel.Composition;
7+
using System.Diagnostics;
8+
9+
namespace Haack.Encourage
10+
{
11+
[Export(typeof(IEncouragements))]
12+
public class Encouragements : IEncouragements
13+
{
14+
const string CollectionPath = "Encouragements";
15+
const string PropertyName = "AllEncouragements";
16+
17+
static readonly Random random = new Random();
18+
static readonly string[] defaultEncouragements = new[]
19+
{
20+
"Nice Job!",
21+
"Way to go!",
22+
"Wow, nice change!",
23+
"So good!",
24+
"Bravo!",
25+
"You rock!",
26+
"Well done!",
27+
"I see what you did there!",
28+
"Genius work!",
29+
"Thumbs up!",
30+
"Coding win!",
31+
"FTW!",
32+
"Yep!",
33+
"Nnnnailed it!"
34+
};
35+
36+
readonly List<string> encouragements = new List<string>(defaultEncouragements);
37+
readonly WritableSettingsStore writableSettingsStore;
38+
39+
public IEnumerable<string> AllEncouragements
40+
{
41+
get { return encouragements; }
42+
set
43+
{
44+
encouragements.Clear();
45+
encouragements.AddRange(value);
46+
if (encouragements.Count == 0)
47+
{
48+
encouragements.AddRange(defaultEncouragements);
49+
}
50+
SaveSettings();
51+
}
52+
}
53+
54+
[ImportingConstructor]
55+
public Encouragements(SVsServiceProvider vsServiceProvider)
56+
{
57+
var shellSettingsManager = new ShellSettingsManager(vsServiceProvider);
58+
writableSettingsStore = shellSettingsManager.GetWritableSettingsStore(SettingsScope.UserSettings);
59+
60+
LoadSettings();
61+
}
62+
63+
public string GetRandomEncouragement()
64+
{
65+
int randomIndex = random.Next(0, encouragements.Count);
66+
return encouragements[randomIndex];
67+
}
68+
69+
void LoadSettings()
70+
{
71+
try
72+
{
73+
if (writableSettingsStore.PropertyExists(CollectionPath, PropertyName))
74+
{
75+
string value = writableSettingsStore.GetString(CollectionPath, PropertyName);
76+
AllEncouragements = value.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
77+
}
78+
}
79+
catch (Exception ex)
80+
{
81+
Debug.Fail(ex.Message);
82+
}
83+
}
84+
85+
void SaveSettings()
86+
{
87+
try
88+
{
89+
if (!writableSettingsStore.CollectionExists(CollectionPath))
90+
{
91+
writableSettingsStore.CreateCollection(CollectionPath);
92+
}
93+
94+
string value = string.Join(Environment.NewLine, encouragements);
95+
writableSettingsStore.SetString(CollectionPath, PropertyName, value);
96+
}
97+
catch (Exception ex)
98+
{
99+
Debug.Fail(ex.Message);
100+
}
101+
}
102+
}
103+
}

EncouragePackage/IEncouragements.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
namespace Haack.Encourage
1+
using System.Collections.Generic;
2+
namespace Haack.Encourage
23
{
34
public interface IEncouragements
45
{
6+
IEnumerable<string> AllEncouragements { get; set; }
7+
58
string GetRandomEncouragement();
69
}
710
}

0 commit comments

Comments
 (0)