Skip to content
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

Break down long method chained calls #13

Closed
nenad-lambdas opened this issue Mar 23, 2020 · 8 comments
Closed

Break down long method chained calls #13

nenad-lambdas opened this issue Mar 23, 2020 · 8 comments
Labels
enhancement New feature or request

Comments

@nenad-lambdas
Copy link

nenad-lambdas commented Mar 23, 2020

Best described by an example:
rc.Logger.WithField("FIELD", *someField).Info("Some Very Long Info Here")

could be possibly broken down to:
rc.Logger.
WithField("FIELD", *someField).
Info("Some Very Long Info Here")

OR

rc.
Logger.
WithField("FIELD", *someField).
Info("Some Very Long Info Here")

Formatting is not working, but essentially move the methods chain one per line.

@yolken-segment
Copy link
Contributor

Hi, thanks for the report. Agreed that that would be a good way to split chained methods in some cases. I'll try to dig in more when I get a chance.

@yolken-segment yolken-segment added the enhancement New feature or request label Mar 23, 2020
@nenad-lambdas
Copy link
Author

If you point me in the right direction, happy to look into this further and provide a PR.

@yolken-segment
Copy link
Contributor

yolken-segment commented Mar 24, 2020

Here's a plot of the AST for the https://github.com/segmentio/golines/blob/master/_fixtures/chained_calls.go example (generated by running golines with the --dot-file flag, and then generating a png from the output):

graphviz

Basically, to change the chaining behavior, we need to update the logic in https://github.com/segmentio/golines/blob/master/shortener.go#L500 to "look ahead" at whether Fun->X is itself a CallExpr, and if so to insert a newline before the CallExpr.

It's a little tricky and definitely not a trivial change, but should be doable.

@ackerr
Copy link

ackerr commented Jan 16, 2021

Is there any news?

@mariotoffia
Copy link

Hi and thanks for a very nice formatter! :)

This is super interesting, I've got the following

tx := NewTxLogReader().UseDir(filepath.Dir(fp)).RegisterReader("coinbasepro", coinbasepro.NewTransactionLogReader()).ReadBuffer("coinbasepro", []byte(data))

and it gets formatted to

	tx := NewTxLogReader().UseDir(
		filepath.Dir(fp),
	).RegisterReader(
		"coinbasepro",
		coinbasepro.NewTransactionLogReader(),
	).ReadBuffer(
		"coinbasepro",
		[]byte(data),
	)

but I really want to have it this way:

	tx := NewTxLogReader().
		UseDir(filepath.Dir(fp)).
		RegisterReader("coinbasepro", coinbasepro.NewTransactionLogReader()).
		ReadBuffer("coinbasepro", []byte(data))

It seems to preffer to break at open paranthesis instead of dot? Could one have a config option for that?

Cheers,
Mario :)

@yolken-segment
Copy link
Contributor

yolken-segment commented Apr 8, 2021

Ok, I think #28 should help things here. With the example above, I get:

	tx := NewTxLogReader().UseDir(filepath.Dir(fp)).
		RegisterReader("coinbasepro", coinbasepro.NewTransactionLogReader()).
		ReadBuffer("coinbasepro", []byte(data))

Feel free to give it a spin and add any feedback you have in the pull request. Otherwise, will merge in a few days.

@mariotoffia
Copy link

Hi @yolken-segment and thanks, it works perfectly!!

Cheers,
Mario :)

@yolken-segment
Copy link
Contributor

Fixed by #28. Thanks again for the feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants