Skip to content
This repository has been archived by the owner on Oct 16, 2020. It is now read-only.

Commit

Permalink
Fixed create static field/property.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrueger committed Mar 26, 2012
1 parent a234f8b commit 402e112
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 7 deletions.
Expand Up @@ -56,15 +56,22 @@ public IEnumerable<CodeAction> GetActions(RefactoringContext context)
if (guessedType == null) {
yield break;
}
var state = context.GetResolverStateBefore(identifier);
bool isStatic = state.CurrentMember.IsStatic;

var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));
if (service != null && !service.IsValidName(identifier.Identifier, AffectedEntity.Field))
if (service != null && !service.IsValidName(identifier.Identifier, AffectedEntity.Field, Modifiers.Private, isStatic)) {
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) }
};
if (isStatic) {
decl.Modifiers |= Modifiers.Static;
}
script.InsertWithCursor(context.TranslateString("Create field"), decl, Script.InsertPosition.Before);
});

Expand Down
Expand Up @@ -54,12 +54,12 @@ public IEnumerable<CodeAction> GetActionsFromIdentifier(RefactoringContext cont
yield break;
}
var methodName = identifier.Identifier;
var state = context.GetResolverStateBefore(identifier);
var guessedType = CreateFieldAction.GuessType(context, identifier);
if (guessedType.Kind != TypeKind.Delegate) {
yield break;
}
var invocationMethod = guessedType.GetDelegateInvokeMethod();
var state = context.GetResolverStateBefore(identifier);
bool isStatic = state.CurrentMember.IsStatic;

var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));
Expand Down
Expand Up @@ -52,9 +52,11 @@ public IEnumerable<CodeAction> GetActions(RefactoringContext context)
if (guessedType == null) {
yield break;
}

var state = context.GetResolverStateBefore(identifier);
bool isStatic = state.CurrentMember.IsStatic;

var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));
if (service != null && !service.IsValidName(identifier.Identifier, AffectedEntity.Property)) {
if (service != null && !service.IsValidName(identifier.Identifier, AffectedEntity.Property, Modifiers.Private, isStatic)) {
yield break;
}

Expand All @@ -65,6 +67,9 @@ public IEnumerable<CodeAction> GetActions(RefactoringContext context)
Getter = new Accessor(),
Setter = new Accessor()
};
if (isStatic) {
decl.Modifiers |= Modifiers.Static;
}
script.InsertWithCursor(context.TranslateString("Create property"), decl, Script.InsertPosition.Before);
});
}
Expand Down
Expand Up @@ -149,5 +149,33 @@ public void TestOutParamCall ()
" }" + Environment.NewLine +
"}", result);
}

[Test()]
public void TestStaticField ()
{
string result = RunContextAction (
new CreateFieldAction (),
"using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" static void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" $foo = 0x10;" + Environment.NewLine +
" }" + Environment.NewLine +
"}"
);

Assert.AreEqual (
"using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" static int foo;" + Environment.NewLine +
"" + Environment.NewLine +
" static void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" foo = 0x10;" + Environment.NewLine +
" }" + Environment.NewLine +
"}", result);
}
}
}
Expand Up @@ -122,5 +122,36 @@ public void TestOutParamCall ()
" }" + Environment.NewLine +
"}", result);
}

[Test()]
public void TestStaticProperty ()
{
string result = RunContextAction (
new CreatePropertyAction (),
"using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" static void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" $Foo = 0x10;" + Environment.NewLine +
" }" + Environment.NewLine +
"}"
);

Assert.AreEqual (
"using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" static int Foo {" + Environment.NewLine +
" get;" + Environment.NewLine +
" set;" + Environment.NewLine +
" }" + Environment.NewLine +
" static void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" Foo = 0x10;" + Environment.NewLine +
" }" + Environment.NewLine +
"}", result);
}

}
}

0 comments on commit 402e112

Please sign in to comment.