Skip to content

Commit

Permalink
Merge pull request #27 from Haacked/haacked/24-customize-encouragements
Browse files Browse the repository at this point in the history
Add property page for encouragements
  • Loading branch information
haacked committed Jul 29, 2014
2 parents 5ff6ae5 + 1768316 commit b883804
Show file tree
Hide file tree
Showing 13 changed files with 283 additions and 85 deletions.
7 changes: 2 additions & 5 deletions EncouragePackage/EncourageIntellisenseController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using EnvDTE;
using Microsoft.VisualStudio.Language.Intellisense;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
Expand Down Expand Up @@ -27,7 +26,7 @@ public EncourageIntellisenseController(

void OnDocumentDirtyStateChanged(object sender, EventArgs e)
{
if (!this.textDocument.IsDirty)
if (!textDocument.IsDirty)
{
DisplayEncouragement();
}
Expand Down Expand Up @@ -55,12 +54,10 @@ public void Detach(ITextView detacedTextView)

public void ConnectSubjectBuffer(ITextBuffer subjectBuffer)
{

}

public void DisconnectSubjectBuffer(ITextBuffer subjectBuffer)
{

}
}
}
}
2 changes: 0 additions & 2 deletions EncouragePackage/EncourageIntellisenseControllerProvider.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System.Collections.Generic;
using System.ComponentModel.Composition;
using EnvDTE;
using Microsoft.VisualStudio.Language.Intellisense;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Utilities;
Expand Down
35 changes: 5 additions & 30 deletions EncouragePackage/EncouragePackage.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices;
using Haack.Encourage.Options;
using Microsoft.VisualStudio.Shell;

namespace Haack.Encourage
{
/// <summary>
/// This is the class that implements the package exposed by this assembly.
///
/// The minimum requirement for a class to be considered a valid package for Visual Studio
/// is to implement the IVsPackage interface and register itself with the shell.
/// This package uses the helper classes defined inside the Managed Package Framework (MPF)
/// to do it: it derives from the Package class that provides the implementation of the
/// IVsPackage interface and uses the registration attributes defined in the framework to
/// to do it: it derives from the Package class that provides the implementation of the
/// IVsPackage interface and uses the registration attributes defined in the framework to
/// register itself and its components with the shell.
/// </summary>
// This attribute tells the PkgDef creation utility (CreatePkgDef.exe) that this class is
Expand All @@ -22,31 +20,8 @@ namespace Haack.Encourage
// in the Help/About dialog of Visual Studio.
[InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
[Guid(GuidList.guidEncouragePackagePkgString)]
[ProvideOptionPage(typeof(OptionsDialogPage), "Encourage", "Encouragement List", 0, 0, supportsAutomation: true)]
public sealed class EncouragePackage : Package
{
/// <summary>
/// Default constructor of the package.
/// Inside this method you can place any initialization code that does not require
/// any Visual Studio service because at this point the package object is created but
/// not sited yet inside Visual Studio environment. The place to do all the other
/// initialization is the Initialize method.
/// </summary>
public EncouragePackage()
{
Debug.WriteLine("Entering constructor for: {0}", this);
}

/////////////////////////////////////////////////////////////////////////////
// Overridden Package Implementation

/// <summary>
/// Initialization of the package; this method is called right after the package is sited, so this is the place
/// where you can put all the initialization code that rely on services provided by VisualStudio.
/// </summary>
protected override void Initialize()
{
Debug.WriteLine("Entering Initialize() of: {0}", this);
base.Initialize();
}
}
}
21 changes: 19 additions & 2 deletions EncouragePackage/EncouragePackage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
<RunCodeAnalysis>true</RunCodeAnalysis>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.ComponentModelHost, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\References\Vs2012\Microsoft.VisualStudio.ComponentModelHost.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.Text.Data, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="Microsoft.VisualStudio.Text.Logic, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="Microsoft.VisualStudio.Text.UI, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
Expand All @@ -71,6 +75,7 @@
<Reference Include="System.Xaml" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
<Reference Include="WindowsFormsIntegration" />
</ItemGroup>
<ItemGroup>
<COMReference Include="EnvDTE">
Expand Down Expand Up @@ -129,14 +134,20 @@
</COMReference>
</ItemGroup>
<ItemGroup>
<Compile Include="Encouragements.cs" />
<Compile Include="EncouragePackage.cs" />
<Compile Include="EncourageIntellisenseController.cs" />
<Compile Include="EncourageIntellisenseControllerProvider.cs" />
<Compile Include="EncourageSignatureHelpSource.cs" />
<Compile Include="EncourageSignatureHelpSourceProvider.cs" />
<Compile Include="Encouragments.cs" />
<Compile Include="Guids.cs" />
<Compile Include="IEncouragements.cs" />
<Compile Include="Options\OptionsDialogPage.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Options\OptionsDialogPageControl.xaml.cs">
<DependentUpon>OptionsDialogPageControl.xaml</DependentUpon>
</Compile>
<Compile Include="Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
Expand Down Expand Up @@ -175,6 +186,12 @@
<ItemGroup>
<WCFMetadata Include="Service References\" />
</ItemGroup>
<ItemGroup>
<Page Include="Options\OptionsDialogPageControl.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<PropertyGroup>
<UseCodebase>true</UseCodebase>
</PropertyGroup>
Expand All @@ -187,4 +204,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
23 changes: 13 additions & 10 deletions EncouragePackage/EncourageSignatureHelpSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ sealed class Signature : ISignature

public ITrackingSpan ApplicableToSpan
{
get { return this.trackingSpan; }
get { return trackingSpan; }
}

public string Content
{
get { return this.content; }
get { return content; }
}

public IParameter CurrentParameter
Expand All @@ -34,17 +34,17 @@ public IParameter CurrentParameter

public string Documentation
{
get { return this.documentation; }
get { return documentation; }
}

public ReadOnlyCollection<IParameter> Parameters
{
get { return new ReadOnlyCollection<IParameter>(new IParameter[] { }); }
get { return new ReadOnlyCollection<IParameter>(new IParameter[] { }); }
}

public string PrettyPrintedContent
{
get { return this.prettyPrintedContent; }
get { return prettyPrintedContent; }
}

internal Signature(ITrackingSpan trackingSpan, string content, string prettyPrintedContent, string documentation)
Expand All @@ -57,9 +57,9 @@ internal Signature(ITrackingSpan trackingSpan, string content, string prettyPrin
}

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

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

string encouragement = encouragements.GetRandomEncouragement();
var signature = new Signature(applicableToSpan, encouragement, "", "");
signatures.Add(signature);
if (encouragement != null)
{
var signature = new Signature(applicableToSpan, encouragement, "", "");
signatures.Add(signature);
}
}

public ISignature GetBestMatch(ISignatureHelpSession session)
Expand Down
35 changes: 0 additions & 35 deletions EncouragePackage/Encouragements.cs

This file was deleted.

103 changes: 103 additions & 0 deletions EncouragePackage/Encouragments.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using Microsoft.VisualStudio.Settings;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Settings;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics;

namespace Haack.Encourage
{
[Export(typeof(IEncouragements))]
public class Encouragements : IEncouragements
{
const string CollectionPath = "Encouragements";
const string PropertyName = "AllEncouragements";

static readonly Random random = new Random();
static readonly string[] defaultEncouragements = new[]
{
"Nice Job!",
"Way to go!",
"Wow, nice change!",
"So good!",
"Bravo!",
"You rock!",
"Well done!",
"I see what you did there!",
"Genius work!",
"Thumbs up!",
"Coding win!",
"FTW!",
"Yep!",
"Nnnnailed it!"
};

readonly List<string> encouragements = new List<string>(defaultEncouragements);
readonly WritableSettingsStore writableSettingsStore;

public IEnumerable<string> AllEncouragements
{
get { return encouragements; }
set
{
encouragements.Clear();
encouragements.AddRange(value);
if (encouragements.Count == 0)
{
encouragements.AddRange(defaultEncouragements);
}
SaveSettings();
}
}

[ImportingConstructor]
public Encouragements(SVsServiceProvider vsServiceProvider)
{
var shellSettingsManager = new ShellSettingsManager(vsServiceProvider);
writableSettingsStore = shellSettingsManager.GetWritableSettingsStore(SettingsScope.UserSettings);

LoadSettings();
}

public string GetRandomEncouragement()
{
int randomIndex = random.Next(0, encouragements.Count);
return encouragements[randomIndex];
}

void LoadSettings()
{
try
{
if (writableSettingsStore.PropertyExists(CollectionPath, PropertyName))
{
string value = writableSettingsStore.GetString(CollectionPath, PropertyName);
AllEncouragements = value.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
}
}
catch (Exception ex)
{
Debug.Fail(ex.Message);
}
}

void SaveSettings()
{
try
{
if (!writableSettingsStore.CollectionExists(CollectionPath))
{
writableSettingsStore.CreateCollection(CollectionPath);
}

string value = string.Join(Environment.NewLine, encouragements);
writableSettingsStore.SetString(CollectionPath, PropertyName, value);
}
catch (Exception ex)
{
Debug.Fail(ex.Message);
}
}
}
}
5 changes: 4 additions & 1 deletion EncouragePackage/IEncouragements.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
namespace Haack.Encourage
using System.Collections.Generic;
namespace Haack.Encourage
{
public interface IEncouragements
{
IEnumerable<string> AllEncouragements { get; set; }

string GetRandomEncouragement();
}
}
Loading

0 comments on commit b883804

Please sign in to comment.