private bool HighlightBrace(BraceKind brace, SnapshotPoint position, int direction) { var buffer = _editorServices.GetBufferInfo(position.Snapshot.TextBuffer); if (buffer == null) { return(false); } var snapshot = position.Snapshot; var span = new SnapshotSpan(snapshot, position.Position - 1, 1); var originalSpan = span; if (!(buffer.GetTokenAtPoint(position)?.Trigger ?? Parsing.TokenTriggers.None).HasFlag(Parsing.TokenTriggers.MatchBraces)) { return(false); } int depth = 0; foreach (var token in (direction > 0 ? buffer.GetTokensForwardFromPoint(position) : buffer.GetTokensInReverseFromPoint(position))) { if (!token.Trigger.HasFlag(Parsing.TokenTriggers.MatchBraces)) { continue; } var tspan = token.ToSnapshotSpan(snapshot); var txt = tspan.GetText(); try { if (IsSameBraceKind(txt, brace)) { if (txt.IsCloseGrouping()) { depth -= direction; } else { depth += direction; } } } catch (InvalidOperationException) { return(false); } if (depth == 0) { RemoveExistingHighlights(); _markedBuffer = snapshot.TextBuffer; // left brace GetTextMarker(snapshot.TextBuffer).CreateTagSpan(snapshot.CreateTrackingSpan(tspan, SpanTrackingMode.EdgeExclusive), _tag); // right brace GetTextMarker(snapshot.TextBuffer).CreateTagSpan(snapshot.CreateTrackingSpan(new SnapshotSpan(snapshot, position - 1, 1), SpanTrackingMode.EdgeExclusive), _tag); return(true); } } return(false); }
private bool FindMatchingPair(BraceKind brace, SnapshotPoint position, int direction, out SnapshotSpan leftSpan, out SnapshotSpan rightSpan) { leftSpan = new SnapshotSpan(position, position); rightSpan = leftSpan; var buffer = PythonTextBufferInfo.ForBuffer(_site, position.Snapshot.TextBuffer); if (buffer == null) { return(false); } if (!(buffer.GetTokenAtPoint(position)?.Trigger ?? TokenTriggers.None).HasFlag(TokenTriggers.MatchBraces)) { return(false); } var snapshot = position.Snapshot; int depth = 0; foreach (var token in (direction > 0 ? buffer.GetTokensForwardFromPoint(position) : buffer.GetTokensInReverseFromPoint(position - 1))) { if (!token.Trigger.HasFlag(TokenTriggers.MatchBraces)) { continue; } var tokenSpan = token.ToSnapshotSpan(snapshot); var txt = tokenSpan.GetText(); var kind = GetBraceKind(txt); if (kind == BraceKind.None) { return(false); } if (kind == brace) { if (txt.IsCloseGrouping()) { depth -= direction; } else { depth += direction; } } if (depth == 0) { leftSpan = tokenSpan; rightSpan = new SnapshotSpan(snapshot, position - 1, 1); return(true); } } return(false); }
private bool HighlightBrace(BraceKind brace, int position, int direction) { var pt = _textView.BufferGraph.MapDownToInsertionPoint( new SnapshotPoint(_textView.TextBuffer.CurrentSnapshot, position), PointTrackingMode.Positive, EditorExtensions.IsPythonContent ); if (pt == null) { return(false); } return(HighlightBrace(brace, pt.Value, direction)); }
private static bool IsSameBraceKind(string brace, BraceKind kind) { return(GetBraceKind(brace) == kind); }
private bool HighlightBrace(BraceKind brace, SnapshotPoint position, int direction) { var classifier = position.Snapshot.TextBuffer.GetPythonClassifier(); if (classifier != null) { var snapshot = position.Snapshot; var span = new SnapshotSpan(snapshot, position.Position - 1, 1); var originalSpan = span; var spans = classifier.GetClassificationSpans(span); // we don't highlight braces if we're in a comment or string literal if (spans.Count == 0 || (spans.Count == 1 && (!spans[0].ClassificationType.IsOfType(PredefinedClassificationTypeNames.String) && !spans[0].ClassificationType.IsOfType(PredefinedClassificationTypeNames.Comment)))) { // find the opening span var curLine = snapshot.GetLineFromPosition(position); int curLineNo = curLine.LineNumber; if (direction == 1) { span = new SnapshotSpan(snapshot, position, curLine.End.Position - position); } else { span = new SnapshotSpan(curLine.Start, position - 1); } int depth = 1; for (; ;) { spans = classifier.GetClassificationSpans(span); for (int i = direction == -1 ? spans.Count - 1 : 0; i >= 0 && i < spans.Count; i += direction) { if (spans[i].IsCloseGrouping()) { if (IsSameBraceKind(spans[i].Span.GetText(), brace)) { depth -= direction; } } else if (spans[i].IsOpenGrouping()) { if (IsSameBraceKind(spans[i].Span.GetText(), brace)) { depth += direction; } } if (depth == 0) { RemoveExistingHighlights(); _markedBuffer = snapshot.TextBuffer; // left brace GetTextMarker(snapshot.TextBuffer).CreateTagSpan(snapshot.CreateTrackingSpan(spans[i].Span, SpanTrackingMode.EdgeExclusive), _tag); // right brace GetTextMarker(snapshot.TextBuffer).CreateTagSpan(snapshot.CreateTrackingSpan(new SnapshotSpan(snapshot, position - 1, 1), SpanTrackingMode.EdgeExclusive), _tag); return(true); } } curLineNo += direction; if (curLineNo < 0 || curLineNo >= snapshot.LineCount) { break; } var line = snapshot.GetLineFromLineNumber(curLineNo); span = new SnapshotSpan(line.Start, line.End); } } } return(false); }
private bool HighlightBrace(BraceKind brace, int position, int direction) { var classifier = _textView.TextBuffer.GetDlrClassifier(); var snapshot = _textView.TextBuffer.CurrentSnapshot; var span = new SnapshotSpan(snapshot, position - 1, 1); var originalSpan = span; var spans = classifier.GetClassificationSpans(span); // we don't highlight braces if we're in a comment or string literal if (spans.Count == 0 || (spans.Count == 1 && (!spans[0].ClassificationType.IsOfType(PredefinedClassificationTypeNames.String) && !spans[0].ClassificationType.IsOfType(PredefinedClassificationTypeNames.Comment)))) { // find the opening span var curLine = snapshot.GetLineFromPosition(position); int curLineNo = curLine.LineNumber; if (direction == 1) { span = new SnapshotSpan(snapshot, position, curLine.End.Position - position); } else { span = new SnapshotSpan(curLine.Start, position - curLine.Start.Position + direction); } int depth = 1; for (; ; ) { spans = classifier.GetClassificationSpans(span); for (int i = direction == -1 ? spans.Count - 1 : 0; i >= 0 && i < spans.Count; i += direction) { if (IsCloseSpan(spans, i)) { if (IsSameBraceKind(spans[i].Span.GetText(), brace)) { depth -= direction; } } else if (IsOpenSpan(spans, i)) { if (IsSameBraceKind(spans[i].Span.GetText(), brace)) { depth += direction; } } if (depth == 0) { _hasSpan = true; // left brace TextMarker.CreateTagSpan(snapshot.CreateTrackingSpan(spans[i].Span, SpanTrackingMode.EdgeExclusive), _tag); // right brace TextMarker.CreateTagSpan(snapshot.CreateTrackingSpan(new SnapshotSpan(snapshot, position - 1, 1), SpanTrackingMode.EdgeExclusive), _tag); return true; } } curLineNo += direction; if (curLineNo < 0 || curLineNo >= snapshot.LineCount) { break; } var line = snapshot.GetLineFromLineNumber(curLineNo); span = new SnapshotSpan(line.Start, line.End); } } return false; }
private static bool IsSameBraceKind(string brace, BraceKind kind) { return GetBraceKind(brace) == kind; }
private bool HighlightBrace(BraceKind brace, int position, int direction) { var pt = _textView.BufferGraph.MapDownToInsertionPoint( new SnapshotPoint(_textView.TextBuffer.CurrentSnapshot, position), PointTrackingMode.Positive, EditorExtensions.IsPythonContent ); if (pt == null) { return false; } return HighlightBrace(brace, pt.Value, direction); }