From 5315c0e2babe9286cd2dca84eb68e19d4e4174f3 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Thu, 2 Sep 2010 20:38:50 +0200 Subject: [PATCH] made IsAutomaticProperty static and internal, removed dependency on ITextEditor, works now with any IProperty in any class, even partial classes --- .../Src/PropertyRefactoringMenuBuilder.cs | 37 ++++++++++++++----- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/src/AddIns/Misc/SharpRefactoring/Project/Src/PropertyRefactoringMenuBuilder.cs b/src/AddIns/Misc/SharpRefactoring/Project/Src/PropertyRefactoringMenuBuilder.cs index 97f710ceb4..a903ee9725 100644 --- a/src/AddIns/Misc/SharpRefactoring/Project/Src/PropertyRefactoringMenuBuilder.cs +++ b/src/AddIns/Misc/SharpRefactoring/Project/Src/PropertyRefactoringMenuBuilder.cs @@ -10,7 +10,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Windows.Documents; - +using ICSharpCode.AvalonEdit.Document; using ICSharpCode.Core.Presentation; using ICSharpCode.Core.WinForms; using ICSharpCode.NRefactory; @@ -48,7 +48,7 @@ namespace SharpRefactoring ITextEditor editor = FindReferencesAndRenameHelper.OpenDefinitionFile(property, false); string field = null; PropertyDeclaration astProp = null; - if (IsAutomaticProperty(editor, property)) { + if (IsAutomaticProperty(property)) { cmd = new MenuCommand("${res:SharpDevelop.Refactoring.ExpandAutomaticProperty}", ExpandAutomaticProperty); cmd.Tag = property; items.Add(cmd); @@ -68,21 +68,38 @@ namespace SharpRefactoring return items.ToArray(); } - bool IsAutomaticProperty(ITextEditor editor, IProperty property) + internal static bool IsAutomaticProperty(IProperty property) { - if (editor == null) + string fileName = property.CompilationUnit.FileName; + + if (fileName == null) return false; + IDocument document; + ITextEditorProvider provider = FileService.GetOpenFile(fileName) as ITextEditorProvider; + + if (provider == null) { + if (!File.Exists(fileName)) + return false; + try { + document = DocumentUtilitites.LoadDocumentFromBuffer(new StringTextBuffer(File.ReadAllText(fileName))); + } catch (IOException) { + return false; + } + } else { + document = provider.TextEditor.Document; + } + bool isAutomatic = false; if (property.CanGet) { if (property.GetterRegion.IsEmpty) isAutomatic = true; else { - int getterStartOffset = editor.Document.PositionToOffset(property.GetterRegion.BeginLine, property.GetterRegion.BeginColumn); - int getterEndOffset = editor.Document.PositionToOffset(property.GetterRegion.EndLine, property.GetterRegion.EndColumn); + int getterStartOffset = document.PositionToOffset(property.GetterRegion.BeginLine, property.GetterRegion.BeginColumn); + int getterEndOffset = document.PositionToOffset(property.GetterRegion.EndLine, property.GetterRegion.EndColumn); - string text = editor.Document.GetText(getterStartOffset, getterEndOffset - getterStartOffset) + string text = document.GetText(getterStartOffset, getterEndOffset - getterStartOffset) .Replace(" ", "").Replace("\t", "").Replace("\n", "").Replace("\r", ""); isAutomatic = text == "get;"; @@ -93,10 +110,10 @@ namespace SharpRefactoring if (property.SetterRegion.IsEmpty) isAutomatic |= true; else { - int setterStartOffset = editor.Document.PositionToOffset(property.SetterRegion.BeginLine, property.SetterRegion.BeginColumn); - int setterEndOffset = editor.Document.PositionToOffset(property.SetterRegion.EndLine, property.SetterRegion.EndColumn); + int setterStartOffset = document.PositionToOffset(property.SetterRegion.BeginLine, property.SetterRegion.BeginColumn); + int setterEndOffset = document.PositionToOffset(property.SetterRegion.EndLine, property.SetterRegion.EndColumn); - string text = editor.Document.GetText(setterStartOffset, setterEndOffset - setterStartOffset) + string text = document.GetText(setterStartOffset, setterEndOffset - setterStartOffset) .Replace(" ", "").Replace("\t", "").Replace("\n", "").Replace("\r", ""); isAutomatic |= text == "set;";