]> O.S.I.I.S - jp/crowedit.git/commitdiff
trigger file loading through filePath instead of full Text source, comments
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Sat, 2 Sep 2017 05:23:20 +0000 (07:23 +0200)
committerJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Sat, 2 Sep 2017 05:23:20 +0000 (07:23 +0200)
src/CodeBuffer.cs
src/CrowEdit.cs
src/Parser.cs
src/SourceEditor.cs
src/XMLParser.cs
ui/main.crow

index 3c82d12f74ed4241d14972a8bdfca0e52be8e72d..5eb4316c941065146f47e71a8e2d73807a4eb027 100644 (file)
@@ -25,8 +25,13 @@ using System.Text.RegularExpressions;
 
 namespace Crow.Coding
 {
+       /// <summary>
+       /// Code buffer, lines are arranged in a List<string>, new line chars are removed during string.split on '\n...',
+       /// </summary>
        public class CodeBuffer
        {
+               //those events are handled in SourceEditor to help keeping sync between textbuffer and parser.
+               //modified lines are marked for reparse
                #region Events
                public event EventHandler<CodeBufferEventArgs> LineUpadateEvent;
                public event EventHandler<CodeBufferEventArgs> LineRemoveEvent;
@@ -158,36 +163,45 @@ namespace Crow.Coding
                /// <summary>
                /// convert buffer postition to visual position
                /// </summary>
-               Point getVisualPos (Point buffPos) {
+               Point getTabulatedPos (Point buffPos) {
                        int vCol = this[buffPos.Y].Substring(0, buffPos.X).Replace("\t", new String(' ', Interface.TabSize)).Length;
                        return new Point (vCol, buffPos.Y);
                }
                /// <summary>
                /// Gets visual position computed from actual buffer position
                /// </summary>
-               public Point VisualPosition {
-                       get { return getVisualPos (new Point (_currentCol, _currentLine)); }
+               public Point TabulatedPosition {
+                       get { return getTabulatedPos (new Point (_currentCol, _currentLine)); }
                }
                /// <summary>
                /// set buffer current position from visual position
                /// </summary>
-               public void SetBufferPos(Point visualPosition) {
-                       CurrentPosition = getBuffPos(visualPosition);
+               public void SetBufferPos(Point tabulatedPosition) {
+                       CurrentPosition = getBuffPos(tabulatedPosition);
                }
 
                #region Editing and moving cursor
                Point selectionStart = -1;
                Point selectionEnd = -1;
-               public void SetSelection (Point visualStart, Point visualEnd) {
-                       selectionStart = getBuffPos (visualStart);
-                       selectionEnd = getBuffPos (visualEnd);
+               /// <summary>
+               /// Set selection in buffer coords from tabulated coords
+               /// </summary>
+               public void SetSelection (Point tabulatedStart, Point tabulatedEnd) {
+                       selectionStart = getBuffPos (tabulatedStart);
+                       selectionEnd = getBuffPos (tabulatedEnd);
                }
+               /// <summary>
+               /// Set selection in buffer to -1, empty selection
+               /// </summary>
                public void ResetSelection () {
                        selectionStart = selectionEnd = -1;
                }
                bool selectionIsEmpty {
                        get { return selectionStart == selectionEnd; }
                }
+               /// <summary>
+               /// Current column in buffer coordinate, tabulation = 1 char
+               /// </summary>
                public int CurrentColumn{
                        get { return _currentCol; }
                        set {
@@ -201,6 +215,9 @@ namespace Crow.Coding
                                        _currentCol = value;
                        }
                }
+               /// <summary>
+               /// Current row in buffer coordinate, tabulation = 1 char
+               /// </summary>
                public int CurrentLine{
                        get { return _currentLine; }
                        set {
@@ -218,6 +235,9 @@ namespace Crow.Coding
                                CurrentColumn = cc;
                        }
                }
+               /// <summary>
+               /// Current position in buffer coordinate, tabulation = 1 char
+               /// </summary>
                public Point CurrentPosition {
                        get { return new Point(CurrentColumn, CurrentLine); }
                        set {
@@ -225,10 +245,13 @@ namespace Crow.Coding
                                _currentLine = value.Y;
                        }
                }
+               /// <summary>
+               /// get char at current position in buffer
+               /// </summary>
                protected Char CurrentChar { get { return lines [CurrentLine] [CurrentColumn]; } }
 
                /// <summary>
-               /// Moves cursor one char to the left.
+               /// Moves cursor one char to the left, move up if cursor reaches start of line
                /// </summary>
                /// <returns><c>true</c> if move succeed</returns>
                public bool MoveLeft(){
@@ -243,7 +266,7 @@ namespace Crow.Coding
                        return true;
                }
                /// <summary>
-               /// Moves cursor one char to the right.
+               /// Moves cursor one char to the right, move down if cursor reaches end of line
                /// </summary>
                /// <returns><c>true</c> if move succeed</returns>
                public bool MoveRight(){
index d23541b9db01661266d144e7d504701db83dfd64..32b1d0ec3893993ffa29277e6da951be1c8582c8 100644 (file)
@@ -138,18 +138,11 @@ namespace CrowEdit
                        CurFilePath = fd.SelectedFile;
                        CurrentDir = fd.SelectedDirectory;
 
-                       using (StreamReader sr = new StreamReader (CurFileFullPath)) {
-                               _origText = sr.ReadToEnd ();
-                       }
-                       _text = _origText;
-
-                       NotifyValueChanged ("Text", _text);
-                       NotifyValueChanged ("IsDirty", false);
-                       redoStack.Clear ();
-                       undoStack.Clear ();
-                       CMDRedo.CanExecute = false;
-                       CMDUndo.CanExecute = false;
-
+//                     redoStack.Clear ();
+//                     undoStack.Clear ();
+//                     CMDRedo.CanExecute = false;
+//                     CMDUndo.CanExecute = false;
+//
                        NotifyValueChanged ("CurFileFullPath", CurFileFullPath);
                }
                void saveFileDialog_OkClicked (object sender, EventArgs e)
index 90caa30fbc0fee0f6db322a4c1062eb7ca34e269..f3f2206ee3603ec5724e04bf1f81342040350a34 100644 (file)
@@ -6,8 +6,15 @@ using System.Diagnostics;
 
 namespace Crow.Coding
 {
+       /// <summary>
+       /// base class for tokenizing sources
+       /// </summary>
        public abstract class Parser
        {
+               /// <summary>
+               /// Default tokens, this enum may be overriden in derived parser with the new keyword,
+               /// see XMLParser for example.
+               /// </summary>
                public enum TokenType {
                        Unknown,
                        WhiteSpace,
@@ -60,32 +67,64 @@ namespace Crow.Coding
                }
 
                #region low level parsing
+               /// <summary>
+               /// Read one char from current position in buffer and store it into the current token
+               /// </summary>
+               /// <param name="startOfTok">if true, set the Start position of the current token to the current position</param>
                protected void readToCurrTok(bool startOfTok = false){
                        if (startOfTok)
                                currentTok.Start = CurrentPosition;
                        currentTok += Read();
                }
+               /// <summary>
+               /// read n char from the buffer and store it into the current token
+               /// </summary>
                protected void readToCurrTok(int length) {
                        for (int i = 0; i < length; i++)
                                currentTok += Read ();
                }
+               /// <summary>
+               /// Save current token into current TokensLine and raz current token
+               /// </summary>
+               protected void saveAndResetCurrentTok() {
+                       currentTok.End = CurrentPosition;
+                       TokensLine.Add (currentTok);
+                       currentTok = default(Token);
+               }
+               /// <summary>
+               /// read one char and add current token to current TokensLine, current token is reset
+               /// </summary>
+               /// <param name="type">Type of the token</param>
+               /// <param name="startToc">set start of token to current position</param>
                protected void readAndResetCurrentTok(System.Enum type, bool startToc = false) {
                        readToCurrTok ();
                        saveAndResetCurrentTok (type);
                }
-               protected void saveAndResetCurrentTok() { this.saveAndResetCurrentTok (currentTok.Type); }
+               /// <summary>
+               /// Save current tok
+               /// </summary>
+               /// <param name="type">set the type of the tok</param>
                protected void saveAndResetCurrentTok(System.Enum type) {
                        currentTok.Type = (TokenType)type;
-                       currentTok.End = CurrentPosition;
-                       TokensLine.Add (currentTok);
-                       currentTok = default(Token);
+                       saveAndResetCurrentTok ();
                }
+               /// <summary>
+               /// Peek next char, emit '\n' if current column > buffer's line length
+               /// Throw error if eof is true
+               /// </summary>
                protected virtual char Peek() {
                        if (eof)
                                throw new ParsingException (this, "Unexpected End of File");
                        return currentColumn < buffer [currentLine].Length ?
                                buffer [currentLine] [currentColumn] : '\n';
                }
+               /// <summary>
+               /// Peek n char from buffer or less if remaining char in buffer's line is less than requested
+               /// if end of line is reached, no '\n' will be emitted, instead, empty string is returned. '\n' should be checked only
+               /// with single char Peek().
+               /// Throw error is eof is true
+               /// </summary>
+               /// <param name="length">Length.</param>
                protected virtual string Peek(int length) {
                        if (eof)
                                throw new ParsingException (this, "Unexpected End of File");
@@ -94,9 +133,13 @@ namespace Crow.Coding
                                return "";
                        return buffer [currentLine].Substring (currentColumn, lg);
                }
+               /// <summary>
+               /// read one char from buffer at current position, if '\n' is read, current line is incremented
+               /// and column is reset to 0
+               /// </summary>
                protected virtual char Read() {
                        char c = Peek ();
-
+                       //TODO: the parsing is done line by line, we should be able to remove the next line handling from read
                        if (c == '\n') {
                                currentLine++;
                                if (currentLine >= buffer.Length)
@@ -106,6 +149,10 @@ namespace Crow.Coding
                                currentColumn++;
                        return c;
                }
+               /// <summary>
+               /// read until end of line is reached
+               /// </summary>
+               /// <returns>string read</returns>
                protected virtual string ReadLine () {
                        string tmp = "";
                        while (!eof) {
@@ -115,6 +162,11 @@ namespace Crow.Coding
                        }
                        return tmp;
                }
+               /// <summary>
+               /// read until end expression is reached or end of line.
+               /// </summary>
+               /// <returns>string read minus the ending expression that has to be read after</returns>
+               /// <param name="endExp">Expression to search for</param>
                protected virtual string ReadLineUntil (string endExp){
                        string tmp = "";
 
@@ -129,6 +181,9 @@ namespace Crow.Coding
                        }
                        return tmp;
                }
+               /// <summary>
+               /// skip white spaces, but not line break. Save spaces in a WhiteSpace token.
+               /// </summary>
                protected void SkipWhiteSpaces () {
                        if (currentTok.Type != TokenType.Unknown)
                                throw new ParsingException (this, "current token should be reset to unknown (0) before skiping white spaces");
index 1f6ae638fa2a30210f9f4e5f019c1dfa55c00731..1cd7e1e2dd00d537e4d64d07552ee7a8d3972ebf 100644 (file)
@@ -35,6 +35,7 @@ using System.Text.RegularExpressions;
 using System.Linq;
 using System.Diagnostics;
 using CrowEdit;
+using System.IO;
 
 namespace Crow.Coding
 {
@@ -78,6 +79,8 @@ namespace Crow.Coding
                }
 
                #region private and protected fields
+               string filePath = "unamed.txt";
+               int leftMargin = 0;     //margin used to display line numbers, folding errors,etc...
                int visibleLines = 1;
                int visibleColumns = 1;
                CodeBuffer buffer;
@@ -111,6 +114,7 @@ namespace Crow.Coding
                        }
                        RegisterForGraphicUpdate ();
                }
+
                #region Buffer events handlers
                void Buffer_BufferCleared (object sender, EventArgs e)
                {
@@ -145,23 +149,31 @@ namespace Crow.Coding
                #endregion
 
                #region Public Crow Properties
-               [XmlAttributeAttribute][DefaultValue("label")]
-               public string Text
+               [XmlAttributeAttribute]
+               public string FilePath
                {
                        get {
                                return buffer == null ? "" : buffer.FullText;
                        }
                        set
                        {
-                               if (string.Equals (value, buffer?.FullText, StringComparison.Ordinal))
+                               if (filePath == value)
+                                       return;
+
+                               filePath = value;
+                               NotifyValueChanged ("FilePath", filePath);
+
+                               if (!File.Exists (filePath))
                                        return;
 
-                               buffer.Load (value);
+                               using (StreamReader sr = new StreamReader (filePath)) {
+                                       string txt = sr.ReadToEnd ();
+                                       buffer.Load (txt);
+                               }
 
                                MaxScrollY = Math.Max (0, buffer.Length - visibleLines);
                                MaxScrollX = Math.Max (0, buffer.longestLineCharCount - visibleColumns);
 
-                               OnTextChanged (this, null);
                                RegisterForGraphicUpdate ();
                        }
                }
@@ -332,7 +344,7 @@ namespace Crow.Coding
                /// <returns><c>true</c> if move succeed</returns>
                public bool MoveLeft(){
                        bool res = buffer.MoveLeft();
-                       CurrentPosition = buffer.VisualPosition;
+                       CurrentPosition = buffer.TabulatedPosition;
                        return res;
                }
                /// <summary>
@@ -341,23 +353,23 @@ namespace Crow.Coding
                /// <returns><c>true</c> if move succeed</returns>
                public bool MoveRight(){
                        bool res = buffer.MoveRight();
-                       CurrentPosition = buffer.VisualPosition;
+                       CurrentPosition = buffer.TabulatedPosition;
                        return res;
                }
                public void GotoWordStart(){
                        buffer.GotoWordStart();
-                       CurrentPosition = buffer.VisualPosition;
+                       CurrentPosition = buffer.TabulatedPosition;
                }
                public void GotoWordEnd(){
                        buffer.GotoWordEnd();
-                       CurrentPosition = buffer.VisualPosition;
+                       CurrentPosition = buffer.TabulatedPosition;
                }
                public void DeleteChar()
                {
                        if (!selectionIsEmpty)
                                buffer.SetSelection (selectionStart, selectionEnd);
                        buffer.DeleteChar ();
-                       CurrentPosition = buffer.VisualPosition;
+                       CurrentPosition = buffer.TabulatedPosition;
                        SelBegin = -1;
                        SelRelease = -1;
                        OnTextChanged (this, null);
@@ -372,7 +384,7 @@ namespace Crow.Coding
                                DeleteChar ();
 
                        buffer.Insert (str);
-                       CurrentPosition = buffer.VisualPosition;
+                       CurrentPosition = buffer.TabulatedPosition;
 
                        OnTextChanged (this, null);
                        RegisterForGraphicUpdate();
@@ -383,7 +395,7 @@ namespace Crow.Coding
                protected void InsertLineBreak()
                {
                        buffer.InsertLineBreak ();
-                       CurrentPosition = buffer.VisualPosition;
+                       CurrentPosition = buffer.TabulatedPosition;
                        OnTextChanged (this, null);
                }
                #endregion
@@ -625,7 +637,7 @@ namespace Crow.Coding
                        else
                                CurrentLine = ScrollY + (int)Math.Floor (mouseLocalPos.Y / fe.Height);
 
-                       CurrentPosition = buffer.VisualPosition; //for rounding if in middle of tabs
+                       CurrentPosition = buffer.TabulatedPosition; //for rounding if in middle of tabs
                }
                public override void onMouseEnter (object sender, MouseMoveEventArgs e)
                {
@@ -731,8 +743,6 @@ namespace Crow.Coding
                                this.InsertLineBreak ();
                                break;
                        case Key.Escape:
-                               Text = "";
-                               CurrentColumn = 0;
                                SelRelease = -1;
                                break;
                        case Key.Home:
index 36a96e740b2fa27f372bbd5fc7bb9209d37e8da4..738361837582ac261857a62b6ce41626cd661e64 100644 (file)
@@ -77,25 +77,6 @@ namespace Crow.Coding
                {
                        get { return rxNameChar.IsMatch(new string(new char[]{Peek()})); }
                }
-//             public bool NameIsValid(string name)
-//             {
-//                     if (!rxNameStartChar.IsMatch(char.ConvertFromUtf32(((string)name)[0])))
-//                             return false;
-//
-//                     return rxNameChar.IsMatch(name);
-//             }
-//             private bool NextCharIsValidPubidChar
-//             {
-//                     get { return rxPubidChar.IsMatch(char.ConvertFromUtf32(Peek())); }
-//             }
-//             private bool AttributeValueIsValid(string name)
-//             {
-//                     return string.IsNullOrEmpty(name) ? true : rxAttributeValue.IsMatch(name);
-//             }
-//             private bool NextCharIsValidEntityValue
-//             {
-//                     get { return rxEntityValue.IsMatch(char.ConvertFromUtf32(Peek())); }
-//             }
                #endregion
 
                public override void SetLineInError (ParsingException ex)
index 8e81eb20bc0020e786485e017b49878dd7cd724d..f01a611fdb6b8975fd5f10c478140b585c797eb6 100755 (executable)
@@ -24,7 +24,7 @@
                <HorizontalStack Height="Stretched" >
                        <SourceEditor Focusable="true" Name="editor" Font="couriernew, 14" VerticalAlignment="Top" Margin="10"
                                        Foreground="Jet" Background="Ivory" Width="Stretched" Height="Stretched"
-                                       Text="{Text}"  KeyDown="textView_KeyDown"/>
+                                        FilePath="{CurFileFullPath}"  KeyDown="textView_KeyDown"/>
                        <ScrollBar Name="scrollbarY" Value="{²../editor.ScrollY}"
                                Maximum="{../editor.MaxScrollY}" Orientation="Vertical"
                                Width="14" />