Diff of /lexers/LexHaskell.cxx [90463b] .. [923af5] Maximize Restore

  Switch to side-by-side view

--- a/lexers/LexHaskell.cxx
+++ b/lexers/LexHaskell.cxx
@@ -75,6 +75,14 @@
       || ch == '^' || ch == '|' || ch == '~' || ch == '\\');
 }
 
+static inline bool IsCommentBlockStyle(int style) {
+   return (style >= SCE_HA_COMMENTBLOCK && style <= SCE_HA_COMMENTBLOCK3);
+}
+
+static inline bool IsCommentStyle(int style) {
+   return (style >= SCE_HA_COMMENTLINE && style <= SCE_HA_COMMENTBLOCK3);
+}
+
 struct OptionsHaskell {
    bool magicHash;
    bool allowQuotes;
@@ -84,6 +92,7 @@
    bool foldComment;
    bool foldCompact;
    bool foldImports;
+   bool foldIndentedImports;
    OptionsHaskell() {
       magicHash = true;
       allowQuotes = true;
@@ -93,6 +102,7 @@
       foldComment = false;
       foldCompact = false;
       foldImports = false;
+      foldIndentedImports = true;
    }
 };
 
@@ -131,6 +141,10 @@
 
       DefineProperty("fold.haskell.imports", &OptionsHaskell::foldImports,
          "Set to 1 to enable folding of import declarations");
+
+      DefineProperty("fold.haskell.imports.indented", &OptionsHaskell::foldIndentedImports,
+         "Set this property to 0 to disable folding imports not starting at "
+         "column 0 when fold.haskell.imports=1");
 
       DefineWordListSets(haskellWordListDesc);
    }
@@ -152,9 +166,30 @@
       }
    }
 
-   inline bool LineContainsImport(int line, Accessor &styler) {
+   bool LineContainsImport(const int line, Accessor &styler) {
       if (options.foldImports) {
-         return styler.Match(styler.LineStart(line), "import");
+         int currentPos = styler.LineStart(line);
+         int style = styler.StyleAt(currentPos);
+
+         if (options.foldIndentedImports) {
+            int ch = styler.SafeGetCharAt(currentPos);
+            int eol_pos = styler.LineStart(line + 1) - 1;
+
+            while(currentPos < eol_pos) {
+               ch = styler[currentPos];
+               style = styler.StyleAt(currentPos);
+
+               if (ch == ' ' || ch == '\t'
+                || IsCommentBlockStyle(style)) {
+                  currentPos++;
+               } else {
+                  break;
+               }
+            }
+         }
+
+         return (style == SCE_HA_KEYWORD
+              && styler.Match(currentPos, "import"));
       } else {
          return false;
       }
@@ -585,10 +620,6 @@
    sc.Complete();
 }
 
-static inline bool IsCommentStyle(int style) {
-   return (style >= SCE_HA_COMMENTLINE && style <= SCE_HA_COMMENTBLOCK3);
-}
-
 static bool LineStartsWithACommentOrPreprocessor(int line, Accessor &styler) {
    int pos = styler.LineStart(line);
    int eol_pos = styler.LineStart(line + 1) - 1;
@@ -660,8 +691,8 @@
       }
       if (firstImportLine != lineCurrent) {
          indentCurrentLevel++;
-         indentCurrent = indentCurrentLevel | indentCurrentMask;
-      }
+      }
+      indentCurrent = indentCurrentLevel | indentCurrentMask;
    }
 
    // Process all characters to end of requested range
@@ -707,8 +738,8 @@
          }
          if (firstImportLine != lineNext) {
             indentNextLevel++;
-            indentNext = indentNextLevel | indentNextMask;
-         }
+         }
+         indentNext = indentNextLevel | indentNextMask;
       }
 
       const int levelBeforeComments = Maximum(indentCurrentLevel,indentNextLevel);