From 048255249dc52ba77a5a9758d4783b4c7848715a Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Fri, 1 Apr 2022 15:02:37 -0500 Subject: [PATCH] separate JSX queries from javascript It looks like a24fb17b2a978d3165bd6304e9edd69bddb6dd82 (and 855e438f55cb278de8203ac4911561c4c7ad656c) broke the typescript highlights because typescript ; inherits: javascript but it doesn't have those named nodes in its grammar. So instead we can separate out JSX into its own language and copy over everything from javascript and supplement it with the new JSX highlights. Luckily there isn't too much duplication, just the language configuration parts - we can re-use the parser with the languages.toml `grammar` key and most of the queries with `inherits`. --- languages.toml | 11 +++++++++ runtime/queries/javascript/highlights.scm | 30 ----------------------- runtime/queries/jsx/highlights.scm | 27 ++++++++++++++++++++ runtime/queries/jsx/indents.scm | 1 + runtime/queries/jsx/injections.scm | 1 + runtime/queries/jsx/locals.scm | 1 + 6 files changed, 41 insertions(+), 30 deletions(-) create mode 100644 runtime/queries/jsx/highlights.scm create mode 100644 runtime/queries/jsx/indents.scm create mode 100644 runtime/queries/jsx/injections.scm create mode 100644 runtime/queries/jsx/locals.scm diff --git a/languages.toml b/languages.toml index ff8e7dfbf92f3..d12093a436526 100644 --- a/languages.toml +++ b/languages.toml @@ -290,6 +290,17 @@ args = { program = "{0}" } name = "javascript" source = { git = "https://github.com/tree-sitter/tree-sitter-javascript", rev = "4a95461c4761c624f2263725aca79eeaefd36cad" } +[[language]] +name = "jsx" +scope = "source.jsx" +injection-regex = "jsx" +file-types = ["jsx"] +roots = [] +comment-token = "//" +language-server = { command = "typescript-language-server", args = ["--stdio"], language-id = "javascript" } +indent = { tab-width = 2, unit = " " } +grammar = "javascript" + [[language]] name = "typescript" scope = "source.ts" diff --git a/runtime/queries/javascript/highlights.scm b/runtime/queries/javascript/highlights.scm index ccc58a9ecdb43..6163b680dce2b 100644 --- a/runtime/queries/javascript/highlights.scm +++ b/runtime/queries/javascript/highlights.scm @@ -1,33 +1,3 @@ -; JSX -;---- - -; Highlight component names differently - -(jsx_opening_element ((identifier) @constructor - (#match? @constructor "^[A-Z]"))) - -; Handle the dot operator effectively - -(jsx_opening_element ((nested_identifier (identifier) @tag (identifier) @constructor))) - -(jsx_closing_element ((identifier) @constructor - (#match? @constructor "^[A-Z]"))) - -; Handle the dot operator effectively - -(jsx_closing_element ((nested_identifier (identifier) @tag (identifier) @constructor))) - -(jsx_self_closing_element ((identifier) @constructor - (#match? @constructor "^[A-Z]"))) - -; Handle the dot operator effectively - -(jsx_self_closing_element ((nested_identifier (identifier) @tag (identifier) @constructor))) - -; TODO: also tag @punctuation.delimiter? - -(jsx_opening_element (identifier) @tag) -(jsx_closing_element (identifier) @tag) -(jsx_self_closing_element (identifier) @tag) -(jsx_attribute (property_identifier) @variable.other.member) - ; Special identifiers ;-------------------- diff --git a/runtime/queries/jsx/highlights.scm b/runtime/queries/jsx/highlights.scm new file mode 100644 index 0000000000000..9be5f51760c54 --- /dev/null +++ b/runtime/queries/jsx/highlights.scm @@ -0,0 +1,27 @@ +; inherits: javascript + +; Highlight component names differently +(jsx_opening_element ((identifier) @constructor + (#match? @constructor "^[A-Z]"))) + +; Handle the dot operator effectively - +(jsx_opening_element ((nested_identifier (identifier) @tag (identifier) @constructor))) + +(jsx_closing_element ((identifier) @constructor + (#match? @constructor "^[A-Z]"))) + +; Handle the dot operator effectively - +(jsx_closing_element ((nested_identifier (identifier) @tag (identifier) @constructor))) + +(jsx_self_closing_element ((identifier) @constructor + (#match? @constructor "^[A-Z]"))) + +; Handle the dot operator effectively - +(jsx_self_closing_element ((nested_identifier (identifier) @tag (identifier) @constructor))) + +; TODO: also tag @punctuation.delimiter? + +(jsx_opening_element (identifier) @tag) +(jsx_closing_element (identifier) @tag) +(jsx_self_closing_element (identifier) @tag) +(jsx_attribute (property_identifier) @variable.other.member) diff --git a/runtime/queries/jsx/indents.scm b/runtime/queries/jsx/indents.scm new file mode 100644 index 0000000000000..ff0ddfacf1359 --- /dev/null +++ b/runtime/queries/jsx/indents.scm @@ -0,0 +1 @@ +; inherits: javascript diff --git a/runtime/queries/jsx/injections.scm b/runtime/queries/jsx/injections.scm new file mode 100644 index 0000000000000..ff0ddfacf1359 --- /dev/null +++ b/runtime/queries/jsx/injections.scm @@ -0,0 +1 @@ +; inherits: javascript diff --git a/runtime/queries/jsx/locals.scm b/runtime/queries/jsx/locals.scm new file mode 100644 index 0000000000000..ff0ddfacf1359 --- /dev/null +++ b/runtime/queries/jsx/locals.scm @@ -0,0 +1 @@ +; inherits: javascript