ENTER not working with lone closing bracket at beginning of line #24

Closed
fschwach opened this Issue Oct 18, 2012 · 15 comments

Comments

Projects
None yet
2 participants

This is in Perl but I guess it is a more general problem.

What I want to end up with:

if () {

} elsif () {

}

However if you get to this point (cursor indicated by | )

if () {

} elsif (){|}

and hit ENTER, you get this:

if () {

}
|
elsif () {
}

i.e. the elsif is placed in a new line and the cursor is above it. What I would have expected is to have the cursor in the indented elsif block.

It works if you avoid the lone closing bracket at beginning of line, i.e. starting like this

if () {

}
elsif () {|}

hitting ENTER now produces

if () {

}
elsif () {

}

but the above (top of page) version with the closing bracket at the beginning of the line followed by elsif is a frequently used coding style in Perl, so this should work.
Thanks for looking into this and thanks for providing this very useful plugin!

addition:

there is an additional problem if the whole block is indented already, as in this one for example

f () {

} elsif () {
if () {

} elsif () {

}
}

hitting ENTER between the curly brackets of the second elsif not only places the elsif in a new line and the cursor above it, it also brakes the indentation and produces this:

if () {

} elsif () {
if (){
}

elsif () {
}
}

in general there seems to be a problem maintaining the indentation when inside an already indented block of code.

Owner

jiangmiao commented Oct 18, 2012

I create a.pl and try above but cannot reproduce the issue. so I need some more infomation.

  1. the output for :imap <CR>

    It shows i <CR> &@<CR><SNR>39_AutoPairsReturn here

  2. create a code block like

    if () {
    
    } elsif () {
    |}
    

    Then press <C-R>=strtrans(AutoPairsReturn()) and return at | in insert mode.

    It shows

    if () {
    
    } elsif () {
    ^[=ko}
    

    here

  1. this is what I see in the output of :imap
    i &@29_AutoPairsReturn

in fact, here's the whole list just in case this is useful

i *@=AutoPairsDelete()
i î *@:call AutoPairsJump()a
i ð *@autopairstoggle()
i â *@=AutoPairsBackInsert()
i å *@=AutoPairsFastWrap()
i &@29_AutoPairsReturn
i *@=AutoPairsSpace()
i " *@=AutoPairsInsert('"')
i ' *@=AutoPairsInsert('''')
i ( *@=AutoPairsInsert('(')
i ) *@=AutoPairsInsert(')')
i [ *@=AutoPairsInsert('[')
i ] *@=AutoPairsInsert(']')
i *@<C-R>=AutoPairsInsert('')
i { *@=AutoPairsInsert('{')
i } *@=AutoPairsInsert('}')
i * =BackwardsSnippet()
i ISurround * =40_insert(1)
i Isurround * =40_insert()
i AutoPairsReturn & 29_AutoPairsReturn
i 29_AutoPairsReturn * =AutoPairsReturn()
!
i S ISurround
i s Isurround
i * =TriggerSnippet()
i * =ShowAvailableSnips()
i Isurround

  1. yes, I get what you get:

if () {

} elsif () {
^[=ko}

Owner

jiangmiao commented Oct 18, 2012

It looks no problem to me,
Try manual press <ESC>=ko or <C-[>=ko at | in the code block instead of <C-R>...
see which key cause the plugin insert new line above instead of a new line below elsif().

ooops, sorry, forgot the markup. This is my imap output:

i <CR> &@<CR><SNR>29_AutoPairsReturn

Owner

jiangmiao commented Oct 18, 2012

That's ok, I could see the original code by edit.

ahh, now I think I understand. Your code uses "=" to do the code formatting, right? This calls perltidy in my case and it is true that perltidy uses the coding standard with "elsif" being on a new line, which will mess up my formatting. So I guess I need to find out how to configure perltify to stop doing this. Would be worthwile anyway for me. Thanks for your help in locating the problem

Owner

jiangmiao commented Oct 18, 2012

You're right, Auto-Pairs use =k to format the cursor line and the line above. the related code is at line 290 in auto-pairs.vim.

I modified my perltidy settings but it hasn't solved my problem.
perltidy is now doing the right thing, i.e. if I start with this

if () {

} elsif () {}

then select everything and press = to start perltidy I get

if () {

} elsif () {
}

but auto-pairs will still produce

if () {

}
|
elsif () {
}

and presing -k-o inside the elsif brackets gives me this:

if () {

}
elsif () { }

I think this is because perltidy only sees the line
} elsif (){}

when it is launched in this way and then it doesn't know that this is the closing bracket of the "if" block.
I guess it may not be possible to get auto-pairs and perltidy to play nice together or do you see any way this could be solved?

Owner

jiangmiao commented Oct 18, 2012

what's your perltidy = related setting?

in my .vimrc I have this:
autocmd filetype perl setlocal equalprg=perltidy\ -i=2\ -q\ -bar\ -ce\ -nibc\ -nolq
is that what you meant?

Owner

jiangmiao commented Oct 18, 2012

If = is mapping to run perltidy, I think it is not good to use auto-pairs's <CR> mapping, or map perltidy to another key, because of everytime when <CR> pressed after ( { or [ will trigger and run shell command perltidy is not worth and takes a lot of CPU.

sometimes when indentation plugin cannot work well. will cause

foo() {|}

to

foo() {
    |
    }

That's why auto-pairs format the line

Owner

jiangmiao commented Oct 18, 2012

I just tried the .vimrc setting, and <CR> become a lot slow as I thought, I think I could add a new flag to avoid auto-pairs format the line.

jiangmiao closed this in 3c18515 Oct 18, 2012

Owner

jiangmiao commented Oct 18, 2012

now AutoPairsReturn will do a insert new blank line above without doing format. the issue is closed by commit. feel free to ask if there is still have any issue.

Thank you so much, works beautifully now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment