Skip to content

Commit

Permalink
Made settings cloneable.
Browse files Browse the repository at this point in the history
Fixes #4.
  • Loading branch information
benquarmby committed May 4, 2015
1 parent 6eed703 commit a99b672
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -152,3 +152,4 @@ $RECYCLE.BIN/

# Mac desktop service store files
.DS_Store
*.GhostDoc.xml
Expand Up @@ -176,7 +176,7 @@ private void OnProjectNodeRun(object sender, EventArgs e)
private void OnProjectNodeSettings(object sender, EventArgs e)
{
var project = this.Environment.SelectedItems.Item(1).Project;
var model = this.VisualStudioJSLintProvider.LoadSettings(project, false);
var model = this.VisualStudioJSLintProvider.LoadSettings(project, false).TypedClone();

using (var viewModel = new SettingsViewModel(model))
{
Expand Down
29 changes: 28 additions & 1 deletion source/JSLintNet/JSLintOptions.cs
@@ -1,10 +1,11 @@
namespace JSLintNet
{
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Newtonsoft.Json;

public partial class JSLintOptions
public partial class JSLintOptions : ICloneable
{
private Dictionary<string, bool> predefinedGlobals;

Expand Down Expand Up @@ -35,6 +36,32 @@ public JSLintOptions()
}
}

/// <summary>
/// Creates a new object that is a copy of the current instance.
/// </summary>
/// <returns>
/// A new object that is a copy of this instance.
/// </returns>
public JSLintOptions TypedClone()
{
var clone = new JSLintOptions();
clone.predefinedGlobals = new Dictionary<string, bool>(this.predefinedGlobals);
this.CloneRoot(clone);

return clone;
}

/// <summary>
/// Creates a new object that is a copy of the current instance.
/// </summary>
/// <returns>
/// A new object that is a copy of this instance.
/// </returns>
public object Clone()
{
return this.TypedClone();
}

/// <summary>
/// Merges the specified options into this instance.
/// </summary>
Expand Down
36 changes: 35 additions & 1 deletion source/JSLintNet/JSLintOptions.generated.cs
Expand Up @@ -2,12 +2,13 @@

namespace JSLintNet
{
using System;
using Newtonsoft.Json;

/// <summary>
/// Provides a simple way to create and manage the options used by JSLint.
/// </summary>
public partial class JSLintOptions
public partial class JSLintOptions: ICloneable
{
/// <summary>
/// Gets or sets a value indicating whether assignment expressions should be allowed.
Expand Down Expand Up @@ -357,6 +358,39 @@ public partial class JSLintOptions
[JsonProperty("white")]
public bool? TolerateMessyWhitespace { get; set; }

private void CloneRoot(JSLintOptions clone)
{
clone.TolerateAssignmentExpressions = this.TolerateAssignmentExpressions;
clone.TolerateBitwiseOperators = this.TolerateBitwiseOperators;
clone.AssumeBrowser = this.AssumeBrowser;
clone.TolerateGoogleClosure = this.TolerateGoogleClosure;
clone.TolerateContinue = this.TolerateContinue;
clone.AssumeCouchDB = this.AssumeCouchDB;
clone.TolerateDebuggerStatements = this.TolerateDebuggerStatements;
clone.AssumeConsole = this.AssumeConsole;
clone.TolerateDoubleEquals = this.TolerateDoubleEquals;
clone.TolerateEval = this.TolerateEval;
clone.TolerateUnfilteredForIn = this.TolerateUnfilteredForIn;
clone.IndentationFactor = this.IndentationFactor;
clone.MaximumErrors = this.MaximumErrors;
clone.MaximumLineLength = this.MaximumLineLength;
clone.TolerateUncapitalizedConstructors = this.TolerateUncapitalizedConstructors;
clone.AssumeNode = this.AssumeNode;
clone.TolerateDanglingUnderscores = this.TolerateDanglingUnderscores;
clone.StopOnFirstError = this.StopOnFirstError;
clone.TolerateIncrementDecrement = this.TolerateIncrementDecrement;
clone.PropertiesDeclared = this.PropertiesDeclared;
clone.TolerateInsecureRegExp = this.TolerateInsecureRegExp;
clone.AssumeRhino = this.AssumeRhino;
clone.TolerateUnusedParameters = this.TolerateUnusedParameters;
clone.TolerateMissingUseStrict = this.TolerateMissingUseStrict;
clone.TolerateStupidPractices = this.TolerateStupidPractices;
clone.TolerateInefficientSubscripting = this.TolerateInefficientSubscripting;
clone.TolerateToDoComments = this.TolerateToDoComments;
clone.TolerateManyVarStatements = this.TolerateManyVarStatements;
clone.TolerateMessyWhitespace = this.TolerateMessyWhitespace;
}

private void MergeRoot(JSLintOptions merge)
{
if (merge.TolerateAssignmentExpressions.HasValue)
Expand Down
15 changes: 14 additions & 1 deletion source/JSLintNet/JSLintOptions.generated.tt
Expand Up @@ -9,12 +9,13 @@

namespace JSLintNet
{
using System;
using Newtonsoft.Json;

/// <summary>
/// Provides a simple way to create and manage the options used by JSLint.
/// </summary>
public partial class JSLintOptions
public partial class JSLintOptions: ICloneable
{
<#
var first = true;
Expand Down Expand Up @@ -62,6 +63,18 @@ namespace JSLintNet
}
#>

private void CloneRoot(JSLintOptions clone)
{
<#
foreach (var definition in OptionDefinitions)
{
#>
clone.<#= definition.PropertyName #> = this.<#= definition.PropertyName #>;
<#
}
#>
}

private void MergeRoot(JSLintOptions merge)
{
<#
Expand Down
34 changes: 33 additions & 1 deletion source/JSLintNet/Settings/JSLintNetSettings.cs
Expand Up @@ -29,7 +29,7 @@ internal enum Output
/// <summary>
/// Represents the settings available to the JSLint.NET suite.
/// </summary>
internal partial class JSLintNetSettings
internal partial class JSLintNetSettings : ICloneable
{
/// <summary>
/// The standard file name for JSLint.NET settings.
Expand Down Expand Up @@ -169,6 +169,38 @@ public int FileLimitOrDefault()
return nullable.HasValue && nullable.Value > 0 ? nullable.Value : JSLintNetSettings.DefaultFileLimit;
}

/// <summary>
/// Creates a new object that is a copy of the current instance.
/// </summary>
/// <returns>
/// A new object that is a copy of this instance.
/// </returns>
public JSLintNetSettings TypedClone()
{
var clone = new JSLintNetSettings()
{
Files = new List<string>(this.Files),
Output = this.Output,
Ignore = new List<string>(this.Ignore),
Options = this.Options == null ? null : this.Options.TypedClone()
};

this.CloneRoot(clone);

return clone;
}

/// <summary>
/// Creates a new object that is a copy of the current instance.
/// </summary>
/// <returns>
/// A new object that is a copy of this instance.
/// </returns>
public object Clone()
{
return this.TypedClone();
}

/// <summary>
/// Merges the specified settings into this instance.
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions source/JSLintNet/Settings/JSLintNetSettings.generated.cs
Expand Up @@ -78,5 +78,14 @@ private void MergeRoot(JSLintNetSettings merge)
this.FileLimit = merge.FileLimit;
}
}

private void CloneRoot(JSLintNetSettings target)
{
target.RunOnSave = this.RunOnSave;
target.RunOnBuild = this.RunOnBuild;
target.CancelBuild = this.CancelBuild;
target.ErrorLimit = this.ErrorLimit;
target.FileLimit = this.FileLimit;
}
}
}
12 changes: 12 additions & 0 deletions source/JSLintNet/Settings/JSLintNetSettings.generated.tt
Expand Up @@ -78,6 +78,18 @@ namespace JSLintNet.Settings
}
<#
}
#>
}

private void CloneRoot(JSLintNetSettings target)
{
<#
foreach (var definition in SettingDefinitions)
{
#>
target.<#= definition.PropertyName #> = this.<#= definition.PropertyName #>;
<#
}
#>
}
}
Expand Down
4 changes: 3 additions & 1 deletion source/JSLintNet/UI/ViewModels/SettingsViewModel.cs
Expand Up @@ -10,6 +10,8 @@

internal partial class SettingsViewModel : ViewModelBase
{
private static readonly Regex SeparatorPattern = new Regex(@"[\s,;'""]+", RegexOptions.Compiled);

public SettingsViewModel(JSLintNetSettings model)
{
this.Model = model;
Expand Down Expand Up @@ -128,7 +130,7 @@ private static void PopulateDictionaryFromString(Dictionary<string, bool> dictio
return;
}

var keys = Regex.Split(value, @"[\s,;'""]+");
var keys = SeparatorPattern.Split(value);

// Add new keys
foreach (var key in keys)
Expand Down
Expand Up @@ -672,7 +672,7 @@ public void Spec02()
testable.Init();
testable.MenuCommand.Invoke(testable.MenuCommand);

testable.Verify<IVisualStudioJSLintProvider>(x => x.SaveSettings(testable.ProjectMock.Object, testable.Settings));
testable.Verify<IVisualStudioJSLintProvider>(x => x.SaveSettings(testable.ProjectMock.Object, It.IsAny<JSLintNetSettings>()));
}
}

Expand Down

0 comments on commit a99b672

Please sign in to comment.