From c16be7b1d467acbe6db9031d6b406e26b36f1d53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anz=CC=8Ce=20Vodovnik?= Date: Tue, 23 Jul 2019 20:01:03 +0200 Subject: Wiring the change handlers for Document Outline control. Fixes https://vsmac.dev/948416/. --- .../CSharpOutlineTextEditorExtension.cs | 43 ++++++++++++++++++++-- .../CSharpOutlineTextEditorExtensionProvider.cs | 2 +- 2 files changed, 40 insertions(+), 5 deletions(-) (limited to 'main/src/addins') diff --git a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.ClassOutline/CSharpOutlineTextEditorExtension.cs b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.ClassOutline/CSharpOutlineTextEditorExtension.cs index 09d51c74e1..ec2ba8e078 100644 --- a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.ClassOutline/CSharpOutlineTextEditorExtension.cs +++ b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.ClassOutline/CSharpOutlineTextEditorExtension.cs @@ -41,6 +41,8 @@ using MonoDevelop.Ide.Editor; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.CSharp; +using Microsoft.VisualStudio.Text.Editor; +using Microsoft.CodeAnalysis.Text; namespace MonoDevelop.CSharp.ClassOutline { @@ -74,16 +76,37 @@ namespace MonoDevelop.CSharp.ClassOutline bool outlineReady; Document providedAnalysisDocument; - + ITextView textView; public CSharpOutlineTextEditorExtension () { // does nothing, but is here for legacy editor support } - public CSharpOutlineTextEditorExtension (Document document) + public CSharpOutlineTextEditorExtension (ITextView textView) { - providedAnalysisDocument = document; + this.textView = textView ?? throw new ArgumentNullException (nameof (textView)); + + textView.Closed += TextView_Closed; + textView.TextBuffer.Changed += UpdateAnalysisDocument; + + UpdateAnalysisDocument (this, null); + } + + void UpdateAnalysisDocument (object sender, Microsoft.VisualStudio.Text.TextContentChangedEventArgs e) + { + providedAnalysisDocument = textView.TextSnapshot.GetOpenDocumentInCurrentContextWithChanges (); + outlineReady = true; // we need this to compensate for the old behaviour + UpdateDocumentOutline (this, e); + } + + void TextView_Closed (object sender, EventArgs e) + { + if (textView == null || textView.TextBuffer == null) return; + + textView.Closed -= TextView_Closed; + textView.TextBuffer.Changed -= UpdateAnalysisDocument; + textView = null; } public override bool IsValidInContext (DocumentContext context) @@ -110,6 +133,10 @@ namespace MonoDevelop.CSharp.ClassOutline lastCU = null; settings = null; comparer = null; + + // also, unsubscribe from text view events, if we're in the new editor + TextView_Closed (this, null); + base.Dispose (); } @@ -232,7 +259,15 @@ namespace MonoDevelop.CSharp.ClassOutline var workspace = providedAnalysisDocument.Project.Solution.Workspace; var navigationService = workspace.Services.GetService (); - navigationService.TryNavigateToSpan (workspace, providedAnalysisDocument.Id, syntaxNode?.Span ?? ((SyntaxTrivia)o).FullSpan); + try { + navigationService.TryNavigateToSpan (workspace, providedAnalysisDocument.Id, syntaxNode?.Span ?? ((SyntaxTrivia)o).FullSpan); + } catch { + // if this happens, there's a big chance that the document was updated and we didn't update our + // tree with the latest analysis. What we can do is try and update the analysis document again. + // Specific use case for this is when the enough code is removed, and we try navigating to the + // last span in the document. + UpdateAnalysisDocument (this, null); + } return; } diff --git a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.ClassOutline/CSharpOutlineTextEditorExtensionProvider.cs b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.ClassOutline/CSharpOutlineTextEditorExtensionProvider.cs index dc5daa54e1..e251455c5b 100644 --- a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.ClassOutline/CSharpOutlineTextEditorExtensionProvider.cs +++ b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.ClassOutline/CSharpOutlineTextEditorExtensionProvider.cs @@ -35,6 +35,6 @@ namespace MonoDevelop.CSharp [TextViewRole (PredefinedTextViewRoles.PrimaryDocument)] class CSharpOutlineTextEditorExtensionProvider : EditorContentInstanceProvider { - protected override CSharpOutlineTextEditorExtension CreateInstance (ITextView view) => new CSharpOutlineTextEditorExtension (view.TextSnapshot.GetOpenDocumentInCurrentContextWithChanges ()); + protected override CSharpOutlineTextEditorExtension CreateInstance (ITextView view) => new CSharpOutlineTextEditorExtension (view); } } -- cgit v1.2.3