[svn] r7577: vs-plugin/trunk/Nemerle.Compiler.Utils/Nemerle.Completion2: CodeFormatting/NonEatingLexer.n C...

kliss svnadmin at nemerle.org
Tue Apr 3 01:08:28 CEST 2007


Log:
Correct special comment colorizing. Now it works as in ReSharper.

Author: kliss
Date: Tue Apr  3 01:08:17 2007
New Revision: 7577

Modified:
   vs-plugin/trunk/Nemerle.Compiler.Utils/Nemerle.Completion2/CodeFormatting/NonEatingLexer.n
   vs-plugin/trunk/Nemerle.Compiler.Utils/Nemerle.Completion2/CodeModel/ScanLexer.n

Modified: vs-plugin/trunk/Nemerle.Compiler.Utils/Nemerle.Completion2/CodeFormatting/NonEatingLexer.n
==============================================================================
--- vs-plugin/trunk/Nemerle.Compiler.Utils/Nemerle.Completion2/CodeFormatting/NonEatingLexer.n	(original)
+++ vs-plugin/trunk/Nemerle.Compiler.Utils/Nemerle.Completion2/CodeFormatting/NonEatingLexer.n	Tue Apr  3 01:08:17 2007
@@ -142,8 +142,6 @@
         | '|' => get_op (ch)
         | ':' => get_op (ch)
 
-        // after executing eat_whitespace it is the only possibility for space
-        // (try..catch above)
         | ' ' => Token.WhiteSpace (" ")
         | '\t' => Token.Indent ("\t");
         | '\n' => Token.NewLine("\n");

Modified: vs-plugin/trunk/Nemerle.Compiler.Utils/Nemerle.Completion2/CodeModel/ScanLexer.n
==============================================================================
--- vs-plugin/trunk/Nemerle.Compiler.Utils/Nemerle.Completion2/CodeModel/ScanLexer.n	(original)
+++ vs-plugin/trunk/Nemerle.Compiler.Utils/Nemerle.Completion2/CodeModel/ScanLexer.n	Tue Apr  3 01:08:17 2007
@@ -74,6 +74,12 @@
     mutable _identifiers    : list[string] = [];
 
     _tokenInfo : ScanTokenInfo = ScanTokenInfo();
+    // This is for supporting partly colorized comments.
+    // Example: this is some text. TODO: And this is special comment. Only part after todo must be colorized.
+    // When comment matches special comment regex, it is then split into two parts.
+    // Part before match location is regular comment and is immediately returned from GetToken().
+    // The rest is turned into special comment and stored in this field.
+    mutable _specialCommentToken : ScanTokenInfo = null;
 
     static _keywords       : Dictionary[string,string] = Dictionary();
     static _quotationTypes : Dictionary[string,string] = Dictionary();
@@ -581,8 +587,17 @@
       }
     }
 
+
     public GetToken(prevState : ScanState) : ScanTokenInfo
     {
+      if(_specialCommentToken != null)
+      {
+        def tmp = _specialCommentToken;
+        _specialCommentToken = null;
+        tmp;
+      }
+      else
+      {
       _tokenInfo.State       = prevState;
       _tokenInfo.ColorizeEnd = false;
 
@@ -742,8 +757,6 @@
       when (resetQuotationStart && !_tokenInfo.IsWhiteSpaceOrCommentType)
         _tokenInfo.State &= ~ScanState.QuotationStart;
 
-      when(_tokenInfo.IsCommentType)
-        CheckForSpecialComments(_tokenInfo);
 
       when (_tokenInfo.IsQuotation && !(_tokenInfo.Token is Token.BeginQuote))
       {
@@ -769,18 +782,24 @@
       }
 
       _tokenInfo.Token.Location = Location(file_idx, _last_line, _last_col, line, col);
+        
+        when(_tokenInfo.IsCommentType)
+          CheckForSpecialComments(_tokenInfo);
+
       _ = checkForHighlights(_tokenInfo);
       
       _tokenInfo //VladD2: 2IT: Блин, приколист. Я час ошибку искал. Все элементы в массиве одинковые. :(
                  //phantom: 2VladD2: они просто маленькие... когда они вырастут, станут различаться. :)
+      
+      }
     }
     
     #region Support for special comments
     static specialCommentRegexes : list[ScanTokenColor * Regex] = 
       [
-        (ScanTokenColor.CommentTODO, Regex(@"\b(TODO)\b\s*:", RegexOptions.Compiled)),
-        (ScanTokenColor.CommentBUG, Regex(@"\b(BUG)\b\s*:", RegexOptions.Compiled)),
-        (ScanTokenColor.CommentHACK, Regex(@"\b(HACK)\b\s*:", RegexOptions.Compiled))
+        (ScanTokenColor.CommentTODO, Regex(@"(\b(TODO)\b\s*:.*)", RegexOptions.Compiled)),
+        (ScanTokenColor.CommentBUG, Regex(@"(\b(BUG)\b\s*:.*)", RegexOptions.Compiled)),
+        (ScanTokenColor.CommentHACK, Regex(@"(\b(HACK)\b\s*:.*)", RegexOptions.Compiled))
       ];
     
     CheckForSpecialComments(tokenInfo : ScanTokenInfo) : void
@@ -790,11 +809,12 @@
         match(regexList)
         {
         | (color, regex) :: xs => 
-                if(regex.Match(str).Success)
-                  (true, color)
+                def m =  regex.Match(str);
+                if(m.Success)
+                  (m.Captures[0].Index, m.Captures[0].Length, color)
                 else
                   chooseColor(str, xs)
-        | _ => (false, ScanTokenColor.Comment) // this color will be ignored.
+        | _ => (-1, -1, ScanTokenColor.Comment) // this color will be ignored.
         }
       }
       
@@ -802,9 +822,19 @@
       {
       | Comment(value) =>
             //Debug.WriteLine($"Comment value: $value");
-            def (success, color) = chooseColor(value.Substring(2), specialCommentRegexes);
-            when(success)
-              tokenInfo.Color = color;
+            def commentWOBeginning = value.Substring(2);
+            def (idx, len, color) = chooseColor(commentWOBeginning, specialCommentRegexes);
+            when(idx != -1)
+            {
+              _specialCommentToken = tokenInfo.Clone();
+              _specialCommentToken.Token = Token.Comment(commentWOBeginning.Substring(idx, len));
+
+              def l = tokenInfo.Token.Location;
+              // shrinking regular comment location
+              tokenInfo.Token.Location = Location(l, l.Line, l.Column, l.EndLine, l.Column + 1 + idx);
+              _specialCommentToken.Color = color;
+              _specialCommentToken.Token.Location = Location(l, l.Line, l.Column + 2 + idx, l.EndLine, l.EndColumn);
+            }
       | _ => () 
       }
     }



More information about the svn mailing list