示例#1
0
        private static IndexToken ScanForMatchingBrace(IndexTokenNode tokenNode)
        {
            TokenType      matchingType   = tokenNode.IndexToken.Token.Type.MatchingBraceType();
            IndexTokenNode candidateToken = tokenNode;
            bool           matchForward   = tokenNode.IndexToken.Token.Type.IsBraceStart();
            int            braceCounter   = 0;

            while (candidateToken != null)
            {
                if (candidateToken.IndexToken.Token.Type == matchingType)
                {
                    braceCounter--;
                }
                if (candidateToken.IndexToken.Token.Type == tokenNode.IndexToken.Token.Type)
                {
                    braceCounter++;
                }
                if (braceCounter == 0)
                {
                    return(candidateToken.IndexToken);
                }
                candidateToken = matchForward ? candidateToken.Next() : candidateToken.Previous();
            }

            return(null);
        }
示例#2
0
        public MatchingBracePair FindMatchingBraces(int caretPosition)
        {
            IndexTokenNode tokenNode = _tokenizedBuffer.CurrentState.FindTokenAtIndex(caretPosition);

            if (tokenNode == null)
            {
                return(new MatchingBracePair(null, null));
            }
            IndexTokenNode previousNode = tokenNode.Previous();

            if (_textBuffer.Length == 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));
        }
示例#3
0
        private static MatchingBracePair FindMatchingBracePair(IndexTokenNode tokenNode)
        {
            IndexToken matchingBrace = ScanForMatchingBrace(tokenNode);

            if (matchingBrace == null)
            {
                return(new MatchingBracePair(tokenNode.IndexToken, null));
            }
            return(new MatchingBracePair(tokenNode.IndexToken, matchingBrace));
        }
        private void ModifyTokensInBuffer(TextChangeData change)
        {
            IndexTokenNode firstToken                      = _tokenizedBuffer.CurrentState.FindTokenBeforeIndex(change.Position);
            Lexer          lexer                           = new Lexer(new PushBackCharacterStream(new StringReader(_textBuffer.GetText(firstToken.IndexToken.StartIndex))));
            int            oldBufferStartIndex             = firstToken.IndexToken.StartIndex + change.Delta;
            int            newBufferStartIndex             = firstToken.IndexToken.StartIndex;
            int            endPosition                     = change.Position + change.LengthOfChangedText;
            LinkedList <LinkedListNode <Token> > oldTokens = new LinkedList <LinkedListNode <Token> >();
            LinkedList <IndexToken> newTokens              = new LinkedList <IndexToken>();
            Token          newToken                        = lexer.Next();
            IndexTokenNode oldToken                        = firstToken;

            while (newBufferStartIndex + newToken.Length != oldBufferStartIndex + oldToken.IndexToken.Token.Length ||
                   (change.Delta < 0 && oldToken.IndexToken.StartIndex + oldToken.IndexToken.Token.Length < endPosition) ||
                   (change.Delta > 0 && newBufferStartIndex + newToken.Length < endPosition))
            {
                if (newBufferStartIndex + newToken.Length < oldBufferStartIndex + oldToken.IndexToken.Token.Length)
                {
                    newTokens.AddLast(new IndexToken(newBufferStartIndex, newToken));
                    newBufferStartIndex += newToken.Length;
                    newToken             = lexer.Next();
                }
                else
                {
                    oldTokens.AddLast(oldToken.Node);
                    oldBufferStartIndex += oldToken.IndexToken.Token.Length;
                    oldToken             = oldToken.Next();
                }
            }

            oldTokens.AddLast(oldToken.Node);
            newTokens.AddLast(new IndexToken(newBufferStartIndex, newToken));
            foreach (var t in newTokens)
            {
                _tokenizedBuffer.CurrentState.AddBefore(firstToken.Node, t.Token);
            }
            foreach (var t in oldTokens)
            {
                _tokenizedBuffer.CurrentState.Remove(t);
            }
            foreach (var t in newTokens)
            {
                TokenChanged(this, new TokenChangedEventArgs(t));
            }
        }
示例#5
0
        public int GetDesiredIndentation(int position, EditorOptions options)
        {
            if (_tokenizedBuffer.CurrentState.Count == 0)
            {
                return(0);
            }
            IndexTokenNode currentToken = _tokenizedBuffer.CurrentState.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("\r\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 + options.IndentSize);
            }
            return(previousIndentAmount + previousLineLength + 1);
        }