[cfe-commits] r49825 - in /cfe/trunk: include/clang/Rewrite/HTMLRewrite.h lib/Rewrite/HTMLRewrite.cpp

Chris Lattner sabre at nondot.org
Wed Apr 16 15:45:51 PDT 2008


Author: lattner
Date: Wed Apr 16 17:45:51 2008
New Revision: 49825

URL: http://llvm.org/viewvc/llvm-project?rev=49825&view=rev
Log:
add a new HighlightRange API, it doesn't handle multiline ranges
yet, but it will soon...

Modified:
    cfe/trunk/include/clang/Rewrite/HTMLRewrite.h
    cfe/trunk/lib/Rewrite/HTMLRewrite.cpp

Modified: cfe/trunk/include/clang/Rewrite/HTMLRewrite.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Rewrite/HTMLRewrite.h?rev=49825&r1=49824&r2=49825&view=diff

==============================================================================
--- cfe/trunk/include/clang/Rewrite/HTMLRewrite.h (original)
+++ cfe/trunk/include/clang/Rewrite/HTMLRewrite.h Wed Apr 16 17:45:51 2008
@@ -25,6 +25,19 @@
   
 namespace html {
 
+  /// HighlightRange - Highlight a range in the source code with the specified
+  /// start/end tags.  B/E must be in the same file.  This ensures that
+  /// start/end tags are placed at the start/end of each line if the range is
+  /// multiline.
+  void HighlightRange(Rewriter &R, SourceLocation B, SourceLocation E,
+                      const char *StartTag, const char *EndTag);
+  
+  /// HighlightRange - This is the same as the above method, but takes
+  /// decomposed file locations.
+  void HighlightRange(RewriteBuffer &RB, unsigned B, unsigned E,
+                      const char *BufferStart,
+                      const char *StartTag, const char *EndTag);
+  
   /// EscapeText - HTMLize a specified file so that special characters are
   /// are translated so that they are not interpreted as HTML tags.  In this
   /// version tabs are not replaced with spaces by default, as this can
@@ -55,6 +68,7 @@
   void HighlightMacros(Rewriter &R, unsigned FileID, Preprocessor &PP);
   
 
+  
 } // end html namespace
 } // end clang namespace
 

Modified: cfe/trunk/lib/Rewrite/HTMLRewrite.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Rewrite/HTMLRewrite.cpp?rev=49825&r1=49824&r2=49825&view=diff

==============================================================================
--- cfe/trunk/lib/Rewrite/HTMLRewrite.cpp (original)
+++ cfe/trunk/lib/Rewrite/HTMLRewrite.cpp Wed Apr 16 17:45:51 2008
@@ -21,6 +21,38 @@
 #include <sstream>
 using namespace clang;
 
+/// HighlightRange - Highlight a range in the source code with the specified
+/// start/end tags.  B/E must be in the same file.  This ensures that
+/// start/end tags are placed at the start/end of each line if the range is
+/// multiline.
+void html::HighlightRange(Rewriter &R, SourceLocation B, SourceLocation E,
+                          const char *StartTag, const char *EndTag) {
+  SourceManager &SM = R.getSourceMgr();
+  B = SM.getLogicalLoc(B);
+  E = SM.getLogicalLoc(E);
+  unsigned FileID = SM.getCanonicalFileID(B);
+  assert(SM.getCanonicalFileID(E) == FileID && "B/E not in the same file!");
+
+  unsigned BOffset = SM.getFullFilePos(B);
+  unsigned EOffset = SM.getFullFilePos(E);
+  
+  // Include the whole end token in the range.
+  EOffset += Lexer::MeasureTokenLength(E, R.getSourceMgr());
+  
+  HighlightRange(R.getEditBuffer(FileID), BOffset, EOffset,
+                 SM.getBufferData(FileID).first, StartTag, EndTag);
+}
+
+/// HighlightRange - This is the same as the above method, but takes
+/// decomposed file locations.
+void html::HighlightRange(RewriteBuffer &RB, unsigned B, unsigned E,
+                          const char *BufferStart,
+                          const char *StartTag, const char *EndTag) {
+  RB.InsertTextAfter(B, StartTag, strlen(StartTag));
+  RB.InsertTextBefore(E, EndTag, strlen(EndTag));
+  
+}
+
 void html::EscapeText(Rewriter& R, unsigned FileID,
                       bool EscapeSpaces, bool ReplaceTabs) {
   
@@ -185,7 +217,7 @@
       " .code { border-spacing:0px; width:100%; }\n"
       " .code { font-family: \"Andale Mono\", monospace; font-size:10pt }\n"
       " .code { line-height: 1.2em }\n"
-      " .comment { color: #A0A0A0 }\n"
+      " .comment { color: #A0A0A0; font-style: oblique }\n"
       " .keyword { color: #FF00FF }\n"
       " .directive { color: #FFFF00 }\n"
       " .macro { color: #FF0000; background-color:#FFC0C0 }\n"
@@ -257,17 +289,14 @@
       IdentifierInfo *II = PP.LookUpIdentifierInfo(Tok, BufferStart+TokOffs);
         
       // If this is a pp-identifier, for a keyword, highlight it as such.
-      if (II->getTokenID() != tok::identifier) {
-        RB.InsertTextAfter(TokOffs, "<span class='keyword'>",
-                           strlen("<span class='keyword'>"));
-        RB.InsertTextBefore(TokOffs+TokLen, "</span>", strlen("</span>"));
-      }
+      if (II->getTokenID() != tok::identifier)
+        HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
+                       "<span class='keyword'>", "</span>");
       break;
     }
     case tok::comment:
-      RB.InsertTextAfter(TokOffs, "<span class='comment'>",
-                         strlen("<span class='comment'>"));
-      RB.InsertTextBefore(TokOffs+TokLen, "</span>", strlen("</span>"));
+      HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
+                     "<span class='comment'>", "</span>");
       break;
     case tok::hash:
       // FIXME: This isn't working because we're not in raw mode in the lexer.
@@ -275,12 +304,11 @@
         
       // If this is a preprocessor directive, all tokens to end of line are too.
       if (Tok.isAtStartOfLine()) {
-        RB.InsertTextAfter(TokOffs, "<span class='directive'>",
-                           strlen("<span class='directive'>"));
         // Find end of line.  This is a hack.
         const char *LineEnd = SourceMgr.getCharacterData(Tok.getLocation());
         unsigned TokEnd = TokOffs+strcspn(LineEnd, "\n\r");
-        RB.InsertTextBefore(TokEnd, "</span>", strlen("</span>"));
+        HighlightRange(RB, TokOffs, TokEnd, BufferStart,
+                       "<span class='directive'>", "</span>");
       }
       break;
     }





More information about the cfe-commits mailing list