⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 indent.cs

📁 c#精彩编程百例(源代码)
💻 CS
字号:
//  Indent.cs 
//  Copyright (c) 2000 Mike Krueger
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

using System;
using System.Drawing;

using SharpDevelop.Tool.Data;

namespace SharpDevelop.Internal.Text {
	
	public class Indent
	{
		public static Point SearchBracketBackward(Point start, TextBuffer buffer, char openingBracket, char closingBracket)
		{
			int brackets = -1;
			for (int y = start.Y ; y >= 0; --y)
				for (int x = (y == start.Y) ? start.X - 1 : buffer[y].Text.Length - 1; x >= 0; --x) {
					char ch = buffer[y].Text[x];
					if (ch == closingBracket) {
						--brackets;				
					} else if (ch == openingBracket) {
						++brackets;
						if (brackets == 0) 			
							return new Point(x, y);
					}
				}
				return new Point(-1, -1);
		}
		
		public static Point SearchBracketForward(Point start, TextBuffer buffer, char openingBracket, char closingBracket)
		{
			int brackets = 1;
			for (int y = start.Y ; y < buffer.Length; ++y) 
				for (int x = (y == start.Y) ? start.X + 1 : 0; x < buffer[y].Text.Length; ++x) {
					char ch = buffer[y].Text[x];
					if (ch == openingBracket) {
						++brackets;
					} else if (ch == closingBracket) {
						--brackets;
						if (brackets == 0)
							return new Point(x, y);
					}
				}
				return new Point(-1, -1);
		}
		
		public static string GetIndent(TextBuffer buffer, Point pos)
		{
			string indent = "";
			for (int i = 0; i < buffer[pos.Y].Text.Length; ++i) {
				if (Char.IsWhiteSpace(buffer[pos.Y].Text[i]))
					indent += buffer[pos.Y].Text[i];
				else {
					for (int j = i; j < pos.X; ++j)
						if (buffer[pos.Y].Text[j] == '{')
							indent += ' ';
							break;
						}
				}
				return indent;
			}
			
			
			public static int IndentLine(TextBuffer buffer, int line, IndentStyle indentstyle)
			{
				if (indentstyle == IndentStyle.None || line >= buffer.Length || line < 0)
					return 0;
				bool   isSpan    = line > 0  ? buffer[line - 1].Span : false;
				string before2   = line <= 1  ? "" : buffer[line - 2].Text;
				string before    = line <= 0  ? "" : buffer[line - 1].Text;
				string current   = buffer[line].Text;
				string indent    = "";
				int bracketsBefore   = 0;
				bool closingBracket  = false;
				bool openingBracket  = false;
				bool semicolon2      = before2.Length <= 1 ? true : before2[before2.Length - 1] == ';';
				bool semicolon       = before.Length  <= 0 ? true : before[before.Length - 1]   == ';';
				bool bracket2        = before2.Length <= 1 ? true : before2[before2.Length - 1] == '}' || before2[before2.Length - 1] == '{' || before2[before2.Length - 1] == ',' || before2[before2.Length - 1] == ']';
				bool bracket         = before.Length  <= 0 ? true : before[before.Length - 1]   == '}' || before[before.Length - 1] == '{'|| before[before.Length - 1] == ',' || before[before.Length - 1] == ']';
				bool whitespacesonly = true;
				
				for (int i = 0; i < before.Length; ++i) {
					whitespacesonly &= Char.IsWhiteSpace(before[i]);
					switch (before[i]) {
						case ' ':
							if (whitespacesonly) {
								indent += ' ';
							}
							break;
							case '\t':
								if (whitespacesonly) {
									indent += '\t';
								}
								break;
								case '}':
									bracketsBefore = Math.Max(0, bracketsBefore - 1);
							break;
						case '{':
							++bracketsBefore;
							break;
					}
				}
				Point closingBracketPoint = new Point(-1, -1);
				
				if (indentstyle == IndentStyle.Smart) {
					
					for (int i = 0; i < current.Length; ++i) {
						if (!Char.IsWhiteSpace(current[i])) {
							openingBracket = current[i] == '{';
								closingBracket = current[i] == '}';
								if (closingBracket) {
									closingBracketPoint = SearchBracketBackward(new Point(i, line), buffer, '{', '}');
								}
								break;
						}
					}
					
					if (!isSpan && bracketsBefore == 0 && !(semicolon || bracket) && !openingBracket && !whitespacesonly) 
						indent += '\t';
					
					while (bracketsBefore-- > 0) {
						indent += '\t';
					}
					
					if (!(semicolon2 || bracket2) && semicolon && indent.Length > 0)  {
						Point point = SearchBracketBackward(new Point(buffer[line - 1].Text.Length - 1, line - 1), buffer, '{', '}');
						if (point.Y >= 0) {
							indent = GetIndent(buffer, point) + "\t";
						} else
							indent = "";
					}
					
					if (closingBracket && indent.Length > 0 && closingBracketPoint.Y >= 0)
						indent = GetIndent(buffer, closingBracketPoint);
					}
					
					string str = buffer[line].Text.TrimStart(new char[] {' ', '\t' });
					buffer.Delete(new Point(0, line), new Point(buffer[line].Text.Length - str.Length, line));
					buffer.Insert(new Point(0, line), indent);
					
					buffer.UndoStack.UndoLast(2);
					
					//	  buffer[line].Text = indent + buffer[line].Text.TrimStart(new char[] {' ', '\t' });
					return indent.Length;
				}
				
				public static void IndentLines(TextBuffer buffer, int begin, int end, IndentStyle indentstyle)
				{
					buffer.BeginUpdate();
					for (int i = begin; i <= end; ++i)
						IndentLine(buffer, i, indentstyle);
					buffer.UndoStack.UndoLast(end - begin + 1);
					buffer.EndUpdate();
				}
		}
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -