79238839

Date: 2024-11-30 04:53:51
Score: 0.5
Natty:
Report link

Thanks to @EdanMaor for the S and cc suggestions, they are helpful. Something that started to make this bearable for me was the <C-f> (control-f) binding in insert mode, which performs the equivalent of == (but with the convenience of insert mode). It's set by default in the indentkeys option.

So if you go into insert mode, start writing and see indent is 0 (cursor indicated by | char):

function main() {
        // ...
        
        // ... deeply nested function...
        function something() {
          console.log("ok");
// Star|ted writing a comment
          console.log("after");
        }
        // ...
}

you can hit <C-f> and it will indent on the spot, even if your cursor is in the middle of the line:

function main() {
        // ...
        
        // ... deeply nested function...
        function something() {
          console.log("ok");
          // Star|ted writing a comment
          console.log("after");
        }
        // ...
}

I experimented with this mapping to make it more automatic:

:nnoremap I I<c-f>

It automatically indents the line whenever you insert at the beginning of the line. I'm sure there are ways to make it more seamless.


For the issues with python files, for me it was due to the default python plugin (running neovim v0.9.1). It runs the python#GetIndent() function when pressing == or <C-f>, which doesn't pick up the previous indent you would expect.

I added these few lines to the end of the python#GetIndent() function (right before the default return of -1). It seems to fix the issue, but I'm sure it's broken in some way.

  if a:lnum - plnum < 3
    if getline(a:lnum) =~ '^\s*def\>'
    " dedent if current line is a def (maybe class too?)
        return max([indent(plnum) - shiftwidth(), 0])
    else
    " keep same indent as previous line
        return indent(plnum)
    endif
  endif

The python#GetIndent function is defined in $VIMRUNTIME/autoload/python.vim. If you can't edit it, you can copy the whole file into ~/.config/nvim/after/plugin/python.vim for neovim, and ~/.vim/after/plugin/python.vim for vim, then do the edits there.


References:

Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @EdanMaor
  • Low reputation (0.5):
Posted by: matt0089