~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Ide/MonoDevelop.Ide/ProjectOperations.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
48
48
using MonoDevelop.Core.Instrumentation;
49
49
using Mono.TextEditor;
50
50
using System.Diagnostics;
51
 
using ICSharpCode.NRefactory.TypeSystem;
52
51
using ICSharpCode.NRefactory.Documentation;
 
52
using ICSharpCode.NRefactory.TypeSystem.Implementation;
 
53
using System.Text;
53
54
 
54
55
namespace MonoDevelop.Ide
55
56
{
156
157
                
157
158
                public IAsyncOperation CurrentRunOperation {
158
159
                        get { return currentRunOperation; }
159
 
                        set { currentRunOperation = value ?? NullAsyncOperation.Success; }
 
160
                        set {
 
161
                                currentRunOperation = value ?? NullAsyncOperation.Success;
 
162
                                OnCurrentRunOperationChanged (EventArgs.Empty);
 
163
                        }
160
164
                }
161
 
                
 
165
 
162
166
                public bool IsBuilding (IBuildTarget target)
163
167
                {
164
168
                        return !currentBuildOperation.IsCompleted && ContainsTarget (target, currentBuildOperationOwner);
213
217
                        return (GetDeclaredFile(item) != null);
214
218
                }*/
215
219
                
216
 
                public bool CanJumpToDeclaration (INamedElement element)
 
220
                public bool CanJumpToDeclaration (object element)
217
221
                {
218
 
                        var entity = element as IEntity;
219
 
                        if (entity == null && element is IType)
220
 
                                entity = ((IType)element).GetDefinition ();
 
222
                        if (element is ICSharpCode.NRefactory.TypeSystem.IVariable)
 
223
                                return true;
 
224
                        var entity = element as ICSharpCode.NRefactory.TypeSystem.IEntity;
 
225
                        if (entity == null && element is ICSharpCode.NRefactory.TypeSystem.IType)
 
226
                                entity = ((ICSharpCode.NRefactory.TypeSystem.IType)element).GetDefinition ();
221
227
                        if (entity == null)
222
228
                                return false;
223
 
                        
224
229
                        if (entity.Region.IsEmpty) {
225
 
                                return !string.IsNullOrEmpty (entity.ParentAssembly.UnresolvedAssembly.Location);
 
230
                                var parentAssembly = entity.ParentAssembly;
 
231
                                if (parentAssembly == null)
 
232
                                        return false;
 
233
                                return !string.IsNullOrEmpty (parentAssembly.UnresolvedAssembly.Location);
226
234
                        }
227
235
                        return true;
228
236
                }
229
 
                
230
 
                
231
 
                static MonoDevelop.Ide.FindInFiles.SearchResult GetJumpTypePartSearchResult (IUnresolvedTypeDefinition part)
 
237
 
 
238
                static MonoDevelop.Ide.FindInFiles.SearchResult GetJumpTypePartSearchResult (ICSharpCode.NRefactory.TypeSystem.IUnresolvedTypeDefinition part)
232
239
                {
233
240
                        var provider = new MonoDevelop.Ide.FindInFiles.FileProvider (part.Region.FileName);
234
241
                        var doc = new Mono.TextEditor.TextDocument ();
242
249
                        return new MonoDevelop.Ide.FindInFiles.SearchResult (provider, position, part.Name.Length);
243
250
                }
244
251
 
245
 
                public void JumpToDeclaration (INamedElement visitable, bool askIfMultipleLocations = true)
 
252
                public void JumpToDeclaration (ICSharpCode.NRefactory.TypeSystem.INamedElement visitable, bool askIfMultipleLocations = true)
246
253
                {
247
254
                        if (askIfMultipleLocations) {
248
 
                                var type = visitable as IType;
 
255
                                var type = visitable as ICSharpCode.NRefactory.TypeSystem.IType;
249
256
                                if (type != null && type.GetDefinition () != null && type.GetDefinition ().Parts.Count > 1) {
250
257
                                        using (var monitor = IdeApp.Workbench.ProgressMonitors.GetSearchProgressMonitor (true, true)) {
251
258
                                                foreach (var part in type.GetDefinition ().Parts)
258
265
                        JumpToDeclaration (visitable);
259
266
                }
260
267
 
261
 
                void JumpToDeclaration (INamedElement element)
 
268
                void JumpToDeclaration (ICSharpCode.NRefactory.TypeSystem.INamedElement element)
262
269
                {
263
 
                        IEntity entity = element as IEntity;
264
 
 
265
 
                        if (entity == null && element is IType)
266
 
                                entity = ((IType)element).GetDefinition ();
 
270
                        var entity = element as ICSharpCode.NRefactory.TypeSystem.IEntity;
 
271
 
 
272
                        if (entity == null && element is ICSharpCode.NRefactory.TypeSystem.IType)
 
273
                                entity = ((ICSharpCode.NRefactory.TypeSystem.IType)element).GetDefinition ();
 
274
                        if (entity is SpecializedMember) 
 
275
                                entity = ((SpecializedMember)entity).MemberDefinition;
 
276
 
267
277
                        if (entity == null) {
268
278
                                LoggingService.LogError ("Unknown element:" + element);
269
279
                                return;
281
291
 
282
292
                        if (isCecilProjectContent && doc != null) {
283
293
                                doc.RunWhenLoaded (delegate {
284
 
                                        MonoDevelop.Ide.Gui.Content.IUrlHandler handler = doc.ActiveView as MonoDevelop.Ide.Gui.Content.IUrlHandler;
 
294
                                        var handler = doc.PrimaryView.GetContent<MonoDevelop.Ide.Gui.Content.IUrlHandler> ();
285
295
                                        if (handler != null)
286
296
                                                handler.Open (entity.GetIdString ());
287
297
                                });
288
298
                        }
289
299
                }
290
300
 
291
 
                public void JumpToDeclaration (IVariable entity)
 
301
                public void JumpToDeclaration (ICSharpCode.NRefactory.TypeSystem.IVariable entity)
292
302
                {
293
303
                        if (entity == null)
294
304
                                throw new ArgumentNullException ("entity");
295
305
                        string fileName = entity.Region.FileName;
296
 
                        var doc = IdeApp.Workbench.OpenDocument (fileName, entity.Region.BeginLine, entity.Region.BeginColumn);
 
306
                        IdeApp.Workbench.OpenDocument (fileName, entity.Region.BeginLine, entity.Region.BeginColumn);
297
307
                }
298
308
 
299
309
                public void RenameItem (IWorkspaceFileObject item, string newName)
464
474
                
465
475
                bool AllowSave (IWorkspaceFileObject item)
466
476
                {
467
 
                        if (HasChanged (item))
 
477
                        if (HasChanged (item)) {
468
478
                                return MessageService.Confirm (
469
 
                                    GettextCatalog.GetString ("Some project files have been changed from outside MonoDevelop. Do you want to overwrite them?"),
470
 
                                    GettextCatalog.GetString ("Changes done in those files will be overwritten by MonoDevelop."),
471
 
                                    AlertButton.OverwriteFile);
472
 
                        else
 
479
                                        GettextCatalog.GetString (
 
480
                                                "Some project files have been changed from outside {0}. Do you want to overwrite them?",
 
481
                                                BrandingService.ApplicationName
 
482
                                        ),
 
483
                                        GettextCatalog.GetString (
 
484
                                                "Changes done in those files will be overwritten by {0}.",
 
485
                                                BrandingService.ApplicationName
 
486
                                        ),
 
487
                                        AlertButton.OverwriteFile);
 
488
                        } else {
473
489
                                return true;
 
490
                        }
474
491
                }
475
492
                
476
493
                bool HasChanged (IWorkspaceFileObject item)
734
751
                                if (MessageService.RunCustomDialog (selDialog) == (int)Gtk.ResponseType.Ok) {
735
752
                                        var newRefs = selDialog.ReferenceInformations;
736
753
                                        
737
 
                                        ArrayList toDelete = new ArrayList ();
 
754
                                        var editEventArgs = new EditReferencesEventArgs (project);
738
755
                                        foreach (ProjectReference refInfo in project.References)
739
756
                                                if (!newRefs.Contains (refInfo))
740
 
                                                        toDelete.Add (refInfo);
741
 
                                        
742
 
                                        foreach (ProjectReference refInfo in toDelete)
743
 
                                                        project.References.Remove (refInfo);
 
757
                                                        editEventArgs.ReferencesToRemove.Add (refInfo);
744
758
 
745
759
                                        foreach (ProjectReference refInfo in selDialog.ReferenceInformations)
746
760
                                                if (!project.References.Contains (refInfo))
747
 
                                                        project.References.Add(refInfo);
748
 
                                        
749
 
                                        return true;
 
761
                                                        editEventArgs.ReferencesToAdd.Add(refInfo);
 
762
 
 
763
                                        if (BeforeEditReferences != null)
 
764
                                                BeforeEditReferences (this, editEventArgs);
 
765
 
 
766
                                        foreach (var reference in editEventArgs.ReferencesToRemove)
 
767
                                                project.References.Remove (reference);
 
768
 
 
769
                                        foreach (var reference in editEventArgs.ReferencesToAdd)
 
770
                                                project.References.Add (reference);
 
771
 
 
772
                                        return editEventArgs.ReferencesToAdd.Count > 0 || editEventArgs.ReferencesToRemove.Count > 0;
750
773
                                }
751
774
                                else
752
775
                                        return false;
839
862
 
840
863
                public bool CanExecute (IBuildTarget entry)
841
864
                {
842
 
                        ExecutionContext context = new ExecutionContext (Runtime.ProcessService.DefaultExecutionHandler, IdeApp.Workbench.ProgressMonitors);
 
865
                        ExecutionContext context = new ExecutionContext (Runtime.ProcessService.DefaultExecutionHandler, IdeApp.Workbench.ProgressMonitors, IdeApp.Workspace.ActiveExecutionTarget);
843
866
                        return CanExecute (entry, context);
844
867
                }
845
868
                
846
869
                public bool CanExecute (IBuildTarget entry, IExecutionHandler handler)
847
870
                {
848
 
                        ExecutionContext context = new ExecutionContext (handler, IdeApp.Workbench.ProgressMonitors);
 
871
                        ExecutionContext context = new ExecutionContext (handler, IdeApp.Workbench.ProgressMonitors, IdeApp.Workspace.ActiveExecutionTarget);
849
872
                        return entry.CanExecute (context, IdeApp.Workspace.ActiveConfiguration);
850
873
                }
851
874
                
861
884
                
862
885
                public IAsyncOperation Execute (IBuildTarget entry, IExecutionHandler handler)
863
886
                {
864
 
                        ExecutionContext context = new ExecutionContext (handler, IdeApp.Workbench.ProgressMonitors);
 
887
                        ExecutionContext context = new ExecutionContext (handler, IdeApp.Workbench.ProgressMonitors, IdeApp.Workspace.ActiveExecutionTarget);
865
888
                        return Execute (entry, context);
866
889
                }
867
890
                
874
897
                        DispatchService.ThreadDispatch (delegate {
875
898
                                ExecuteSolutionItemAsync (monitor, entry, context);
876
899
                        });
877
 
                        currentRunOperation = monitor.AsyncOperation;
 
900
                        CurrentRunOperation = monitor.AsyncOperation;
878
901
                        currentRunOperationOwner = entry;
879
902
                        currentRunOperation.Completed += delegate {
880
903
                                DispatchService.GuiDispatch (() => {
1010
1033
                                tempProject.Dispose ();
1011
1034
                                return res;
1012
1035
                        }
1013
 
                        else
1014
 
                                return false;
 
1036
                        else {
 
1037
                                var cmd = Runtime.ProcessService.CreateCommand (file);
 
1038
                                if (context.ExecutionHandler.CanExecute (cmd))
 
1039
                                        return true;
 
1040
                        }
 
1041
                        return false;
1015
1042
                }
1016
1043
                
1017
1044
                public IAsyncOperation ExecuteFile (string file, IExecutionHandler handler)
1118
1145
                                }
1119
1146
                        } catch (Exception ex) {
1120
1147
                                monitor.ReportError (GettextCatalog.GetString ("Build failed."), ex);
 
1148
                                result.AddError ("Build failed. See the build log for details.");
1121
1149
                        } finally {
1122
1150
                                tt.Trace ("Done building");
1123
1151
                        }
1213
1241
                                        string errorString = GettextCatalog.GetPluralString("{0} error", "{0} errors", result.ErrorCount, result.ErrorCount);
1214
1242
                                        string warningString = GettextCatalog.GetPluralString("{0} warning", "{0} warnings", result.WarningCount, result.WarningCount);
1215
1243
 
1216
 
                                        if (result.ErrorCount == 0 && result.WarningCount == 0 && lastResult.FailedBuildCount == 0) {
 
1244
                                        if (monitor.IsCancelRequested) {
 
1245
                                                monitor.ReportError (GettextCatalog.GetString ("Build canceled."), null);
 
1246
                                        } else if (result.ErrorCount == 0 && result.WarningCount == 0 && lastResult.FailedBuildCount == 0) {
1217
1247
                                                monitor.ReportSuccess (GettextCatalog.GetString ("Build successful."));
1218
1248
                                        } else if (result.ErrorCount == 0 && result.WarningCount > 0) {
1219
1249
                                                monitor.ReportWarning(GettextCatalog.GetString("Build: ") + errorString + ", " + warningString);
1313
1343
                                if (folder.IsRoot) {
1314
1344
                                        // Don't allow adding files to the root folder. VS doesn't allow it
1315
1345
                                        // If there is no existing folder, create one
1316
 
                                        var itemsFolder = (SolutionFolder) folder.Items.Where (item => item.Name == "Solution Items").FirstOrDefault ();
1317
 
                                        if (itemsFolder == null) {
1318
 
                                                itemsFolder = new SolutionFolder ();
1319
 
                                                itemsFolder.Name = "Solution Items";
1320
 
                                                folder.AddItem (itemsFolder);
1321
 
                                        }
1322
 
                                        folder = itemsFolder;
 
1346
                                        folder = folder.ParentSolution.DefaultSolutionFolder;
1323
1347
                                }
1324
1348
                                
1325
1349
                                if (!fp.IsChildPathOf (folder.BaseDirectory)) {
1617
1641
                        ProjectFile sourceParent = null;
1618
1642
                        if (filesToMove.Count == 1 && sourceProject != null) {
1619
1643
                                var pf = filesToMove[0];
1620
 
                                if (pf != null && pf.HasChildren)
1621
 
                                        foreach (ProjectFile child in pf.DependentChildren)
 
1644
                                if (pf != null && pf.HasChildren) {
 
1645
                                        foreach (ProjectFile child in pf.DependentChildren) {
 
1646
                                                filesToRemove.Add (child);
1622
1647
                                                filesToMove.Add (child);
 
1648
                                        }
 
1649
                                }
1623
1650
                                sourceParent = pf;
1624
1651
                        }
1625
1652
                        
1645
1672
                                        return;
1646
1673
                                }
1647
1674
                        }
1648
 
                        
1649
 
                        monitor.BeginTask (GettextCatalog.GetString ("Copying files..."), filesToMove.Count);
 
1675
 
 
1676
                        if (removeFromSource)
 
1677
                                monitor.BeginTask (GettextCatalog.GetString ("Moving files..."), filesToMove.Count);
 
1678
                        else
 
1679
                                monitor.BeginTask (GettextCatalog.GetString ("Copying files..."), filesToMove.Count);
1650
1680
                        
1651
1681
                        ProjectFile targetParent = null;
1652
1682
                        foreach (ProjectFile file in filesToMove) {
1671
1701
                                                FilePath fileDir = newFile.ParentDirectory;
1672
1702
                                                if (!Directory.Exists (fileDir) && !file.IsLink)
1673
1703
                                                        FileService.CreateDirectory (fileDir);
1674
 
                                                if (removeFromSource)
 
1704
                                                if (removeFromSource) {
 
1705
                                                        // File.Move() does not have an overwrite argument and will fail if the destFile path exists, however, the user
 
1706
                                                        // has already chosen to overwrite the destination file.
 
1707
                                                        if (File.Exists (newFile))
 
1708
                                                                File.Delete (newFile);
 
1709
 
1675
1710
                                                        FileService.MoveFile (sourceFile, newFile);
1676
 
                                                else
 
1711
                                                } else
1677
1712
                                                        FileService.CopyFile (sourceFile, newFile);
1678
1713
                                        } catch (Exception ex) {
1679
 
                                                monitor.ReportError (GettextCatalog.GetString ("File '{0}' could not be created.", newFile), ex);
 
1714
                                                if (removeFromSource)
 
1715
                                                        monitor.ReportError (GettextCatalog.GetString ("File '{0}' could not be moved.", sourceFile), ex);
 
1716
                                                else
 
1717
                                                        monitor.ReportError (GettextCatalog.GetString ("File '{0}' could not be copied.", sourceFile), ex);
1680
1718
                                                monitor.Step (1);
1681
1719
                                                continue;
1682
1720
                                        }
1715
1753
                                // Remove all files and directories under 'sourcePath'
1716
1754
                                foreach (var v in filesToRemove)
1717
1755
                                        sourceProject.Files.Remove (v);
 
1756
 
 
1757
                                // Moving an empty folder. A new folder object has to be added to the project.
 
1758
                                if (movingFolder && !sourceProject.Files.GetFilesInVirtualPath (targetPath).Any ()) {
 
1759
                                        var folderFile = new ProjectFile (targetPath) { Subtype = Subtype.Directory };
 
1760
                                        sourceProject.Files.Add (folderFile);
 
1761
                                }
1718
1762
                        }
1719
1763
                        
1720
1764
                        var pfolder = sourcePath.ParentDirectory;
1895
1939
                
1896
1940
                // Fired just before an entry is added to a combine
1897
1941
                public event AddEntryEventHandler AddingEntryToCombine;
 
1942
 
 
1943
                public event EventHandler CurrentRunOperationChanged;
 
1944
                public event EventHandler<EditReferencesEventArgs> BeforeEditReferences;
 
1945
                protected virtual void OnCurrentRunOperationChanged (EventArgs e)
 
1946
                {
 
1947
                        var handler = CurrentRunOperationChanged;
 
1948
                        if (handler != null)
 
1949
                                handler (this, e);
 
1950
                }
1898
1951
        }
1899
1952
        
1900
1953
        class ParseProgressMonitorFactory: IProgressMonitorFactory
2011
2064
                        
2012
2065
                        return new ProviderProxy (data, file.SourceEncoding, file.HadBOM);
2013
2066
                }
2014
 
                
 
2067
 
 
2068
                /// <summary>
 
2069
                /// Performs an edit operation on a text file regardless of it's open in the IDE or not.
 
2070
                /// </summary>
 
2071
                /// <returns><c>true</c>, if file operation was saved, <c>false</c> otherwise.</returns>
 
2072
                /// <param name="filePath">File path.</param>
 
2073
                /// <param name="operation">The operation.</param>
 
2074
                public bool EditFile (FilePath filePath, Action<TextEditorData> operation)
 
2075
                {
 
2076
                        if (operation == null)
 
2077
                                throw new ArgumentNullException ("operation");
 
2078
                        bool hadBom;
 
2079
                        Encoding encoding;
 
2080
                        bool isOpen;
 
2081
                        var data = GetTextEditorData (filePath, out hadBom, out encoding, out isOpen);
 
2082
                        operation (data);
 
2083
                        if (!isOpen) {
 
2084
                                try { 
 
2085
                                        Mono.TextEditor.Utils.TextFileUtility.WriteText (filePath, data.Text, encoding, hadBom);
 
2086
                                } catch (Exception e) {
 
2087
                                        LoggingService.LogError ("Error while saving changes to : " + filePath, e);
 
2088
                                        return false;
 
2089
                                }
 
2090
                        }
 
2091
                        return true;
 
2092
                }
 
2093
 
2015
2094
                public TextEditorData GetTextEditorData (FilePath filePath)
2016
2095
                {
2017
2096
                        bool isOpen;
2018
2097
                        return GetTextEditorData (filePath, out isOpen);
2019
2098
                }
2020
2099
 
 
2100
                public TextEditorData GetReadOnlyTextEditorData (FilePath filePath)
 
2101
                {
 
2102
                        foreach (var doc in IdeApp.Workbench.Documents) {
 
2103
                                if (doc.FileName == filePath) {
 
2104
                                        return doc.Editor;
 
2105
                                }
 
2106
                        }
 
2107
                        bool hadBom;
 
2108
                        Encoding encoding;
 
2109
                        var text = Mono.TextEditor.Utils.TextFileUtility.ReadAllText (filePath, out hadBom, out encoding);
 
2110
                        var data = new TextEditorData (TextDocument.CreateImmutableDocument (text));
 
2111
                        data.Document.MimeType = DesktopService.GetMimeTypeForUri (filePath);
 
2112
                        data.Document.FileName = filePath;
 
2113
                        data.Text = text;
 
2114
                        return data;
 
2115
                }
 
2116
 
2021
2117
                public TextEditorData GetTextEditorData (FilePath filePath, out bool isOpen)
2022
2118
                {
 
2119
                        bool hadBom;
 
2120
                        Encoding encoding;
 
2121
                        return GetTextEditorData (filePath, out hadBom, out encoding, out isOpen);
 
2122
                }
 
2123
 
 
2124
                public TextEditorData GetTextEditorData (FilePath filePath, out bool hadBom, out Encoding encoding, out bool isOpen)
 
2125
                {
2023
2126
                        foreach (var doc in IdeApp.Workbench.Documents) {
2024
2127
                                if (doc.FileName == filePath) {
2025
2128
                                        isOpen = true;
 
2129
                                        hadBom = false;
 
2130
                                        encoding = Encoding.Default;
2026
2131
                                        return doc.Editor;
2027
2132
                                }
2028
2133
                        }
2029
 
                        
2030
 
                        TextFile file = TextFile.ReadFile (filePath);
 
2134
 
 
2135
                        var text = Mono.TextEditor.Utils.TextFileUtility.ReadAllText (filePath, out hadBom, out encoding);
2031
2136
                        TextEditorData data = new TextEditorData ();
 
2137
                        data.Document.SuppressHighlightUpdate = true;
 
2138
                        data.Document.MimeType = DesktopService.GetMimeTypeForUri (filePath);
2032
2139
                        data.Document.FileName = filePath;
2033
 
                        data.Text = file.Text;
 
2140
                        data.Text = text;
2034
2141
                        isOpen = false;
2035
2142
                        return data;
2036
2143
                }