diff options
author | Aaron Bockover <abock@microsoft.com> | 2019-07-29 19:41:40 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-29 19:41:40 +0300 |
commit | e702dbf121ff3f6ec33834f6c87e69c211badcb4 (patch) | |
tree | 062600f6c4b1b78b431c00a78949252380031319 /main/src/addins | |
parent | 4e737edae0176a621e7a256ada45691322b0774c (diff) | |
parent | c16be7b1d467acbe6db9031d6b406e26b36f1d53 (diff) |
Merge pull request #8262 from mono/pr-anvod-outlineupdate
Fixes VSTS 948416: Document Outline does not refresh
Diffstat (limited to 'main/src/addins')
2 files changed, 40 insertions, 5 deletions
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<Microsoft.CodeAnalysis.Navigation.IDocumentNavigationService> (); - 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<CSharpOutlineTextEditorExtension> { - protected override CSharpOutlineTextEditorExtension CreateInstance (ITextView view) => new CSharpOutlineTextEditorExtension (view.TextSnapshot.GetOpenDocumentInCurrentContextWithChanges ()); + protected override CSharpOutlineTextEditorExtension CreateInstance (ITextView view) => new CSharpOutlineTextEditorExtension (view); } } |