Patch #3313 2012-08-07 15:33

alpha0010

Improve handling of inactive preprocessor code
Download
3313-Improve_handli.patch (32.0 KB)
Category
Lexer
Status
Accepted
Close date
2012-08-13 05:23
Assigned to
mortenmacfly
Index: src/sdk/editorconfigurationdlg.cpp
===================================================================
--- src/sdk/editorconfigurationdlg.cpp    (revision 8219)
+++ src/sdk/editorconfigurationdlg.cpp    (working copy)
@@ -141,6 +141,8 @@
     XRCCTRL(*this, "rbTabText",                   wxRadioBox)->SetSelection(cfg->ReadBool(_T("/tab_text_relative"),      true)? 1 : 0);
 
     XRCCTRL(*this, "chkTrackPreprocessor",        wxCheckBox)->SetValue(cfg->ReadBool(_T("/track_preprocessor"),         false));
+    XRCCTRL(*this, "chkCollectPrjDefines",        wxCheckBox)->SetValue(cfg->ReadBool(_T("/collect_prj_defines"),        false));
+    XRCCTRL(*this, "chkPlatDefines",              wxCheckBox)->SetValue(cfg->ReadBool(_T("/platform_defines"),           false));
     XRCCTRL(*this, "chkColoursWxSmith",           wxCheckBox)->SetValue(cfg->ReadBool(_T("/highlight_wxsmith"),          true));
 
 #if defined __WXMSW__
@@ -152,7 +154,7 @@
 #endif
     XRCCTRL(*this, "txtOpenFolder", wxTextCtrl)->SetValue(cfg->Read(_T("/open_containing_folder"), openFolderCmds));
 
-    // Highlight Occurence
+    // Highlight Occurrence
     bool highlightEnabled = cfg->ReadBool(_T("/highlight_occurrence/enabled"), true);
     XRCCTRL(*this, "chkHighlightOccurrences",              wxCheckBox)->SetValue(highlightEnabled);
     XRCCTRL(*this, "chkHighlightOccurrencesCaseSensitive", wxCheckBox)->SetValue(cfg->ReadBool(_T("/highlight_occurrence/case_sensitive"), true));
@@ -814,6 +816,8 @@
         cfg->Write(_T("/camel_case"),                          XRCCTRL(*this, "chkCamelCase",          wxCheckBox)->GetValue());
 
         cfg->Write(_T("/track_preprocessor"),                  XRCCTRL(*this, "chkTrackPreprocessor",  wxCheckBox)->GetValue());
+        cfg->Write(_T("/collect_prj_defines"),                 XRCCTRL(*this, "chkCollectPrjDefines",  wxCheckBox)->GetValue());
+        cfg->Write(_T("/platform_defines"),                    XRCCTRL(*this, "chkPlatDefines",        wxCheckBox)->GetValue());
         cfg->Write(_T("/highlight_wxsmith"),                   XRCCTRL(*this, "chkColoursWxSmith",     wxCheckBox)->GetValue());
 
         bool resetZoom = XRCCTRL(*this, "chkResetZoom", wxCheckBox)->GetValue();
Index: src/sdk/cbeditor.cpp
===================================================================
--- src/sdk/cbeditor.cpp    (revision 8219)
+++ src/sdk/cbeditor.cpp    (working copy)
@@ -2570,6 +2570,49 @@
     if (matchingBrace == wxSCI_INVALID_POSITION)
         matchingBrace = control->BraceMatch(control->GetCurrentPos() - 1);
 
+    // else look for a matching preprocessor command
+    if (matchingBrace == wxSCI_INVALID_POSITION)
+    {
+        wxRegEx ppIf(wxT("^[ \t]*#[ \t]*if"));
+        wxRegEx ppElse(wxT("^[ \t]*#[ \t]*el"));
+        wxRegEx ppEnd(wxT("^[ \t]*#[ \t]*endif"));
+        wxRegEx pp(wxT("^[ \t]*#[ \t]*[a-z]*")); // generic match to get length
+        if (ppIf.Matches(control->GetCurLine()) || ppElse.Matches(control->GetCurLine()))
+        {
+            int depth = 1; // search forwards
+            for (int i = control->GetCurrentLine() + 1; i < control->GetLineCount(); ++i)
+            {
+                if (ppIf.Matches(control->GetLine(i))) // ignore else's, elif's, ...
+                    ++depth;
+                else if (ppEnd.Matches(control->GetLine(i)))
+                    --depth;
+                if (depth == 0)
+                {
+                    pp.Matches(control->GetLine(i));
+                    matchingBrace = control->PositionFromLine(i) + pp.GetMatch(control->GetLine(i)).Length();
+                    break;
+                }
+            }
+        }
+        else if (ppEnd.Matches(control->GetCurLine()))
+        {
+            int depth = -1; // search backwards
+            for (int i = control->GetCurrentLine() - 1; i > 0; --i)
+            {
+                if (ppIf.Matches(control->GetLine(i))) // ignore else's, elif's, ...
+                    ++depth;
+                else if (ppEnd.Matches(control->GetLine(i)))
+                    --depth;
+                if (depth == 0)
+                {
+                    pp.Matches(control->GetLine(i));
+                    matchingBrace = control->PositionFromLine(i) + pp.GetMatch(control->GetLine(i)).Length();
+                    break;
+                }
+            }
+        }
+    }
+
     // now, we either found it or not
     if (matchingBrace != wxSCI_INVALID_POSITION)
         control->GotoPos(matchingBrace);
Index: src/sdk/wxscintilla/src/scintilla/lexers/LexCPP.cxx
===================================================================
--- src/sdk/wxscintilla/src/scintilla/lexers/LexCPP.cxx    (revision 8219)
+++ src/sdk/wxscintilla/src/scintilla/lexers/LexCPP.cxx    (working copy)
@@ -88,6 +88,11 @@
     int i =0;
     char ch = styler.SafeGetCharAt(start, '\n');
     while ((ch != '\r') && (ch != '\n')) {
+/* C::B begin */
+        if (ch == '/' && (styler.SafeGetCharAt(start + i + 1, '\n') == '/' ||
+
download for full patch...
mortenmacfly 2012-08-07 16:03

Just one note:

(editormanager is screwed anyways... but...) Next time you add a method please keep the order in the implementation file the same as is the header. Not at the bottom.

alpha0010 2012-08-07 18:05

OK; I think it is in the correct position now. (I also added a bit more to define collection.)

From my testing, the modifications in this patch (when define collection is enabled) give correct highlighting almost all of the time. Because of this and that greyed out code now remains syntax highlighted, I would recommend that both interpreting preprocessor code and define collection be enabled by default; what do you think? (They are both currently disabled by default.)

mortenmacfly 2012-08-08 05:00

BTW what doesn't work for me w/ highlighting is the wxWidgets stuff. I wonder if you should add the __WXMSW__ stuff (and others), too...?!

Oh - and btw: What, if I cross-compile? In that case I don't want i.e. the Linux defines to be set automatically, so actually the platform should be an option, too. The same (eve harder) applies to the no. of bits: Most of the time I am compiling 32 bit apps on a 64 Windows, like C::B. :-)

alpha0010 2012-08-08 12:04

From what I can tell, normally items like __WXMSW__ should be defined in the project file. When loading completes, and each time a build target is switched, the compiler switches are re-scanned for defines (assuming define collection is enabled). However, the current editor must be closed/reopened to refresh its define list (I could not yet find a workaround for this). When I tested on, for example, CodeBlocks.cbp, all (simple to moderate complexity) wxWidgets and pch related defines worked correctly.

I could additionally add some of these defines with the platform specific ones.

I forgot about cross compiling... I will add a switch to control platform specific defines.

alpha0010 2012-08-10 04:34

Platform defines now have a check-box.

I was unable to replicate your issue with wxWidgets projects, so there is nothing new here specifically targeting to fix that.

mortenmacfly 2012-08-11 10:14

Looks OK now. The reason with the wxWidgets was another one... not related to this patch. Let me do some testing and then I think I can commit...

mortenmacfly 2012-08-13 05:23

Works - Applied - Thanks! :-)