📄 textbuffer.cs
字号:
protected virtual void OnChanged()
{
if (!updated && Changed != null) {
Changed(this, null);
}
}
public void OnMoveIndices(int index, int count)
{
if (MoveIndices != null)
MoveIndices(index, count);
}
void OnLineCountChanged()
{
if (LineCountChanged != null) {
LineCountChanged(this, null);
}
}
protected virtual void OnFoldEvent()
{
if (!updated && FoldChanged != null)
FoldChanged(this, null);
}
public void Load(string filename)
{
line = new MyArrayList(this);
StreamReader stream = File.OpenText(filename);
int linenumber = 0;
int lastpercent = -1;
while (true) {
string curLine = stream.ReadLine();
int percent = (int)((stream.BaseStream.Position * 100) / (stream.BaseStream.Length + 1));
if (Progress != null && percent != lastpercent) {
Progress(this, percent);
lastpercent = percent;
}
if (curLine == null) {
if (linenumber == 0)
line.Add(new TextLine(this, ++linenumber, ""));
break;
}
line.Add(new TextLine(this, ++linenumber, curLine));
}
if (line.Count == 0) // file empty, insert a blank line
line.Add(new TextLine(this, ++linenumber, ""));
stream.Close();
OnChanged();
}
public void SetDefaultHighlighting()
{
SetHighlightingTo("Default");
Reparse();
}
public void SetHighlightingFor(string filename)
{
string extension = Path.GetExtension(filename).ToUpper();
foreach (Syntax syn in Syntax.SyntaxDefinitions) {
foreach (string ext in syn.Extensions) {
if (ext.ToUpper() == extension) {
// update syntax highlighting
if (syntax != syn) {
syntax = syn;
}
Reparse();
return;
}
}
}
}
public void SetHighlightingTo(string name)
{
foreach (Syntax syn in Syntax.SyntaxDefinitions) {
if (name == syn.Name) {
syntax = syn;
Reparse();
break;
}
}
}
public void Save(string filename)
{
if (filename == null)
throw new ArgumentException("TextBuffer public void Save(string filename) : filename can't be null");
try {
StreamWriter writer = File.CreateText(filename);
int curline = 0;
int lastpercent = -1;
foreach (TextLine l in line) {
int percent = (int)((curline * 100) / line.Count);
if (Progress != null && percent != lastpercent) {
Progress(this, percent);
lastpercent = percent;
}
if (l != null)
writer.WriteLine(l.Text);
}
writer.Flush();
writer.Close();
OnChanged();
} catch (Exception e) {
MessageBox.Show(e.ToString(), "Can't save " + filename,
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
internal Point _Insert(Point where, string text)
{
Debug.Assert(!ReadOnly, "SharpDevelop.Internal.Text.TextBuffer._Insert(Point where, string text) : Buffer is ReadOnly");
int x = where.X;
int y = where.Y;
while (((TextLine)line[y]).Text.Length < where.X)
((TextLine)line[y]).Text += " ";
string curline = ((TextLine)line[y]).Text;
int linenumber = ((TextLine)line[y]).LineNumber;
bool foldevent = false;
bool foldon = ((TextLine)line[y]).foldon;
bool foldoff= ((TextLine)line[y]).foldoff;
bool ofoldon = foldon;
bool ofoldoff = foldon;
bool lcevt = false; // linecountevent;
for (int i = 0; i < text.Length; ++i) {
if (text[i] == '\r') {
} else
if (text[i] == '\n') {
lcevt = true;
string rest = curline.Substring(x);
if (rest.Length > 0)
curline = curline.Remove(x, rest.Length);
TextLine tl = new TextLine(this, ++linenumber, rest);
if (y + 1 < Length) {
foldon = this[y + 1].foldon;
foldoff = this[y + 1].foldoff;
} else
foldon = foldoff = false;
line.Insert(++y, tl);
foldevent |= foldon ^ this[y].foldon || foldoff ^ this[y].foldoff;
tl.BlockSpanOn = ((TextLine)line[y - 1]).BlockSpanOn;
((TextLine)line[y - 1]).Text = curline;
curline = rest;
x = 0;
} else {
if (text[i] == '\t' && Options.TabsToSpaces) {
for (int j = 0; j < Options.TabIndent; ++j)
curline = curline.Insert(x, " ");
x += Options.TabIndent;
} else {
curline = curline.Insert(x, text[i].ToString());
++x;
}
}
}
((TextLine)line[y]).Text = curline;
foldevent |= ofoldon ^ ((TextLine)line[y]).foldon || ofoldoff ^ ((TextLine)line[y]).foldoff;
OnChanged();
if (foldevent)
OnFoldEvent();
if (lcevt)
OnLineCountChanged();
return new Point(x, y);
}
public Point Insert(Point where, string text)
{
Debug.Assert(!ReadOnly, "SharpDevelop.Internal.Text.TextBuffer.Insert(Point where, string text) : Buffer is ReadOnly");
Point p = _Insert(where, text);
undostack.Push(new UndoableInsert(this, where, p, text));
return p;
}
public string GetText(Point p1, Point p2)
{
Point begin, end;
if (p1.Y < p2.Y || (p1.Y == p2.Y && p1.X < p2.X)) {
begin = p1;
end = p2;
} else {
begin = p2;
end = p1;
}
if (begin.Y == end.Y) {
if (begin.X < ((TextLine)line[begin.Y]).Text.Length)
return ((TextLine)line[begin.Y]).Text.Substring(begin.X, end.X - begin.X);
else
return "";
} else {
string s = (begin.X < ((TextLine)line[begin.Y]).Text.Length) ? ((TextLine)line[begin.Y]).Text.Substring(begin.X) : "";
while (begin.Y < end.Y - 1)
s += "\r\n" + ((TextLine)line[++begin.Y]).Text;
s += "\r\n";
s += ((TextLine)line[end.Y]).Text.Substring(0, Math.Min(end.X, ((TextLine)line[end.Y]).Text.Length));
return s;
}
}
internal void _Delete(Point begin, Point end)
{
Debug.Assert(!ReadOnly, "SharpDevelop.Internal.Text.TextBuffer._Delete(Point where, string text) : Buffer is ReadOnly");
bool foldevent = false;
bool foldon;
bool foldoff;
bool lcevt = false; // linecountevent;
if (begin.Y == end.Y) {
foldon = this[begin.Y].foldon;
foldoff = this[begin.Y].foldoff;
if (begin.X < ((TextLine)line[begin.Y]).Text.Length)
((TextLine)line[begin.Y]).Text = ((TextLine)line[begin.Y]).Text.Remove(begin.X, end.X - begin.X);
foldevent = foldon ^ this[begin.Y].foldon || foldoff ^ this[begin.Y].foldoff;
} else {
lcevt = true;
for (int y = 1; y < end.Y - begin.Y; ++y) {
foldon = this[begin.Y + 1].foldon;
foldoff = this[begin.Y + 1].foldoff;
((TextLine)line[begin.Y + 1]).Text = ""; // (for updaterequested)
line.RemoveAt(begin.Y + 1);
foldevent |= foldon ^ this[begin.Y].foldon || foldoff ^ this[begin.Y].foldoff;
}
foldon = this[begin.Y].foldon;
foldoff = this[begin.Y].foldoff;
string cur = ((TextLine)line[begin.Y]).Text;
string str = cur;
if (begin.X < cur.Length) {
str = cur.Substring(0, begin.X);
}
string next = ((TextLine)line[begin.Y + 1]).Text;
if (next.Length - end.X > 0)
str += next.Substring(end.X);
((TextLine)line[begin.Y]).Text = str;
foldevent |= foldon ^ this[begin.Y].foldon || foldoff ^ this[begin.Y].foldoff;
line.RemoveAt(begin.Y + 1);
}
if (foldevent)
OnFoldEvent();
OnChanged();
if (lcevt)
OnLineCountChanged();
}
public string Delete(Point p1, Point p2)
{
Debug.Assert(!ReadOnly, "SharpDevelop.Internal.Text.TextBuffer.Delete(Point where, string text) : Buffer is ReadOnly");
Point begin, end;
if (p1.Y < p2.Y || (p1.Y == p2.Y && p1.X < p2.X)) {
begin = p1;
end = p2;
} else {
begin = p2;
end = p1;
}
string back = GetText(begin, end);
_Delete(begin, end);
undostack.Push(new UndoableDelete(this, begin, end, back));
return back;
}
internal class MyArrayList : ArrayList
{
TextBuffer buffer;
public MyArrayList(TextBuffer buffer)
{
this.buffer = buffer;
}
public override void Insert(int index, object value)
{
base.Insert(index, value);
buffer.OnMoveIndices(index, 1);
}
public override void RemoveAt(int index)
{
base.RemoveAt(index);
buffer.OnMoveIndices(index, -1);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -