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

Add basic line length based formatting support #66

Merged

Conversation

gingermusketeer
Copy link
Member

@gingermusketeer gingermusketeer commented Nov 18, 2017

First step as part of #54.

Adds:

  • doc_printer: This is used to print the assembled document.
  • doc_builder: This is used to assemble a document for printing.
  • Support for multiple config options per test.
  • Ability to configure how long a line should be via print_width
  • Line length based formatting for arrays.

Remaining:

  • Heredoc handling (this is what is causing the last 5 tests to fail).

@mjago
Copy link
Member

mjago commented Nov 20, 2017

@gingermusketeer This is looking great! 👍

Copy link
Member

@danielma danielma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking great! I have a few small things I'd tweak, but no major hangups. This is a great step forward!

# arg1, ..., *star
doc = visit args
else
pre_doc, *_ = with_doc_mode {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a multiline block, can we use do...end?

doc << B.concat([last, B.if_break(",", "")])
end

B.group(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This expression is really nice to read 👏.

In the end, I think this points to a more readable future for rufo. Instead of needing to keep the entire stack in your head to understand a point in code, we can capture output and express the ultimate shape of an expression at the end


elements.each_with_index do |elem, i|
doc_el = visit(elem)
if doc_el.is_a?(Array)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: what do you think about coalescing every doc_el into an array? This can replace this entire if expression

doc.concat Array(doc_el)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tried this and it causes the tests to break. Perhaps we can revisit this once we are further down the implementation of prettier printer.

value = options.fetch(name, default)
unless valid_options.include?(value)
$stderr.puts "Invalid value for #{name}: #{value.inspect}. Valid " \
"values are: #{valid_options.map(&:inspect).join(', ')}"
"values are: #{valid_options.map(&:inspect).join(', ')}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did a space get added here? Was it added manually, or did rufo do that in the formatting pass?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Um not sure to be honest. I will run the formatter over it once the tests are passing. Hopefully that will remove it.

@gingermusketeer
Copy link
Member Author

@danielma Thanks for the feedback :)

@mjago
Copy link
Member

mjago commented Nov 22, 2017

@gingermusketeer just a heads-up whilst checking out your printer:
I noticed the following (print_width: 80):

[([1,2]), ([3,4]), ([5,6])]

formats to:

[), ), )]

Why: So that syntax highlighting will work in some editors.
Copy link
Member

@bessey bessey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good to me.

Why: content was being removed due to the regex being used.
Why: So that each type does not need to be wrapped in a capture_output block.
Why: Until this is used by other parts of the formatter we cannot handle anything before the array other than indentation.
Why: So that tests work on ruby 2.3.
@gingermusketeer gingermusketeer force-pushed the add-basic-line-length-based-formatting-support branch from f6ab762 to ba42b5c Compare December 2, 2017 14:06
@gingermusketeer
Copy link
Member Author

@bessey Apologies for the slow response. Yes your understanding is correct :)

I have all the tests passing now that heredocs are finally working. Only concern is this pending test: https://github.com/ruby-formatter/rufo/pull/66/files#diff-1be4af1647655801c693a52968d5d64dR754

The changes causes any multiline method calls inside an array to have incorrect indentation. I am hesitant to try and tackle that in this PR due to how big this getting. I can see a couple options for how we proceed:

  1. Merge into master and then work on fixing the problem and divide and conquer.
    • Won't be able to release a new version of the gem until this happens (perhaps we should do a release prior?)
  2. Merge into another branch and then divide and conquer.
  3. Fix on this branch.

Thoughts?

@gingermusketeer gingermusketeer changed the base branch from master to new-formatter December 11, 2017 15:28
@danielma
Copy link
Member

✋ vote for

Merge into master and then work on fixing the problem and divide and conquer.

@danielma
Copy link
Member

danielma commented Dec 11, 2017

FYI, I'm working on a branch right now that would fix that indent error. Trying to get familiar with using this doc building code

@mjago
Copy link
Member

mjago commented Dec 11, 2017

I've been talking to @gingermusketeer on Gitter this afternoon and we feel it would probably be better in the long run to develop a new formatter along side the old one, in a branch called new-formatter - complete with its own specs. Once complete the new formatter can be switched into prime time. This allows the current formatter to be maintained and receive bug fixes as reported in isolation to the new formatter work. The idea is to strip the new formatter of much of the old `wiring mechanism' and build up the doc-based formatter piece by piece, working back up from the simpler sexp structures to the more complex structures containing the simpler structures.

@bessey
Copy link
Member

bessey commented Dec 12, 2017 via email

@bessey
Copy link
Member

bessey commented Jan 25, 2018

Any news here? I notice the new-formatter branch is missing this work, perhaps someone hasn't pushed in a while?

@gingermusketeer gingermusketeer merged commit 7bb9e85 into new-formatter Jan 26, 2018
@gingermusketeer gingermusketeer deleted the add-basic-line-length-based-formatting-support branch January 26, 2018 11:37
@gingermusketeer
Copy link
Member Author

@bessey I will merge this branch into there now :)

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

Successfully merging this pull request may close these issues.

4 participants