Browse Source

make IsAutomaticProperty an extension method. Now named IsAutoImplemented.

pull/18/head
Siegfried Pammer 14 years ago
parent
commit
32f9a25ed3
  1. 6
      src/AddIns/Misc/SharpRefactoring/Project/Src/Gui/OverrideEqualsGetHashCodeMethodsDialog.xaml.cs
  2. 2
      src/AddIns/Misc/SharpRefactoring/Project/Src/InsertCtorSnippetRefactoring.cs
  3. 2
      src/AddIns/Misc/SharpRefactoring/Project/Src/OverrideToStringMethodRefactoring.cs
  4. 48
      src/AddIns/Misc/SharpRefactoring/Project/Src/PropertyRefactoringMenuBuilder.cs
  5. 51
      src/AddIns/Misc/SharpRefactoring/Project/Src/RefactoringHelpers.cs

6
src/AddIns/Misc/SharpRefactoring/Project/Src/Gui/OverrideEqualsGetHashCodeMethodsDialog.xaml.cs

@ -336,7 +336,7 @@ namespace SharpRefactoring.Gui @@ -336,7 +336,7 @@ namespace SharpRefactoring.Gui
}
foreach (IProperty property in currentClass.Properties) {
if (property.IsStatic || !PropertyRefactoringMenuBuilder.IsAutomaticProperty(property)) continue;
if (property.IsStatic || !property.IsAutoImplemented()) continue;
if (expr == null) {
expr = TestEquality("other", property);
} else {
@ -366,7 +366,7 @@ namespace SharpRefactoring.Gui @@ -366,7 +366,7 @@ namespace SharpRefactoring.Gui
getHashCodeMethod.Body.AddChild(new LocalVariableDeclaration(hashCodeVar));
if (currentClass.Fields.Any(f => !f.IsStatic) || currentClass.Properties.Any(p => !p.IsStatic && PropertyRefactoringMenuBuilder.IsAutomaticProperty(p))) {
if (currentClass.Fields.Any(f => !f.IsStatic) || currentClass.Properties.Any(p => !p.IsStatic && p.IsAutoImplemented())) {
bool usePrimeMultiplication = currentClass.ProjectContent.Language == LanguageProperties.CSharp;
BlockStatement hashCalculationBlock;
@ -386,7 +386,7 @@ namespace SharpRefactoring.Gui @@ -386,7 +386,7 @@ namespace SharpRefactoring.Gui
}
foreach (IProperty property in currentClass.Properties) {
if (property.IsStatic || !PropertyRefactoringMenuBuilder.IsAutomaticProperty(property)) continue;
if (property.IsStatic || !property.IsAutoImplemented()) continue;
AddToBlock(hashCodeVar, getHashCodeMethod, usePrimeMultiplication, hashCalculationBlock, ref fieldIndex, property);
}

2
src/AddIns/Misc/SharpRefactoring/Project/Src/InsertCtorSnippetRefactoring.cs

@ -83,7 +83,7 @@ namespace SharpRefactoring @@ -83,7 +83,7 @@ namespace SharpRefactoring
}
foreach (var p in sourceClass.Properties.Where(prop => prop.CanSet && !prop.IsIndexer
&& PropertyRefactoringMenuBuilder.IsAutomaticProperty(prop)
&& prop.IsAutoImplemented()
&& prop.IsStatic == sourceClass.IsStatic
&& prop.ReturnType != null)) {
yield return new PropertyOrFieldWrapper(p) { Index = i };

2
src/AddIns/Misc/SharpRefactoring/Project/Src/OverrideToStringMethodRefactoring.cs

@ -99,7 +99,7 @@ namespace SharpRefactoring @@ -99,7 +99,7 @@ namespace SharpRefactoring
}
foreach (var p in sourceClass.Properties.Where(prop => prop.CanGet && !prop.IsIndexer
&& PropertyRefactoringMenuBuilder.IsAutomaticProperty(prop)
&& prop.IsAutoImplemented()
&& prop.IsStatic == sourceClass.IsStatic
&& prop.ReturnType != null)) {
yield return new PropertyOrFieldWrapper(p) { Index = i };

48
src/AddIns/Misc/SharpRefactoring/Project/Src/PropertyRefactoringMenuBuilder.cs

@ -48,7 +48,7 @@ namespace SharpRefactoring @@ -48,7 +48,7 @@ namespace SharpRefactoring
ITextEditor editor = FindReferencesAndRenameHelper.OpenDefinitionFile(property, false);
string field = null;
PropertyDeclaration astProp = null;
if (IsAutomaticProperty(property)) {
if (property.IsAutoImplemented()) {
cmd = new MenuCommand("${res:SharpDevelop.Refactoring.ExpandAutomaticProperty}", ExpandAutomaticProperty);
cmd.Tag = property;
items.Add(cmd);
@ -68,51 +68,6 @@ namespace SharpRefactoring @@ -68,51 +68,6 @@ namespace SharpRefactoring
return items.ToArray();
}
#region ExpandAutomaticProperty
internal static bool IsAutomaticProperty(IProperty property)
{
if (property.IsAbstract || property.DeclaringType.ClassType == ICSharpCode.SharpDevelop.Dom.ClassType.Interface)
return false;
string fileName = property.CompilationUnit.FileName;
if (fileName == null)
return false;
IDocument document = DocumentUtilitites.LoadDocumentFromBuffer(ParserService.GetParseableFileContent(fileName));
bool isAutomatic = false;
if (property.CanGet) {
if (property.GetterRegion.IsEmpty)
isAutomatic = true;
else {
int getterStartOffset = document.PositionToOffset(property.GetterRegion.BeginLine, property.GetterRegion.BeginColumn);
int getterEndOffset = document.PositionToOffset(property.GetterRegion.EndLine, property.GetterRegion.EndColumn);
string text = document.GetText(getterStartOffset, getterEndOffset - getterStartOffset)
.Replace(" ", "").Replace("\t", "").Replace("\n", "").Replace("\r", "");
isAutomatic = text == "get;";
}
}
if (property.CanSet) {
if (property.SetterRegion.IsEmpty)
isAutomatic |= true;
else {
int setterStartOffset = document.PositionToOffset(property.SetterRegion.BeginLine, property.SetterRegion.BeginColumn);
int setterEndOffset = document.PositionToOffset(property.SetterRegion.EndLine, property.SetterRegion.EndColumn);
string text = document.GetText(setterStartOffset, setterEndOffset - setterStartOffset)
.Replace(" ", "").Replace("\t", "").Replace("\n", "").Replace("\r", "");
isAutomatic |= text == "set;";
}
}
return isAutomatic;
}
void ExpandAutomaticProperty(object sender, EventArgs e)
{
MenuCommand item = (MenuCommand)sender;
@ -161,7 +116,6 @@ namespace SharpRefactoring @@ -161,7 +116,6 @@ namespace SharpRefactoring
ParserService.ParseCurrentViewContent();
}
}
#endregion
#region ConvertToAutomaticProperty
bool IsSimpleProperty(ITextEditor editor, IProperty property, out Ast.PropertyDeclaration astProperty, out string fieldName)

51
src/AddIns/Misc/SharpRefactoring/Project/Src/RefactoringHelpers.cs

@ -6,6 +6,7 @@ using System.IO; @@ -6,6 +6,7 @@ using System.IO;
using ICSharpCode.Core;
using ICSharpCode.SharpDevelop;
using ICSharpCode.SharpDevelop.Dom;
using ICSharpCode.SharpDevelop.Editor;
using ICSharpCode.SharpDevelop.Project;
namespace SharpRefactoring
@ -52,5 +53,55 @@ namespace SharpRefactoring @@ -52,5 +53,55 @@ namespace SharpRefactoring
return c;
}
/// <summary>
/// Determines if the property is a so-called auto-implemented property.
/// </summary>
public static bool IsAutoImplemented(this IProperty property)
{
if (property == null)
throw new ArgumentNullException("property");
if (property.IsAbstract || property.DeclaringType.ClassType == ICSharpCode.SharpDevelop.Dom.ClassType.Interface)
return false;
string fileName = property.CompilationUnit.FileName;
if (fileName == null)
return false;
IDocument document = DocumentUtilitites.LoadDocumentFromBuffer(ParserService.GetParseableFileContent(fileName));
bool isAutomatic = false;
if (property.CanGet) {
if (property.GetterRegion.IsEmpty)
isAutomatic = true;
else {
int getterStartOffset = document.PositionToOffset(property.GetterRegion.BeginLine, property.GetterRegion.BeginColumn);
int getterEndOffset = document.PositionToOffset(property.GetterRegion.EndLine, property.GetterRegion.EndColumn);
string text = document.GetText(getterStartOffset, getterEndOffset - getterStartOffset)
.Replace(" ", "").Replace("\t", "").Replace("\n", "").Replace("\r", "");
isAutomatic = text == "get;";
}
}
if (property.CanSet) {
if (property.SetterRegion.IsEmpty)
isAutomatic |= true;
else {
int setterStartOffset = document.PositionToOffset(property.SetterRegion.BeginLine, property.SetterRegion.BeginColumn);
int setterEndOffset = document.PositionToOffset(property.SetterRegion.EndLine, property.SetterRegion.EndColumn);
string text = document.GetText(setterStartOffset, setterEndOffset - setterStartOffset)
.Replace(" ", "").Replace("\t", "").Replace("\n", "").Replace("\r", "");
isAutomatic |= text == "set;";
}
}
return isAutomatic;
}
}
}

Loading…
Cancel
Save