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

📄 textbuffer.cs

📁 c#精彩编程百例(源代码)
💻 CS
📖 第 1 页 / 共 2 页
字号:
//  TextBuffer.cs
//  Copyright (C) 2000 Mike Krueger
//  Copyright (C) 2000 Andrea Paatz
//
//  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 System.Diagnostics;
using System.Collections;
using System.IO;
using System.Windows.Forms;

using SharpDevelop.Tool.Data;
using SharpDevelop.Internal.Undo;
using SharpDevelop.Gui.Edit; // for the ProgressEventHandler

namespace SharpDevelop.Internal.Text {
	
	public class Fold
	{
		public int Start;
		public int Length;
		
		public Fold(int start, int length)
		{
			this.Start  = start;
			this.Length = length;
		}
	}
	
	public class Foldings
	{
		TextBuffer buffer;
		public SortedList   foldings = new SortedList();
		
		public Foldings(TextBuffer buffer)
		{
			this.buffer = buffer;
			buffer.MoveIndices += new MoveIndicesHandler(MoveIndices);
		}
		
		public void MoveIndices(int index, int count)
		{
			SortedList newfoldings = new SortedList();
			bool redraw = false;
			foreach (DictionaryEntry folding in foldings) {
				int foldat = (int)folding.Key;
				Console.WriteLine("index " + index + " count " + count + " foldat " + foldat);
				if (count < 0 && foldat >= index + count && foldat < index) {// delete cleared folding;
					Console.WriteLine("ShouldRedraw");
					redraw = true;
					continue;
				}
				if (foldat >= index) {
					foldat += count;
				}
				newfoldings[foldat] = folding.Value;
			}
			buffer.UpdateRequested |= redraw;
			foldings = newfoldings;
		}
		
		public int  GetPhysicalLine(int linenumber)
		{
			int maxline = 0;
			foreach (DictionaryEntry folding in foldings) {
				if (folding.Value == null)
					continue;
				int foldat = (int)folding.Key;
				int length = (int)folding.Value;
				if (foldat <= linenumber && maxline <= foldat + length) {
					maxline = foldat + length;
					linenumber += length;
				} else
					break;
			}
			return linenumber;
		}
		
		public int  GetLogicalLine(int linenumber)
		{
			int folded = 0;
			int maxline = 0;
			foreach (DictionaryEntry folding in foldings) {
				if (folding.Value == null)
					continue;
				int foldat = (int)folding.Key;
				int length = (int)folding.Value;
				if (foldat <= linenumber && maxline <= foldat + length) {
					maxline = foldat + length;
					folded += length;
				} else
					break;
			}
			return linenumber - folded;
		}
		
		public void FoldPos(Point start)
		{
			int y1 = -1;
			int y2 = -1;
			
			int foldlevel = 1;
			for (int i = start.Y;  i >= 0; --i) {
				if (buffer[i].foldoff)
					++foldlevel;
				if (buffer[i].foldon) {
					--foldlevel;
					if (foldlevel <= 0) {
						y1 = i;
						break;
					}
				}					
			}
			
			foldlevel = 1;
			if (buffer[start.Y].foldon)
				foldlevel--;
			for (int i = start.Y;  i < buffer.Length; ++i) {
				if (buffer[i].foldon) 
					++foldlevel;
				if (buffer[i].foldoff) {
					--foldlevel;
					if (foldlevel <= 0) {
						y2 = i;
						break;
					}
				}
			}
			if (y1 == -1 || y2 == -1)
				return;
			buffer.Foldings.Fold(y1 + 1, y2 - y1 - 1);
		}
		
		public void Fold(int start, int length)
		{
			if (foldings[start] == null) {
				foldings[start] = length;
				for (int i = 0; i < length; ++i) {
					buffer[start + i].Visible = false;
				}
			}
		}
		
		public void UnFold(int start)
		{
			if (foldings[start] != null) {
				int length = (int)foldings[start];
				buffer[start].Visible = true;
				for (int i = 1; i < length; ++i) {
					if (foldings[start + i] != null) // skip inner folding
						i += (int)foldings[start + i];
					buffer[start + i].Visible = true;
				}
				foldings[start] = null;
			}
		}
		public void UnFoldAll()
		{
			foldings.Clear();
			for (int i = 0; i < buffer.Length; ++i)
				buffer[i].Visible = true;
		}
	}
	
	public delegate void MoveIndicesHandler(int index, int count);
	/// <summary>
	/// This class represents a Text. The text is in the container MyArrayList line
	/// it is divided up in TextLines and TextLines provide the string as a line or 
	/// as divided up into words, which contain syntax highlighting information.
	/// </summary>
	public class TextBuffer
	{
		Syntax    syntax     = new Syntax();
		UndoStack undostack  = null;
		
		MyArrayList line     = null;
		
		Bookmark    bookmark;
		bool updaterequested = false;
		bool onlyreadable    = false;
		bool updated         = false;
		
		BufferOptions options = (BufferOptions)((ICloneable)Option.GetProperty("SharpDevelop.Internal.Text.TextBuffer.BufferOptions", new BufferOptions())).Clone();
		
		public Foldings Foldings = null;
		
		public BufferOptions Options {
			get {
				return options;
			}
		}
		
		public event MoveIndicesHandler   MoveIndices;
		public event ProgressEventHandler Progress;
		public event EventHandler         Changed;
		public event EventHandler         FoldChanged;
		public event EventHandler         LineCountChanged;
		
		public UndoStack UndoStack {
			get {
				return undostack;
			}
		}
		public bool ReadOnly {
			get {
				return onlyreadable;
			}
			set {
				onlyreadable = value;
			}
		}
		
		public Syntax Syntax {
			get {
				return syntax;
			}
		}
		
		public Bookmark Bookmark {
			get {
				return bookmark;
			}
		}
		
		public bool UpdateRequested {
			get {
				return updaterequested;
			}
			set {
				updaterequested= value;
			}
		}
		
		public string Text {
			get {
				string back = "";
				for (int i = 0; i < line.Count - 1; ++i)
					back += ((TextLine)line[i]).Text + "\n";
				back += ((TextLine)line[line.Count - 1]).Text;
				return back;
			}
			set {
				line            = new MyArrayList(this);
				string[] lines  = value.Split(new char[] {'\n'});
				for (int i = 0; i < lines.Length; ++i)
					line.Add(new TextLine(this, i, lines[i]));
			}
		}
		
		public ArrayList Line {
			get {
				return line;
			}
		}
		
		public int Length {
			get {
				return line.Count;
			}
		}
		
		public TextLine this[int i] {
			get {
				if (line.Count == 0)
					throw new ArgumentException("TextBuffer public TextLine this[int i] : Buffer is empty");
				return (TextLine)line[i];
			}
		}
		
		public TextBuffer()
		{
			undostack = new UndoStack();
			bookmark = new Bookmark(this);
			Foldings = new Foldings(this);
			line = new MyArrayList(this);
			line.Add(new TextLine(this, 0, ""));
			
			SetDefaultHighlighting();
			
			OnChanged();
		}
		
		void Reparse()
		{
			for (int i = 0; i < this.Length; ++i) 
				this[i].Text = this[i].Text;
		}
		
		public void BeginUpdate()
		{
			updated = true;
		}
		public void EndUpdate()
		{
			updated = false;
		}

⌨️ 快捷键说明

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