-
Notifications
You must be signed in to change notification settings - Fork 677
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #555 from zooba/issue-406
Issue 406
- Loading branch information
Showing
8 changed files
with
607 additions
and
10 deletions.
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
55 changes: 55 additions & 0 deletions
55
Python/Product/PythonTools/PythonTools/Repl/InlineReplAdornment.cs
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* **************************************************************************** | ||
* | ||
* Copyright (c) Microsoft Corporation. | ||
* | ||
* This source code is subject to terms and conditions of the Apache License, Version 2.0. A | ||
* copy of the license can be found in the License.html file at the root of this distribution. If | ||
* you cannot locate the Apache License, Version 2.0, please send an email to | ||
* [email protected]. By using this source code in any fashion, you are agreeing to be bound | ||
* by the terms of the Apache License, Version 2.0. | ||
* | ||
* You must not remove this notice, or any other, from this software. | ||
* | ||
* ***************************************************************************/ | ||
|
||
using System.ComponentModel.Composition; | ||
using System.Windows; | ||
#if DEV14_OR_LATER | ||
using Microsoft.VisualStudio.InteractiveWindow; | ||
#else | ||
using Microsoft.VisualStudio.Repl; | ||
#endif | ||
using Microsoft.VisualStudio.Text; | ||
using Microsoft.VisualStudio.Text.Editor; | ||
using Microsoft.VisualStudio.Text.Tagging; | ||
using Microsoft.VisualStudio.Utilities; | ||
|
||
namespace Microsoft.PythonTools.Repl { | ||
[Export(typeof(IViewTaggerProvider))] | ||
[TagType(typeof(IntraTextAdornmentTag))] | ||
#if DEV14_OR_LATER | ||
[ContentType(PredefinedInteractiveContentTypes.InteractiveContentTypeName)] | ||
#else | ||
[ContentType(ReplConstants.ReplContentTypeName)] | ||
#endif | ||
internal class InlineReplAdornmentProvider : IViewTaggerProvider { | ||
public ITagger<T> CreateTagger<T>(ITextView textView, ITextBuffer buffer) where T : ITag { | ||
if (buffer == null || textView == null || typeof(T) != typeof(IntraTextAdornmentTag)) { | ||
return null; | ||
} | ||
|
||
return (ITagger<T>)textView.Properties.GetOrCreateSingletonProperty<InlineReplAdornmentManager>( | ||
typeof(InlineReplAdornmentManager), | ||
() => new InlineReplAdornmentManager(textView) | ||
); | ||
} | ||
|
||
internal static InlineReplAdornmentManager GetManager(ITextView view) { | ||
InlineReplAdornmentManager result; | ||
if (!view.Properties.TryGetProperty<InlineReplAdornmentManager>(typeof(InlineReplAdornmentManager), out result)) { | ||
return null; | ||
} | ||
return result; | ||
} | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
Python/Product/PythonTools/PythonTools/Repl/InlineReplAdornmentManager.cs
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 |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* **************************************************************************** | ||
* | ||
* Copyright (c) Microsoft Corporation. | ||
* | ||
* This source code is subject to terms and conditions of the Apache License, Version 2.0. A | ||
* copy of the license can be found in the License.html file at the root of this distribution. If | ||
* you cannot locate the Apache License, Version 2.0, please send an email to | ||
* [email protected]. By using this source code in any fashion, you are agreeing to be bound | ||
* by the terms of the Apache License, Version 2.0. | ||
* | ||
* You must not remove this notice, or any other, from this software. | ||
* | ||
* ***************************************************************************/ | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Windows; | ||
using System.Windows.Threading; | ||
using Microsoft.VisualStudio.Text; | ||
using Microsoft.VisualStudio.Text.Editor; | ||
using Microsoft.VisualStudio.Text.Tagging; | ||
|
||
namespace Microsoft.PythonTools.Repl { | ||
class InlineReplAdornmentManager : ITagger<IntraTextAdornmentTag> { | ||
private readonly ITextView _textView; | ||
private readonly List<Tuple<SnapshotPoint, UIElement>> _tags; | ||
private readonly Dispatcher _dispatcher; | ||
|
||
internal InlineReplAdornmentManager(ITextView textView) { | ||
_textView = textView; | ||
_tags = new List<Tuple<SnapshotPoint, UIElement>>(); | ||
_dispatcher = Dispatcher.CurrentDispatcher; | ||
textView.TextBuffer.Changed += TextBuffer_Changed; | ||
} | ||
|
||
void TextBuffer_Changed(object sender, TextContentChangedEventArgs e) { | ||
if (e.After.Length == 0) { | ||
// screen was cleared... | ||
RemoveAll(); | ||
} | ||
} | ||
|
||
public IEnumerable<ITagSpan<IntraTextAdornmentTag>> GetTags(NormalizedSnapshotSpanCollection spans) { | ||
var result = new List<TagSpan<IntraTextAdornmentTag>>(); | ||
for (int i = 0; i < _tags.Count; i++) { | ||
if (_tags[i].Item1.Snapshot != _textView.TextSnapshot) { | ||
// update to the latest snapshot | ||
_tags[i] = new Tuple<SnapshotPoint, UIElement>( | ||
_tags[i].Item1.TranslateTo(_textView.TextSnapshot, PointTrackingMode.Negative), | ||
_tags[i].Item2 | ||
); | ||
} | ||
|
||
var span = new SnapshotSpan(_textView.TextSnapshot, _tags[i].Item1, 0); | ||
bool intersects = false; | ||
foreach (var applicableSpan in spans) { | ||
if (applicableSpan.TranslateTo(_textView.TextSnapshot, SpanTrackingMode.EdgeInclusive).IntersectsWith(span)) { | ||
intersects = true; | ||
break; | ||
} | ||
} | ||
if (!intersects) { | ||
continue; | ||
} | ||
var tag = new IntraTextAdornmentTag(_tags[i].Item2, null); | ||
result.Add(new TagSpan<IntraTextAdornmentTag>(span, tag)); | ||
} | ||
return result; | ||
} | ||
|
||
public void AddAdornment(UIElement uiElement, SnapshotPoint targetLoc) { | ||
if (Dispatcher.CurrentDispatcher != _dispatcher) { | ||
_dispatcher.BeginInvoke(new Action(() => AddAdornment(uiElement, targetLoc))); | ||
return; | ||
} | ||
var targetLine = targetLoc.GetContainingLine(); | ||
_tags.Add(new Tuple<SnapshotPoint, UIElement>(targetLoc, uiElement)); | ||
var handler = TagsChanged; | ||
if (handler != null) { | ||
var span = new SnapshotSpan(_textView.TextSnapshot, targetLine.Start, targetLine.LengthIncludingLineBreak); | ||
var args = new SnapshotSpanEventArgs(span); | ||
handler(this, args); | ||
} | ||
} | ||
|
||
public IList<Tuple<SnapshotPoint, UIElement>> Adornments { | ||
get { return _tags; } | ||
} | ||
|
||
public void RemoveAll() { | ||
_tags.Clear(); | ||
} | ||
|
||
public event EventHandler<SnapshotSpanEventArgs> TagsChanged; | ||
} | ||
} |
Oops, something went wrong.