From 3e0548caa2cf7d6b32cc029dbc9044ef877f6cee Mon Sep 17 00:00:00 2001 From: pommicket Date: Thu, 5 Jan 2023 13:18:08 -0500 Subject: allow `-delimited multiline strings in ted.cfg also fix multiline string highlighting of keywords/# in config files --- config.c | 13 +++++----- main.c | 5 ++-- syntax.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++------- ted.h | 58 ----------------------------------------- 4 files changed, 91 insertions(+), 76 deletions(-) diff --git a/config.c b/config.c index 41dc3dd..b49b031 100644 --- a/config.c +++ b/config.c @@ -521,10 +521,10 @@ static i64 config_read_string(Ted *ted, ConfigReader *cfg, char **ptext) { char *p; int backslashes = 0; u32 start_line = cfg->line_number; + char delimiter = **ptext; char *start = *ptext + 1; char *str = NULL; for (p = start; ; ++p) { - bool done = false; switch (*p) { case '\\': ++backslashes; @@ -532,6 +532,7 @@ static i64 config_read_string(Ted *ted, ConfigReader *cfg, char **ptext) { switch (*p) { case '\\': case '"': + case '`': break; case 'n': arr_add(str, '\n'); @@ -551,9 +552,6 @@ static i64 config_read_string(Ted *ted, ConfigReader *cfg, char **ptext) { return -1; } break; - case '"': - done = true; - break; case '\n': ++cfg->line_number; break; @@ -565,7 +563,8 @@ static i64 config_read_string(Ted *ted, ConfigReader *cfg, char **ptext) { arr_clear(str); return -1; } - if (done) break; + if (*p == delimiter) + break; arr_add(str, *p); } @@ -747,7 +746,7 @@ static void config_parse_line(ConfigReader *cfg, Settings *settings, const Confi char *endp; argument = strtoll(value, &endp, 10); value = endp; - } else if (*value == '"') { + } else if (*value == '"' || *value == '`') { // string argument // restore newline to handle multi-line strings @@ -816,7 +815,7 @@ static void config_parse_line(ConfigReader *cfg, Settings *settings, const Confi boolean = false; } - if (value[0] == '"') { + if (value[0] == '"' || value[0] == '`') { // restore newline to handle multi-line strings // a little bit hacky oh well *newline = '\n'; diff --git a/main.c b/main.c index 0a9e379..87c76c6 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,8 @@ /* @TODO: -- can we display publishDiagnostics notifications from rust-analyzer (& others)? +- LSP configuration in ted.cfg + - remove LSP_REQUEST_JDTLS_CONFIGURATION + - disable publishDiagnostics for rust-analyzer (& others)? - phantom completions - debug-lsp option (which logs LSP messages) - check LSP process status (TEST: what happens if LSP server is not installed) @@ -13,7 +15,6 @@ - grep -i -n TODO *.[ch] - when searching files, put exact matches at the top - auto-set build command for cmake (both for windows and unix) ---- LSP MERGE --- - some way of opening + closing all C files in directory for clangd textDocument/references to work? - maybe it can be done with the clangd config instead. diff --git a/syntax.c b/syntax.c index 1e4f084..9b60d6a 100644 --- a/syntax.c +++ b/syntax.c @@ -6,6 +6,65 @@ // all characters that can appear in a number #define SYNTAX_DIGITS "0123456789.xXoObBlLuUiIabcdefABCDEF_" + +// ---- syntax state constants ---- +// syntax state is explained in development.md + +// these all say "CPP" but really they're C/C++ +enum { + SYNTAX_STATE_CPP_MULTI_LINE_COMMENT = 0x1u, // are we in a multi-line comment? (delineated by /* */) + SYNTAX_STATE_CPP_SINGLE_LINE_COMMENT = 0x2u, // if you add a \ to the end of a single-line comment, it is continued to the next line. + SYNTAX_STATE_CPP_PREPROCESSOR = 0x4u, // similar to above + SYNTAX_STATE_CPP_STRING = 0x8u, + SYNTAX_STATE_CPP_RAW_STRING = 0x10u, +}; + +enum { + SYNTAX_STATE_RUST_COMMENT_DEPTH_MASK = 0xfu, // in rust, /* */ comments can nest. + SYNTAX_STATE_RUST_COMMENT_DEPTH_MUL = 0x1u, + SYNTAX_STATE_RUST_COMMENT_DEPTH_BITS = 4, // number of bits we allocate for the comment depth. + SYNTAX_STATE_RUST_STRING = 0x10u, + SYNTAX_STATE_RUST_STRING_IS_RAW = 0x20u, +}; + +enum { + SYNTAX_STATE_PYTHON_STRING = 0x01u, // multiline strings (''' and """) + SYNTAX_STATE_PYTHON_STRING_DBL_QUOTED = 0x02u, // is this a """ string, as opposed to a ''' string? +}; + +enum { + SYNTAX_STATE_TEX_DOLLAR = 0x01u, // inside math $ ... $ + SYNTAX_STATE_TEX_DOLLARDOLLAR = 0x02u, // inside math $$ ... $$ + SYNTAX_STATE_TEX_VERBATIM = 0x04u, // inside \begin{verbatim} ... \end{verbatim} +}; + +enum { + SYNTAX_STATE_MARKDOWN_CODE = 0x01u, // inside ``` ``` code section +}; + +enum { + SYNTAX_STATE_HTML_COMMENT = 0x01u +}; + +enum { + SYNTAX_STATE_JAVASCRIPT_TEMPLATE_STRING = 0x01u, + SYNTAX_STATE_JAVASCRIPT_MULTILINE_COMMENT = 0x02u, +}; + +enum { + SYNTAX_STATE_JAVA_MULTILINE_COMMENT = 0x01u +}; + +enum { + SYNTAX_STATE_GO_RAW_STRING = 0x01u, // backtick-enclosed string + SYNTAX_STATE_GO_MULTILINE_COMMENT = 0x02u +}; + +enum { + SYNTAX_STATE_TED_CFG_STRING = 0x01u, // ` or "-delimited string + SYNTAX_STATE_TED_CFG_STRING_BACKTICK = 0x01u, // `-delimited string +}; + typedef struct { Language lang; const char *name; @@ -1146,6 +1205,7 @@ static void syntax_highlight_xml(SyntaxState *state, const char32_t *line, u32 l static void syntax_highlight_config(SyntaxState *state, const char32_t *line, u32 line_len, SyntaxCharType *char_types, bool is_ted_cfg) { bool string = (*state & SYNTAX_STATE_TED_CFG_STRING) != 0; + char32_t string_delimiter = (*state & SYNTAX_STATE_TED_CFG_STRING_BACKTICK) ? '`' : '"'; if (line_len == 0) return; @@ -1165,21 +1225,27 @@ static void syntax_highlight_config(SyntaxState *state, const char32_t *line, u3 char_types[i] = string ? SYNTAX_STRING : SYNTAX_NORMAL; switch (line[i]) { case '"': - if (string && backslashes % 2 == 0) { - string = false; + case '`': + if (string) { + if (backslashes % 2 == 0 && line[i] == string_delimiter) { + string = false; + } } else { string = true; + string_delimiter = line[i]; } if (char_types) char_types[i] = SYNTAX_STRING; break; case '#': - // don't try highlighting the rest of the line. - // for ted.cfg, this could be a color, but for other cfg files, - // it might be a comment - if (char_types) - memset(&char_types[i], 0, line_len - i); - i = line_len; + if (!string) { + // don't try highlighting the rest of the line. + // for ted.cfg, this could be a color, but for other cfg files, + // it might be a comment + if (char_types) + memset(&char_types[i], 0, line_len - i); + i = line_len; + } break; case ANY_DIGIT: if (char_types && i > 0 && !string) { @@ -1194,6 +1260,8 @@ static void syntax_highlight_config(SyntaxState *state, const char32_t *line, u3 default: { if (!char_types) break; // don't care + if (string) + break; if (i == 0) // none of the keywords in syntax_all_keywords_config should appear at the start of the line break; if (is32_word(line[i-1]) || line[i-1] == '-' || !is32_word(line[i])) @@ -1219,7 +1287,12 @@ static void syntax_highlight_config(SyntaxState *state, const char32_t *line, u3 } if (is_ted_cfg) { - *state = SYNTAX_STATE_TED_CFG_STRING * string; + *state = 0; + if (string) { + *state |= SYNTAX_STATE_TED_CFG_STRING; + if (string_delimiter == '`') + *state |= SYNTAX_STATE_TED_CFG_STRING_BACKTICK; + } } } diff --git a/ted.h b/ted.h index 0443087..1bc3fce 100644 --- a/ted.h +++ b/ted.h @@ -26,64 +26,6 @@ // max number of LSPs running at once #define TED_LSP_MAX 200 - -// ---- syntax state constants ---- -// syntax state is explained in development.md - -// these all say "CPP" but really they're C/C++ -enum { - SYNTAX_STATE_CPP_MULTI_LINE_COMMENT = 0x1u, // are we in a multi-line comment? (delineated by /* */) - SYNTAX_STATE_CPP_SINGLE_LINE_COMMENT = 0x2u, // if you add a \ to the end of a single-line comment, it is continued to the next line. - SYNTAX_STATE_CPP_PREPROCESSOR = 0x4u, // similar to above - SYNTAX_STATE_CPP_STRING = 0x8u, - SYNTAX_STATE_CPP_RAW_STRING = 0x10u, -}; - -enum { - SYNTAX_STATE_RUST_COMMENT_DEPTH_MASK = 0xfu, // in rust, /* */ comments can nest. - SYNTAX_STATE_RUST_COMMENT_DEPTH_MUL = 0x1u, - SYNTAX_STATE_RUST_COMMENT_DEPTH_BITS = 4, // number of bits we allocate for the comment depth. - SYNTAX_STATE_RUST_STRING = 0x10u, - SYNTAX_STATE_RUST_STRING_IS_RAW = 0x20u, -}; - -enum { - SYNTAX_STATE_PYTHON_STRING = 0x01u, // multiline strings (''' and """) - SYNTAX_STATE_PYTHON_STRING_DBL_QUOTED = 0x02u, // is this a """ string, as opposed to a ''' string? -}; - -enum { - SYNTAX_STATE_TEX_DOLLAR = 0x01u, // inside math $ ... $ - SYNTAX_STATE_TEX_DOLLARDOLLAR = 0x02u, // inside math $$ ... $$ - SYNTAX_STATE_TEX_VERBATIM = 0x04u, // inside \begin{verbatim} ... \end{verbatim} -}; - -enum { - SYNTAX_STATE_MARKDOWN_CODE = 0x01u, // inside ``` ``` code section -}; - -enum { - SYNTAX_STATE_HTML_COMMENT = 0x01u -}; - -enum { - SYNTAX_STATE_JAVASCRIPT_TEMPLATE_STRING = 0x01u, - SYNTAX_STATE_JAVASCRIPT_MULTILINE_COMMENT = 0x02u, -}; - -enum { - SYNTAX_STATE_JAVA_MULTILINE_COMMENT = 0x01u -}; - -enum { - SYNTAX_STATE_GO_RAW_STRING = 0x01u, // backtick-enclosed string - SYNTAX_STATE_GO_MULTILINE_COMMENT = 0x02u -}; - -enum { - SYNTAX_STATE_TED_CFG_STRING = 0x01u, -}; - typedef u8 SyntaxState; // types of syntax highlighting -- cgit v1.2.3