From c6a766a57571f3a3bf64e745565201825fe2e5e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20=E9=87=91=E5=8F=AF=E6=98=8E?= Date: Fri, 29 Sep 2023 18:18:43 +0200 Subject: [PATCH 01/12] feat(svelte): parse script tags in svelte filetypes --- ftplugin/svelte.vim | 11 +++++++ helper/Cargo.lock | 10 ++++++ helper/Cargo.toml | 1 + helper/src/config.rs | 1 + helper/src/docblock.rs | 2 ++ helper/src/lib.rs | 1 + helper/src/svelte/mod.rs | 1 + helper/src/svelte/parser.rs | 63 +++++++++++++++++++++++++++++++++++++ 8 files changed, 90 insertions(+) create mode 100644 ftplugin/svelte.vim create mode 100644 helper/src/svelte/mod.rs create mode 100644 helper/src/svelte/parser.rs diff --git a/ftplugin/svelte.vim b/ftplugin/svelte.vim new file mode 100644 index 00000000..c5477f1c --- /dev/null +++ b/ftplugin/svelte.vim @@ -0,0 +1,11 @@ +let s:save_cpo = &cpoptions +set cpoptions&vim + +let b:doge_parser = 'svelte' +let b:doge_insert = 'above' + +let b:doge_supported_doc_standards = ['jsdoc'] +let b:doge_doc_standard = doge#buffer#get_doc_standard('svelte') + +let &cpoptions = s:save_cpo +unlet s:save_cpo diff --git a/helper/Cargo.lock b/helper/Cargo.lock index 8e2d12a5..eea6a6c7 100644 --- a/helper/Cargo.lock +++ b/helper/Cargo.lock @@ -708,6 +708,15 @@ dependencies = [ "tree-sitter", ] +[[package]] +name = "tree-sitter-svelte" +version = "0.10.2" +source = "git+https://github.com/Himujjal/tree-sitter-svelte?rev=697bb51#697bb515471871e85ff799ea57a76298a71a9cca" +dependencies = [ + "cc", + "tree-sitter", +] + [[package]] name = "tree-sitter-typescript" version = "0.20.2" @@ -826,6 +835,7 @@ dependencies = [ "tree-sitter-ruby", "tree-sitter-rust", "tree-sitter-scala", + "tree-sitter-svelte", "tree-sitter-typescript", ] diff --git a/helper/Cargo.toml b/helper/Cargo.toml index aeaffef9..d5e847c6 100644 --- a/helper/Cargo.toml +++ b/helper/Cargo.toml @@ -28,3 +28,4 @@ tree-sitter-typescript = { git = "https://github.com/tree-sitter/tree-sitter-typ tree-sitter-php = { git = "https://github.com/tree-sitter/tree-sitter-php", rev = "d43130f" } tree-sitter-r = { git = "https://github.com/r-lib/tree-sitter-r", rev = "c55f8b4" } tree-sitter-scala = { git = "https://github.com/tree-sitter/tree-sitter-scala", rev = "f14629b" } +tree-sitter-svelte = { git = "https://github.com/Himujjal/tree-sitter-svelte", rev = "697bb51" } diff --git a/helper/src/config.rs b/helper/src/config.rs index a5188088..2eb8f066 100644 --- a/helper/src/config.rs +++ b/helper/src/config.rs @@ -27,6 +27,7 @@ pub fn load_doc_config_str<'a>(parser_name: &'a str, doc_name: &'a str) -> &'a s "php_phpdoc" => include_str!("php/docs/phpdoc.yaml"), "typescript_jsdoc" => include_str!("typescript/docs/jsdoc.yaml"), + "svelte_jsdoc" => include_str!("typescript/docs/jsdoc.yaml"), "lua_ldoc" => include_str!("lua/docs/ldoc.yaml"), "java_javadoc" => include_str!("java/docs/javadoc.yaml"), "ruby_YARD" => include_str!("ruby/docs/YARD.yaml"), diff --git a/helper/src/docblock.rs b/helper/src/docblock.rs index 39d68131..8ede1f3a 100644 --- a/helper/src/docblock.rs +++ b/helper/src/docblock.rs @@ -19,6 +19,7 @@ use crate::rust::parser::RustParser; use crate::typescript::parser::TypescriptParser; use crate::r::parser::RParser; use crate::scala::parser::ScalaParser; +use crate::svelte::parser::SvelteParser; fn replace_indent_placeholders(docblock: &str, use_tabs: bool, indent: usize) -> String { @@ -141,6 +142,7 @@ pub fn generate( "typescript" => Box::new(TypescriptParser::new(code, line, &node_types, options)) as Box, "r" => Box::new(RParser::new(code, line, &node_types)) as Box, "scala" => Box::new(ScalaParser::new(code, line, &node_types)) as Box, + "svelte" => Box::new(SvelteParser::new(code, line, &node_types, options)) as Box, _ => panic!("Unsupported parser: {}", &parser_name), }; diff --git a/helper/src/lib.rs b/helper/src/lib.rs index a741f201..a7f9705e 100644 --- a/helper/src/lib.rs +++ b/helper/src/lib.rs @@ -17,3 +17,4 @@ pub mod cpp; pub mod typescript; pub mod r; pub mod scala; +pub mod svelte; diff --git a/helper/src/svelte/mod.rs b/helper/src/svelte/mod.rs new file mode 100644 index 00000000..67c567fa --- /dev/null +++ b/helper/src/svelte/mod.rs @@ -0,0 +1 @@ +pub mod parser; diff --git a/helper/src/svelte/parser.rs b/helper/src/svelte/parser.rs new file mode 100644 index 00000000..33e0e862 --- /dev/null +++ b/helper/src/svelte/parser.rs @@ -0,0 +1,63 @@ +use std::collections::HashMap; + +use tree_sitter::{Parser, Node}; +use serde_json::{Map, Value}; + +use crate::base_parser::BaseParser; +use crate::typescript::parser::TypescriptParser; + +pub struct SvelteParser<'a> { + code: &'a str, + tree: tree_sitter::Tree, + line: &'a usize, + node_types: &'a [&'a str], + options: &'a HashMap<&'a str, bool>, +} + +impl<'a> BaseParser for SvelteParser<'a> { + fn parse(&self) -> Option, String>> { + self.parse_node(&self.tree.root_node()) + } + + fn get_code_bytes(&self) -> &[u8] { + &self.code.as_bytes() + } +} + +impl<'a> SvelteParser<'a> { + pub fn new(code: &'a str, line: &'a usize, node_types: &'a [&'a str], options: &'a HashMap<&'a str, bool>) -> Self { + let mut parser = Parser::new(); + parser.set_language(tree_sitter_svelte::language()).unwrap(); + + let tree = parser.parse(code, None).unwrap(); + + Self { code, tree, line, options, node_types } + } + + fn parse_node(&self, node: &Node) -> Option, String>> { + for child_node in node.children(&mut node.walk()) { + return match child_node.kind() { + "script_element" => self.parse_script_element(&child_node), + _ => None, + }; + } + + None + } + + fn parse_script_element(&self, node: &Node) -> Option, String>> { + let raw_text = node + .children(&mut node.walk()) + .filter(|node| node.kind() == "raw_text") + .next() + .and_then(|node| Some(self.get_node_text(&node))) + .unwrap(); + + // The new line must be based on the script tag starting position. + let line = self.line - (node.start_position().row + 1); + + // Parse the inner content of the script tag with the TS parser. + let ts_parser = TypescriptParser::new(&raw_text, &line, self.node_types, self.options); + ts_parser.parse() + } +} From 6604ecb5a6390fca553de7683fb81407941be8e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20=E9=87=91=E5=8F=AF=E6=98=8E?= Date: Fri, 29 Sep 2023 18:28:14 +0200 Subject: [PATCH 02/12] feat: add basic svelte tests --- test/filetypes/svelte/hello-world.vader | 28 +++++++++++++++++++++++++ test/vimrc | 2 +- 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 test/filetypes/svelte/hello-world.vader diff --git a/test/filetypes/svelte/hello-world.vader b/test/filetypes/svelte/hello-world.vader new file mode 100644 index 00000000..180f53b6 --- /dev/null +++ b/test/filetypes/svelte/hello-world.vader @@ -0,0 +1,28 @@ +Given svelte (html with a script tag): + + +

Hello {name}!

+ +Do (trigger doge): + :3\ + \ + +Expect typescript (generated comment with generated comment inside + +

Hello {name}!

diff --git a/test/vimrc b/test/vimrc index 6bf78be6..e6ec945e 100644 --- a/test/vimrc +++ b/test/vimrc @@ -46,7 +46,7 @@ set autoindent set smartindent set smarttab -augroup styles +augroup vim_doge_tests autocmd! " Force 2 spaces for Python to prevent other filetypes inherit these settings. From 7c569914442e850af2ce018cbbcd98519b8fe550 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20=E9=87=91=E5=8F=AF=E6=98=8E?= Date: Fri, 29 Sep 2023 18:32:16 +0200 Subject: [PATCH 03/12] docs(readme): add svelte info --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 3055da3c..9cb00350 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,7 @@ Is your favorite doc standard not supported? | :white_check_mark: | PHP | [phpdoc][phpdoc] | | :white_check_mark: | JavaScript (Including: ES6, FlowJS and NodeJS) | [JSDoc][jsdoc] | | :white_check_mark: | TypeScript | [JSDoc][jsdoc] | +| :white_check_mark: | Svelte | [JSDoc][jsdoc] | | :white_check_mark: | Lua | [LDoc][ldoc] | | :white_check_mark: | Java | [JavaDoc][javadoc] | | :white_check_mark: | Groovy | [JavaDoc][javadoc] | @@ -164,6 +165,7 @@ Here is the full list of available doc standards per filetype: | `g:doge_doc_standard_python` | `'reST'` | `'reST'`, `'numpy'`, `'google'`, `'sphinx'` | | `g:doge_doc_standard_php` | `'phpdoc'` | `'phpdoc'` | | `g:doge_doc_standard_javascript` | `'jsdoc'` | `'jsdoc'` | +| `g:doge_doc_standard_svelte` | `'jsdoc'` | `'jsdoc'` | | `g:doge_doc_standard_lua` | `'ldoc'` | `'ldoc'` | | `g:doge_doc_standard_java` | `'javadoc'` | `'javadoc'` | | `g:doge_doc_standard_groovy` | `'javadoc'` | `'javadoc'` | From d2d99e4ade66fd74ef1a57424389d15009787022 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20=E9=87=91=E5=8F=AF=E6=98=8E?= Date: Sat, 30 Sep 2023 11:40:03 +0200 Subject: [PATCH 04/12] chore(ci): use vim v9.0.1500 for windows instead of HEAD --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 08bd9089..37411ee6 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -54,7 +54,7 @@ jobs: fail-fast: false matrix: os: [windows-latest] - vim-version: [v8.2.5172, head] + vim-version: [v8.2.5172, v9.0.1500] runs-on: ${{ matrix.os }} steps: - name: Checkout kkoomen/vim-doge From 6833e17d4e836bde2fb6a7de11f9be7feec9af49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20=E9=87=91=E5=8F=AF=E6=98=8E?= Date: Sat, 30 Sep 2023 12:07:50 +0200 Subject: [PATCH 05/12] fix(svelte): use pre-order traverse to find top-level script tag --- helper/src/svelte/parser.rs | 10 +- test/filetypes/svelte/hello-world.vader | 53 ++++++-- test/filetypes/svelte/table-component.vader | 138 ++++++++++++++++++++ 3 files changed, 183 insertions(+), 18 deletions(-) create mode 100644 test/filetypes/svelte/table-component.vader diff --git a/helper/src/svelte/parser.rs b/helper/src/svelte/parser.rs index 33e0e862..4f25bf05 100644 --- a/helper/src/svelte/parser.rs +++ b/helper/src/svelte/parser.rs @@ -3,6 +3,7 @@ use std::collections::HashMap; use tree_sitter::{Parser, Node}; use serde_json::{Map, Value}; +use crate::traverse; use crate::base_parser::BaseParser; use crate::typescript::parser::TypescriptParser; @@ -35,11 +36,10 @@ impl<'a> SvelteParser<'a> { } fn parse_node(&self, node: &Node) -> Option, String>> { - for child_node in node.children(&mut node.walk()) { - return match child_node.kind() { - "script_element" => self.parse_script_element(&child_node), - _ => None, - }; + for child_node in traverse::PreOrder::new(node.walk()) { + if child_node.kind() == "script_element" { + return self.parse_script_element(&child_node); + } } None diff --git a/test/filetypes/svelte/hello-world.vader b/test/filetypes/svelte/hello-world.vader index 180f53b6..8df102d4 100644 --- a/test/filetypes/svelte/hello-world.vader +++ b/test/filetypes/svelte/hello-world.vader @@ -1,28 +1,55 @@ -Given svelte (html with a script tag): - -

Hello {name}!

- Do (trigger doge): - :3\ + :9\ + \ + :20\ \ Expect typescript (generated comment with generated comment inside - -

Hello {name}!

diff --git a/test/filetypes/svelte/table-component.vader b/test/filetypes/svelte/table-component.vader new file mode 100644 index 00000000..bf9710a2 --- /dev/null +++ b/test/filetypes/svelte/table-component.vader @@ -0,0 +1,138 @@ +Given svelte (table layout component with script, style and template elements): + + + + + + +Do (trigger doge): + :24\ + \ + +Expect typescript (generated comment with generated comment inside + + From 633aa46fb28a6236997c66f970a6881c59a7a499 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20=E9=87=91=E5=8F=AF=E6=98=8E?= Date: Sun, 1 Oct 2023 13:49:20 +0200 Subject: [PATCH 06/12] chore(ci): use action-setup-vim@v1.0.2 --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 37411ee6..dd3417bc 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -114,7 +114,7 @@ jobs: shell: bash - name: Setup vim - uses: thinca/action-setup-vim@v1 + uses: thinca/action-setup-vim@v1.0.2 id: vim with: vim_version: ${{ matrix.vim-version }} From 8005f5280c9cee8bd8993a0008141ca0d51e1db2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20=E9=87=91=E5=8F=AF=E6=98=8E?= Date: Sun, 1 Oct 2023 13:53:52 +0200 Subject: [PATCH 07/12] chore(ci): use action-setup-vim@v1.2.10 --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index dd3417bc..8e6d0eed 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -114,7 +114,7 @@ jobs: shell: bash - name: Setup vim - uses: thinca/action-setup-vim@v1.0.2 + uses: thinca/action-setup-vim@v1.2.10 id: vim with: vim_version: ${{ matrix.vim-version }} From 5824a406658f0d97cf839eb2455b15c4dfb3363d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20=E9=87=91=E5=8F=AF=E6=98=8E?= Date: Sun, 1 Oct 2023 14:07:50 +0200 Subject: [PATCH 08/12] chore(ci): revert action-setup-vim to v1 and adjust macos version from 12.7 -> 12.6.9 --- .github/workflows/tests.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 8e6d0eed..7d39e5d2 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -14,7 +14,7 @@ jobs: strategy: fail-fast: false matrix: - os: [ubuntu-latest, macos-latest] + os: [ubuntu-latest, macos-12.6.9] vim-version: [v7.4.2119, v8.2.5172, v9.0.1500] runs-on: ${{ matrix.os }} steps: @@ -91,7 +91,7 @@ jobs: strategy: fail-fast: false matrix: - os: [ubuntu-latest, macos-latest] + os: [ubuntu-latest, macos-12.6.9] vim-version: [v0.3.2, head] runs-on: ${{ matrix.os }} steps: @@ -114,7 +114,7 @@ jobs: shell: bash - name: Setup vim - uses: thinca/action-setup-vim@v1.2.10 + uses: thinca/action-setup-vim@v1 id: vim with: vim_version: ${{ matrix.vim-version }} From 489d0cd05a1ffd58513f89cca60522b087ea96d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20=E9=87=91=E5=8F=AF=E6=98=8E?= Date: Sun, 1 Oct 2023 14:11:17 +0200 Subject: [PATCH 09/12] chore(ci): adjust macos version from 12.6.9 -> 12.6 --- .github/workflows/tests.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 7d39e5d2..e523ce9b 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -14,7 +14,7 @@ jobs: strategy: fail-fast: false matrix: - os: [ubuntu-latest, macos-12.6.9] + os: [ubuntu-latest, macos-12.6] vim-version: [v7.4.2119, v8.2.5172, v9.0.1500] runs-on: ${{ matrix.os }} steps: @@ -91,7 +91,7 @@ jobs: strategy: fail-fast: false matrix: - os: [ubuntu-latest, macos-12.6.9] + os: [ubuntu-latest, macos-12.6] vim-version: [v0.3.2, head] runs-on: ${{ matrix.os }} steps: From 8a115d9714b78be247e047276227b1ee714f9d1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20=E9=87=91=E5=8F=AF=E6=98=8E?= Date: Sun, 1 Oct 2023 14:21:32 +0200 Subject: [PATCH 10/12] chore(ci): revert macos runner version; adjust vim version --- .github/workflows/tests.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index e523ce9b..8c54b07d 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -14,8 +14,8 @@ jobs: strategy: fail-fast: false matrix: - os: [ubuntu-latest, macos-12.6] - vim-version: [v7.4.2119, v8.2.5172, v9.0.1500] + os: [ubuntu-latest, macos-latest] + vim-version: [v7.4.2367, v8.2.5172, v9.0.1500] runs-on: ${{ matrix.os }} steps: - name: Checkout kkoomen/vim-doge @@ -91,7 +91,7 @@ jobs: strategy: fail-fast: false matrix: - os: [ubuntu-latest, macos-12.6] + os: [ubuntu-latest, macos-latest] vim-version: [v0.3.2, head] runs-on: ${{ matrix.os }} steps: From cdf36818b51c28b03da69cfe679778082e371986 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20=E9=87=91=E5=8F=AF=E6=98=8E?= Date: Sun, 1 Oct 2023 14:31:03 +0200 Subject: [PATCH 11/12] chore(ci): adjust vim version to v7.4 --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 8c54b07d..69a23e3b 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -15,7 +15,7 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest, macos-latest] - vim-version: [v7.4.2367, v8.2.5172, v9.0.1500] + vim-version: [v7.4, v8.2.5172, v9.0.1500] runs-on: ${{ matrix.os }} steps: - name: Checkout kkoomen/vim-doge From a6d9e288761994e70c7751f00bbf2bc8a0c3530d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20=E9=87=91=E5=8F=AF=E6=98=8E?= Date: Sun, 1 Oct 2023 14:43:23 +0200 Subject: [PATCH 12/12] chore(ci): remove vim v7 tests --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 69a23e3b..b9154235 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -15,7 +15,7 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest, macos-latest] - vim-version: [v7.4, v8.2.5172, v9.0.1500] + vim-version: [v8.2.5172, v9.0.1500] runs-on: ${{ matrix.os }} steps: - name: Checkout kkoomen/vim-doge