Skip to content

Commit

Permalink
feat(html parser): only parse the script element surrounding the cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
kkoomen committed Oct 3, 2023
1 parent c4756ce commit c91c8c8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
6 changes: 5 additions & 1 deletion helper/src/html/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ impl<'a> HtmlParser<'a> {

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" {
// Parse the script element that is surrounding the cursor.
if child_node.kind() == "script_element" &&
child_node.start_position().row <= self.line - 1 &&
child_node.end_position().row >= self.line - 1
{
return self.parse_script_element(&child_node);
}
}
Expand Down
18 changes: 16 additions & 2 deletions test/filetypes/svelte/hello-world.vader
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ Given svelte (hello world example with template and script tag):
</div>
</template>

<!--
add another dummy script tag to make sure when the cursor is inside the
script element below this one, vim-doge detects the current script element
where the comment should be inserted.
-->
<script></script>

<script>
export default {
data() {
Expand All @@ -19,9 +26,9 @@ Given svelte (hello world example with template and script tag):
</script>

Do (trigger doge):
:9\<CR>
:16\<CR>
\<C-d>
:20\<CR>
:27\<CR>
\<C-d>

Expect typescript (generated comment with generated comment inside <script>):
Expand All @@ -31,6 +38,13 @@ Expect typescript (generated comment with generated comment inside <script>):
</div>
</template>

<!--
add another dummy script tag to make sure when the cursor is inside the
script element below this one, vim-doge detects the current script element
where the comment should be inserted.
-->
<script></script>

<script>
export default {
/**
Expand Down

0 comments on commit c91c8c8

Please sign in to comment.