Beispiel #1
0
        async Task <(bool Success, Document Content)> RealOpenFile(ProgressMonitor monitor, FileOpenInformation openFileInfo)
        {
            FilePath fileName;

            await InitDesktopService();

            Counters.OpenDocumentTimer.Trace("Checking file");

            string origName = openFileInfo.FileName;

            if (origName == null)
            {
                monitor.ReportError(GettextCatalog.GetString("Invalid file name"), null);
                return(false, null);
            }

            fileName = openFileInfo.FileName;
            if (!origName.StartsWith("http://", StringComparison.Ordinal))
            {
                fileName = fileName.FullPath;
            }

            //Debug.Assert(FileService.IsValidPath(fileName));
            if (FileService.IsDirectory(fileName))
            {
                monitor.ReportError(GettextCatalog.GetString("{0} is a directory", fileName), null);
                return(false, null);
            }

            // test, if file fileName exists
            if (!origName.StartsWith("http://", StringComparison.Ordinal))
            {
                // test, if an untitled file should be opened
                if (!Path.IsPathRooted(origName))
                {
                    foreach (Document doc in Documents)
                    {
                        if (doc.IsNewDocument && doc.FilePath == origName)
                        {
                            doc.Select();
                            ScrollToRequestedCaretLocation(doc, openFileInfo);
                            return(true, doc);
                        }
                    }
                }

                if (!File.Exists(fileName))
                {
                    monitor.ReportError(GettextCatalog.GetString("File not found: {0}", fileName), null);
                    return(false, null);
                }
            }

            Counters.OpenDocumentTimer.Trace("Looking for binding");

            var documentControllerService = await ServiceProvider.GetService <DocumentControllerService> ();

            IExternalDisplayBinding       externalBinding = null;
            DocumentControllerDescription internalBinding = null;
            WorkspaceObject project = null;

            if (openFileInfo.Owner == null)
            {
                var workspace = await ServiceProvider.GetService <RootWorkspace> ();

                // Set the project if one can be found. The project on the FileOpenInformation
                // is used to add project metadata to the OpenDocumentTimer counter.
                project = workspace.GetProjectContainingFile(fileName);

                // In some cases, the file may be a symlinked file. We cannot find the resolved symlink path
                // in the project, so we should try looking up the original file.
                if (project == null)
                {
                    project = workspace.GetProjectContainingFile(openFileInfo.OriginalFileName);
                }
                openFileInfo.Owner = project;
            }
            else
            {
                project = openFileInfo.Owner;
            }

            var displayBindingService = await ServiceProvider.GetService <DisplayBindingService> ();

            var fileDescriptor  = new FileDescriptor(fileName, null, project);
            var internalViewers = await documentControllerService.GetSupportedControllers(fileDescriptor);

            var externalViewers = displayBindingService.GetDisplayBindings(fileName, null, project as Project).OfType <IExternalDisplayBinding> ().ToList();

            if (openFileInfo.DocumentControllerDescription != null)
            {
                internalBinding = openFileInfo.DocumentControllerDescription;
            }
            else
            {
                var bindings = displayBindingService.GetDisplayBindings(fileName, null, project as Project).ToList();
                if (openFileInfo.Options.HasFlag(OpenDocumentOptions.OnlyInternalViewer))
                {
                    internalBinding = internalViewers.FirstOrDefault(d => d.CanUseAsDefault) ?? internalViewers.FirstOrDefault();
                }
                else if (openFileInfo.Options.HasFlag(OpenDocumentOptions.OnlyExternalViewer))
                {
                    externalBinding = externalViewers.FirstOrDefault(d => d.CanUseAsDefault) ?? externalViewers.FirstOrDefault();
                }
                else
                {
                    internalBinding = internalViewers.FirstOrDefault(d => d.CanUseAsDefault);
                    if (internalBinding == null)
                    {
                        externalBinding = externalViewers.FirstOrDefault(d => d.CanUseAsDefault);
                        if (externalBinding == null)
                        {
                            internalBinding = internalViewers.FirstOrDefault();
                            if (internalBinding == null)
                            {
                                externalBinding = externalViewers.FirstOrDefault();
                            }
                        }
                    }
                }
            }

            Document newContent = null;

            try {
                if (internalBinding != null)
                {
                    newContent = await LoadFile(fileName, monitor, internalBinding, project, openFileInfo);
                }
                else if (externalBinding != null)
                {
                    var extBinding = (IExternalDisplayBinding)externalBinding;
                    var app        = extBinding.GetApplication(fileName, null, project as Project);
                    app.Launch(fileName);
                }
                else if (!openFileInfo.Options.HasFlag(OpenDocumentOptions.OnlyInternalViewer))
                {
                    try {
                        Counters.OpenDocumentTimer.Trace("Showing in browser");
                        desktopService.OpenFile(fileName);
                    } catch (Exception ex) {
                        LoggingService.LogError("Error opening file: " + fileName, ex);
                        MessageService.ShowError(GettextCatalog.GetString("File '{0}' could not be opened", fileName));
                        return(false, null);
                    }
                }
                Counters.OpenDocumentTimer.Trace("Adding to recent files");
                desktopService.RecentFiles.AddFile(fileName, project);
            } catch (Exception ex) {
                monitor.ReportError(GettextCatalog.GetString("The file '{0}' could not be opened.", fileName), ex);
                return(false, null);
            }
            return(true, newContent);
        }
Beispiel #2
0
        public async Task <Document> OpenDocument(FileOpenInformation info)
        {
            if (string.IsNullOrEmpty(info.FileName))
            {
                return(null);
            }

            // Make sure composition manager is ready since ScrollToRequestedCaretLocation will use it
            await Runtime.GetService <CompositionManager> ();

            var metadata       = CreateOpenDocumentTimerMetadata();
            var fileDescriptor = new FileDescriptor(info.FileName, null, info.Owner);

            using (var timer = Counters.OpenDocumentTimer.BeginTiming("Opening file " + info.FileName, metadata)) {
                navigationHistoryManager?.LogActiveDocument();
                timer.Trace("Look for open document");
                foreach (Document doc in Documents)
                {
                    if (info.Options.HasFlag(OpenDocumentOptions.TryToReuseViewer) && doc.TryReuseDocument(fileDescriptor))
                    {
                        // If TryReuseDocument returns true it means that the document can be reused and has been reused, so look no further
                        ReuseDocument(doc, info);
                        return(doc);
                    }

                    // If the document can't explicitly handle the new descriptor, check the file names.
                    // If the file of the document is the same, let's just focus it

                    if (!doc.IsFile || doc.FilePath != fileDescriptor.FilePath)
                    {
                        continue;
                    }

                    // Reuse the document if the controller factory didn't change

                    if (info.Options.HasFlag(OpenDocumentOptions.TryToReuseViewer) || doc.DocumentControllerDescription == info.DocumentControllerDescription)
                    {
                        ReuseDocument(doc, info);
                        return(doc);
                    }
                    else
                    {
                        if (!await doc.Close())
                        {
                            return(doc);
                        }
                        break;
                    }
                }
                timer.Trace("Initializing monitor");
                var progressMonitorManager = await ServiceProvider.GetService <ProgressMonitorManager> ();

                var pm = progressMonitorManager.GetStatusProgressMonitor(
                    GettextCatalog.GetString("Opening {0}", info.Owner is SolutionFolderItem item ?
                                             info.FileName.ToRelative(item?.ParentSolution?.BaseDirectory ?? item.BaseDirectory) :
                                             info.FileName),
                    Stock.StatusWorking,
                    true
                    );

                var result = await RealOpenFile(pm, info, timer);

                pm.Dispose();

                AddOpenDocumentTimerMetadata(metadata, info, result.Content, result.Success);

                if (result.Content != null)
                {
                    timer.Trace("Wrapping document");
                    Document doc = result.Content;

                    if (doc != null && info.Options.HasFlag(OpenDocumentOptions.BringToFront))
                    {
                        doc.Select();
                    }
                    return(doc);
                }
                return(null);
            }
        }
Beispiel #3
0
        public async Task <Document> OpenDocument(FileOpenInformation info)
        {
            if (string.IsNullOrEmpty(info.FileName))
            {
                return(null);
            }

            if (navigationHistoryManager == null)
            {
                navigationHistoryManager = await ServiceProvider.GetService <NavigationHistoryService> ();
            }

            // Make sure composition manager is ready since ScrollToRequestedCaretLocation will use it
            await Runtime.GetService <CompositionManager> ();

            var metadata       = CreateOpenDocumentTimerMetadata();
            var fileDescriptor = new FileDescriptor(info.FileName, null, info.Owner);

            using (Counters.OpenDocumentTimer.BeginTiming("Opening file " + info.FileName, metadata)) {
                navigationHistoryManager.LogActiveDocument();
                Counters.OpenDocumentTimer.Trace("Look for open document");
                foreach (Document doc in Documents)
                {
                    // Search all ViewContents to see if they can "re-use" this filename.
                    if (!doc.TryReuseDocument(fileDescriptor))
                    {
                        continue;
                    }

                    //if found, try to reuse or close the old view
                    // reuse the view if the binidng didn't change
                    if (info.Options.HasFlag(OpenDocumentOptions.TryToReuseViewer) || doc.DocumentControllerDescription == info.DocumentControllerDescription)
                    {
                        if (info.Owner != null && doc.Owner != info.Owner)
                        {
                            doc.AttachToProject(info.Owner);
                        }

                        ScrollToRequestedCaretLocation(doc, info);

                        if (info.Options.HasFlag(OpenDocumentOptions.BringToFront))
                        {
                            doc.Select();
                            navigationHistoryManager.LogActiveDocument();
                        }
                        return(doc);
                    }
                    else
                    {
                        if (!await doc.Close())
                        {
                            return(doc);
                        }
                        break;
                    }
                }
                Counters.OpenDocumentTimer.Trace("Initializing monitor");
                var progressMonitorManager = await ServiceProvider.GetService <ProgressMonitorManager> ();

                var pm = progressMonitorManager.GetStatusProgressMonitor(
                    GettextCatalog.GetString("Opening {0}", info.Owner is SolutionFolderItem item ?
                                             info.FileName.ToRelative(item.ParentSolution.BaseDirectory) :
                                             info.FileName),
                    Stock.StatusWorking,
                    true
                    );

                var result = await RealOpenFile(pm, info);

                pm.Dispose();

                AddOpenDocumentTimerMetadata(metadata, info, result.Content, result.Success);

                if (result.Content != null)
                {
                    Counters.OpenDocumentTimer.Trace("Wrapping document");
                    Document doc = result.Content;

                    if (doc != null && info.Options.HasFlag(OpenDocumentOptions.BringToFront))
                    {
                        doc.Select();
                    }
                    return(doc);
                }
                return(null);
            }
        }
 public override Task <DocumentController> CreateController(FileDescriptor modelDescriptor, DocumentControllerDescription controllerDescription)
 {
     return(Task.FromResult <DocumentController> (new TestFileController()));
 }
        protected override IEnumerable <DocumentControllerDescription> GetSupportedControllers(FileDescriptor modelDescriptor)
        {
            if (modelDescriptor.FilePath.Extension == ".test")
            {
                yield return(new DocumentControllerDescription("Test Source View", true, DocumentControllerRole.Source));

                yield return(new DocumentControllerDescription("Test Design View", false, DocumentControllerRole.VisualDesign));
            }
        }
 protected override IEnumerable <DocumentControllerDescription> GetSupportedControllers(FileDescriptor modelDescriptor)
 {
     if (Enabled && (modelDescriptor.FilePath.Extension == ".dll_test" || modelDescriptor.FilePath.Extension == ".exe_test"))
     {
         yield return(new DocumentControllerDescription("Assembly_Test", true, DocumentControllerRole.Tool));
     }
 }
        public async Task ReuseFileDocument()
        {
            var f1 = new ReusableControllerFactory();
            var f2 = new ReusableFileControllerFactory();

            documentControllerService.RegisterFactory(f1);
            documentControllerService.RegisterFactory(f2);

            var foo_dll_test = GetTempFile(".dll_test");
            var bar_dll_test = GetTempFile(".dll_test");
            var bar_exe_test = GetTempFile(".exe_test");
            var foo_txt      = GetTempFile(".txt");

            try {
                var controller = new ReusableFileController();
                var descriptor = new FileDescriptor(foo_dll_test, null, null);
                await controller.Initialize(descriptor);

                var doc = await documentManager.OpenDocument(controller);

                Assert.NotNull(doc);

                var doc2 = await documentManager.OpenDocument(new FileDescriptor (foo_dll_test, null, null));

                Assert.AreSame(doc, doc2);

                doc2 = await documentManager.OpenDocument(new FileDescriptor (bar_dll_test, null, null));

                Assert.AreSame(doc, doc2);

                doc2 = await documentManager.OpenDocument(new FileDescriptor (bar_exe_test, null, null));

                Assert.AreSame(doc, doc2);

                doc2 = await documentManager.OpenDocument(new FileDescriptor (foo_txt, null, null));

                Assert.AreNotSame(doc, doc2);
                await doc2.Close();

                doc2 = await documentManager.OpenDocument(new FileOpenInformation (foo_dll_test));

                Assert.AreSame(doc, doc2);

                doc2 = await documentManager.OpenDocument(new FileOpenInformation (bar_dll_test));

                Assert.AreSame(doc, doc2);

                doc2 = await documentManager.OpenDocument(new FileOpenInformation (bar_exe_test));

                Assert.AreSame(doc, doc2);

                doc2 = await documentManager.OpenDocument(new FileOpenInformation (foo_txt));

                Assert.AreNotSame(doc, doc2);
                await doc2.Close();

                await documentManager.CloseAllDocuments(false);

                doc2 = await documentManager.OpenDocument(new FileOpenInformation (foo_dll_test) { Options = OpenDocumentOptions.None });

                Assert.AreNotSame(doc, doc2);
                await doc2.Close();

                doc2 = await documentManager.OpenDocument(new FileOpenInformation (bar_dll_test) { Options = OpenDocumentOptions.None });

                Assert.AreNotSame(doc, doc2);
                await doc2.Close();

                doc2 = await documentManager.OpenDocument(new FileOpenInformation (bar_exe_test) { Options = OpenDocumentOptions.None });

                Assert.AreNotSame(doc, doc2);
                await doc2.Close();

                doc2 = await documentManager.OpenDocument(new FileOpenInformation (foo_txt) { Options = OpenDocumentOptions.None });

                Assert.AreNotSame(doc, doc2);
                await doc2.Close();
            } finally {
                documentControllerService.UnregisterFactory(f1);
                documentControllerService.UnregisterFactory(f2);
                File.Delete(foo_dll_test);
                File.Delete(bar_dll_test);
                File.Delete(bar_exe_test);
                File.Delete(foo_txt);
            }
        }
Beispiel #8
0
 protected override IEnumerable <DocumentControllerDescription> GetSupportedControllers(FileDescriptor file)
 {
     if (attribute.CanHandle(file.FilePath, file.MimeType))
     {
         yield return(new DocumentControllerDescription {
             CanUseAsDefault = attribute.CanUseAsDefault,
             Role = attribute.Role,
             Name = attribute.Name
         });
     }
 }
Beispiel #9
0
        public override Task <DocumentController> CreateController(FileDescriptor file, DocumentControllerDescription controllerDescription)
        {
            var node = (InstanceExtensionNode)attribute.ExtensionNode;

            return(Task.FromResult((DocumentController)node.CreateInstance(typeof(DocumentController))));
        }
 protected virtual Task <IEnumerable <DocumentControllerDescription> > GetSupportedControllersAsync(FileDescriptor modelDescriptor)
 {
     return(Task.FromResult(GetSupportedControllers(modelDescriptor)));
 }
 protected virtual IEnumerable <DocumentControllerDescription> GetSupportedControllers(FileDescriptor modelDescriptor)
 {
     throw new NotImplementedException();
 }
 public abstract Task <DocumentController> CreateController(FileDescriptor modelDescriptor, DocumentControllerDescription controllerDescription);