Skip to content

Commit

Permalink
Add code folding for CSV file, currently fold after every 100 lines, …
Browse files Browse the repository at this point in the history
…issue #685.
  • Loading branch information
zufuliu committed Jul 16, 2023
1 parent 52893de commit a305e19
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions scintilla/lexers/LexCSV.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,26 @@ namespace {
enum {
CsvOption_BackslashEscape = 1 << 15,
CsvOption_MergeDelimiter = 1 << 16,
CsvRowGroup = 100,
};

constexpr uint32_t asU4(const char *s) noexcept {
return *(const uint32_t *)s;
}

void ColouriseCSVDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int initStyle, LexerWordList /*keywordLists*/, Accessor &styler) {
const bool fold = styler.GetPropertyBool("fold");
const char * const option = styler.GetProperty("lexer.lang");
const uint32_t csvOption = asU4(option);
const uint8_t delimiter = csvOption & 0xff;
const uint8_t quoteChar = (csvOption >> 8) & 0x7f;

bool quoted = false;
int rows = 0;
initStyle = SCE_CSV_COLUMN_0;
Sci_Line lineCurrent = styler.GetLine(startPos);
if (lineCurrent > 0) {
rows = static_cast<int>(lineCurrent % CsvRowGroup);
const int lineState = styler.GetLineState(lineCurrent - 1);
if (lineState) {
quoted = true;
Expand Down Expand Up @@ -104,6 +108,16 @@ void ColouriseCSVDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int initSty
}

if (startPos == lineStartNext) {
if (fold) {
++rows;
int lev = SC_FOLDLEVELBASE + 1;
if (rows == CsvRowGroup || lineCurrent == 0) {
rows = 0;
lev = SC_FOLDLEVELBASE | SC_FOLDLEVELHEADERFLAG;
}
styler.SetLevel(lineCurrent, lev);
}

chPrev = 0;
chPrevNonWhite = delimiter;
styler.ColorTo(startPos, initStyle);
Expand Down

0 comments on commit a305e19

Please sign in to comment.