Browse Source

Merge branch 'master' of github.com:icsharpcode/NRefactory

newNRvisualizers
Mike Krüger 14 years ago
parent
commit
ee71c30b64
  1. 1
      ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj
  2. 27
      ICSharpCode.NRefactory.CSharp/Refactoring/BaseRefactoringContext.cs
  3. 21
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateFieldAction.cs
  4. 18
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateMethodDeclarationAction.cs
  5. 5
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreatePropertyAction.cs
  6. 6
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/InconsistentNamingIssue.cs
  7. 58
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/NamingConventionService.cs
  8. 32
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateFieldTests.cs
  9. 18
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreatePropertyTests.cs
  10. 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
}
}

21
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateFieldAction.cs

@ -51,18 +51,23 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -51,18 +51,23 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
if (!(context.Resolve(identifier).IsError)) {
yield break;
}
var guessedType = CreateFieldAction.GuessAstType(context, identifier);
if (guessedType == null) {
yield break;
}
var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));
if (service != null && !service.IsValidName(identifier.Identifier, AffectedEntity.Field))
yield break;
yield return new CodeAction(context.TranslateString("Create field"), script => {
var decl = new FieldDeclaration() {
ReturnType = guessedType,
Variables = { new VariableInitializer(identifier.Identifier) }
};
ReturnType = guessedType,
Variables = { new VariableInitializer(identifier.Identifier) }
};
script.InsertWithCursor(context.TranslateString("Create field"), decl, Script.InsertPosition.Before);
});
}
#region Type guessing
@ -159,11 +164,13 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -159,11 +164,13 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
return Enumerable.Empty<IType>();
}
static readonly IType[] emptyTypes = new IType[0];
internal static AstType GuessAstType(RefactoringContext context, Expression expr)
{
var type = GetValidTypes(context, expr).ToArray();
var inferedType = new TypeInference(context.Compilation).FindTypeInBounds(type, type);
var typeInference = new TypeInference(context.Compilation);
typeInference.Algorithm = TypeInferenceAlgorithm.ImprovedReturnAllResults;
var inferedType = typeInference.FindTypeInBounds(type, emptyTypes);
if (inferedType.Kind == TypeKind.Unknown) {
return new PrimitiveType("object");
}
@ -173,7 +180,9 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -173,7 +180,9 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
internal static IType GuessType(RefactoringContext context, Expression expr)
{
var type = GetValidTypes(context, expr).ToArray();
var inferedType = new TypeInference(context.Compilation).FindTypeInBounds(type, type);
var typeInference = new TypeInference(context.Compilation);
typeInference.Algorithm = TypeInferenceAlgorithm.ImprovedReturnAllResults;
var inferedType = typeInference.FindTypeInBounds(type, emptyTypes);
return inferedType;
}

18
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateMethodDeclarationAction.cs

@ -60,6 +60,12 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -60,6 +60,12 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
yield break;
}
var invocationMethod = guessedType.GetDelegateInvokeMethod();
bool isStatic = state.CurrentMember.IsStatic;
var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));
if (service != null && !service.IsValidName(methodName, AffectedEntity.Method, Modifiers.Private, isStatic)) {
yield break;
}
yield return new CodeAction(context.TranslateString("Create delegate handler"), script => {
var decl = new MethodDeclaration() {
@ -69,7 +75,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -69,7 +75,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
new ThrowStatement(new ObjectCreateExpression(context.CreateShortType("System", "NotImplementedException")))
}
};
if (state.CurrentMember.IsStatic) {
if (isStatic) {
decl.Modifiers |= Modifiers.Static;
}
@ -109,6 +115,14 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -109,6 +115,14 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
}
var state = context.GetResolverStateBefore(invocation);
var guessedType = invocation.Parent is ExpressionStatement ? new PrimitiveType("void") : CreateFieldAction.GuessAstType(context, invocation);
var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));
bool isStatic = invocation.Target is IdentifierExpression && state.CurrentMember.IsStatic;
if (service != null && !service.IsValidName(methodName, AffectedEntity.Method, Modifiers.Private, isStatic)) {
yield break;
}
yield return new CodeAction(context.TranslateString("Create method"), script => {
var decl = new MethodDeclaration() {
ReturnType = guessedType,
@ -117,7 +131,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -117,7 +131,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
new ThrowStatement(new ObjectCreateExpression(context.CreateShortType("System", "NotImplementedException")))
}
};
if (invocation.Target is IdentifierExpression && state.CurrentMember.IsStatic) {
if (isStatic) {
decl.Modifiers |= Modifiers.Static;
}

5
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreatePropertyAction.cs

@ -52,6 +52,11 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -52,6 +52,11 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
if (guessedType == null) {
yield break;
}
var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));
if (service != null && !service.IsValidName(identifier.Identifier, AffectedEntity.Property)) {
yield break;
}
yield return new CodeAction(context.TranslateString("Create property"), script => {
var decl = new PropertyDeclaration() {

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;
}

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

@ -0,0 +1,58 @@ @@ -0,0 +1,58 @@
//
// 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;
using System.Linq;
namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
public abstract class NamingConventionService
{
public abstract IEnumerable<NamingRule> Rules {
get;
}
public bool IsValidName(string name, AffectedEntity entity, Modifiers accessibilty = Modifiers.Private, bool isStatic = false)
{
foreach (var rule in Rules) {
if (!rule.AffectedEntity.HasFlag(entity)) {
continue;
}
if (!rule.VisibilityMask.HasFlag(accessibilty)) {
continue;
}
if (isStatic && !rule.IncludeStaticEntities || !isStatic && !rule.IncludeInstanceMembers) {
continue;
}
if (!rule.IsValid(name)) {
return false;
}
}
return true;
}
}
}

32
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateFieldTests.cs

@ -32,6 +32,38 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions @@ -32,6 +32,38 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
[TestFixture]
public class CreateFieldTests : ContextActionTestBase
{
[Test()]
public void TestWrongContext1 ()
{
TestWrongContext (
new CreateFieldAction (),
"using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" Console.WriteLine ($Foo);" + Environment.NewLine +
" }" + Environment.NewLine +
"}"
);
}
[Test()]
public void TestWrongContext2 ()
{
TestWrongContext (
new CreateFieldAction (),
"using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" Console.WriteLine ($Foo());" + Environment.NewLine +
" }" + Environment.NewLine +
"}"
);
}
[Test()]
public void TestSimpleMethodCall ()
{

18
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreatePropertyTests.cs

@ -42,7 +42,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions @@ -42,7 +42,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
"{" + Environment.NewLine +
" void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" Console.WriteLine ($foo);" + Environment.NewLine +
" Console.WriteLine ($Foo);" + Environment.NewLine +
" }" + Environment.NewLine +
"}"
);
@ -50,13 +50,13 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions @@ -50,13 +50,13 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
"using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" object foo {" + Environment.NewLine +
" object Foo {" + Environment.NewLine +
" get;" + Environment.NewLine +
" set;" + Environment.NewLine +
" }" + Environment.NewLine +
" void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" Console.WriteLine (foo);" + Environment.NewLine +
" Console.WriteLine (Foo);" + Environment.NewLine +
" }" + Environment.NewLine +
"}", result);
}
@ -71,7 +71,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions @@ -71,7 +71,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
"{" + Environment.NewLine +
" void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" $foo = 0x10;" + Environment.NewLine +
" $Foo = 0x10;" + Environment.NewLine +
" }" + Environment.NewLine +
"}"
);
@ -80,13 +80,13 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions @@ -80,13 +80,13 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
"using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" int foo {" + Environment.NewLine +
" int Foo {" + Environment.NewLine +
" get;" + Environment.NewLine +
" set;" + Environment.NewLine +
" }" + Environment.NewLine +
" void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" foo = 0x10;" + Environment.NewLine +
" Foo = 0x10;" + Environment.NewLine +
" }" + Environment.NewLine +
"}", result);
}
@ -102,7 +102,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions @@ -102,7 +102,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
" void FooBar(out string par) {}" + Environment.NewLine +
" void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" FooBar(out $foo);" + Environment.NewLine +
" FooBar(out $Foo);" + Environment.NewLine +
" }" + Environment.NewLine +
"}"
);
@ -112,13 +112,13 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions @@ -112,13 +112,13 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" void FooBar(out string par) {}" + Environment.NewLine +
" string foo {" + Environment.NewLine +
" string Foo {" + Environment.NewLine +
" get;" + Environment.NewLine +
" set;" + Environment.NewLine +
" }" + Environment.NewLine +
" void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" FooBar(out foo);" + Environment.NewLine +
" FooBar(out Foo);" + Environment.NewLine +
" }" + Environment.NewLine +
"}", result);
}

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