From 54ae7cb719ff4e389d9117409f773197a3e221ff Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jean-Philippe=20Bruy=C3=A8re?= Date: Tue, 5 Sep 2017 10:11:23 +0200 Subject: [PATCH] choose parser depending on extension of loaded file --- src/CodeBuffer.cs | 3 +++ src/Parser.cs | 2 +- src/SourceEditor.cs | 27 +++++++++++++++++++++++---- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/CodeBuffer.cs b/src/CodeBuffer.cs index f5abb04..c063c18 100644 --- a/src/CodeBuffer.cs +++ b/src/CodeBuffer.cs @@ -110,6 +110,9 @@ namespace Crow.Coding FindLongestVisualLine (); } + /// + /// Finds the longest visual line as printed on screen with tabulation replaced with n spaces + /// public void FindLongestVisualLine(){ longestLineCharCount = 0; for (int i = 0; i < this.LineCount; i++) { diff --git a/src/Parser.cs b/src/Parser.cs index ef31463..5d0a92d 100644 --- a/src/Parser.cs +++ b/src/Parser.cs @@ -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; diff --git a/src/SourceEditor.cs b/src/SourceEditor.cs index ea5065e..7e8cafc 100644 --- a/src/SourceEditor.cs +++ b/src/SourceEditor.cs @@ -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 formatting = new Dictionary(); + Dictionary parsing = new Dictionary(); 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); -- 2.47.3