-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
track non roslyn text buffer changes as well as roslyn text buffer chang... #2515
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bb715ae
track non roslyn text buffer changes as well as roslyn text buffer ch…
heejaechang 42d1a95
changed the way tracks non roslyn files
heejaechang f56e0c9
Merge branch 'master' of https://github.com/dotnet/roslyn
heejaechang 1c2dfde
detach Close events and use GetTextView code from platform
heejaechang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,34 @@ | ||
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Diagnostics; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Editor.Shared.Utilities; | ||
using Microsoft.VisualStudio.ComponentModelHost; | ||
using Microsoft.VisualStudio.Shell.Interop; | ||
using Microsoft.VisualStudio.Text; | ||
using Roslyn.Utilities; | ||
|
||
namespace Microsoft.VisualStudio.LanguageServices.Implementation | ||
{ | ||
[DebuggerDisplay("{GetDebuggerDisplay(),nq}")] | ||
internal class VisualStudioDocumentTrackingService : IDocumentTrackingService, IVsSelectionEvents, IDisposable | ||
{ | ||
private readonly NonRoslynTextBufferTracker _tracker; | ||
|
||
private IVsMonitorSelection _monitorSelection; | ||
private uint _cookie; | ||
private ImmutableList<FrameListener> _visibleFrames; | ||
private IVsWindowFrame _activeFrame; | ||
|
||
public VisualStudioDocumentTrackingService(IServiceProvider serviceProvider) | ||
{ | ||
_tracker = new NonRoslynTextBufferTracker(this); | ||
|
||
_visibleFrames = ImmutableList<FrameListener>.Empty; | ||
|
||
_monitorSelection = (IVsMonitorSelection)serviceProvider.GetService(typeof(SVsShellMonitorSelection)); | ||
|
@@ -132,6 +140,18 @@ public int OnCmdUIContextChanged([ComAliasName("Microsoft.VisualStudio.Shell.Int | |
return VSConstants.E_NOTIMPL; | ||
} | ||
|
||
public event EventHandler<EventArgs> NonRoslynBufferTextChanged; | ||
|
||
public void OnNonRoslynBufferOpened(ITextBuffer buffer) | ||
{ | ||
_tracker.OnOpened(buffer); | ||
} | ||
|
||
public void OnNonRoslynBufferClosed(ITextBuffer buffer) | ||
{ | ||
_tracker.OnClosed(buffer); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (_cookie != VSConstants.VSCOOKIE_NIL && _monitorSelection != null) | ||
|
@@ -262,5 +282,51 @@ internal string GetDebuggerDisplay() | |
return caption.ToString(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// It tracks non roslyn text buffer text changes. | ||
/// </summary> | ||
private class NonRoslynTextBufferTracker : ForegroundThreadAffinitizedObject | ||
{ | ||
private readonly VisualStudioDocumentTrackingService _owner; | ||
private readonly HashSet<ITextBuffer> _buffers; | ||
|
||
public NonRoslynTextBufferTracker(VisualStudioDocumentTrackingService owner) | ||
{ | ||
_owner = owner; | ||
_buffers = new HashSet<ITextBuffer>(); | ||
} | ||
|
||
public void OnOpened(ITextBuffer buffer) | ||
{ | ||
AssertIsForeground(); | ||
|
||
if (_buffers.Contains(buffer)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens for multiple views over the same buffer? |
||
{ | ||
return; | ||
} | ||
|
||
_buffers.Add(buffer); | ||
buffer.PostChanged += OnTextChanged; | ||
} | ||
|
||
public void OnClosed(ITextBuffer buffer) | ||
{ | ||
AssertIsForeground(); | ||
|
||
if (!_buffers.Contains(buffer)) | ||
{ | ||
return; | ||
} | ||
|
||
buffer.PostChanged -= OnTextChanged; | ||
_buffers.Remove(buffer); | ||
} | ||
|
||
private void OnTextChanged(object sender, EventArgs e) | ||
{ | ||
_owner.NonRoslynBufferTextChanged?.Invoke(sender, e); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if the document, after already being open, was then added to the workspace? At this point this code path will get skipped, and the view unsubscribe might not run?
Trying to do this state tracking in this class really isn't the right thing to do. We should just have a separate class to subscribe to the text view notifications.