Skip to content

Incorrect indentation following single-line comments #493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
TamsynUlthara opened this issue Jun 17, 2016 · 31 comments
Closed

Incorrect indentation following single-line comments #493

TamsynUlthara opened this issue Jun 17, 2016 · 31 comments

Comments

@TamsynUlthara
Copy link
Contributor

On both master and develop, a line following a single-line comment is incorrectly indented:

function func() {
  // comment
 // new indent starts here
}

obj = {
  // comment
 // new indent starts here
}

arr = [
  // comment
 // new indent starts here
]

call(
  // comment
 // new indent starts here
)

But indentation between single-line comments is (usually) fine:

function func() {
  // comment 1
  // new indent starts here
  // comment 2
}

obj = {
  // comment 1
  // new indent starts here
  // comment 2
}

arr = [
  // comment 1
  // new indent starts here
  // comment 2
]

But even those seem to break if your last action was deleting a line
between // comment 1 and // comment 2.

Calls seem a bit wonky:

call(
  // comment 1
    // new indent starts here
  // comment 2
)

And everything behaves differently if there's a non-comment line above the comments:

function func() {
  x
  // comment 1
    // new indent starts here
  // comment 2
}


obj = {
  x
  // comment 1
    // new indent starts here
  // comment 2
}

arr = [
  x
  // comment 1
  // new indent starts here
  // comment 2
]
call(
  x
  // comment 1
  // new indent starts here
  // comment 2
)
@bounceme
Copy link
Collaborator

try the newest dev update

@TamsynUlthara
Copy link
Contributor Author

Just tried; all the aforementioned bugs are still present.

@bounceme
Copy link
Collaborator

i can't reproduce any of these

@TamsynUlthara
Copy link
Contributor Author

I just tested with -u NONE and only this plugin enabled, and I'm seeing some differences.

  • It doesn't happen at all if formatoptions has o and r. I usually disable those; I virtually never want o or O to create a new comment, and rarely <Enter>.
  • Most of the examples I gave work normally even with o and r unset from formatoptions, but a few of them are still indenting incorrectly.

I'm trying to track down what combination of settings and/or plugins is causing the behavior; I'll post back as soon as I figure it out.

@TamsynUlthara
Copy link
Contributor Author

Actually, I'm seeing it pretty reliably now with -u NONE and setlocal formatoptions-=o, with only this plugin active.

Here's what I'm doing after starting with -u NONE:

  • I source a file with these commands:
set runtimepath=/path-to-javascript-plugin,/other-standard-runtime-paths-except-home-dir-ones
runtime! filetype.vim
runtime! ftplugin.vim
runtime! indent.vim
runtime! syntax/syntax.vim
runtime! plugin/**/*.vim
  • I open a file with the above examples.
  • I set formatoptions-=o, formatoptions-=r

@bounceme
Copy link
Collaborator

@TamsynUlthara
Copy link
Contributor Author

The indentkeys setting? Commenting that out breaks hitting Enter (no context-sensitive indentation at all), but that's what you'd expect. o/O is still in indentkeys, and manually removing those has the same effect.

@bounceme
Copy link
Collaborator

maybe the line above:formatexpr? I'm not able to test now

@TamsynUlthara
Copy link
Contributor Author

Found the problem:

  " indent/javascript.vim 413:419
  " If the line is empty and the previous nonblank line was a multi-line
  " comment, use that comment's indent. Deduct one char to account for the
  " space in ' */'.
  if line =~ '^\s*$' && getline(prevline) !~ '\%(\%(^\s*\/\/\|\/\*\).*\)\@<!\*\/' &&
        \ s:IsInComment(prevline, 1)
    return indent(prevline) - 1
  endif

That's erroneously deleting a character from the indentation in the case of a preceding single-line (//) comment.

@TamsynUlthara
Copy link
Contributor Author

Changing the above code to the following always does the right thing for me now:

  " If the line is empty and the previous nonblank line was a comment, use
  " that comment's indent.
  if line =~ '^\s*$' && s:IsInComment(prevline, 1)
    return indent(prevline)
  endif

I'm not sure what the "deduct one char" bit was trying to do; I'd expect a line following the close of a multi-line comment to indent to the start of the preceding */. If changing the behavior here is acceptable, I can submit a PR.

@bounceme
Copy link
Collaborator

i would wait to make a pr, my guess is something else is going on. also, i believe deducting a char is necessary when starting a new line after a multiline comment

@bounceme
Copy link
Collaborator

 if line =~ '^\s*$' && getline(prevline) =~ '\%(\%(^\s*\/\/\|\/\*\).*\)\@<!\*\/' &&
        \ s:IsInComment(prevline, 1)
    return indent(prevline) - 1
  endif

maybe it will work with the above? my mistake if so for the incorrect condition

bounceme added a commit that referenced this issue Jun 18, 2016
bounceme added a commit that referenced this issue Jun 18, 2016
@bounceme
Copy link
Collaborator

give develop another try

@iryan2
Copy link

iryan2 commented Jun 20, 2016

Just chiming in to let you guys know I came across this problem earlier today, and updating to the latest develop fixed the issue for me.

@amadeus
Copy link
Collaborator

amadeus commented Jun 20, 2016

Closing this because I believe it has been fixed in develop. If not someone please chime in and I can re-open.

@amadeus amadeus closed this as completed Jun 20, 2016
@TamsynUlthara
Copy link
Contributor Author

Almost everything looks right now; thanks!

There's still one case with single-line comments I'm seeing an issue with:

call(
  // existing comment 1
    // opening a new line here indents to here
  // existing comment 2
)

@bounceme
Copy link
Collaborator

what settings do you use? does this happen while typing or after =? still am not able to reproduce

@bounceme
Copy link
Collaborator

bounceme commented Jun 20, 2016

I see now, this is an issue with the syntax: @amadeus

call(
  // existing comment 1

  // opening a new line here indents to here
)

the third line at col 1, is incorrectly being highlighted (though there are no characters to show it) as a comment

@bounceme bounceme reopened this Jun 20, 2016
@amadeus
Copy link
Collaborator

amadeus commented Jun 21, 2016

@bounceme not sure I follow. If I type this out like you have above, I get:

example

@bounceme
Copy link
Collaborator

here is the only way i can reproduce, first install https://github.com/tpope/vim-scriptease
set fo-=o set fo-=r
paste this into a blank buffer:

call(
  // existing comment 1
  // opening a new line here indents to here
  // existing comment 2
)

on the third line press o or O,sometimes it seems to not happen, but if there is another indent created press esc then zS (from scriptease)

@amadeus
Copy link
Collaborator

amadeus commented Jun 21, 2016

Still can't reproduce. I don't use scriptease, but I have something similar for getting syntax highlighting state:

function! SynStack()
  echo join(map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")'),
    \ " > ")
endfunc
nnoremap <F7> :call SynStack()<CR>

I get the scenario you mentioned, where if I press O from the third line, it's indented twice. If I press Esc though, the white space gets automatically removed, and the cursor i placed at the beginning of the line, and checking my highlighting context appears normal

example

@bounceme
Copy link
Collaborator

what echoes when I use your function is jsparen > jscomment

@bounceme
Copy link
Collaborator

bounceme commented Jun 21, 2016

this then uses c-indent, try removing this paragraph(edit not on master but dev):
https://github.com/pangloss/vim-javascript/blob/master/indent/javascript.vim#L333

@bounceme
Copy link
Collaborator

screen shot 2016-06-20 at 7 22 52 pm

@amadeus
Copy link
Collaborator

amadeus commented Jun 21, 2016

@bounceme what version of Vim/MacVim do you have?

I've seen this sorta thing before, although usually if you type something or wait a second, it fixes itself.

@bounceme
Copy link
Collaborator

vim 7.4.1941

@bounceme
Copy link
Collaborator

I can do something to fix the indenting at least, if this is just a general syntax problem

@amadeus
Copy link
Collaborator

amadeus commented Jun 21, 2016

All the jsComment stuff is super legacy. I can take a stab at modernizing it a bit, and see if that fixes things?

@bounceme
Copy link
Collaborator

that'd be great! thanks

@amadeus
Copy link
Collaborator

amadeus commented Jun 21, 2016

@bounceme with what I just merged into develop, can this issue be marked as solved?

@bounceme
Copy link
Collaborator

yes, this troublesome issue is gone haha

@amadeus amadeus closed this as completed Jun 21, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants