[PATCH] indent: fix for html inside javascript again

14 views
Skip to first unread message

Felipe Contreras

unread,
Jul 26, 2022, 3:20:10 PM7/26/22
to vim...@vim.org, Felipe Contreras, Chris Paul
In issue #3205 a monkey patch was mentioned, but it wasn't the proper
fix, the proper fix is in GetJavascriptIndent(), which was done in
2018 [1].

However, the proper fix was not included, in fact, the javascript files
have not been updated since 2017.

The monkey patch screwed all indentation inside blocks, like:

<script>
function test() {
let v1;
}
test();
</script>

We need to remove the monkey patch, and include the correct fix.

[1] https://github.com/pangloss/vim-javascript/commit/e110653

Cc: Chris Paul <chrispe...@me.com>
Signed-off-by: Felipe Contreras <felipe.c...@gmail.com>
---
runtime/indent/html.vim | 2 +-
runtime/indent/javascript.vim | 6 +++++-
2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/runtime/indent/html.vim b/runtime/indent/html.vim
index a3c32d634..65e0ffc40 100644
--- a/runtime/indent/html.vim
+++ b/runtime/indent/html.vim
@@ -600,7 +600,7 @@ func s:Alien3()
endif
if b:hi_indent.scripttype == "javascript"
" indent for further lines
- return eval(b:hi_js1indent) + GetJavascriptIndent()
+ return GetJavascriptIndent()
else
return -1
endif
diff --git a/runtime/indent/javascript.vim b/runtime/indent/javascript.vim
index f3bf96aa9..8aa5ff3d2 100644
--- a/runtime/indent/javascript.vim
+++ b/runtime/indent/javascript.vim
@@ -473,7 +473,11 @@ function GetJavascriptIndent()
elseif num
return s:Nat(num_ind + get(l:,'case_offset',s:sw()) + l:switch_offset + b_l + is_op)
endif
- return b_l + is_op
+ let ret = b_l + is_op
+ if !ret && exists('b:hi_indent.blocklnr')
+ return indent(b:hi_indent.blocklnr) + s:sw()
+ endif
+ return ret
endfunction

let &cpo = s:cpo_save
--
2.37.1.225.gfa48d685d2

Felipe Contreras

unread,
Jul 26, 2022, 5:04:46 PM7/26/22
to Bram Moolenaar, vim_dev, vim...@vim.org, Chris Paul
On Tue, Jul 26, 2022 at 3:27 PM Bram Moolenaar <Br...@moolenaar.net> wrote:
>
>
> Felipe Contreras wrote:
>
> > In issue #3205 a monkey patch was mentioned, but it wasn't the proper
> > fix, the proper fix is in GetJavascriptIndent(), which was done in
> > 2018 [1].
> >
> > However, the proper fix was not included, in fact, the javascript files
> > have not been updated since 2017.
> >
> > The monkey patch screwed all indentation inside blocks, like:
> >
> > <script>
> > function test() {
> > let v1;
> > }
> > test();
> > </script>
> >
> > We need to remove the monkey patch, and include the correct fix.
>
> I tried including this, but "make test" in runtime/indent fails.

I see, I missed subsequent changes, I've grouped them together and now
the tests pass.

> I would think the right solution is:
>
> <script>
> var v1 = "v1";
> var v2 = "v2";
> </script>

That is the correct behavior only when g:html_indent_script1 is not
"zero", which in that particular test it is.

> Have you tried contacting Chris Paul? His activity on github has
> stopped a few years ago.

He is in Cc.

--
Felipe Contreras

Felipe Contreras

unread,
Jul 26, 2022, 5:14:25 PM7/26/22
to vim...@vim.org, Felipe Contreras, Chris Paul
In issue #3205 a monkey patch was mentioned, but it wasn't the proper
fix, the proper fix is in GetJavascriptIndent(), which was done in
2018 [1].

The monkey patch screwed all indentation inside blocks, like:

<script>
function test() {
let v1;
}
test();
</script>

We need to remove the monkey patch, and include the correct fix. I've
agregated a few fixes from Chris Paul and this is the resulting patch.

[1] https://github.com/pangloss/vim-javascript/commit/e110653

Cc: Chris Paul <chrispe...@me.com>
Signed-off-by: Felipe Contreras <felipe.c...@gmail.com>
---
runtime/indent/html.vim | 2 +-
runtime/indent/javascript.vim | 4 ++++
2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/runtime/indent/html.vim b/runtime/indent/html.vim
index a3c32d634..65e0ffc40 100644
--- a/runtime/indent/html.vim
+++ b/runtime/indent/html.vim
@@ -600,7 +600,7 @@ func s:Alien3()
endif
if b:hi_indent.scripttype == "javascript"
" indent for further lines
- return eval(b:hi_js1indent) + GetJavascriptIndent()
+ return GetJavascriptIndent()
else
return -1
endif
diff --git a/runtime/indent/javascript.vim b/runtime/indent/javascript.vim
index f3bf96aa9..92c370824 100644
--- a/runtime/indent/javascript.vim
+++ b/runtime/indent/javascript.vim
@@ -473,6 +473,10 @@ function GetJavascriptIndent()
elseif num
return s:Nat(num_ind + get(l:,'case_offset',s:sw()) + l:switch_offset + b_l + is_op)
endif
+ let nest = get(get(b:,'hi_indent',{}),'blocklnr')
+ if nest
+ return indent(nextnonblank(nest+1)) + b_l + is_op
+ endif
return b_l + is_op
endfunction

--
2.37.1.225.gfa48d685d2

Bram Moolenaar

unread,
Jul 27, 2022, 5:35:46 AM7/27/22
to vim...@googlegroups.com, Felipe Contreras, vim...@vim.org, Chris Paul

Felipe -

> In issue #3205 a monkey patch was mentioned, but it wasn't the proper
> fix, the proper fix is in GetJavascriptIndent(), which was done in
> 2018 [1].
>
> The monkey patch screwed all indentation inside blocks, like:
>
> <script>
> function test() {
> let v1;
> }
> test();
> </script>
>
> We need to remove the monkey patch, and include the correct fix. I've
> agregated a few fixes from Chris Paul and this is the resulting patch.
>
> [1] https://github.com/pangloss/vim-javascript/commit/e110653

When I include this then the tests passes.

Perhaps you can also add some more in the indent test, to make sure
there is no regression.

- Bram

--
hundred-and-one symptoms of being an internet addict:
146. You experience ACTUAL physical withdrawal symptoms when away
from your 'puter and the net.

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///
Reply all
Reply to author
Forward
0 new messages