Browse Source

Added IServiceProvided infrastructure for the refactoring context.

newNRvisualizers
Mike Krüger 14 years ago
parent
commit
ce7bdd7349
  1. 1
      ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj
  2. 27
      ICSharpCode.NRefactory.CSharp/Refactoring/BaseRefactoringContext.cs
  3. 6
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/InconsistentNamingIssue.cs
  4. 38
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/NamingConventionService.cs
  5. 19
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/TestRefactoringContext.cs

1
ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj

@ -349,6 +349,7 @@ @@ -349,6 +349,7 @@
<Compile Include="Refactoring\CodeIssues\InconsistentNamingIssue\DefaultRules.cs" />
<Compile Include="Refactoring\CodeIssues\InconsistentNamingIssue\InconsistentNamingIssue.cs" />
<Compile Include="Refactoring\CodeActions\CreateMethodDeclarationAction.cs" />
<Compile Include="Refactoring\CodeIssues\InconsistentNamingIssue\NamingConventionService.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ICSharpCode.NRefactory\ICSharpCode.NRefactory.csproj">

27
ICSharpCode.NRefactory.CSharp/Refactoring/BaseRefactoringContext.cs

@ -33,10 +33,11 @@ using ICSharpCode.NRefactory.Semantics; @@ -33,10 +33,11 @@ using ICSharpCode.NRefactory.Semantics;
using ICSharpCode.NRefactory.TypeSystem;
using ICSharpCode.NRefactory.TypeSystem.Implementation;
using ICSharpCode.NRefactory.Editor;
using System.ComponentModel.Design;
namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
public abstract class BaseRefactoringContext
public abstract class BaseRefactoringContext : IServiceProvider
{
protected readonly CSharpAstResolver resolver;
readonly CancellationToken cancellationToken;
@ -64,10 +65,6 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -64,10 +65,6 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
this.cancellationToken = cancellationToken;
}
/// <summary>
/// Requests data that can be provided by the IDE
/// </summary>
public abstract T RequestData<T>();
#region Resolving
public ResolveResult Resolve (AstNode node)
@ -111,6 +108,26 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -111,6 +108,26 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
return str;
}
#region IServiceProvider implementation
readonly ServiceContainer services = new ServiceContainer();
/// <summary>
/// Gets a service container used to associate services with this context.
/// </summary>
public ServiceContainer Services {
get { return services; }
}
/// <summary>
/// Retrieves a service from the refactoring context.
/// If the service is not found in the <see cref="Services"/> container.
/// </summary>
public object GetService(Type serviceType)
{
return services.GetService(serviceType);
}
#endregion
}
}

6
ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/InconsistentNamingIssue.cs

@ -47,13 +47,13 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -47,13 +47,13 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
class GatherVisitor : GatherVisitorBase
{
readonly InconsistentNamingIssue inspector;
readonly NamingConventionService service;
List<NamingRule> rules;
public GatherVisitor (BaseRefactoringContext ctx, InconsistentNamingIssue inspector) : base (ctx)
{
this.inspector = inspector;
rules = new List<NamingRule> (ctx.RequestData<IEnumerable<NamingRule>> () ?? Enumerable.Empty<NamingRule> ());
service = (NamingConventionService)ctx.GetService (typeof (NamingConventionService));
}
void CheckName(AstNode node, AffectedEntity entity, Identifier identifier, Modifiers accessibilty)
@ -86,7 +86,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -86,7 +86,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
void CheckNamedResolveResult(ResolveResult resolveResult, AffectedEntity entity, Identifier identifier, Modifiers accessibilty)
{
foreach (var rule in rules) {
foreach (var rule in service.Rules) {
if (!rule.AffectedEntity.HasFlag(entity)) {
continue;
}

38
ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/NamingConventionService.cs

@ -0,0 +1,38 @@ @@ -0,0 +1,38 @@
//
// NamingConventionService.cs
//
// Author:
// Mike Krüger <mkrueger@xamarin.com>
//
// Copyright (c) 2012 Xamarin <http://xamarin.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;
namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
public abstract class NamingConventionService
{
public abstract IEnumerable<NamingRule> Rules {
get;
}
}
}

19
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/TestRefactoringContext.cs

@ -44,10 +44,20 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions @@ -44,10 +44,20 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
internal readonly IDocument doc;
readonly TextLocation location;
public TestRefactoringContext(IDocument document, TextLocation location, CSharpAstResolver resolver) : base(resolver, CancellationToken.None)
public TestRefactoringContext (IDocument document, TextLocation location, CSharpAstResolver resolver) : base(resolver, CancellationToken.None)
{
this.doc = document;
this.location = location;
Services.AddService (typeof(NamingConventionService), new TestNameService ());
}
class TestNameService : NamingConventionService
{
public override IEnumerable<NamingRule> Rules {
get {
return DefaultRules.GetFdgRules ();
}
}
}
public override bool Supports(Version version)
@ -87,13 +97,6 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions @@ -87,13 +97,6 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
InsertBefore (entity, node);
}
}
public override T RequestData<T> ()
{
if (typeof(T).Equals (typeof(IEnumerable<NamingRule>)))
return (T)DefaultRules.GetFdgRules ();
return default (T);
}
#region Text stuff
public override string EolMarker { get { return Environment.NewLine; } }

Loading…
Cancel
Save