2013-08-28 00:44:27 +00:00
|
|
|
// Scintilla source code edit control
|
|
|
|
/** @file LexCoffeeScript.cxx
|
|
|
|
** Lexer for CoffeeScript.
|
|
|
|
**/
|
|
|
|
// Copyright 1998-2011 by Neil Hodgson <neilh@scintilla.org>
|
|
|
|
// Based on the Scintilla C++ Lexer
|
|
|
|
// Written by Eric Promislow <ericp@activestate.com> in 2011 for the Komodo IDE
|
|
|
|
// The License.txt file describes the conditions under which this software may be distributed.
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
#include "ILexer.h"
|
|
|
|
#include "Scintilla.h"
|
|
|
|
#include "SciLexer.h"
|
|
|
|
|
|
|
|
#include "WordList.h"
|
|
|
|
#include "LexAccessor.h"
|
|
|
|
#include "Accessor.h"
|
|
|
|
#include "StyleContext.h"
|
|
|
|
#include "CharacterSet.h"
|
|
|
|
#include "LexerModule.h"
|
|
|
|
|
|
|
|
using namespace Scintilla;
|
|
|
|
|
|
|
|
static bool IsSpaceEquiv(int state) {
|
2015-06-07 21:19:26 +00:00
|
|
|
return (state == SCE_COFFEESCRIPT_DEFAULT
|
|
|
|
|| state == SCE_COFFEESCRIPT_COMMENTLINE
|
2013-08-28 00:44:27 +00:00
|
|
|
|| state == SCE_COFFEESCRIPT_COMMENTBLOCK
|
|
|
|
|| state == SCE_COFFEESCRIPT_VERBOSE_REGEX
|
|
|
|
|| state == SCE_COFFEESCRIPT_VERBOSE_REGEX_COMMENT
|
2015-06-07 21:19:26 +00:00
|
|
|
|| state == SCE_COFFEESCRIPT_WORD
|
|
|
|
|| state == SCE_COFFEESCRIPT_REGEX);
|
2013-08-28 00:44:27 +00:00
|
|
|
}
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
// Store the current lexer state and brace count prior to starting a new
|
|
|
|
// `#{}` interpolation level.
|
|
|
|
// Based on LexRuby.cxx.
|
|
|
|
static void enterInnerExpression(int *p_inner_string_types,
|
|
|
|
int *p_inner_expn_brace_counts,
|
|
|
|
int& inner_string_count,
|
|
|
|
int state,
|
|
|
|
int& brace_counts
|
|
|
|
) {
|
|
|
|
p_inner_string_types[inner_string_count] = state;
|
|
|
|
p_inner_expn_brace_counts[inner_string_count] = brace_counts;
|
|
|
|
brace_counts = 0;
|
|
|
|
++inner_string_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore the lexer state and brace count for the previous `#{}` interpolation
|
|
|
|
// level upon returning to it.
|
|
|
|
// Note the previous lexer state is the return value and needs to be restored
|
|
|
|
// manually by the StyleContext.
|
|
|
|
// Based on LexRuby.cxx.
|
|
|
|
static int exitInnerExpression(int *p_inner_string_types,
|
|
|
|
int *p_inner_expn_brace_counts,
|
|
|
|
int& inner_string_count,
|
|
|
|
int& brace_counts
|
|
|
|
) {
|
|
|
|
--inner_string_count;
|
|
|
|
brace_counts = p_inner_expn_brace_counts[inner_string_count];
|
|
|
|
return p_inner_string_types[inner_string_count];
|
|
|
|
}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
// Preconditions: sc.currentPos points to a character after '+' or '-'.
|
|
|
|
// The test for pos reaching 0 should be redundant,
|
|
|
|
// and is in only for safety measures.
|
|
|
|
// Limitation: this code will give the incorrect answer for code like
|
|
|
|
// a = b+++/ptn/...
|
|
|
|
// Putting a space between the '++' post-inc operator and the '+' binary op
|
|
|
|
// fixes this, and is highly recommended for readability anyway.
|
|
|
|
static bool FollowsPostfixOperator(StyleContext &sc, Accessor &styler) {
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_Position pos = (Sci_Position) sc.currentPos;
|
2013-08-28 00:44:27 +00:00
|
|
|
while (--pos > 0) {
|
|
|
|
char ch = styler[pos];
|
|
|
|
if (ch == '+' || ch == '-') {
|
|
|
|
return styler[pos - 1] == ch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
static bool followsKeyword(StyleContext &sc, Accessor &styler) {
|
|
|
|
Sci_Position pos = (Sci_Position) sc.currentPos;
|
|
|
|
Sci_Position currentLine = styler.GetLine(pos);
|
|
|
|
Sci_Position lineStartPos = styler.LineStart(currentLine);
|
2013-08-28 00:44:27 +00:00
|
|
|
while (--pos > lineStartPos) {
|
2015-06-07 21:19:26 +00:00
|
|
|
char ch = styler.SafeGetCharAt(pos);
|
2013-08-28 00:44:27 +00:00
|
|
|
if (ch != ' ' && ch != '\t') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-05-04 18:14:48 +00:00
|
|
|
styler.Flush();
|
|
|
|
return styler.StyleAt(pos) == SCE_COFFEESCRIPT_WORD;
|
2013-08-28 00:44:27 +00:00
|
|
|
}
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
static void ColouriseCoffeeScriptDoc(Sci_PositionU startPos, Sci_Position length, int initStyle, WordList *keywordlists[],
|
2013-08-28 00:44:27 +00:00
|
|
|
Accessor &styler) {
|
|
|
|
|
|
|
|
WordList &keywords = *keywordlists[0];
|
|
|
|
WordList &keywords2 = *keywordlists[1];
|
|
|
|
WordList &keywords4 = *keywordlists[3];
|
|
|
|
|
|
|
|
CharacterSet setOKBeforeRE(CharacterSet::setNone, "([{=,:;!%^&*|?~+-");
|
|
|
|
CharacterSet setCouldBePostOp(CharacterSet::setNone, "+-");
|
|
|
|
|
2015-06-07 21:19:26 +00:00
|
|
|
CharacterSet setWordStart(CharacterSet::setAlpha, "_$@", 0x80, true);
|
|
|
|
CharacterSet setWord(CharacterSet::setAlphaNum, "._$", 0x80, true);
|
2013-08-28 00:44:27 +00:00
|
|
|
|
|
|
|
int chPrevNonWhite = ' ';
|
|
|
|
int visibleChars = 0;
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
// String/Regex interpolation variables, based on LexRuby.cxx.
|
|
|
|
// In most cases a value of 2 should be ample for the code the user is
|
|
|
|
// likely to enter. For example,
|
|
|
|
// "Filling the #{container} with #{liquid}..."
|
|
|
|
// from the CoffeeScript homepage nests to a level of 2
|
|
|
|
// If the user actually hits a 6th occurrence of '#{' in a double-quoted
|
|
|
|
// string (including regexes), it will stay as a string. The problem with
|
|
|
|
// this is that quotes might flip, a 7th '#{' will look like a comment,
|
|
|
|
// and code-folding might be wrong.
|
|
|
|
#define INNER_STRINGS_MAX_COUNT 5
|
|
|
|
// These vars track our instances of "...#{,,,'..#{,,,}...',,,}..."
|
|
|
|
int inner_string_types[INNER_STRINGS_MAX_COUNT];
|
|
|
|
// Track # braces when we push a new #{ thing
|
|
|
|
int inner_expn_brace_counts[INNER_STRINGS_MAX_COUNT];
|
|
|
|
int inner_string_count = 0;
|
|
|
|
int brace_counts = 0; // Number of #{ ... } things within an expression
|
|
|
|
for (int i = 0; i < INNER_STRINGS_MAX_COUNT; i++) {
|
|
|
|
inner_string_types[i] = 0;
|
|
|
|
inner_expn_brace_counts[i] = 0;
|
|
|
|
}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
// look back to set chPrevNonWhite properly for better regex colouring
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_Position endPos = startPos + length;
|
2013-08-28 00:44:27 +00:00
|
|
|
if (startPos > 0 && IsSpaceEquiv(initStyle)) {
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_PositionU back = startPos;
|
2013-08-28 00:44:27 +00:00
|
|
|
styler.Flush();
|
|
|
|
while (back > 0 && IsSpaceEquiv(styler.StyleAt(--back)))
|
|
|
|
;
|
2015-06-07 21:19:26 +00:00
|
|
|
if (styler.StyleAt(back) == SCE_COFFEESCRIPT_OPERATOR) {
|
2013-08-28 00:44:27 +00:00
|
|
|
chPrevNonWhite = styler.SafeGetCharAt(back);
|
|
|
|
}
|
|
|
|
if (startPos != back) {
|
|
|
|
initStyle = styler.StyleAt(back);
|
|
|
|
if (IsSpaceEquiv(initStyle)) {
|
2015-06-07 21:19:26 +00:00
|
|
|
initStyle = SCE_COFFEESCRIPT_DEFAULT;
|
2013-08-28 00:44:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
startPos = back;
|
|
|
|
}
|
|
|
|
|
|
|
|
StyleContext sc(startPos, endPos - startPos, initStyle, styler);
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
for (; sc.More();) {
|
2013-08-28 00:44:27 +00:00
|
|
|
|
|
|
|
if (sc.atLineStart) {
|
2015-06-07 21:19:26 +00:00
|
|
|
// Reset states to beginning of colourise so no surprises
|
2013-08-28 00:44:27 +00:00
|
|
|
// if different sets of lines lexed.
|
|
|
|
visibleChars = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine if the current state should terminate.
|
|
|
|
switch (sc.state) {
|
2015-06-07 21:19:26 +00:00
|
|
|
case SCE_COFFEESCRIPT_OPERATOR:
|
|
|
|
sc.SetState(SCE_COFFEESCRIPT_DEFAULT);
|
2013-08-28 00:44:27 +00:00
|
|
|
break;
|
2015-06-07 21:19:26 +00:00
|
|
|
case SCE_COFFEESCRIPT_NUMBER:
|
2013-08-28 00:44:27 +00:00
|
|
|
// We accept almost anything because of hex. and number suffixes
|
2015-06-07 21:19:26 +00:00
|
|
|
if (!setWord.Contains(sc.ch) || sc.Match('.', '.')) {
|
|
|
|
sc.SetState(SCE_COFFEESCRIPT_DEFAULT);
|
2013-08-28 00:44:27 +00:00
|
|
|
}
|
|
|
|
break;
|
2015-06-07 21:19:26 +00:00
|
|
|
case SCE_COFFEESCRIPT_IDENTIFIER:
|
2013-08-28 00:44:27 +00:00
|
|
|
if (!setWord.Contains(sc.ch) || (sc.ch == '.') || (sc.ch == '$')) {
|
|
|
|
char s[1000];
|
|
|
|
sc.GetCurrent(s, sizeof(s));
|
|
|
|
if (keywords.InList(s)) {
|
2015-06-07 21:19:26 +00:00
|
|
|
sc.ChangeState(SCE_COFFEESCRIPT_WORD);
|
2013-08-28 00:44:27 +00:00
|
|
|
} else if (keywords2.InList(s)) {
|
2015-06-07 21:19:26 +00:00
|
|
|
sc.ChangeState(SCE_COFFEESCRIPT_WORD2);
|
2013-08-28 00:44:27 +00:00
|
|
|
} else if (keywords4.InList(s)) {
|
2015-06-07 21:19:26 +00:00
|
|
|
sc.ChangeState(SCE_COFFEESCRIPT_GLOBALCLASS);
|
2019-05-04 18:14:48 +00:00
|
|
|
} else if (sc.LengthCurrent() > 0 && s[0] == '@') {
|
|
|
|
sc.ChangeState(SCE_COFFEESCRIPT_INSTANCEPROPERTY);
|
2013-08-28 00:44:27 +00:00
|
|
|
}
|
2015-06-07 21:19:26 +00:00
|
|
|
sc.SetState(SCE_COFFEESCRIPT_DEFAULT);
|
2013-08-28 00:44:27 +00:00
|
|
|
}
|
|
|
|
break;
|
2015-06-07 21:19:26 +00:00
|
|
|
case SCE_COFFEESCRIPT_WORD:
|
|
|
|
case SCE_COFFEESCRIPT_WORD2:
|
|
|
|
case SCE_COFFEESCRIPT_GLOBALCLASS:
|
2019-05-04 18:14:48 +00:00
|
|
|
case SCE_COFFEESCRIPT_INSTANCEPROPERTY:
|
2015-06-07 21:19:26 +00:00
|
|
|
if (!setWord.Contains(sc.ch)) {
|
|
|
|
sc.SetState(SCE_COFFEESCRIPT_DEFAULT);
|
2013-08-28 00:44:27 +00:00
|
|
|
}
|
|
|
|
break;
|
2015-06-07 21:19:26 +00:00
|
|
|
case SCE_COFFEESCRIPT_COMMENTLINE:
|
2013-08-28 00:44:27 +00:00
|
|
|
if (sc.atLineStart) {
|
2015-06-07 21:19:26 +00:00
|
|
|
sc.SetState(SCE_COFFEESCRIPT_DEFAULT);
|
2013-08-28 00:44:27 +00:00
|
|
|
}
|
|
|
|
break;
|
2015-06-07 21:19:26 +00:00
|
|
|
case SCE_COFFEESCRIPT_STRING:
|
|
|
|
if (sc.ch == '\\') {
|
2013-08-28 00:44:27 +00:00
|
|
|
if (sc.chNext == '\"' || sc.chNext == '\'' || sc.chNext == '\\') {
|
|
|
|
sc.Forward();
|
|
|
|
}
|
|
|
|
} else if (sc.ch == '\"') {
|
2015-06-07 21:19:26 +00:00
|
|
|
sc.ForwardSetState(SCE_COFFEESCRIPT_DEFAULT);
|
2019-05-04 18:14:48 +00:00
|
|
|
} else if (sc.ch == '#' && sc.chNext == '{' && inner_string_count < INNER_STRINGS_MAX_COUNT) {
|
|
|
|
// process interpolated code #{ ... }
|
|
|
|
enterInnerExpression(inner_string_types,
|
|
|
|
inner_expn_brace_counts,
|
|
|
|
inner_string_count,
|
|
|
|
sc.state,
|
|
|
|
brace_counts);
|
|
|
|
sc.SetState(SCE_COFFEESCRIPT_OPERATOR);
|
|
|
|
sc.ForwardSetState(SCE_COFFEESCRIPT_DEFAULT);
|
2013-08-28 00:44:27 +00:00
|
|
|
}
|
|
|
|
break;
|
2015-06-07 21:19:26 +00:00
|
|
|
case SCE_COFFEESCRIPT_CHARACTER:
|
2013-08-28 00:44:27 +00:00
|
|
|
if (sc.ch == '\\') {
|
|
|
|
if (sc.chNext == '\"' || sc.chNext == '\'' || sc.chNext == '\\') {
|
|
|
|
sc.Forward();
|
|
|
|
}
|
|
|
|
} else if (sc.ch == '\'') {
|
2015-06-07 21:19:26 +00:00
|
|
|
sc.ForwardSetState(SCE_COFFEESCRIPT_DEFAULT);
|
2013-08-28 00:44:27 +00:00
|
|
|
}
|
|
|
|
break;
|
2015-06-07 21:19:26 +00:00
|
|
|
case SCE_COFFEESCRIPT_REGEX:
|
2013-08-28 00:44:27 +00:00
|
|
|
if (sc.atLineStart) {
|
2015-06-07 21:19:26 +00:00
|
|
|
sc.SetState(SCE_COFFEESCRIPT_DEFAULT);
|
2013-08-28 00:44:27 +00:00
|
|
|
} else if (sc.ch == '/') {
|
|
|
|
sc.Forward();
|
|
|
|
while ((sc.ch < 0x80) && islower(sc.ch))
|
|
|
|
sc.Forward(); // gobble regex flags
|
2015-06-07 21:19:26 +00:00
|
|
|
sc.SetState(SCE_COFFEESCRIPT_DEFAULT);
|
2013-08-28 00:44:27 +00:00
|
|
|
} else if (sc.ch == '\\') {
|
|
|
|
// Gobble up the quoted character
|
|
|
|
if (sc.chNext == '\\' || sc.chNext == '/') {
|
|
|
|
sc.Forward();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2015-06-07 21:19:26 +00:00
|
|
|
case SCE_COFFEESCRIPT_STRINGEOL:
|
2013-08-28 00:44:27 +00:00
|
|
|
if (sc.atLineStart) {
|
2015-06-07 21:19:26 +00:00
|
|
|
sc.SetState(SCE_COFFEESCRIPT_DEFAULT);
|
2013-08-28 00:44:27 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SCE_COFFEESCRIPT_COMMENTBLOCK:
|
|
|
|
if (sc.Match("###")) {
|
|
|
|
sc.Forward();
|
|
|
|
sc.Forward();
|
2015-06-07 21:19:26 +00:00
|
|
|
sc.ForwardSetState(SCE_COFFEESCRIPT_DEFAULT);
|
2013-08-28 00:44:27 +00:00
|
|
|
} else if (sc.ch == '\\') {
|
|
|
|
sc.Forward();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SCE_COFFEESCRIPT_VERBOSE_REGEX:
|
|
|
|
if (sc.Match("///")) {
|
|
|
|
sc.Forward();
|
|
|
|
sc.Forward();
|
2015-06-07 21:19:26 +00:00
|
|
|
sc.ForwardSetState(SCE_COFFEESCRIPT_DEFAULT);
|
2013-08-28 00:44:27 +00:00
|
|
|
} else if (sc.Match('#')) {
|
|
|
|
sc.SetState(SCE_COFFEESCRIPT_VERBOSE_REGEX_COMMENT);
|
|
|
|
} else if (sc.ch == '\\') {
|
|
|
|
sc.Forward();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SCE_COFFEESCRIPT_VERBOSE_REGEX_COMMENT:
|
|
|
|
if (sc.atLineStart) {
|
|
|
|
sc.SetState(SCE_COFFEESCRIPT_VERBOSE_REGEX);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine if a new state should be entered.
|
2015-06-07 21:19:26 +00:00
|
|
|
if (sc.state == SCE_COFFEESCRIPT_DEFAULT) {
|
|
|
|
if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
|
|
|
|
sc.SetState(SCE_COFFEESCRIPT_NUMBER);
|
|
|
|
} else if (setWordStart.Contains(sc.ch)) {
|
|
|
|
sc.SetState(SCE_COFFEESCRIPT_IDENTIFIER);
|
2013-08-28 00:44:27 +00:00
|
|
|
} else if (sc.Match("///")) {
|
|
|
|
sc.SetState(SCE_COFFEESCRIPT_VERBOSE_REGEX);
|
|
|
|
sc.Forward();
|
|
|
|
sc.Forward();
|
|
|
|
} else if (sc.ch == '/'
|
|
|
|
&& (setOKBeforeRE.Contains(chPrevNonWhite)
|
2019-05-04 18:14:48 +00:00
|
|
|
|| followsKeyword(sc, styler))
|
2013-08-28 00:44:27 +00:00
|
|
|
&& (!setCouldBePostOp.Contains(chPrevNonWhite)
|
|
|
|
|| !FollowsPostfixOperator(sc, styler))) {
|
2015-06-07 21:19:26 +00:00
|
|
|
sc.SetState(SCE_COFFEESCRIPT_REGEX); // JavaScript's RegEx
|
2013-08-28 00:44:27 +00:00
|
|
|
} else if (sc.ch == '\"') {
|
2015-06-07 21:19:26 +00:00
|
|
|
sc.SetState(SCE_COFFEESCRIPT_STRING);
|
2013-08-28 00:44:27 +00:00
|
|
|
} else if (sc.ch == '\'') {
|
2015-06-07 21:19:26 +00:00
|
|
|
sc.SetState(SCE_COFFEESCRIPT_CHARACTER);
|
2013-08-28 00:44:27 +00:00
|
|
|
} else if (sc.ch == '#') {
|
|
|
|
if (sc.Match("###")) {
|
|
|
|
sc.SetState(SCE_COFFEESCRIPT_COMMENTBLOCK);
|
|
|
|
sc.Forward();
|
|
|
|
sc.Forward();
|
|
|
|
} else {
|
2015-06-07 21:19:26 +00:00
|
|
|
sc.SetState(SCE_COFFEESCRIPT_COMMENTLINE);
|
2013-08-28 00:44:27 +00:00
|
|
|
}
|
|
|
|
} else if (isoperator(static_cast<char>(sc.ch))) {
|
2015-06-07 21:19:26 +00:00
|
|
|
sc.SetState(SCE_COFFEESCRIPT_OPERATOR);
|
2019-05-04 18:14:48 +00:00
|
|
|
// Handle '..' and '...' operators correctly.
|
|
|
|
if (sc.ch == '.') {
|
|
|
|
for (int i = 0; i < 2 && sc.chNext == '.'; i++, sc.Forward()) ;
|
|
|
|
} else if (sc.ch == '{') {
|
|
|
|
++brace_counts;
|
|
|
|
} else if (sc.ch == '}' && --brace_counts <= 0 && inner_string_count > 0) {
|
|
|
|
// Return to previous state before #{ ... }
|
|
|
|
sc.ForwardSetState(exitInnerExpression(inner_string_types,
|
|
|
|
inner_expn_brace_counts,
|
|
|
|
inner_string_count,
|
|
|
|
brace_counts));
|
|
|
|
continue; // skip sc.Forward() at loop end
|
|
|
|
}
|
2013-08-28 00:44:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IsASpace(sc.ch) && !IsSpaceEquiv(sc.state)) {
|
|
|
|
chPrevNonWhite = sc.ch;
|
|
|
|
visibleChars++;
|
|
|
|
}
|
2019-05-04 18:14:48 +00:00
|
|
|
sc.Forward();
|
2013-08-28 00:44:27 +00:00
|
|
|
}
|
|
|
|
sc.Complete();
|
|
|
|
}
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
static bool IsCommentLine(Sci_Position line, Accessor &styler) {
|
|
|
|
Sci_Position pos = styler.LineStart(line);
|
|
|
|
Sci_Position eol_pos = styler.LineStart(line + 1) - 1;
|
|
|
|
for (Sci_Position i = pos; i < eol_pos; i++) {
|
2013-08-28 00:44:27 +00:00
|
|
|
char ch = styler[i];
|
|
|
|
if (ch == '#')
|
|
|
|
return true;
|
|
|
|
else if (ch != ' ' && ch != '\t')
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
static void FoldCoffeeScriptDoc(Sci_PositionU startPos, Sci_Position length, int,
|
2013-08-28 00:44:27 +00:00
|
|
|
WordList *[], Accessor &styler) {
|
|
|
|
// A simplified version of FoldPyDoc
|
2019-05-04 18:14:48 +00:00
|
|
|
const Sci_Position maxPos = startPos + length;
|
|
|
|
const Sci_Position maxLines = styler.GetLine(maxPos - 1); // Requested last line
|
|
|
|
const Sci_Position docLines = styler.GetLine(styler.Length() - 1); // Available last line
|
2013-08-28 00:44:27 +00:00
|
|
|
|
|
|
|
// property fold.coffeescript.comment
|
|
|
|
const bool foldComment = styler.GetPropertyInt("fold.coffeescript.comment") != 0;
|
|
|
|
|
|
|
|
const bool foldCompact = styler.GetPropertyInt("fold.compact") != 0;
|
|
|
|
|
|
|
|
// Backtrack to previous non-blank line so we can determine indent level
|
|
|
|
// for any white space lines
|
|
|
|
// and so we can fix any preceding fold level (which is why we go back
|
|
|
|
// at least one line in all cases)
|
|
|
|
int spaceFlags = 0;
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_Position lineCurrent = styler.GetLine(startPos);
|
2013-08-28 00:44:27 +00:00
|
|
|
int indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags, NULL);
|
|
|
|
while (lineCurrent > 0) {
|
|
|
|
lineCurrent--;
|
|
|
|
indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags, NULL);
|
|
|
|
if (!(indentCurrent & SC_FOLDLEVELWHITEFLAG)
|
|
|
|
&& !IsCommentLine(lineCurrent, styler))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
int indentCurrentLevel = indentCurrent & SC_FOLDLEVELNUMBERMASK;
|
|
|
|
|
|
|
|
// Set up initial loop state
|
|
|
|
int prevComment = 0;
|
|
|
|
if (lineCurrent >= 1)
|
|
|
|
prevComment = foldComment && IsCommentLine(lineCurrent - 1, styler);
|
|
|
|
|
|
|
|
// Process all characters to end of requested range
|
|
|
|
// or comment that hangs over the end of the range. Cap processing in all cases
|
|
|
|
// to end of document (in case of comment at end).
|
|
|
|
while ((lineCurrent <= docLines) && ((lineCurrent <= maxLines) || prevComment)) {
|
|
|
|
|
|
|
|
// Gather info
|
|
|
|
int lev = indentCurrent;
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_Position lineNext = lineCurrent + 1;
|
2013-08-28 00:44:27 +00:00
|
|
|
int indentNext = indentCurrent;
|
|
|
|
if (lineNext <= docLines) {
|
|
|
|
// Information about next line is only available if not at end of document
|
|
|
|
indentNext = styler.IndentAmount(lineNext, &spaceFlags, NULL);
|
|
|
|
}
|
|
|
|
const int comment = foldComment && IsCommentLine(lineCurrent, styler);
|
|
|
|
const int comment_start = (comment && !prevComment && (lineNext <= docLines) &&
|
|
|
|
IsCommentLine(lineNext, styler) && (lev > SC_FOLDLEVELBASE));
|
|
|
|
const int comment_continue = (comment && prevComment);
|
|
|
|
if (!comment)
|
|
|
|
indentCurrentLevel = indentCurrent & SC_FOLDLEVELNUMBERMASK;
|
|
|
|
if (indentNext & SC_FOLDLEVELWHITEFLAG)
|
|
|
|
indentNext = SC_FOLDLEVELWHITEFLAG | indentCurrentLevel;
|
|
|
|
|
|
|
|
if (comment_start) {
|
|
|
|
// Place fold point at start of a block of comments
|
|
|
|
lev |= SC_FOLDLEVELHEADERFLAG;
|
|
|
|
} else if (comment_continue) {
|
|
|
|
// Add level to rest of lines in the block
|
|
|
|
lev = lev + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip past any blank lines for next indent level info; we skip also
|
|
|
|
// comments (all comments, not just those starting in column 0)
|
|
|
|
// which effectively folds them into surrounding code rather
|
|
|
|
// than screwing up folding.
|
|
|
|
|
|
|
|
while ((lineNext < docLines) &&
|
|
|
|
((indentNext & SC_FOLDLEVELWHITEFLAG) ||
|
|
|
|
(lineNext <= docLines && IsCommentLine(lineNext, styler)))) {
|
|
|
|
|
|
|
|
lineNext++;
|
|
|
|
indentNext = styler.IndentAmount(lineNext, &spaceFlags, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
const int levelAfterComments = indentNext & SC_FOLDLEVELNUMBERMASK;
|
2019-05-04 18:14:48 +00:00
|
|
|
const int levelBeforeComments = std::max(indentCurrentLevel,levelAfterComments);
|
2013-08-28 00:44:27 +00:00
|
|
|
|
|
|
|
// Now set all the indent levels on the lines we skipped
|
|
|
|
// Do this from end to start. Once we encounter one line
|
|
|
|
// which is indented more than the line after the end of
|
|
|
|
// the comment-block, use the level of the block before
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_Position skipLine = lineNext;
|
2013-08-28 00:44:27 +00:00
|
|
|
int skipLevel = levelAfterComments;
|
|
|
|
|
|
|
|
while (--skipLine > lineCurrent) {
|
|
|
|
int skipLineIndent = styler.IndentAmount(skipLine, &spaceFlags, NULL);
|
|
|
|
|
|
|
|
if (foldCompact) {
|
|
|
|
if ((skipLineIndent & SC_FOLDLEVELNUMBERMASK) > levelAfterComments)
|
|
|
|
skipLevel = levelBeforeComments;
|
|
|
|
|
|
|
|
int whiteFlag = skipLineIndent & SC_FOLDLEVELWHITEFLAG;
|
|
|
|
|
|
|
|
styler.SetLevel(skipLine, skipLevel | whiteFlag);
|
|
|
|
} else {
|
|
|
|
if ((skipLineIndent & SC_FOLDLEVELNUMBERMASK) > levelAfterComments &&
|
|
|
|
!(skipLineIndent & SC_FOLDLEVELWHITEFLAG) &&
|
|
|
|
!IsCommentLine(skipLine, styler))
|
|
|
|
skipLevel = levelBeforeComments;
|
|
|
|
|
|
|
|
styler.SetLevel(skipLine, skipLevel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set fold header on non-comment line
|
|
|
|
if (!comment && !(indentCurrent & SC_FOLDLEVELWHITEFLAG)) {
|
|
|
|
if ((indentCurrent & SC_FOLDLEVELNUMBERMASK) < (indentNext & SC_FOLDLEVELNUMBERMASK))
|
|
|
|
lev |= SC_FOLDLEVELHEADERFLAG;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep track of block comment state of previous line
|
|
|
|
prevComment = comment_start || comment_continue;
|
|
|
|
|
|
|
|
// Set fold level for this line and move to next line
|
|
|
|
styler.SetLevel(lineCurrent, lev);
|
|
|
|
indentCurrent = indentNext;
|
|
|
|
lineCurrent = lineNext;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *const csWordLists[] = {
|
|
|
|
"Keywords",
|
2015-06-07 21:19:26 +00:00
|
|
|
"Secondary keywords",
|
|
|
|
"Unused",
|
|
|
|
"Global classes",
|
2013-08-28 00:44:27 +00:00
|
|
|
0,
|
|
|
|
};
|
|
|
|
|
|
|
|
LexerModule lmCoffeeScript(SCLEX_COFFEESCRIPT, ColouriseCoffeeScriptDoc, "coffeescript", FoldCoffeeScriptDoc, csWordLists);
|