]> O.S.I.I.S - jp/crowedit.git/commitdiff
choose parser depending on extension of loaded file
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Tue, 5 Sep 2017 08:11:23 +0000 (10:11 +0200)
committerJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Tue, 5 Sep 2017 08:11:23 +0000 (10:11 +0200)
src/CodeBuffer.cs
src/Parser.cs
src/SourceEditor.cs

index f5abb0459234198476089a3594f90e6428bb8c65..c063c187f953dc4941963c76b64bcea0ff5fbdd3 100644 (file)
@@ -110,6 +110,9 @@ namespace Crow.Coding
                        FindLongestVisualLine ();
                }
 
+               /// <summary>
+               /// Finds the longest visual line as printed on screen with tabulation replaced with n spaces
+               /// </summary>
                public void FindLongestVisualLine(){
                        longestLineCharCount = 0;
                        for (int i = 0; i < this.LineCount; i++) {
index ef31463fdac2f98fb5fead334eb0f023df9cff71..5d0a92dd4be54698d95d4f2ae81e07a41d62a186 100644 (file)
@@ -59,7 +59,7 @@ namespace Crow.Coding
                protected TokenList TokensLine;
 
                public Point CurrentPosition {
-                       get { return new Point (currentLine, currentColumn); } 
+                       get { return new Point (currentLine, currentColumn); }
                        set {
                                currentLine = value.Y;
                                currentColumn = value.X;
index ea5065e4c4a5dec823fe9e96b967856947660aa1..7e8cafc5c1be3b82880a41cdfde719e79bb9d973 100644 (file)
@@ -60,6 +60,8 @@ namespace Crow.Coding
                        formatting.Add ((int)XMLParser.TokenType.XMLDecl, new TextFormatting (Color.BlueCrayola, Color.Transparent, true));
                        formatting.Add ((int)XMLParser.TokenType.BlockComment, new TextFormatting (Color.Gray, Color.Transparent, false, true));
 
+                       parsing.Add (".crow", "Crow.Coding.XMLParser");
+
                        buffer = new CodeBuffer ();
                        buffer.LineUpadateEvent += Buffer_LineUpadateEvent;
                        buffer.LineAdditionEvent += Buffer_LineAdditionEvent;;
@@ -68,6 +70,9 @@ namespace Crow.Coding
                }
                #endregion
 
+               const int leftMarginGap = 0;//gap between items in margin and text
+               const int foldSize = 9;//folding rectangles size
+
                #region private and protected fields
                bool foldingEnabled = false;
                string filePath = "unamed.txt";
@@ -84,6 +89,7 @@ namespace Crow.Coding
                Point _selRelease = -1; //selection end (row,column)
 
                Dictionary<int, TextFormatting> formatting = new Dictionary<int, TextFormatting>();
+               Dictionary<string, string> parsing = new Dictionary<string, string>();
 
                protected Rectangle rText;
                protected FontExtents fe;
@@ -149,15 +155,14 @@ namespace Crow.Coding
                        }
                        RegisterForGraphicUpdate ();
                }
-               const int leftMarginGap = 0;
-               const int foldSize = 9;
                void measureLeftMargin () {
                        leftMargin = 0;
                        if (PrintLineNumbers)
                                leftMargin += (int)Math.Ceiling((double)buffer.LineCount.ToString().Length * fe.MaxXAdvance);
                        if (foldingEnabled)
                                leftMargin += foldSize;
-                       leftMargin += leftMarginGap;
+                       if (leftMargin > 0)
+                               leftMargin += leftMarginGap;
                        updateVisibleColumns ();
                }
                void findLongestLineAndUpdateMaxScrollX() {
@@ -183,7 +188,8 @@ namespace Crow.Coding
                #region Buffer events handlers
                void Buffer_BufferCleared (object sender, EventArgs e)
                {
-                       parser = new XMLParser (buffer);
+                       if (parser != null)
+                               parser.Tokens.Clear ();
                        buffer.longestLineCharCount = 0;
                        buffer.longestLineIdx = 0;
                        measureLeftMargin ();
@@ -249,6 +255,17 @@ namespace Crow.Coding
                }
                #endregion
 
+               Parser getParserFromExt (string extension) {
+                       if (string.IsNullOrEmpty(extension))
+                               return null;
+                       if (!parsing.ContainsKey(extension))
+                               return null;
+                       Type parserType = Type.GetType (parsing [extension]);
+                       if (parserType == null)
+                               return null;
+                       return (Parser)Activator.CreateInstance (parserType, buffer );
+               }
+
                #region Public Crow Properties
                [XmlAttributeAttribute]
                public bool PrintLineNumbers
@@ -281,6 +298,8 @@ namespace Crow.Coding
                                if (!File.Exists (filePath))
                                        return;
 
+                               parser = getParserFromExt (System.IO.Path.GetExtension (filePath));
+
                                using (StreamReader sr = new StreamReader (filePath)) {
                                        string txt = sr.ReadToEnd ();
                                        buffer.Load (txt);