Skip to content

Instantly share code, notes, and snippets.

testing gist
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Windows.Media;
using Microsoft.VisualStudio.ApplicationModel.Environments;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Classification;
using Microsoft.VisualStudio.Utilities;
//// Using the caret directly
// assuming you have ITextView textView
// assuming you have the int line/offset (where offset is from the start of the line)
var position = textView.TextSnapshot.GetLineFromLineNumber(line) + offset;
textView.Caret.MoveTo(position);
textView.Selection.Select(new SnapshotSpan(position, position), false);
//// Moving to a new visual line
// Assuming you have ITextView textView
// 1. Assuming you have the new line number
var bufferLine = textView.TextSnapshot.GetLineFromLineNumber(lineNumber);
var viewLine = textView.GetTextViewLineContainingBufferPosition(bufferLine.Start);
textView.Caret.MoveTo(viewLine); // preserves x coordinate
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.Utilities;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Outlining;
using System.Windows.Input;
using Microsoft.VisualStudio.Text;
namespace CollapseAllButThis
{
[Export(typeof(IKeyProcessorProvider))]
// Make the background goldenrod yellow :)
[Export(typeof(IWpfTextViewCreationListener))]
[ContentType("text")]
[TextViewRole(PredefinedTextViewRoles.Document)]
internal sealed class CreationListener : IWpfTextViewCreationListener
{
public void TextViewCreated(IWpfTextView textView, IEnvironment context)
{
textView.Background = Brushes.PaleGoldenrod;
// starting with a SnapshotPoint, text line, or SnapshotSpan
SnapshotPoint point;
ITextSnapshotLine line = point.GetContainingLine();
SnapshotSpan extent = line.Extent;
SnapshotPoint start;
for (start = extent.Start; start < line.End && char.IsWhitespace(start.GetChar()); start++);
SnapshotSpan extentExcludingLeadingWhitespace = new SnapshotPoint(start, extent.End);
[Export(typeof(ITaggerProvider))]
[TagType(typeof(SquiggleTag))] // this will probably be "IErrorTag" in the next release
[ContentType("text")] // "text" is the base of basically all content types
internal sealed class ToDoTaggerProvider : ITaggerProvider
{
// Note: the "context" arg has been removed, but is still there in Beta 1
public ITagger<T> GetTagger<T>(ITextBuffer buffer, IEnvironment context) where T : ITag
{
return new ToDoTagger() as ITagger<T>;
}
internal sealed class ToDoTagger : ITagger<SquiggleTag>
{
// Our simple tagger can ignore this for now
public Event<SnapshotSpanChangedArgs> TagsChanged;
public IEnumerable<ITagSpan<SquiggleTag>> GetTags(NormalizedSnapshotSpanCollection spans)
{
foreach(SnapshotSpan span in spans)
{
string spanText = span.GetText();
[Export(typeof(ITaggerProvider))]
[TagType(typeof(SquiggleTag))] // this will probably be "IErrorTag" in the next release
[ContentType("text")] // "text" is the base of basically all content types
internal sealed class ToDoTaggerProvider : ITaggerProvider
{
// Note: the "context" arg has been removed, but is still there in Beta 1
public ITagger<T> GetTagger<T>(ITextBuffer buffer, IEnvironment context) where T : ITag
{
return new ToDoTagger() as ITagger<T>;
}