AvalonStudio.ShellViewModel.OpenDocument C# (CSharp) Method

OpenDocument() public method

public OpenDocument ( ISourceFile file, int line, int column = 1, bool debugHighlight = false, bool selectLine = false ) : Task
file ISourceFile
line int
column int
debugHighlight bool
selectLine bool
return Task
        public async Task<IEditor> OpenDocument(ISourceFile file, int line, int column = 1, bool debugHighlight = false,
            bool selectLine = false)
        {
            var currentTab = DocumentTabs.Documents.OfType<EditorViewModel>().FirstOrDefault(t => t.Model.ProjectFile.FilePath == file.FilePath);

            var selectedDocumentTCS = new TaskCompletionSource<IDocumentTabViewModel>();

            if (currentTab == null)
            {
                await Dispatcher.UIThread.InvokeTaskAsync(async () =>
                {
                    if (DocumentTabs.TemporaryDocument != null)
                    {
                        var documentToClose = DocumentTabs.TemporaryDocument;
                        DocumentTabs.TemporaryDocument = null;
                        await documentToClose.CloseCommand.ExecuteAsyncTask(null);
                        SelectedDocument = null;
                    }
                });

                EditorViewModel newEditor = null;
                await Dispatcher.UIThread.InvokeTaskAsync(async () =>
                {
                    newEditor = new EditorViewModel(new EditorModel());

                    newEditor.Margins.Add(new BreakPointMargin(IoC.Get<IDebugManager>().BreakPointManager));
                    newEditor.Margins.Add(new LineNumberMargin());

                    await Dispatcher.UIThread.InvokeTaskAsync(() =>
                    {
                        DocumentTabs.Documents.Add(newEditor);
                        DocumentTabs.TemporaryDocument = newEditor;
                    });

                    DocumentTabs.SelectedDocument = newEditor;

                    await Dispatcher.UIThread.InvokeTaskAsync(() => { newEditor.Model.OpenFile(file, newEditor.Intellisense, newEditor.Intellisense.CompletionAssistant); });

                    selectedDocumentTCS.SetResult(DocumentTabs.SelectedDocument);
                });
            }
            else
            {
                await Dispatcher.UIThread.InvokeTaskAsync(() => { DocumentTabs.SelectedDocument = currentTab; });

                selectedDocumentTCS.SetResult(DocumentTabs.SelectedDocument);
            }

            await selectedDocumentTCS.Task;

            if (DocumentTabs.SelectedDocument is EditorViewModel)
            {
                if (debugHighlight)
                {
                    (DocumentTabs.SelectedDocument as EditorViewModel).DebugLineHighlighter.Line = line;
                }

                if (selectLine || debugHighlight)
                {
                    Dispatcher.UIThread.InvokeAsync(() => (DocumentTabs.SelectedDocument as EditorViewModel).Model.ScrollToLine(line));
                    (DocumentTabs.SelectedDocument as EditorViewModel).GotoPosition(line, column);
                }
            }

            return DocumentTabs.SelectedDocument as EditorViewModel;
        }