forked from dotnet/ResXResourceManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDteConfiguration.cs
More file actions
127 lines (104 loc) · 4.14 KB
/
Copy pathDteConfiguration.cs
File metadata and controls
127 lines (104 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
namespace tomenglertde.ResXManager.VSIX
{
using System;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using JetBrains.Annotations;
using Microsoft.VisualStudio.Shell;
using tomenglertde.ResXManager.Infrastructure;
using tomenglertde.ResXManager.Model;
[Export(typeof(IConfiguration))]
[Export(typeof(Configuration))]
[Export(typeof(DteConfiguration))]
internal class DteConfiguration : Configuration
{
[NotNull]
private readonly DteSolution _solution;
[ImportingConstructor]
// ReSharper disable once NotNullMemberIsNotInitialized
public DteConfiguration([NotNull] DteSolution solution, [NotNull] ITracer tracer)
: base(tracer)
{
Contract.Requires(solution != null);
Contract.Requires(tracer != null);
_solution = solution;
}
[NotNull, UsedImplicitly]
[DefaultValue(MoveToResourceConfiguration.Default)]
public MoveToResourceConfiguration MoveToResources { get; }
[UsedImplicitly]
[DefaultValue(true)]
public bool ShowErrorsInErrorList { get; set; }
[UsedImplicitly]
[DefaultValue(TaskErrorCategory.Warning)]
public TaskErrorCategory TaskErrorCategory { get; set; }
public override bool IsScopeSupported => true;
public override ConfigurationScope Scope => _solution.Globals != null ? ConfigurationScope.Solution : ConfigurationScope.Global;
protected override T InternalGetValue<T>(T defaultValue, string key)
{
return TryGetValue(GetKey(key), defaultValue, out var value) ? value : base.InternalGetValue(defaultValue, key);
}
protected override void InternalSetValue<T>(T value, string key)
{
var globals = _solution.Globals;
if (globals != null)
{
TrySetValue(globals, GetKey(key), value);
}
else
{
base.InternalSetValue(value, key);
}
}
private bool TryGetValue<T>([CanBeNull] string key, [CanBeNull] T defaultValue, [CanBeNull] out T value)
{
value = defaultValue;
return TryGetValue(_solution.Globals, key, ref value);
}
private static bool TryGetValue<T>([CanBeNull] EnvDTE.Globals globals, [CanBeNull] string key, [CanBeNull] ref T value)
{
try
{
if ((globals != null) && globals.VariableExists[key])
{
value = ConvertFromString(globals[key] as string, value);
return true;
}
}
catch
{
// Just return false in case of errors. If there is some garbage in the solution, falback to the default.
}
return false;
}
private void TrySetValue<T>([NotNull] EnvDTE.Globals globals, [CanBeNull] string internalKey, [CanBeNull] T value)
{
Contract.Requires(globals != null);
try
{
globals[internalKey] = ConvertToString(value);
globals.VariablePersists[internalKey] = true;
}
catch (Exception ex)
{
Tracer.TraceError("Error saving configuration value to solution: {0}", ex.Message);
}
}
[NotNull]
private static string GetKey([CanBeNull] string propertyName)
{
Contract.Ensures(Contract.Result<string>() != null);
return @"RESX_" + propertyName;
}
[ContractInvariantMethod]
[SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "Required for code contracts.")]
[Conditional("CONTRACTS_FULL")]
private void ObjectInvariant()
{
Contract.Invariant(_solution != null);
}
}
}