Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix CodingConventions support in 17.10 (#125)
* In Dev17, use IEditorOptions to retrieve coding conventions instead of the CodingConventions package * Remove global.json Because VS 2019 doesn't support dotnet 8 and VS 2022 doesn't install dotnet 7 Let each version pick the latest supported SDK. * Revert back to 17.0 SDK * Seal CodingConventions class
- Loading branch information
Showing
6 changed files
with
170 additions
and
39 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// 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.Tasks; | ||
using System.Threading; | ||
using Microsoft.VisualStudio.Text.Editor; | ||
using System.Collections.Generic; | ||
|
||
namespace EditorGuidelines | ||
{ | ||
/// <summary> | ||
/// Dev 17 support for Coding Conventions. Uses the dictionary supplied via <see cref="IEditorOptions"/>. | ||
/// </summary> | ||
[Export] | ||
internal sealed class CodingConventions | ||
{ | ||
public Task<Context> CreateContextAsync(IWpfTextView view, CancellationToken cancellationToken) | ||
{ | ||
return Task.FromResult(new Context(view.Options, cancellationToken)); | ||
} | ||
|
||
/// <summary> | ||
/// Coding conventions narrowed to a single file. | ||
/// </summary> | ||
public class Context | ||
{ | ||
private readonly IEditorOptions _editorOptions; | ||
private readonly CancellationToken _cancellationToken; | ||
private IReadOnlyDictionary<string, object> _currentConventions; | ||
|
||
// This is the same as DefaultOptions.RawCodingConventionsSnapshotOptionName from the 17.6 | ||
// editor SDK. However, by not referencing that constant, we can avoid taking a dependency | ||
// on the 17.6 SDK and can continue to load on earlier versions (albeit without | ||
// CodingConventions support). | ||
private const string c_codingConventionsSnapshotOptionName = "CodingConventionsSnapshot"; | ||
|
||
public Context(IEditorOptions editorOptions, CancellationToken cancellationToken) | ||
{ | ||
_editorOptions = editorOptions; | ||
_cancellationToken = cancellationToken; | ||
|
||
_editorOptions.OptionChanged += OnEditorOptionChanged; | ||
_cancellationToken.Register(() => _editorOptions.OptionChanged -= OnEditorOptionChanged); | ||
} | ||
|
||
private void OnEditorOptionChanged(object sender, EditorOptionChangedEventArgs e) | ||
{ | ||
if (e.OptionId == c_codingConventionsSnapshotOptionName) | ||
{ | ||
_currentConventions = _editorOptions.GetOptionValue<IReadOnlyDictionary<string, object>>(c_codingConventionsSnapshotOptionName); | ||
ConventionsChanged?.Invoke(this); | ||
} | ||
} | ||
|
||
public delegate void ConventionsChangedEventHandler(Context sender); | ||
|
||
public event ConventionsChangedEventHandler ConventionsChanged; | ||
|
||
public bool TryGetCurrentSetting(string key, out string value) | ||
{ | ||
if (_currentConventions is null || !_currentConventions.TryGetValue(key, out object obj) || obj is null) | ||
{ | ||
value = null; | ||
return false; | ||
} | ||
|
||
value = obj.ToString(); | ||
return !string.IsNullOrEmpty(value); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters