コード例 #1
0
 public void Initialize()
 {
     _tokenizer = new Tokenizer();
     _tokenizedBufferEntity = new Entity<LinkedList<Token>>();
     _clojureSmartIndent = new ClojureSmartIndent(_tokenizedBufferEntity);
     _defaultOptions = new EditorOptions(4);
 }
コード例 #2
0
ファイル: AutoFormatter.cs プロジェクト: rpete4130/vsClojure
		public void Format(EditorOptions editorOptions)
		{
			StringBuilder output = new StringBuilder();
			_currentToken = _tokenizedBuffer.CurrentState.First;
			_dataStructureStack = new Stack<Token>();
			_indentStack = new Stack<int>();

			while (_currentToken != null)
			{
				string tokenText = _currentToken.Value.Text;
				if (_currentToken.Value.Type.IsBraceStart()) _dataStructureStack.Push(_currentToken.Value);
				if (_currentToken.Value.Type.IsBraceEnd() && _dataStructureStack.Count > 0 && _dataStructureStack.Peek().Type.MatchingBraceType() == _currentToken.Value.Type) _dataStructureStack.Pop();

				if (_currentToken.Value.Type == TokenType.Whitespace)
				{
					bool moreThanOneLineBreak = tokenText.Replace(" ", "").Contains("\r\n\r\n");
					bool hasAtLeastOneLineBreak = tokenText.Replace(" ", "").Contains("\r\n");

					if (_currentToken.Previous == null) tokenText = "";
					else if (_currentToken.Next == null) tokenText = "";
					else if (_currentToken.Next.Value.Type == TokenType.Comment && !tokenText.Contains("\r\n")) tokenText = " ";
					else if (_dataStructureStack.Count == 0 && IsPreviousTokenACommentOnTheSameLineAsExpression()) tokenText = "\r\n\r\n";
					else if (_dataStructureStack.Count == 0 && _currentToken.Previous.Value.Type != TokenType.Comment) tokenText = "\r\n\r\n";
					else if (_dataStructureStack.Count == 0) tokenText = moreThanOneLineBreak ? "\r\n\r\n" : hasAtLeastOneLineBreak ? "\r\n" : "";
					else if (_currentToken.Next.Value.Type == TokenType.Comment && !tokenText.EndsWith(" ")) tokenText = "\r\n";
					else if (tokenText.Contains("\r\n")) tokenText = "\r\n" + GetIndent(editorOptions);
					else if (_currentToken.Next.Value.Type.IsBraceEnd()) tokenText = "";
					else if (_currentToken.Previous.Value.Type.IsBraceStart()) tokenText = "";
					else tokenText = " ";
				}
				else if (_currentToken.Next != null && _currentToken.Next.Value.Type != TokenType.Whitespace)
				{
					if (_currentToken.Next.Value.Type == TokenType.Comment) tokenText += " ";
					else if (_dataStructureStack.Count == 0) tokenText += "\r\n\r\n";
					else if (!_currentToken.Value.Type.IsBrace() && !_currentToken.Next.Value.Type.IsBrace()) tokenText += " ";	
				}

				if (tokenText.Contains("\r\n")) _currentLineIndex = tokenText.Count(' ');
				else _currentLineIndex += tokenText.Length;

				if (_currentToken.Value.Type.IsBraceStart()) _indentStack.Push(_currentLineIndex - tokenText.Count(' '));
				else if (_indentStack.Count > _dataStructureStack.Count) _indentStack.Pop();

				_currentToken = _currentToken.Next;
				output.Append(tokenText);
			}

			_textBuffer.SetText(output.ToString());
		}
コード例 #3
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("\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;
        }
コード例 #4
0
ファイル: AutoFormatter.cs プロジェクト: rpete4130/vsClojure
		private string GetIndent(EditorOptions editorOptions)
		{
			if (_dataStructureStack.Count == 0) return "";
			if (_dataStructureStack.Peek().Type == TokenType.ListStart) return " ".Repeat(editorOptions.IndentSize + _indentStack.Peek() - 1);
			return " ".Repeat(_indentStack.Peek());
		}