/// </summary>
public class CodeBuffer
{
- //those events are handled in SourceEditor to help keeping sync between textbuffer and parser.
+ //those events are handled in SourceEditor to help keeping sync between textbuffer,parser and editor.
//modified lines are marked for reparse
#region Events
public event EventHandler<CodeBufferEventArgs> LineUpadateEvent;
#endregion
#region CTOR
- public CodeBuffer () : base() {}
+ public CodeBuffer () {
+ this.Add ("");
+ }
#endregion
string lineBreak = Interface.LineBreak;
lines.Insert (i, item);
LineAdditionEvent.Raise (this, new CodeBufferEventArgs (i));
}
+ public void Add(string item){
+ lines.Add (item);
+ LineAdditionEvent.Raise (this, new CodeBufferEventArgs (lines.Count - 1));
+ }
public void AddRange (string[] items){
int start = lines.Count;
lines.AddRange (items);
#region Editing and moving cursor
public string SelectedText {
- get {
+ get {
if (selectionIsEmpty)
return "";
if (selectionStart.Y == selectionEnd.Y)
tmp += Interface.LineBreak + this [l];
}
tmp += Interface.LineBreak + this [selectionEnd.Y].Substring (0, selectionEnd.X);
- return tmp;
+ return tmp;
}
}
Point selectionStart = -1;
/// </summary>
public void InsertLineBreak()
{
- Insert(CurrentLine + 1, this[CurrentLine].Substring(CurrentColumn));
- this [CurrentLine] = this [CurrentLine].Substring (0, CurrentColumn);
+ if (CurrentColumn > 0) {
+ Insert (CurrentLine + 1, this [CurrentLine].Substring (CurrentColumn));
+ this [CurrentLine] = this [CurrentLine].Substring (0, CurrentColumn);
+ } else
+ Insert(CurrentLine, "");
+
CurrentLine++;
CurrentColumn = 0;
}
buffer.LineAdditionEvent += Buffer_LineAdditionEvent;;
buffer.LineRemoveEvent += Buffer_LineRemoveEvent;
buffer.BufferCleared += Buffer_BufferCleared;
-
- parser = new XMLParser (buffer);
}
#endregion
-
- public event EventHandler TextChanged;
-
- public virtual void OnTextChanged(Object sender, EventArgs e)
- {
- TextChanged.Raise (this, e);
- }
-
#region private and protected fields
- bool foldingEnabled = true;
+ bool foldingEnabled = false;
string filePath = "unamed.txt";
int leftMargin = 0; //margin used to display line numbers, folding errors,etc...
int visibleLines = 1;
// int tkPtr = fstTK+2;
// while (tkPtr < tl.Count) {
// if (tl [tkPtr] == XMLParser.TokenType.ElementClosing)
-//
+//
// tkPtr++;
// }
// if (tl.EndingState == (int)XMLParser.States.Content)
// parser.CurrentPosition = tls [fstTK + 1].Start;
// parser.SetLineInError(new ParsingException(parser, "closing tag not corresponding"));
// }
-//
+//
// }
// }
}
void reparseSource () {
+ if (parser == null)
+ return;
for (int i = 0; i < parser.Tokens.Count; i++) {
if (parser.Tokens[i].Dirty)
tryParseBufferLine (i);
updateFolding ();
}
void tryParseBufferLine(int lPtr) {
+ if (parser == null)
+ return;
try {
parser.Parse (lPtr);
} catch (ParsingException ex) {
}
RegisterForGraphicUpdate ();
}
- const int leftMarginGap = 2;
+ const int leftMarginGap = 0;
const int foldSize = 9;
- void measureLeftMargin () {
+ void measureLeftMargin () {
leftMargin = 0;
if (PrintLineNumbers)
leftMargin += (int)Math.Ceiling((double)buffer.LineCount.ToString().Length * fe.MaxXAdvance);
leftMargin += leftMarginGap;
updateVisibleColumns ();
}
+ void findLongestLineAndUpdateMaxScrollX() {
+ buffer.FindLongestVisualLine ();
+ MaxScrollX = Math.Max (0, buffer.longestLineCharCount - visibleColumns);
+ Debug.WriteLine ("SourceEditor: Find Longest line and update maxscrollx: {0} visible cols:{1}", MaxScrollX, visibleColumns);
+ }
+ void updateVisibleLines(){
+ visibleLines = (int)Math.Floor ((double)ClientRectangle.Height / fe.Height);
+ MaxScrollY = Math.Max (0, buffer.LineCount - visibleLines);
+
+ System.Diagnostics.Debug.WriteLine ("update visible lines: " + visibleLines);
+ System.Diagnostics.Debug.WriteLine ("update MaxScrollY: " + MaxScrollY);
+ }
+ void updateVisibleColumns(){
+ visibleColumns = (int)Math.Floor ((double)(ClientRectangle.Width - leftMargin)/ fe.MaxXAdvance);
+ MaxScrollX = Math.Max (0, buffer.longestLineCharCount - visibleColumns);
+
+ System.Diagnostics.Debug.WriteLine ("update visible columns: {0} leftMargin:{1}",visibleColumns, leftMargin);
+ System.Diagnostics.Debug.WriteLine ("update MaxScrollX: " + MaxScrollX);
+ }
#region Buffer events handlers
void Buffer_BufferCleared (object sender, EventArgs e)
buffer.longestLineCharCount = charCount;
}else if (lptr <= buffer.longestLineIdx)
buffer.longestLineIdx++;
- parser.Tokens.Insert (lptr, new TokenList());
- tryParseBufferLine (e.LineStart + i);
+ if (parser != null) {
+ parser.Tokens.Insert (lptr, new TokenList ());
+ tryParseBufferLine (e.LineStart + i);
+ }
}
measureLeftMargin ();
- reparseSource ();
+ if (parser != null)
+ reparseSource ();
RegisterForGraphicUpdate ();
}
int lptr = e.LineStart + i;
if (lptr <= buffer.longestLineIdx)
trigFindLongestLine = true;
- parser.Tokens.RemoveAt (lptr);
+ if (parser != null)
+ parser.Tokens.RemoveAt (lptr);
}
if (trigFindLongestLine)
findLongestLineAndUpdateMaxScrollX ();
measureLeftMargin ();
- reparseSource ();
+ if (parser != null)
+ reparseSource ();
RegisterForGraphicUpdate ();
}
int lptr = e.LineStart + i;
if (lptr == buffer.longestLineIdx)
trigFindLongestLine = true;
+ else if (buffer.GetPrintableLine (lptr).Length > buffer.longestLineCharCount) {
+ buffer.longestLineCharCount = buffer.GetPrintableLine (lptr).Length;
+ buffer.longestLineIdx = lptr;
+ }
tryParseBufferLine (lptr);
}
reparseSource ();
findLongestLineAndUpdateMaxScrollX ();
RegisterForGraphicUpdate ();
}
- void findLongestLineAndUpdateMaxScrollX() {
- buffer.FindLongestVisualLine ();
- MaxScrollX = Math.Max (0, buffer.longestLineCharCount - visibleColumns);
- Debug.WriteLine ("SourceEditor: Find Longest line and update maxscrollx: {0} visible cols:{1}", MaxScrollX, visibleColumns);
- }
#endregion
#region Public Crow Properties
- [XmlAttributeAttribute][DefaultValue(true)]
+ [XmlAttributeAttribute]
public bool PrintLineNumbers
{
get { return Configuration.Get<bool> ("PrintLineNumbers");
NotifyValueChanged ("CurrentLine", _currentLine);
}
}
+ /// <summary>
+ /// Current position is in the printed coord system, tabulation chars are replaced with 4 spaces,
+ /// while in the buffer, the position holds tabulations as single chars
+ /// </summary>
+ /// <value>The current position.</value>
[XmlIgnore]public Point CurrentPosition {
get { return new Point(CurrentColumn, CurrentLine); }
set {
findLongestLineAndUpdateMaxScrollX ();
CurrentPosition = buffer.TabulatedPosition;
+
+ RegisterForGraphicUpdate();
}
#endregion
int curL = i + ScrollY;
if (curL >= buffer.LineCount)
break;
- string lstr = buffer[curL];
+ string lstr = buffer.GetPrintableLine(curL);
if (ScrollX < lstr.Length)
lstr = lstr.Substring (ScrollX);
else
if (CurrentLine == curL)
mgFg = Color.White;
else
- mgFg = Color.LightGray;
+ mgFg = Color.LightGray;
}else if (CurrentLine == curL) {
mgFg = Color.Black;
}
base.onMouseLeave (sender, e);
currentInterface.MouseCursor = XCursor.Default;
}
- protected override void onFocused (object sender, EventArgs e)
- {
- base.onFocused (sender, e);
-
- // SelBegin = new Point(0,0);
- // SelRelease = new Point (lines.LastOrDefault ().Length, lines.Count-1);
- RegisterForRedraw ();
- }
- protected override void onUnfocused (object sender, EventArgs e)
- {
- base.onUnfocused (sender, e);
-
- // SelBegin = -1;
- // SelRelease = -1;
- RegisterForRedraw ();
- }
public override void onMouseMove (object sender, MouseMoveEventArgs e)
{
base.onMouseMove (sender, e);
RegisterForGraphicUpdate();
}
#endregion
-
- void updateVisibleLines(){
- visibleLines = (int)Math.Floor ((double)ClientRectangle.Height / fe.Height);
- MaxScrollY = Math.Max (0, buffer.LineCount - visibleLines);
-
- System.Diagnostics.Debug.WriteLine ("update visible lines: " + visibleLines);
- System.Diagnostics.Debug.WriteLine ("update MaxScrollY: " + MaxScrollY);
- }
- void updateVisibleColumns(){
- visibleColumns = (int)Math.Floor ((double)(ClientRectangle.Width - leftMargin)/ fe.MaxXAdvance);
- MaxScrollX = Math.Max (0, buffer.longestLineCharCount - visibleColumns);
-
- System.Diagnostics.Debug.WriteLine ("update visible columns: {0} leftMargin:{1}",visibleColumns, leftMargin);
- System.Diagnostics.Debug.WriteLine ("update MaxScrollX: " + MaxScrollX);
- }
}
}
\ No newline at end of file