private void UpdateClassifications(CaretPosition caretPosition) { var bufferPosition = caretPosition.BufferPosition; var snapshot = bufferPosition.Snapshot; var position = bufferPosition.Position; var oldMatchingBraces = _matchingBraces; if (HasBraceAtPosition(snapshot, position)) { MatchingBraces newMatchingBraces = null; ClassificationParseResult parseResult; if (ParseUtil.TryParse(_textBuffer, out parseResult) && snapshot.Version == parseResult.Snapshot.Version) { SearchMatchingBraces(parseResult.Tokens, parseResult.Snapshot, position, ref newMatchingBraces); } _matchingBraces = newMatchingBraces; if (null != oldMatchingBraces) { NotifyTagsChanged(oldMatchingBraces); } if (null != newMatchingBraces) { NotifyTagsChanged(newMatchingBraces); } } else if (oldMatchingBraces != null) { _matchingBraces = null; NotifyTagsChanged(oldMatchingBraces); } }
private static void AddMatchingBraces(ITextSnapshot snapshot, int openBrace, int closeBrace, ref MatchingBraces matchingBraces) { if (null == matchingBraces) { matchingBraces = new MatchingBraces(snapshot, new List <Span>(4)); } matchingBraces.Item2.Add(new Span(openBrace, 1)); matchingBraces.Item2.Add(new Span(closeBrace, 1)); }
private void NotifyTagsChanged(MatchingBraces matchingBraces) { var handler = TagsChanged; if (handler != null) { foreach (var x in matchingBraces.Item2) { handler(this, new SnapshotSpanEventArgs(new SnapshotSpan(matchingBraces.Item1, x))); } } }
private static void TryAddMatchingBraces(ITextSnapshot snapshot, int caretPos, ref MatchingBraces matchingBraces, Span tokenSpan, object closeBrace) { if (IsMatchingBraces(caretPos, tokenSpan, closeBrace)) { AddMatchingBraces(snapshot, tokenSpan.Start, tokenSpan.End - 1, ref matchingBraces); } }
private static void SearchMatchingBraces(Token token, ITextSnapshot snapshot, int caretPos, ref MatchingBraces matchingBraces) { for (; token != null; token = token.Next) { var tokenSpan = Utils.NLocationToSpan(snapshot, token.Location); if (tokenSpan.End < caretPos) { continue; } if (caretPos < tokenSpan.Start) { break; } if (token is Token.BracesGroup) { var groupToken = (Token.BracesGroup)token; TryAddMatchingBraces(snapshot, caretPos, ref matchingBraces, tokenSpan, groupToken.CloseBrace); SearchMatchingBraces(groupToken.Child, snapshot, caretPos, ref matchingBraces); } else if (token is Token.RoundGroup) { var groupToken = (Token.RoundGroup)token; TryAddMatchingBraces(snapshot, caretPos, ref matchingBraces, tokenSpan, groupToken.CloseBrace); SearchMatchingBraces(groupToken.Child, snapshot, caretPos, ref matchingBraces); } else if (token is Token.SquareGroup) { var groupToken = (Token.SquareGroup)token; TryAddMatchingBraces(snapshot, caretPos, ref matchingBraces, tokenSpan, groupToken.CloseBrace); SearchMatchingBraces(groupToken.Child, snapshot, caretPos, ref matchingBraces); } else if (token is Token.LooseGroup) { var groupToken = (Token.LooseGroup)token; SearchMatchingBraces(groupToken.Child, snapshot, caretPos, ref matchingBraces); } else if (token is Token.QuoteGroup) { var groupToken = (Token.QuoteGroup)token; SearchMatchingBraces(groupToken.Child, snapshot, caretPos, ref matchingBraces); } else if (token is Token.Namespace) { var nsToken = (Token.Namespace)token; SearchMatchingBraces(nsToken.KeywordToken, snapshot, caretPos, ref matchingBraces); SearchMatchingBraces(nsToken.Body, snapshot, caretPos, ref matchingBraces); } else if (token is Token.Using) { var nsToken = (Token.Using)token; SearchMatchingBraces(nsToken.KeywordToken, snapshot, caretPos, ref matchingBraces); SearchMatchingBraces(nsToken.Body, snapshot, caretPos, ref matchingBraces); } } }