Skip to content

Commit a5da977

Browse files
Merge pull request #271 from X-Sharp/master
Made some methods protected virtual so a subclass can returns its own…
2 parents 8639b04 + 968b6db commit a5da977

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

src/toolkit/Community.VisualStudio.Toolkit.Shared/MEF/BraceCompletionBase.cs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public bool TryCreateContext(ITextView textView, SnapshotPoint openingPoint, cha
2020
{
2121
if (IsValidBraceCompletionContext(openingPoint))
2222
{
23-
context = new DefaultBraceCompletionContext();
23+
context = GetCompletionContext();
2424
return true;
2525
}
2626
else
@@ -29,14 +29,30 @@ public bool TryCreateContext(ITextView textView, SnapshotPoint openingPoint, cha
2929
return false;
3030
}
3131
}
32+
/// <summary>
33+
/// Return object that implements the CompletionContext that you want to use
34+
/// </summary>
35+
/// <returns>an object of type IBraceCompletionContext</returns>
36+
/// <remarks>The default implementation returns an object that allows 'everything'</remarks>
37+
protected virtual IBraceCompletionContext GetCompletionContext()
38+
{
39+
return new DefaultBraceCompletionContext();
40+
}
3241

33-
private bool IsValidBraceCompletionContext(SnapshotPoint openingPoint)
42+
/// <summary>
43+
/// Determine if brace completion should be active in this context
44+
/// </summary>
45+
/// <param name="openingPoint">Point where the brace completion is triggered</param>
46+
/// <remarks>You can use this method to disable brace completion in comments or inside literal strings
47+
/// The default behavior is to disable completion inside comments and literal strings, which is determined
48+
/// by looking at the
49+
/// </remarks>
50+
/// <returns>true when completion should be allowed</returns>
51+
protected virtual bool IsValidBraceCompletionContext(SnapshotPoint openingPoint)
3452
{
35-
if (openingPoint.Position > 0 && _classifierService != null)
53+
IList<ClassificationSpan>? classificationSpans = GetSpans(openingPoint);
54+
if (classificationSpans != null)
3655
{
37-
IList<ClassificationSpan> classificationSpans = _classifierService.GetClassifier(openingPoint.Snapshot.TextBuffer)
38-
.GetClassificationSpans(new SnapshotSpan(openingPoint - 1, 1));
39-
4056
foreach (ClassificationSpan span in classificationSpans)
4157
{
4258
if (span.ClassificationType.IsOfType(PredefinedClassificationTypeNames.Comment))
@@ -51,6 +67,21 @@ private bool IsValidBraceCompletionContext(SnapshotPoint openingPoint)
5167
}
5268
return true;
5369
}
70+
/// <summary>
71+
/// Return a list of Classification spans for the point in the snapshot
72+
/// </summary>
73+
/// <param name="point">Point for which the list should be returned</param>
74+
/// <returns>A List of null when the position is invalid or when the classifier service is not available</returns>
75+
protected IList<ClassificationSpan>? GetSpans(SnapshotPoint point)
76+
{
77+
if (point.Position > 0 && _classifierService != null)
78+
{
79+
IList<ClassificationSpan> classificationSpans = _classifierService.GetClassifier(point.Snapshot.TextBuffer)
80+
.GetClassificationSpans(new SnapshotSpan(point - 1, 1));
81+
return classificationSpans;
82+
}
83+
return null;
84+
}
5485
}
5586

5687
internal class DefaultBraceCompletionContext : IBraceCompletionContext

0 commit comments

Comments
 (0)