Skip to content

Instantly share code, notes, and snippets.

@NLKNguyen
Last active April 26, 2016 02:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NLKNguyen/dba399724135b5c1dac4ade9d3910342 to your computer and use it in GitHub Desktop.
Save NLKNguyen/dba399724135b5c1dac4ade9d3910342 to your computer and use it in GitHub Desktop.
Visual Studio Extension Recipes

Given: SnapshotPoint snapshotPoint

Get: ITextSnapshot which the snapshotPoint refers to

ITextSnapshot snapshot = snapshotPoint.Value.Snapshot;

Given: SnapshotPoint snapshotPoint

Get: ITextSnapshotLine at the line that contains snapshotPoint

ITextSnapshotLine snapshotLine = snapshotPoint.GetContainingLine();

Given: ITextSnapshot snapshot

Get: ITextSnapshotLine at line number

ITextSnapshotLine snapshotLine = snapshot.GetLineFromLineNumber(42);

Given: ITextSnapshotLine snapshotLine

Get: SnapshotPoint of where the line starts and ends

SnapshotPoint startPoint = snapshotLine.Start; // file-wise index number - can be used as int
SnapshotPoint endPoint   = snapshotLine.End;

Given: a pair of SnapshotPoint

Get: SnapshotSpan between them

var span = new SnapshotSpan(startPoint,  endPoint);

Given: ITextBuffer buffer, IClassifierAggregatorService classifierAggregatorService - obtain by MEF import

[Import]
internal IClassifierAggregatorService classifierAggregatorService = null;

Get: IClassifier of the text buffer

IClassifier m_classifier = classifierAggregatorService.GetClassifier(buffer);

Given: SnapshotSpan targetSpan, IClassifier m_classifier

Get: list of ClassificationSpan - within targetSpan, classified by a TokenTagger

var classificationSpans = m_classifier.GetClassificationSpans(targetSpan);

Given: ClassificationSpan classificationSpan

Get: ClassificationType of it

var classificationType = classificationSpan.ClassificationType;

Given: ClassificationType classificationType

Get: Classification name OR

Check: whether it is some certain type

string typeName = classificationType.Classification;
bool isComment = classificationType.IsOfType("comment");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment