Skip to content

dot chained calls are not indented correctly #139

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
nisaacson opened this issue Oct 10, 2013 · 37 comments
Closed

dot chained calls are not indented correctly #139

nisaacson opened this issue Oct 10, 2013 · 37 comments

Comments

@nisaacson
Copy link

When chaining calls together via the . operator, the following lines are not indented correctly

Desired

one()
  .then(two)
  .then(function() {
  })
  .fail(failHandler)

Current

one()
.then(two)
.then(function() {
})
.fail(failHandler)

Is there a way I can add a custom regular expression to perform additional indentation after the standard vim-javascript indent plugin has finished? Adding an additional two spaces to any line that begins with . would be a vast improvement

@qstrahl
Copy link
Collaborator

qstrahl commented Oct 15, 2013

Is there a way I can add a custom regular expression to perform additional indentation after the standard vim-javascript indent plugin has finished?

Don't bother; I think this (indenting the chained calls) should be the default behaviour, we'll likely add support if the other maintainers agree.

@amadeus
Copy link
Collaborator

amadeus commented Oct 15, 2013

I would love this functionality as well, unfortunately I haven't been able to spend the time to figure out how the indent files actually work, so I am unable to fix at the this time.

@slindberg
Copy link

👍 I would have done this already if simply adding a regex would work... alas the indentation logic is quite complicated.

@goatslacker
Copy link
Collaborator

yes it is. Although I can probably get this in, it should be similar to the comma logic.

@cpixl
Copy link

cpixl commented Apr 16, 2014

A great feature that is missing! +1

@FrigoEU
Copy link

FrigoEU commented Apr 29, 2014

Any news on this one?

@jeffrom
Copy link

jeffrom commented Jul 15, 2014

+1 would love this

@danielchabr
Copy link

I would also greatly appreciate this feature +1

@smdahlen
Copy link

smdahlen commented Aug 4, 2014

+1

@doxavore
Copy link

I'm not a vim scripter, but I have some very basic support for this here: https://github.com/doxavore/vim-javascript/tree/dot-chained-indent

It handles things like

one()
  .then(two)
  .then(function() {
  })

But stumbles a little with the original example, creating:

one()
  .then(two)
  .then(function() {
  })
    .fail(failHandler)

If someone has some time to work on this, perhaps it will offer a start.

@bryankennedy
Copy link

Nice work doxavore. That is partially working for me. If I visually select a group it will correctly format things with an = call.

However, it doesn't help if I'm writing a statement like this:

var thing = one.two()
    .chain()

and then hit enter and type, I end up with this.

var thing = one.two()
    .chain()
.nextChain()

I'm also not really a Vim Scripter and not sure how plugins handle this sort of eventuality.

@anshul
Copy link

anshul commented Sep 19, 2014

@qstrahl @amadeus Any updates?

@jameslai
Copy link

jameslai commented Dec 2, 2014

+1

@qstrahl
Copy link
Collaborator

qstrahl commented Dec 3, 2014

I really need to get around to learning how the indentation stuff works, because the absence of this feature has been bothering me for months. I'll try to look into it tonight. We'll see if I can't shake a stick at implementing decent behaviour for this.

@amadeus
Copy link
Collaborator

amadeus commented Dec 3, 2014

@qstrahl I've poked around a bit in the file, and it's not gonna be an easy task.

As for everyone else in this thread, the person who wrote the indent logic for this originally is no longer contributing to the project. So editing/updating it is going to require one or some of us to figure it out. And it's been tough to find time to do this. Please note we aren't just ignoring the issue, I would love this feature too.

@jeffrom
Copy link

jeffrom commented Dec 3, 2014

Hey everyone maybe this is a good time to figure out and document some of the steps for testing this. seems like it would help us understand the code better. javascript has a pretty flexible syntax as many of us are aware and i'll bet a lot of it is not explicit, plus vimscript is witchcraft (this plugin is pretty hairy too, at least to me), and the creator is gone, so seems like a good reason to test some of this out. I'll try to figure something out tonight or tomorrow night, with the caveat that i have never made anything but the most trivial vimscripts.

@qstrahl
Copy link
Collaborator

qstrahl commented Dec 4, 2014

So, after taking a look at our indent file and vim's help files on indenting, I can see that our indent file kinda sorta does some of what I was thinking of doing. Basically, it uses a combination of regexes and keywords and syntax group cues to determine what the indent should be.

I think we can cut that down to just using syntax cues. That is, we'll have a list of syntax items that, if the line ends with them, you should indent up/down. I don't think we need to be more specific than that, but if we do, I'm sure very very simple syntax patterns will be enough.

I'm going to do a little more investigation (asking Freenode#vim) to see if this approach is a terrible idea, because I suspect it might be -- I think vim wants syntax and indent to be separate concerns. If this is the case, we can put it to a vote whether or not we care. =P

@jeffrom
Copy link

jeffrom commented Dec 4, 2014

@qstrahl Hey if it works it works, no need for my vote :)

@qstrahl
Copy link
Collaborator

qstrahl commented Dec 4, 2014

@jeffrom The issue (if there really is an issue) would be that you need to have syntax enabled in order for indent to work, which might be counter to what vim (and by extension, a vim user) expects. If that's the case, I feel it's important to have a consensus from users/devs before implementing, even if I'm pretty sure no modern editor has any such expectation of indent/syntax being separate concerns. =P

@sukima
Copy link

sukima commented Mar 2, 2015

I don't think there is any reliable way to do this and not force semi-colons (without tricks like syntax). Now for me that's totally okay since I like semi-colons (for this very reason) but many developers prefer the more renegade style of flexing JavaScript's auto semi-colon insertion (ASI).

Thoughts?

@goatslacker
Copy link
Collaborator

If it starts with a dot, indent. This is probably not bullet proof however.

@sukima
Copy link

sukima commented Mar 3, 2015

@goatslacker good point. I guess I took for granted that the dot can also be trailing. Investigating...

@goatslacker
Copy link
Collaborator

So I did this for leading commas and it worked for 80% of the use cases, not sure if dot would be the same case or not. I haven't fully thought this through.

Trailing dot wouldn't work, but I guess if the dot is trailing you could also indent. I think this is the current behaviour actually.

sukima added a commit to sukima/vim-javascript that referenced this issue Mar 3, 2015
Most style guides depict chained methods to have a leading dot ans be
indented on level deeper then the initial statement.

This patch adds a period as a key symbol for a nested indent. In the
same fashion as the leading comma does.

It allows an indent of the following:

      var x = {
        x: 'foo',
        y: 'bar',
        z: (function() {
          return [1, 2, 3]
            .map(toFruits)
            .compact()
            .sortBy('color');
        })()
      };

This should close issue pangloss#139.
@sukima
Copy link

sukima commented Mar 3, 2015

@goatslacker see PR #248. My dev tests show this as working.

@devm33
Copy link

devm33 commented Jun 12, 2015

Any update on this? I'd be happy to spend some time getting this working.

@sukima
Copy link

sukima commented Jun 14, 2015

@devm33 No progress, I've learned to live with the dot as a line prefix not a suffix.

@lvarayut
Copy link

lvarayut commented Oct 6, 2015

Any progress on this issue? I'm having the same problem.

@chalmagean
Copy link

+1

1 similar comment
@robinheghan
Copy link

+1

@randunel
Copy link

Had to build my own fix for the dot start https://github.com/randunel/vim-javascript. Would be nice to include something similar in this lib though.

@djmccormick
Copy link

+1

@ksmithbaylor
Copy link

@randunel Is that something you could make a pull request out of? How well does it work?

@mistercrunch
Copy link

+1

bounceme added a commit to bounceme/vim-javascript that referenced this issue Apr 19, 2016
generally this will give us fairly accurate indents without having specific code for each operator.Thanks to pangloss#139 (comment) which this is based on
I think this needs a few things before it's ready
bounceme added a commit to bounceme/vim-javascript that referenced this issue Apr 19, 2016
generally this will give us fairly accurate indents without having
specific code for each operator.Thanks to
pangloss#139 (comment)
which this is based on
bounceme added a commit to bounceme/vim-javascript that referenced this issue Apr 19, 2016
generally this will give us fairly accurate indents without having
specific code for each operator.Thanks to
pangloss#139 (comment)
which this is based on
bounceme added a commit to bounceme/vim-javascript that referenced this issue Apr 19, 2016
generally this will give us fairly accurate indents without having
specific code for each operator.Thanks to
pangloss#139 (comment)
which this is based on Update javascript.vim
bounceme added a commit to bounceme/vim-javascript that referenced this issue Apr 19, 2016
generally this will give us fairly accurate indents without having
specific code for each operator.Thanks to
pangloss#139 (comment)
which this is based on Update javascript.vim
bounceme added a commit to bounceme/vim-javascript that referenced this issue Apr 20, 2016
generally this will give us fairly accurate indents without having
specific code for each operator.Thanks to
pangloss#139 (comment)
which this is based on Update javascript.vim
bounceme added a commit to bounceme/vim-javascript that referenced this issue Apr 20, 2016
generally this will give us fairly accurate indents without having
specific code for each operator.Thanks to
pangloss#139 (comment)
which this is based on Update javascript.vim
@bounceme
Copy link
Collaborator

a problem no longer

@jeffrom
Copy link

jeffrom commented Apr 30, 2016

A hero in our time

@pgraham
Copy link

pgraham commented Jan 9, 2018

So is this a case of squeaky wheel gets the grease? I liked the original behaviour better, can it be restored, maybe with a variable?

@bounceme
Copy link
Collaborator

bounceme commented Jan 9, 2018

let g:javascript_opfirst = '^\C\%([<>=,?^%|/&]\|\([-:+]\)\1\@!\|\*\+\|!=\|in\%(stanceof\)\=\>\)'

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

No branches or pull requests