~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to src/addins/MonoDevelop.Refactoring/MonoDevelop.Refactoring/RefactoryCommands.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
58
58
                FindReferences,
59
59
                FindDerivedClasses,
60
60
                DeclareLocal,
61
 
                Rename,
62
61
                RemoveUnusedImports,
63
62
                SortImports,
64
63
                RemoveSortImports,
69
68
                ImportSymbol
70
69
        }
71
70
        
72
 
        
73
 
        
74
 
        
75
71
        public class CurrentRefactoryOperationsHandler: CommandHandler
76
72
        {
77
73
                protected override void Run (object data)
112
108
                        resolveResult = GetResolveResult (doc, editor);
113
109
                        if (resolveResult is AggregatedResolveResult)
114
110
                                resolveResult = ((AggregatedResolveResult)resolveResult).PrimaryResult;
115
 
                        
116
111
                        item = null;
117
112
                        if (resolveResult is ParameterResolveResult) {
118
113
                                item = ((ParameterResolveResult)resolveResult).Parameter;
228
223
                                }
229
224
                        }
230
225
                        
231
 
                        
232
 
                        
233
 
                        if (doc.CompilationUnit != null && doc.CompilationUnit.Usings.Any (u => !u.IsFromNamespace && u.Region.Contains (line, column))) {
 
226
                        var unit = doc.CompilationUnit;
 
227
                        if (unit != null && unit.Usings != null && unit.Usings.Any (u => !u.IsFromNamespace && u.Region.Contains (line, column))) {
234
228
                                CommandInfoSet organizeUsingsMenu = new CommandInfoSet ();
235
229
                                organizeUsingsMenu.Text = GettextCatalog.GetString ("_Organize Usings");
236
230
                                organizeUsingsMenu.CommandInfos.Add (IdeApp.CommandService.GetCommandInfo (RefactoryCommands.RemoveUnusedImports), new RefactoryOperation (delegate {
251
245
                        foreach (var refactoring in RefactoringService.Refactorings) {
252
246
                                if (refactoring.IsValid (options)) {
253
247
                                        CommandInfo info = new CommandInfo (refactoring.GetMenuDescription (options));
254
 
                                        info.AccelKey = KeyBindingManager.BindingToDisplayLabel (refactoring.AccelKey, true);
 
248
                                        info.AccelKey = refactoring.AccelKey;
255
249
                                        ciset.CommandInfos.Add (info, new RefactoryOperation (new RefactoringOperationWrapper (refactoring, options).Operation));
256
250
                                }
257
251
                        }
352
346
//                                      ciset.CommandInfos.Add (GettextCatalog.GetString ("_Rename"), new RefactoryOperation (refactorer.Rename));
353
347
                                
354
348
                                if (canRename && cls.ClassType == ClassType.Interface && eclass != null) {
355
 
                                        // An interface is selected, so just need to provide these 2 submenu items
356
 
                                        ciset.CommandInfos.Add (GettextCatalog.GetString ("Implement Interface (implicit)"), new RefactoryOperation (refactorer.ImplementImplicitInterface));
357
 
                                        ciset.CommandInfos.Add (GettextCatalog.GetString ("Implement Interface (explicit)"), new RefactoryOperation (refactorer.ImplementExplicitInterface));
 
349
                                        // is now provided by the refactoring command infrastructure:
 
350
//                                      ciset.CommandInfos.Add (GettextCatalog.GetString ("Implement Interface (explicit)"), new RefactoryOperation (refactorer.ImplementExplicitInterface));
 
351
//                                      ciset.CommandInfos.Add (GettextCatalog.GetString ("Implement Interface (implicit)"), new RefactoryOperation (refactorer.ImplementImplicitInterface));
358
352
                                } else if (canRename && includeModifyCommands && cls.BaseType != null && cls.ClassType != ClassType.Interface && cls == eclass) {
359
353
                                        // Class might have interfaces... offer to implement them
360
354
                                        CommandInfoSet impset = new CommandInfoSet ();
517
511
                        
518
512
                        public void ResolveName ()
519
513
                        {
520
 
                                // TODO: Move this to a expression refactorer !!!!
521
 
                                int pos = doc.TextEditor.GetPositionFromLineColumn (resolveResult.ResolvedExpression.Region.Start.Line, resolveResult.ResolvedExpression.Region.Start.Column);
522
 
                                doc.TextEditor.InsertText (pos, ns +"." );
 
514
                                int pos = doc.Editor.Document.LocationToOffset (resolveResult.ResolvedExpression.Region.Start.Line, resolveResult.ResolvedExpression.Region.Start.Column);
 
515
                                if (pos < 0) {
 
516
                                        LoggingService.LogError ("Invalie expression position: " + resolveResult.ResolvedExpression);
 
517
                                        return;
 
518
                                }
 
519
                                doc.Editor.Insert (pos, ns + ".");
 
520
                                if (doc.Editor.Caret.Offset >= pos)
 
521
                                        doc.Editor.Caret.Offset += (ns + ".").Length;
 
522
                                doc.Editor.Document.CommitLineUpdate (resolveResult.ResolvedExpression.Region.Start.Line);
523
523
                        }
524
524
                }
525
525
 
652
652
                        foreach (var refactoring in RefactoringService.Refactorings) {
653
653
                                if (refactoring.IsValid (options)) {
654
654
                                        CommandInfo info = new CommandInfo (refactoring.GetMenuDescription (options));
655
 
                                        info.AccelKey = KeyBindingManager.BindingToDisplayLabel (refactoring.AccelKey, true);
 
655
                                        info.AccelKey = refactoring.AccelKey;
656
656
                                        ciset.CommandInfos.Add (info, new RefactoryOperation (new RefactoringOperationWrapper (refactoring, options).Operation));
657
657
                                }
658
658
                        }
836
836
                                                System.IO.TextReader textReader = provider.Open ();
837
837
                                                doc.Text = textReader.ReadToEnd ();
838
838
                                                textReader.Close ();
839
 
                                                int position = doc.LocationToOffset (part.Location.Line - 1, part.Location.Column - 1);
 
839
                                                int position = doc.LocationToOffset (part.Location.Line, part.Location.Column);
840
840
                                                while (position + part.Name.Length < doc.Length) {
841
841
                                                        if (doc.GetTextAt (position, part.Name.Length) == part.Name)
842
842
                                                                break;
855
855
                public void FindReferences ()
856
856
                {
857
857
                        monitor = IdeApp.Workbench.ProgressMonitors.GetSearchProgressMonitor (true, true);
858
 
                        Thread t = new Thread (new ThreadStart (FindReferencesThread));
859
 
                        t.Name = "Find references";
860
 
                        t.IsBackground = true;
861
 
                        t.Start ();
 
858
                        ThreadPool.QueueUserWorkItem (FindReferencesThread);
862
859
                }
863
860
                
864
 
                void FindReferencesThread ()
 
861
                void FindReferencesThread (object state)
865
862
                {
866
863
                        try {
867
 
                                CodeRefactorer refactorer = IdeApp.Workspace.GetCodeRefactorer (IdeApp.ProjectOperations.CurrentSelectedSolution);
868
 
                                if (item is IType) {
869
 
                                        references = refactorer.FindClassReferences (monitor, (IType)item, RefactoryScope.Solution, true);
870
 
                                } else if (item is LocalVariable) {
871
 
                                        references = refactorer.FindVariableReferences (monitor, (LocalVariable)item);
872
 
                                } else if (item is IParameter) {
873
 
                                        references = refactorer.FindParameterReferences (monitor, (IParameter)item, true);
874
 
                                } else if (item is IMember) {
875
 
                                        IMember member = (IMember)item;
876
 
/*                                      references = new MemberReferenceCollection ();
877
 
                                        foreach (IMember m in MonoDevelop.Refactoring.Rename.RenameRefactoring.CollectMembers (member.DeclaringType.SourceProjectDom, member)) {
878
 
                                                foreach (MemberReference r in refactorer.FindMemberReferences (monitor, m.DeclaringType, m, true)) {
879
 
                                                        references.Add (r);
880
 
                                                }
881
 
                                        }*/
882
 
                                        
883
 
                                        references = refactorer.FindMemberReferences (monitor, member.DeclaringType, member, true);
884
 
                                }
885
 
                                if (references != null) {
886
 
                                        foreach (MemberReference mref in references) {
887
 
                                                monitor.ReportResult (new MonoDevelop.Ide.FindInFiles.SearchResult (new FileProvider (mref.FileName), mref.Position, mref.Name.Length));
888
 
                                        }
 
864
                                foreach (MemberReference mref in ReferenceFinder.FindReferences (IdeApp.ProjectOperations.CurrentSelectedSolution, item, monitor)) {
 
865
                                        monitor.ReportResult (new MonoDevelop.Ide.FindInFiles.SearchResult (new FileProvider (mref.FileName), mref.Position, mref.Name.Length));
889
866
                                }
890
867
                        } catch (Exception ex) {
891
868
                                if (monitor != null)
905
882
                                foreach (IReturnType bc in cls.BaseTypes) {
906
883
                                        IType bcls = ctx.GetType (bc);
907
884
                                        if (bcls != null && bcls.ClassType != ClassType.Interface && !bcls.Location.IsEmpty) {
908
 
                                                IdeApp.Workbench.OpenDocument (bcls.CompilationUnit.FileName, bcls.Location.Line, bcls.Location.Column, true);
 
885
                                                IdeApp.Workbench.OpenDocument (bcls.CompilationUnit.FileName, bcls.Location.Line, bcls.Location.Column);
909
886
                                                return;
910
887
                                        }
911
888
                                }
924
901
                                                        }
925
902
                                                }
926
903
                                                if (baseMethod != null)
927
 
                                                        IdeApp.Workbench.OpenDocument (bcls.CompilationUnit.FileName, baseMethod.Location.Line, baseMethod.Location.Column, true);
 
904
                                                        IdeApp.Workbench.OpenDocument (bcls.CompilationUnit.FileName, baseMethod.Location.Line, baseMethod.Location.Column);
928
905
                                                return;
929
906
                                        }
930
907
                                }
934
911
                
935
912
                public void FindDerivedClasses ()
936
913
                {
937
 
                        monitor = IdeApp.Workbench.ProgressMonitors.GetSearchProgressMonitor (true, true);
938
 
                        Thread t = new Thread (new ThreadStart (FindDerivedThread));
939
 
                        t.Name = "Find subclasses";
940
 
                        t.IsBackground = true;
941
 
                        t.Start ();
 
914
                        ThreadPool.QueueUserWorkItem (FindDerivedThread);
942
915
                }
943
916
                
944
 
                void FindDerivedThread ()
 
917
                void FindDerivedThread (object state)
945
918
                {
 
919
                        monitor = IdeApp.Workbench.ProgressMonitors.GetSearchProgressMonitor (true, true);
946
920
                        using (monitor) {
947
921
                                IType cls = (IType) item;
948
922
                                if (cls == null) return;
962
936
                
963
937
                void ImplementInterface (bool explicitly)
964
938
                {
965
 
                        CodeRefactorer refactorer = IdeApp.Workspace.GetCodeRefactorer (IdeApp.ProjectOperations.CurrentSelectedSolution);
966
 
                        IType iface = item as IType;
967
 
                        
968
 
                        if (klass == null)
969
 
                                return;
970
 
                        
971
 
                        if (iface == null)
972
 
                                return;
973
 
                                
974
 
                        IEditableTextBuffer editor = IdeApp.Workbench.ActiveDocument.GetContent <IEditableTextBuffer>();
975
 
                        if (editor != null)
976
 
                                editor.BeginAtomicUndo ();
977
 
                                
978
 
                        try {
979
 
                                refactorer.ImplementInterface (pinfo, klass, iface, explicitly, iface, this.hintReturnType);
980
 
                        } finally {
981
 
                                if (editor != null)
982
 
                                        editor.EndAtomicUndo ();
983
 
                        }
 
939
                        var doc = IdeApp.Workbench.ActiveDocument;
 
940
                        var editor = doc.Editor.Parent;
 
941
                        IType interfaceType = item as IType;
 
942
                        IType declaringType = klass;
 
943
                        
 
944
                        var mode = new Mono.TextEditor.InsertionCursorEditMode (editor, CodeGenerationService.GetInsertionPoints (doc, declaringType));
 
945
                        var helpWindow = new Mono.TextEditor.PopupWindow.ModeHelpWindow ();
 
946
                        helpWindow.TransientFor = IdeApp.Workbench.RootWindow;
 
947
                        helpWindow.TitleText = GettextCatalog.GetString ("<b>Implement Interface -- Targeting</b>");
 
948
                        helpWindow.Items.Add (new KeyValuePair<string, string> (GettextCatalog.GetString ("<b>Key</b>"), GettextCatalog.GetString ("<b>Behavior</b>")));
 
949
                        helpWindow.Items.Add (new KeyValuePair<string, string> (GettextCatalog.GetString ("<b>Up</b>"), GettextCatalog.GetString ("Move to <b>previous</b> target point.")));
 
950
                        helpWindow.Items.Add (new KeyValuePair<string, string> (GettextCatalog.GetString ("<b>Down</b>"), GettextCatalog.GetString ("Move to <b>next</b> target point.")));
 
951
                        helpWindow.Items.Add (new KeyValuePair<string, string> (GettextCatalog.GetString ("<b>Enter</b>"), GettextCatalog.GetString ("<b>Declare interface implementation</b> at target point.")));
 
952
                        helpWindow.Items.Add (new KeyValuePair<string, string> (GettextCatalog.GetString ("<b>Esc</b>"), GettextCatalog.GetString ("<b>Cancel</b> this refactoring.")));
 
953
                        mode.HelpWindow = helpWindow;
 
954
                        mode.CurIndex = mode.InsertionPoints.Count - 1;
 
955
                        mode.StartMode ();
 
956
                        mode.Exited += delegate(object s, Mono.TextEditor.InsertionCursorEventArgs args) {
 
957
                                if (args.Success) {
 
958
                                        var generator = doc.CreateCodeGenerator ();
 
959
                                        args.InsertionPoint.Insert (doc.Editor, generator.CreateInterfaceImplementation (declaringType, interfaceType, explicitly));
 
960
                                }
 
961
                        };
984
962
                }
985
963
                
986
964
                public void ImplementImplicitInterface ()
995
973
                
996
974
                public void ImplementAbstractMembers ()
997
975
                {
998
 
                        CodeRefactorer refactorer = IdeApp.Workspace.GetCodeRefactorer (IdeApp.ProjectOperations.CurrentSelectedSolution);
999
 
                        IType aclass = item as IType;
1000
 
                        
1001
 
                        if (klass == null)
1002
 
                                return;
1003
 
                        
1004
 
                        if (aclass == null)
1005
 
                                return;
1006
 
                                
1007
 
                        IEditableTextBuffer editor = IdeApp.Workbench.ActiveDocument.GetContent <IEditableTextBuffer>();
1008
 
                        if (editor != null)
1009
 
                                editor.BeginAtomicUndo ();
1010
 
                                
1011
 
                        try {
1012
 
                                List<KeyValuePair<IMember,IReturnType>> members = new List<KeyValuePair<IMember, IReturnType>> ();
1013
 
                                foreach (IMember member in aclass.Members) {
1014
 
                                        if (member.IsAbstract && !klass.Members.Any (m => member.Name == m.Name)) 
1015
 
                                                members.Add (new KeyValuePair<IMember,IReturnType> (member, null));
1016
 
                                }
1017
 
                                refactorer.ImplementMembers (klass, members, "implemented abstract members of " + aclass.FullName);
1018
 
                        } finally {
1019
 
                                if (editor != null)
1020
 
                                        editor.EndAtomicUndo ();
1021
 
                        }
 
976
                        var doc = IdeApp.Workbench.ActiveDocument;
 
977
                        IType interfaceType = item as IType;
 
978
                        MonoDevelop.Refactoring.ImplementInterface.ImplementAbstractMembers.Implement (doc, interfaceType);
1022
979
                }
1023
980
                
1024
981
                public void EncapsulateField ()
1025
982
                {
1026
983
                        EncapsulateFieldDialog dialog;
1027
984
                        if (item is IField) {
1028
 
                                dialog = new EncapsulateFieldDialog (IdeApp.Workbench.ActiveDocument.TextEditorData.Parent, ctx, (IField) item);
 
985
                                dialog = new EncapsulateFieldDialog (IdeApp.Workbench.ActiveDocument, ctx, (IField) item);
1029
986
                        } else {
1030
 
                                dialog = new EncapsulateFieldDialog (IdeApp.Workbench.ActiveDocument.TextEditorData.Parent, ctx, (IType) item);
 
987
                                dialog = new EncapsulateFieldDialog (IdeApp.Workbench.ActiveDocument, ctx, (IType) item);
1031
988
                        }
1032
989
                        MessageService.ShowCustomDialog (dialog);
1033
990
                }
1034
991
                
1035
992
                public void OverrideOrImplementMembers ()
1036
993
                {
1037
 
                        MessageService.ShowCustomDialog (new OverridesImplementsDialog (IdeApp.Workbench.ActiveDocument.TextEditorData.Parent, (IType)item));
 
994
                        MessageService.ShowCustomDialog (new OverridesImplementsDialog (IdeApp.Workbench.ActiveDocument, (IType)item));
1038
995
                }
1039
996
                
1040
997
                public void Rename ()