提交 ac850257 编写于 作者: C Cyrus Najmabadi

Event hookup should follow user naming styles

上级 4f4ccc41
......@@ -29,6 +29,8 @@ public NamingStylesTestOptionSets(string languageName)
_optionKey = NamingStyleOptions.GetNamingPreferencesOptionKey(languageName);
}
public OptionKey OptionKey => _optionKey;
public IDictionary<OptionKey, object> MergeStyles(IDictionary<OptionKey, object> first, IDictionary<OptionKey, object> second, string languageName)
{
var firstPreferences = (NamingStylePreferences)first.First().Value;
......@@ -57,6 +59,9 @@ public NamingStylesTestOptionSets(string languageName)
public IDictionary<OptionKey, object> MethodNamesArePascalCase =>
Options(_optionKey, MethodNamesArePascalCaseOption());
public IDictionary<OptionKey, object> MethodNamesAreCamelCase =>
Options(_optionKey, MethodNamesAreCamelCaseOption());
public IDictionary<OptionKey, object> ParameterNamesAreCamelCase =>
Options(_optionKey, ParameterNamesAreCamelCaseOption());
......@@ -290,6 +295,12 @@ private NamingStylePreferences FieldNamesAreCamelCaseWithFieldUnderscorePrefixAn
}
private static NamingStylePreferences MethodNamesArePascalCaseOption()
=> MethodNamesAreCasedOption(Capitalization.PascalCase);
internal static NamingStylePreferences MethodNamesAreCamelCaseOption()
=> MethodNamesAreCasedOption(Capitalization.CamelCase);
private static NamingStylePreferences MethodNamesAreCasedOption(Capitalization capitalization)
{
var symbolSpecification = new SymbolSpecification(
null,
......@@ -300,7 +311,7 @@ private static NamingStylePreferences MethodNamesArePascalCaseOption()
var namingStyle = new NamingStyle(
Guid.NewGuid(),
capitalizationScheme: Capitalization.PascalCase,
capitalizationScheme: capitalization,
name: "Name",
prefix: "",
suffix: "",
......
......@@ -3,11 +3,15 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics.Analyzers.NamingStyles;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.Internal.Log;
......@@ -19,6 +23,7 @@
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Roslyn.Utilities;
using static Microsoft.CodeAnalysis.Diagnostics.Analyzers.NamingStyles.SymbolSpecification;
namespace Microsoft.CodeAnalysis.Editor.CSharp.EventHookup
{
......@@ -96,6 +101,7 @@ public void Cancel()
{
AssertIsForeground();
_cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = _cancellationTokenSource.Token;
_textView = textView;
_subjectBuffer = subjectBuffer;
this.TESTSessionHookupMutex = testSessionHookupMutex;
......@@ -110,22 +116,22 @@ public void Cancel()
var asyncToken = asyncListener.BeginAsyncOperation(GetType().Name + ".Start");
this.GetEventNameTask = Task.Factory.SafeStartNewFromAsync(
() => DetermineIfEventHookupAndGetHandlerNameAsync(document, position, _cancellationTokenSource.Token),
_cancellationTokenSource.Token,
() => DetermineIfEventHookupAndGetHandlerNameAsync(document, position, cancellationToken),
cancellationToken,
TaskScheduler.Default);
var continuedTask = this.GetEventNameTask.SafeContinueWithFromAsync(
async t =>
{
await ThreadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(alwaysYield: true, _cancellationTokenSource.Token);
_cancellationTokenSource.Token.ThrowIfCancellationRequested();
await ThreadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(alwaysYield: true, cancellationToken);
cancellationToken.ThrowIfCancellationRequested();
if (t.Result != null)
{
commandHandler.EventHookupSessionManager.EventHookupFoundInSession(this);
}
},
_cancellationTokenSource.Token,
cancellationToken,
TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously,
TaskScheduler.Default);
......@@ -167,7 +173,14 @@ private async Task<string> DetermineIfEventHookupAndGetHandlerNameAsync(Document
return null;
}
return GetEventHandlerName(eventSymbol, plusEqualsToken.Value, semanticModel, document.GetLanguageService<ISyntaxFactsService>());
var namingRule = await document.GetApplicableNamingRuleAsync(
new SymbolKindOrTypeKind(MethodKind.Ordinary),
new DeclarationModifiers(isStatic: plusEqualsToken.Value.Parent.IsInStaticContext()),
Accessibility.Private, cancellationToken).ConfigureAwait(false);
return GetEventHandlerName(
eventSymbol, plusEqualsToken.Value, semanticModel,
document.GetLanguageService<ISyntaxFactsService>(), namingRule);
}
}
......@@ -207,11 +220,14 @@ private IEventSymbol GetEventSymbol(SemanticModel semanticModel, SyntaxToken plu
return symbol as IEventSymbol;
}
private string GetEventHandlerName(IEventSymbol eventSymbol, SyntaxToken plusEqualsToken, SemanticModel semanticModel, ISyntaxFactsService syntaxFactsService)
private string GetEventHandlerName(
IEventSymbol eventSymbol, SyntaxToken plusEqualsToken, SemanticModel semanticModel,
ISyntaxFactsService syntaxFactsService, NamingRule namingRule)
{
AssertIsBackground();
var basename = string.Format("{0}_{1}", GetNameObjectPart(eventSymbol, plusEqualsToken, semanticModel, syntaxFactsService), eventSymbol.Name);
basename = basename.ToPascalCase(trimLeadingTypePrefix: false);
var objectPart = GetNameObjectPart(eventSymbol, plusEqualsToken, semanticModel, syntaxFactsService);
var basename = namingRule.NamingStyle.CreateName(ImmutableArray.Create(
string.Format("{0}_{1}", objectPart, eventSymbol.Name)));
var reservedNames = semanticModel.LookupSymbols(plusEqualsToken.SpanStart).Select(m => m.Name);
......
......@@ -3,9 +3,11 @@
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.Editor.UnitTests.Diagnostics.NamingStyles;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
......@@ -16,6 +18,8 @@ namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.EventHookup
[UseExportProvider]
public class EventHookupCommandHandlerTests
{
private readonly NamingStylesTestOptionSets _namingOptions = new NamingStylesTestOptionSets(LanguageNames.CSharp);
[WpfFact, Trait(Traits.Feature, Traits.Features.EventHookup)]
public async Task HandlerName_EventInThisClass()
{
......@@ -34,6 +38,27 @@ void M()
testState.AssertShowing("C_MyEvent");
}
[WorkItem(20999, "https://github.com/dotnet/roslyn/issues/20999")]
[WpfFact, Trait(Traits.Feature, Traits.Features.EventHookup)]
public async Task HandlerName_EventInThisClass_CamelCaseRule()
{
var markup = @"
class C
{
event System.Action MyEvent;
void M()
{
MyEvent +$$
}
}";
using var testState = new EventHookupTestState(
EventHookupTestState.GetWorkspaceXml(markup), _namingOptions.MethodNamesAreCamelCase);
testState.SendTypeChar('=');
await testState.WaitForAsynchronousOperationsAsync();
testState.AssertShowing("c_MyEvent");
}
[WpfFact, Trait(Traits.Feature, Traits.Features.EventHookup)]
public async Task HandlerName_EventOnLocal()
{
......
......@@ -44,16 +44,15 @@ private static ComposableCatalog GetExtraParts()
}
public static EventHookupTestState CreateTestState(string markup, IDictionary<OptionKey, object> options = null)
{
var workspaceXml = string.Format(@"
=> new EventHookupTestState(GetWorkspaceXml(markup), options);
public static XElement GetWorkspaceXml(string markup)
=> XElement.Parse(string.Format(@"
<Workspace>
<Project Language=""C#"" CommonReferences=""true"">
<Document>{0}</Document>
</Project>
</Workspace>", markup);
return new EventHookupTestState(XElement.Parse(workspaceXml), options);
}
</Workspace>", markup));
internal void AssertShowing(string expectedText)
{
......
......@@ -13,6 +13,7 @@
<ProjectReference Include="..\..\..\Compilers\Test\Utilities\CSharp\Microsoft.CodeAnalysis.CSharp.Test.Utilities.csproj" />
<ProjectReference Include="..\..\..\Compilers\Test\Utilities\VisualBasic\Microsoft.CodeAnalysis.VisualBasic.Test.Utilities.vbproj" />
<ProjectReference Include="..\..\..\Compilers\VisualBasic\Portable\Microsoft.CodeAnalysis.VisualBasic.vbproj" />
<ProjectReference Include="..\..\..\EditorFeatures\DiagnosticsTestUtilities\Microsoft.CodeAnalysis.EditorFeatures.DiagnosticsTests.Utilities.csproj" />
<ProjectReference Include="..\..\..\EditorFeatures\TestUtilities2\Microsoft.CodeAnalysis.EditorFeatures.Test.Utilities.vbproj" />
<ProjectReference Include="..\..\..\EditorFeatures\VisualBasic\Microsoft.CodeAnalysis.VisualBasic.EditorFeatures.vbproj" />
<ProjectReference Include="..\..\..\Features\LanguageServer\Protocol\Microsoft.CodeAnalysis.LanguageServer.Protocol.csproj" />
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册