-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #629 from kkoomen/feature/svelte
[Feature] add support for Svelte
- Loading branch information
Showing
13 changed files
with
288 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,4 @@ pub mod cpp; | |
pub mod typescript; | ||
pub mod r; | ||
pub mod scala; | ||
pub mod svelte; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod parser; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
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; | ||
|
||
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<Result<Map<String, Value>, 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<Result<Map<String, Value>, String>> { | ||
for child_node in traverse::PreOrder::new(node.walk()) { | ||
if child_node.kind() == "script_element" { | ||
return self.parse_script_element(&child_node); | ||
} | ||
} | ||
|
||
None | ||
} | ||
|
||
fn parse_script_element(&self, node: &Node) -> Option<Result<Map<String, Value>, 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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
Given svelte (hello world example with template and script tag): | ||
<template> | ||
<div> | ||
<h1>Hello {{ name }}!</h1> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
data() { | ||
return { | ||
name: 'world' | ||
} | ||
}, | ||
methods: { | ||
foo(test) {} | ||
} | ||
} | ||
</script> | ||
|
||
Do (trigger doge): | ||
:9\<CR> | ||
\<C-d> | ||
:20\<CR> | ||
\<C-d> | ||
|
||
Expect typescript (generated comment with generated comment inside <script>): | ||
<template> | ||
<div> | ||
<h1>Hello {{ name }}!</h1> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
/** | ||
* [TODO:description] | ||
* | ||
* @returns [TODO:description] | ||
*/ | ||
data() { | ||
return { | ||
name: 'world' | ||
} | ||
}, | ||
methods: { | ||
/** | ||
* [TODO:description] | ||
* | ||
* @param {[TODO:type]} test - [TODO:description] | ||
*/ | ||
foo(test) {} | ||
} | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
Given svelte (table layout component with script, style and template elements): | ||
<template> | ||
<div class="table-container"> | ||
<table> | ||
<thead> | ||
<tr> | ||
<th>Header 1</th> | ||
<th>Header 2</th> | ||
<th>Header 3</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr v-for="item in items" :key="item.id"> | ||
<td>{{ item.field1 }}</td> | ||
<td>{{ item.field2 }}</td> | ||
<td>{{ item.field3 }}</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
data() { | ||
return { | ||
items: [ | ||
{ id: 1, field1: 'Data 1', field2: 'Data 2', field3: 'Data 3' }, | ||
{ id: 2, field1: 'Data 4', field2: 'Data 5', field3: 'Data 6' }, | ||
{ id: 3, field1: 'Data 7', field2: 'Data 8', field3: 'Data 9' } | ||
] | ||
}; | ||
} | ||
}; | ||
</script> | ||
|
||
<style scoped> | ||
.table-container { | ||
font-family: Arial, sans-serif; | ||
margin: 20px; | ||
} | ||
|
||
table { | ||
border-collapse: collapse; | ||
width: 100%; | ||
} | ||
|
||
th, td { | ||
border: 1px solid #ddd; | ||
padding: 8px; | ||
} | ||
|
||
th { | ||
background-color: #f2f2f2; | ||
} | ||
|
||
tr:nth-child(even) { | ||
background-color: #f9f9f9; | ||
} | ||
|
||
tr:hover { | ||
background-color: #ddd; | ||
} | ||
</style> | ||
|
||
Do (trigger doge): | ||
:24\<CR> | ||
\<C-d> | ||
|
||
Expect typescript (generated comment with generated comment inside <script>): | ||
<template> | ||
<div class="table-container"> | ||
<table> | ||
<thead> | ||
<tr> | ||
<th>Header 1</th> | ||
<th>Header 2</th> | ||
<th>Header 3</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr v-for="item in items" :key="item.id"> | ||
<td>{{ item.field1 }}</td> | ||
<td>{{ item.field2 }}</td> | ||
<td>{{ item.field3 }}</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
/** | ||
* [TODO:description] | ||
* | ||
* @returns [TODO:description] | ||
*/ | ||
data() { | ||
return { | ||
items: [ | ||
{ id: 1, field1: 'Data 1', field2: 'Data 2', field3: 'Data 3' }, | ||
{ id: 2, field1: 'Data 4', field2: 'Data 5', field3: 'Data 6' }, | ||
{ id: 3, field1: 'Data 7', field2: 'Data 8', field3: 'Data 9' } | ||
] | ||
}; | ||
} | ||
}; | ||
</script> | ||
|
||
<style scoped> | ||
.table-container { | ||
font-family: Arial, sans-serif; | ||
margin: 20px; | ||
} | ||
|
||
table { | ||
border-collapse: collapse; | ||
width: 100%; | ||
} | ||
|
||
th, td { | ||
border: 1px solid #ddd; | ||
padding: 8px; | ||
} | ||
|
||
th { | ||
background-color: #f2f2f2; | ||
} | ||
|
||
tr:nth-child(even) { | ||
background-color: #f9f9f9; | ||
} | ||
|
||
tr:hover { | ||
background-color: #ddd; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters