public MatchingBracePair FindMatchingBraces(LinkedList<Token> buffer, int totalBufferLength, int caretPosition) { IndexTokenNode tokenNode = buffer.FindTokenAtIndex(caretPosition); if (tokenNode == null) return new MatchingBracePair(null, null); IndexTokenNode previousNode = tokenNode.Previous(); if (totalBufferLength == caretPosition && tokenNode.IndexToken.Token.Type.IsBraceEnd()) { return FindMatchingBracePair(tokenNode); } if (previousNode != null && previousNode.IndexToken.Token.Type.IsBraceEnd() && caretPosition == tokenNode.IndexToken.StartIndex) { return FindMatchingBracePair(previousNode); } if (tokenNode.IndexToken.Token.Type.IsBraceStart()) return FindMatchingBracePair(tokenNode); return new MatchingBracePair(null, null); }
public int GetDesiredIndentation(LinkedList<Token> buffer, int position, int indentAmount) { if (buffer.Count == 0) return 0; IndexTokenNode currentToken = buffer.FindTokenAtIndex(position); currentToken = currentToken.Previous(); IndexTokenNode firstOpenBrace = null; int braceCount = 0; while (currentToken != null && firstOpenBrace == null) { if (currentToken.IndexToken.Token.Type.IsBraceEnd()) braceCount--; if (currentToken.IndexToken.Token.Type.IsBraceStart()) braceCount++; if (braceCount == 1) firstOpenBrace = currentToken; else currentToken = currentToken.Previous(); } if (firstOpenBrace == null) return 0; int previousLineLength = 0; IndexTokenNode startOfLine = firstOpenBrace.Previous(); while (startOfLine != null && (startOfLine.IndexToken.Token.Type != TokenType.Whitespace || (startOfLine.IndexToken.Token.Type == TokenType.Whitespace && !startOfLine.IndexToken.Token.Text.Contains("\n")))) { previousLineLength += startOfLine.IndexToken.Token.Length; startOfLine = startOfLine.Previous(); } int previousIndentAmount = 0; if (startOfLine != null) { string startOfLineText = startOfLine.Node.Value.Text; string lineWhitespaceWithoutIndent = startOfLineText.TrimEnd(new[] {' '}); previousIndentAmount = startOfLineText.Length - lineWhitespaceWithoutIndent.Length; } if (firstOpenBrace.Node.Value.Type == TokenType.ListStart) return previousIndentAmount + previousLineLength + indentAmount; return previousIndentAmount + previousLineLength + 1; }