-
Notifications
You must be signed in to change notification settings - Fork 19
/
CodingConventions.cs
71 lines (60 loc) · 2.69 KB
/
CodingConventions.cs
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
// Copyright (c) Paul Harrington. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information.
using System.ComponentModel.Composition;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.CodingConventions;
using Microsoft.VisualStudio.Text.Editor;
namespace EditorGuidelines
{
/// <summary>
/// Coding conventions support via Microsoft.VisualStudio.CodingConventions assembly.
/// </summary>
[Export]
internal sealed class CodingConventions
{
/// <summary>
/// Try to import from Microsoft.VisualStudio.CodingConventions.
/// </summary>
[Import(AllowDefault = true)]
private ICodingConventionsManager CodingConventionsManager { get; set; }
public async Task<Context> CreateContextAsync(IWpfTextView view, CancellationToken cancellationToken)
{
if (CodingConventionsManager is null)
{
// Coding Conventions not available in this SKU.
return null;
}
if (!view.TryGetTextDocument(out var textDocument))
{
return null;
}
string filePath = textDocument.FilePath;
ICodingConventionContext codingConventionContext = await CodingConventionsManager.GetConventionContextAsync(filePath, cancellationToken).ConfigureAwait(false);
return new Context(codingConventionContext, cancellationToken);
}
/// <summary>
/// Coding conventions narrowed to a single file.
/// </summary>
public class Context
{
private readonly ICodingConventionContext _innerContext;
public Context(ICodingConventionContext innerContext, CancellationToken cancellationToken)
{
_innerContext = innerContext;
_innerContext.CodingConventionsChangedAsync += OnCodingConventionsChangedAsync;
cancellationToken.Register(() => _innerContext.CodingConventionsChangedAsync -= OnCodingConventionsChangedAsync);
}
private Task OnCodingConventionsChangedAsync(object sender, CodingConventionsChangedEventArgs arg)
{
ConventionsChanged?.Invoke(this);
return Task.CompletedTask;
}
public delegate void ConventionsChangedEventHandler(Context sender);
public event ConventionsChangedEventHandler ConventionsChanged;
public bool TryGetCurrentSetting(string key, out string value)
{
return _innerContext.CurrentConventions.TryGetConventionValue(key, out value);
}
}
}
}