From c9907c20599728286e73115d11b46b169d10e1a3 Mon Sep 17 00:00:00 2001 From: pommicket Date: Tue, 27 Dec 2022 14:39:01 -0500 Subject: add typescript highlighting --- keywords.h | 40 ++++++++++++++++++++++++++++++++++++++++ keywords.py | 14 ++++++++++++++ lsp-write.c | 2 ++ main.c | 3 --- syntax.c | 13 ++++++++++--- ted.cfg | 3 ++- ted.h | 4 +++- 7 files changed, 71 insertions(+), 8 deletions(-) diff --git a/keywords.h b/keywords.h index 6cf14f8..38c9478 100644 --- a/keywords.h +++ b/keywords.h @@ -170,6 +170,46 @@ static Keyword const *const syntax_all_keywords_javascript[] = { ['A'] = syntax_keywords_javascript_A, ['B'] = syntax_keywords_javascript_B, ['D'] = syntax_keywords_javascript_D, ['E'] = syntax_keywords_javascript_E, ['F'] = syntax_keywords_javascript_F, ['G'] = syntax_keywords_javascript_G, ['I'] = syntax_keywords_javascript_I, ['J'] = syntax_keywords_javascript_J, ['M'] = syntax_keywords_javascript_M, ['N'] = syntax_keywords_javascript_N, ['O'] = syntax_keywords_javascript_O, ['P'] = syntax_keywords_javascript_P, ['R'] = syntax_keywords_javascript_R, ['S'] = syntax_keywords_javascript_S, ['T'] = syntax_keywords_javascript_T, ['U'] = syntax_keywords_javascript_U, ['W'] = syntax_keywords_javascript_W, ['a'] = syntax_keywords_javascript_a, ['b'] = syntax_keywords_javascript_b, ['c'] = syntax_keywords_javascript_c, ['d'] = syntax_keywords_javascript_d, ['e'] = syntax_keywords_javascript_e, ['f'] = syntax_keywords_javascript_f, ['g'] = syntax_keywords_javascript_g, ['i'] = syntax_keywords_javascript_i, ['l'] = syntax_keywords_javascript_l, ['n'] = syntax_keywords_javascript_n, ['p'] = syntax_keywords_javascript_p, ['r'] = syntax_keywords_javascript_r, ['s'] = syntax_keywords_javascript_s, ['t'] = syntax_keywords_javascript_t, ['u'] = syntax_keywords_javascript_u, ['v'] = syntax_keywords_javascript_v, ['w'] = syntax_keywords_javascript_w, ['y'] = syntax_keywords_javascript_y }; +static Keyword const syntax_keywords_typescript_A[8] = {{"AggregateError", SYNTAX_BUILTIN},{"Array", SYNTAX_BUILTIN},{"ArrayBuffer", SYNTAX_BUILTIN},{"AsyncFunction", SYNTAX_BUILTIN},{"AsyncGenerator", SYNTAX_BUILTIN},{"AsyncGeneratorFunction", SYNTAX_BUILTIN},{"Atomics", SYNTAX_BUILTIN}}; +static Keyword const syntax_keywords_typescript_B[5] = {{"BigInt", SYNTAX_BUILTIN},{"BigInt64Array", SYNTAX_BUILTIN},{"BigUint64Array", SYNTAX_BUILTIN},{"Boolean", SYNTAX_BUILTIN}}; +static Keyword const syntax_keywords_typescript_D[3] = {{"DataView", SYNTAX_BUILTIN},{"Date", SYNTAX_BUILTIN}}; +static Keyword const syntax_keywords_typescript_E[3] = {{"Error", SYNTAX_BUILTIN},{"EvalError", SYNTAX_BUILTIN}}; +static Keyword const syntax_keywords_typescript_F[5] = {{"FinalizationRegistry", SYNTAX_BUILTIN},{"Float32Array", SYNTAX_BUILTIN},{"Float64Array", SYNTAX_BUILTIN},{"Function", SYNTAX_BUILTIN}}; +static Keyword const syntax_keywords_typescript_G[3] = {{"Generator", SYNTAX_BUILTIN},{"GeneratorFunction", SYNTAX_BUILTIN}}; +static Keyword const syntax_keywords_typescript_I[7] = {{"Infinity", SYNTAX_BUILTIN},{"Int16Array", SYNTAX_BUILTIN},{"Int32Array", SYNTAX_BUILTIN},{"Int8Array", SYNTAX_BUILTIN},{"InternalError", SYNTAX_BUILTIN},{"Intl", SYNTAX_BUILTIN}}; +static Keyword const syntax_keywords_typescript_J[2] = {{"JSON", SYNTAX_BUILTIN}}; +static Keyword const syntax_keywords_typescript_M[3] = {{"Map", SYNTAX_BUILTIN},{"Math", SYNTAX_BUILTIN}}; +static Keyword const syntax_keywords_typescript_N[3] = {{"NaN", SYNTAX_BUILTIN},{"Number", SYNTAX_BUILTIN}}; +static Keyword const syntax_keywords_typescript_O[2] = {{"Object", SYNTAX_BUILTIN}}; +static Keyword const syntax_keywords_typescript_P[3] = {{"Promise", SYNTAX_BUILTIN},{"Proxy", SYNTAX_BUILTIN}}; +static Keyword const syntax_keywords_typescript_R[5] = {{"RangeError", SYNTAX_BUILTIN},{"ReferenceError", SYNTAX_BUILTIN},{"Reflect", SYNTAX_BUILTIN},{"RegExp", SYNTAX_BUILTIN}}; +static Keyword const syntax_keywords_typescript_S[6] = {{"Set", SYNTAX_BUILTIN},{"SharedArrayBuffer", SYNTAX_BUILTIN},{"String", SYNTAX_BUILTIN},{"Symbol", SYNTAX_BUILTIN},{"SyntaxError", SYNTAX_BUILTIN}}; +static Keyword const syntax_keywords_typescript_T[3] = {{"TypeError", SYNTAX_BUILTIN},{"TypedArray", SYNTAX_BUILTIN}}; +static Keyword const syntax_keywords_typescript_U[6] = {{"URIError", SYNTAX_BUILTIN},{"Uint16Array", SYNTAX_BUILTIN},{"Uint32Array", SYNTAX_BUILTIN},{"Uint8Array", SYNTAX_BUILTIN},{"Uint8ClampedArray", SYNTAX_BUILTIN}}; +static Keyword const syntax_keywords_typescript_W[5] = {{"WeakMap", SYNTAX_BUILTIN},{"WeakRef", SYNTAX_BUILTIN},{"WeakSet", SYNTAX_BUILTIN},{"WebAssembly", SYNTAX_BUILTIN}}; +static Keyword const syntax_keywords_typescript_a[4] = {{"any", SYNTAX_KEYWORD},{"as", SYNTAX_KEYWORD},{"await", SYNTAX_KEYWORD}}; +static Keyword const syntax_keywords_typescript_b[2] = {{"break", SYNTAX_KEYWORD}}; +static Keyword const syntax_keywords_typescript_c[6] = {{"case", SYNTAX_KEYWORD},{"catch", SYNTAX_KEYWORD},{"class", SYNTAX_KEYWORD},{"const", SYNTAX_KEYWORD},{"continue", SYNTAX_KEYWORD}}; +static Keyword const syntax_keywords_typescript_d[7] = {{"debugger", SYNTAX_KEYWORD},{"default", SYNTAX_KEYWORD},{"delete", SYNTAX_KEYWORD},{"do", SYNTAX_KEYWORD},{"decodeURI", SYNTAX_BUILTIN},{"decodeURIComponent", SYNTAX_BUILTIN}}; +static Keyword const syntax_keywords_typescript_e[8] = {{"else", SYNTAX_KEYWORD},{"enum", SYNTAX_KEYWORD},{"export", SYNTAX_KEYWORD},{"extends", SYNTAX_KEYWORD},{"encodeURI", SYNTAX_BUILTIN},{"encodeURIComponent", SYNTAX_BUILTIN},{"eval", SYNTAX_BUILTIN}}; +static Keyword const syntax_keywords_typescript_f[5] = {{"finally", SYNTAX_KEYWORD},{"for", SYNTAX_KEYWORD},{"function", SYNTAX_KEYWORD},{"false", SYNTAX_CONSTANT}}; +static Keyword const syntax_keywords_typescript_g[3] = {{"get", SYNTAX_BUILTIN},{"globalThis", SYNTAX_BUILTIN}}; +static Keyword const syntax_keywords_typescript_i[9] = {{"if", SYNTAX_KEYWORD},{"implements", SYNTAX_KEYWORD},{"import", SYNTAX_KEYWORD},{"in", SYNTAX_KEYWORD},{"instanceof", SYNTAX_KEYWORD},{"interface", SYNTAX_KEYWORD},{"isFinite", SYNTAX_BUILTIN},{"isNaN", SYNTAX_BUILTIN}}; +static Keyword const syntax_keywords_typescript_l[2] = {{"let", SYNTAX_KEYWORD}}; +static Keyword const syntax_keywords_typescript_m[2] = {{"module", SYNTAX_KEYWORD}}; +static Keyword const syntax_keywords_typescript_n[4] = {{"new", SYNTAX_KEYWORD},{"null", SYNTAX_BUILTIN},{"number", SYNTAX_BUILTIN}}; +static Keyword const syntax_keywords_typescript_p[6] = {{"package", SYNTAX_KEYWORD},{"private", SYNTAX_KEYWORD},{"public", SYNTAX_KEYWORD},{"parseFloat", SYNTAX_BUILTIN},{"parseInt", SYNTAX_BUILTIN}}; +static Keyword const syntax_keywords_typescript_r[2] = {{"return", SYNTAX_KEYWORD}}; +static Keyword const syntax_keywords_typescript_s[6] = {{"static", SYNTAX_KEYWORD},{"super", SYNTAX_KEYWORD},{"switch", SYNTAX_KEYWORD},{"set", SYNTAX_BUILTIN},{"string", SYNTAX_BUILTIN}}; +static Keyword const syntax_keywords_typescript_t[7] = {{"this", SYNTAX_KEYWORD},{"throw", SYNTAX_KEYWORD},{"try", SYNTAX_KEYWORD},{"type", SYNTAX_KEYWORD},{"typeof", SYNTAX_KEYWORD},{"true", SYNTAX_CONSTANT}}; +static Keyword const syntax_keywords_typescript_u[2] = {{"undefined", SYNTAX_BUILTIN}}; +static Keyword const syntax_keywords_typescript_v[3] = {{"var", SYNTAX_KEYWORD},{"void", SYNTAX_KEYWORD}}; +static Keyword const syntax_keywords_typescript_w[3] = {{"while", SYNTAX_KEYWORD},{"with", SYNTAX_KEYWORD}}; +static Keyword const syntax_keywords_typescript_y[2] = {{"yield", SYNTAX_KEYWORD}}; +static Keyword const *const syntax_all_keywords_typescript[] = { + ['A'] = syntax_keywords_typescript_A, ['B'] = syntax_keywords_typescript_B, ['D'] = syntax_keywords_typescript_D, ['E'] = syntax_keywords_typescript_E, ['F'] = syntax_keywords_typescript_F, ['G'] = syntax_keywords_typescript_G, ['I'] = syntax_keywords_typescript_I, ['J'] = syntax_keywords_typescript_J, ['M'] = syntax_keywords_typescript_M, ['N'] = syntax_keywords_typescript_N, ['O'] = syntax_keywords_typescript_O, ['P'] = syntax_keywords_typescript_P, ['R'] = syntax_keywords_typescript_R, ['S'] = syntax_keywords_typescript_S, ['T'] = syntax_keywords_typescript_T, ['U'] = syntax_keywords_typescript_U, ['W'] = syntax_keywords_typescript_W, ['a'] = syntax_keywords_typescript_a, ['b'] = syntax_keywords_typescript_b, ['c'] = syntax_keywords_typescript_c, ['d'] = syntax_keywords_typescript_d, ['e'] = syntax_keywords_typescript_e, ['f'] = syntax_keywords_typescript_f, ['g'] = syntax_keywords_typescript_g, ['i'] = syntax_keywords_typescript_i, ['l'] = syntax_keywords_typescript_l, ['m'] = syntax_keywords_typescript_m, ['n'] = syntax_keywords_typescript_n, ['p'] = syntax_keywords_typescript_p, ['r'] = syntax_keywords_typescript_r, ['s'] = syntax_keywords_typescript_s, ['t'] = syntax_keywords_typescript_t, ['u'] = syntax_keywords_typescript_u, ['v'] = syntax_keywords_typescript_v, ['w'] = syntax_keywords_typescript_w, ['y'] = syntax_keywords_typescript_y +}; + static Keyword const syntax_keywords_go_a[2] = {{"append", SYNTAX_BUILTIN}}; static Keyword const syntax_keywords_go_b[4] = {{"break", SYNTAX_KEYWORD},{"bool", SYNTAX_BUILTIN},{"byte", SYNTAX_BUILTIN}}; static Keyword const syntax_keywords_go_c[12] = {{"case", SYNTAX_KEYWORD},{"chan", SYNTAX_KEYWORD},{"const", SYNTAX_KEYWORD},{"continue", SYNTAX_KEYWORD},{"cap", SYNTAX_BUILTIN},{"close", SYNTAX_BUILTIN},{"comparable", SYNTAX_BUILTIN},{"complex", SYNTAX_BUILTIN},{"complex128", SYNTAX_BUILTIN},{"complex64", SYNTAX_BUILTIN},{"copy", SYNTAX_BUILTIN}}; diff --git a/keywords.py b/keywords.py index 1e345d9..880cf09 100755 --- a/keywords.py +++ b/keywords.py @@ -296,6 +296,18 @@ keywords_java = [ 'const', 'float', 'native', 'super', 'volatile', 'while' ] +keywords_typescript = keywords_javascript + [ + 'public', 'any', 'as', 'module', + 'static', 'interface', 'enum', 'type', + 'implements', 'private', 'package' +] + +builtins_typescript = builtins_javascript + [ + 'string', 'number', 'get', 'set' +] + +constants_typescript = constants_javascript + constants_java = [ 'true', 'false', 'null' ] @@ -347,6 +359,8 @@ output_keywords(file, cpp_things, 'cpp') output_keywords(file, label(keywords_rust, SYNTAX_KEYWORD) + label(builtins_rust, SYNTAX_BUILTIN) + label(constants_rust, SYNTAX_CONSTANT), 'rust') output_keywords(file, label(keywords_javascript, SYNTAX_KEYWORD) + label(builtins_javascript, SYNTAX_BUILTIN) + label(constants_javascript, SYNTAX_CONSTANT), 'javascript') +output_keywords(file, label(keywords_typescript, SYNTAX_KEYWORD) + label(builtins_typescript, SYNTAX_BUILTIN) + + label(constants_typescript, SYNTAX_CONSTANT), 'typescript') output_keywords(file, label(keywords_go, SYNTAX_KEYWORD) + label(builtins_go, SYNTAX_BUILTIN) + label(constants_go, SYNTAX_CONSTANT), 'go') output_keywords(file, label(keywords_java, SYNTAX_KEYWORD) + label(constants_java, SYNTAX_CONSTANT), 'java') diff --git a/lsp-write.c b/lsp-write.c index ecffe76..5f6c858 100644 --- a/lsp-write.c +++ b/lsp-write.c @@ -14,6 +14,8 @@ static const char *lsp_language_id(Language lang) { return "java"; case LANG_JAVASCRIPT: return "javascript"; + case LANG_TYPESCRIPT: + return "typescript"; case LANG_MARKDOWN: return "markdown"; case LANG_GO: diff --git a/main.c b/main.c index fc32768..1de9985 100644 --- a/main.c +++ b/main.c @@ -2,9 +2,6 @@ @TODO: - https://github.com/typescript-language-server/typescript-language-server - NOTE: This supports javascript. - - We should also add a typescript language (but just use javascript syntax highlighting for now) - - (don't want to add .ts as a Javascript extension since that won't be forwards-compatible - if we ever do add real typescript highlighting support) - make sure "save as" works - more LSP stuff: - signature help diff --git a/syntax.c b/syntax.c index dfd7035..18bc076 100644 --- a/syntax.c +++ b/syntax.c @@ -29,6 +29,7 @@ char const *language_comment_start(Language l) { case LANG_RUST: case LANG_CPP: case LANG_JAVASCRIPT: + case LANG_TYPESCRIPT: case LANG_JAVA: case LANG_GO: return "// "; @@ -1168,7 +1169,8 @@ static void syntax_highlight_config(SyntaxState *state, char32_t const *line, u3 } } -static void syntax_highlight_javascript(SyntaxState *state, char32_t const *line, u32 line_len, SyntaxCharType *char_types) { +static void syntax_highlight_javascript_typescript( + SyntaxState *state, char32_t const *line, u32 line_len, SyntaxCharType *char_types, bool is_typescript) { (void)state; bool string_is_template = (*state & SYNTAX_STATE_JAVASCRIPT_TEMPLATE_STRING) != 0; bool in_multiline_comment = (*state & SYNTAX_STATE_JAVASCRIPT_MULTILINE_COMMENT) != 0; @@ -1281,7 +1283,9 @@ static void syntax_highlight_javascript(SyntaxState *state, char32_t const *line if (char_types && !in_string && !in_number && !in_multiline_comment) { u32 keyword_len = syntax_keyword_len(LANG_JAVASCRIPT, line, i, line_len); - Keyword const *keyword = syntax_keyword_lookup(syntax_all_keywords_javascript, arr_count(syntax_all_keywords_javascript), + Keyword const *keyword = syntax_keyword_lookup( + is_typescript ? syntax_all_keywords_typescript : syntax_all_keywords_javascript, + is_typescript ? arr_count(syntax_all_keywords_typescript) : arr_count(syntax_all_keywords_javascript), &line[i], keyword_len); if (keyword) { SyntaxCharType type = keyword->type; @@ -1622,7 +1626,10 @@ void syntax_highlight(SyntaxState *state, Language lang, char32_t const *line, u syntax_highlight_config(state, line, line_len, char_types, true); break; case LANG_JAVASCRIPT: - syntax_highlight_javascript(state, line, line_len, char_types); + syntax_highlight_javascript_typescript(state, line, line_len, char_types, false); + break; + case LANG_TYPESCRIPT: + syntax_highlight_javascript_typescript(state, line, line_len, char_types, true); break; case LANG_JAVA: syntax_highlight_java(state, line, line_len, char_types); diff --git a/ted.cfg b/ted.cfg index 53784f2..8a23968 100644 --- a/ted.cfg +++ b/ted.cfg @@ -311,7 +311,8 @@ HTML = .html, .php, .xml, .xhtml, .iml Config = .cfg, .toml # ted.cfg has its own syntax highlighting for multiline strings. TedCfg = ted.cfg -Javascript = .js +JavaScript = .js +TypeScript = .ts Java = .java Go = .go diff --git a/ted.h b/ted.h index a33a1eb..ae02fac 100644 --- a/ted.h +++ b/ted.h @@ -79,6 +79,7 @@ typedef enum { LANG_JAVA = 10, LANG_GO = 11, LANG_TED_CFG = 12, // like LANG_CONFIG, but with multiline strings. + LANG_TYPESCRIPT = 13, LANG_COUNT } Language; @@ -97,10 +98,11 @@ static LanguageName const language_names[] = { {LANG_MARKDOWN, "Markdown"}, {LANG_HTML, "HTML"}, {LANG_CONFIG, "Config"}, - {LANG_JAVASCRIPT, "Javascript"}, + {LANG_JAVASCRIPT, "JavaScript"}, {LANG_JAVA, "Java"}, {LANG_GO, "Go"}, {LANG_TED_CFG, "TedCfg"}, + {LANG_TYPESCRIPT, "TypeScript"}, }; static_assert_if_possible(arr_count(language_names) == LANG_COUNT) -- cgit v1.2.3