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 simple newline insertion to formatter #334

Merged
merged 5 commits into from
Aug 1, 2017

Conversation

sbarzowski
Copy link
Collaborator

This fixes "bracebug" (reported here: #299) and many
variants of it.

Of course it's not a complete solution to any newline problems.
In particular it never removes or rearranges newlines, it only
adds some. But it still should help a lot.

This fixes "bracebug" (reported here: google#299) and many
variants of it.

Of course it's not a complete solution to any newline problems.
In particular it never removes or rearranges newlines, it only
adds some. But it still should help a lot.
@sbarzowski
Copy link
Collaborator Author

Some pretty big test cases with a lot of examples are committed. Please take a look at them to check if the output style is acceptable.

The code in FixNewlines may look a little bit repetitive, but it's hard to avoid that. A very simple task is performed on many AST types. Some of it could be abstracted away, e. g. by creating a function that returns "interesting" fodder objects for each. But I think we would still end up with similar number of functions and much less obvious code that requires some dancing around special cases.

There are also some improvements to test scripts:

  • I got frustrated looking for a one line difference in 300 lines tests, so now diff is also presented
  • A few times I passed .fmt.golden to refresh_fmt_test, so we check for that now (and to refresh_test as well).
  • Now a temporary directory is created for running tests. It's only used to help with diff now, but it will be also useful for other tests in the future (for example jsonnet fmt --in-place tests).
  • Now it's possible to set VERBOSE mode through ENV variable and not only by changing the code.

@lwander @sparkprime

Copy link

@lwander lwander left a comment

Choose a reason for hiding this comment

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

Looks good, but I'm not very familiar with what the formatting was intended to produce

get_temp_dir() {
if [ -z "$TMP_DIR" ]; then
# Simplest and most portable way I could find.
TMP_DIR="`python2 -c "import tempfile; print(tempfile.mkdtemp(prefix='jsonnet_'))"`"
Copy link

Choose a reason for hiding this comment

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

Did you consider something along the lines of

TMP_DIR=$(mktemp -d jsonnet_XXXX)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes. There are portability problems. OS X and GNU versions of mktemp need different arguments. Invoking Python seems much cleaner to me than trying to autodetect the version or trying the other version if it fails (the latter is the top answer here https://unix.stackexchange.com/questions/30091/fix-or-alternative-for-mktemp-in-os-x).

// very_long_function_on_a_long_line_so_arguments_dont_fit_on_the_same_line(
// 1, 2, 3)
// Should be expanded to:
// very_long_function_on_a_long_line_so_arguments_dont_fit_on_the_same_line(
Copy link

Choose a reason for hiding this comment

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

I'm not familiar with the other formatting passes, but are any considerations taken for line length? Reading the code here it seems the expansion happens when args are already on another line, not if function is too long.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No, it doesn't take the length into account. The name was only an example situation when someone would format it this way (as opposed to keeping arguments on the same line). Hmmm... but I can see how it can be confusing. Maybe it's better to change it to something generic.

// Should be expanded to:
// f(1,
// 2,
// 3)
Copy link

Choose a reason for hiding this comment

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

Shouldn't the closing paren live on a new line to match the below style? Or is that handled separately?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The point of this comment was to explain what this particular function does, expandBetween as opposed to expandNearParens (both operate on ArgParams).

But still no, it wouldn't be touched by the other one (it would if there was a newline before 1 or after 3).

collCorrect2c: [
{
name: "Bad",
}, self.someOther(),],
Copy link
Contributor

Choose a reason for hiding this comment

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

looks like 2c is the same as 2b

/// 'a': 'b',
/// 'c': 'd',
/// }]
/// The outer array can be unexpanded, because there are no newlines between
Copy link
Contributor

Choose a reason for hiding this comment

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

Incomplete sentence

Copy link
Contributor

@sparkprime sparkprime left a comment

Choose a reason for hiding this comment

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

I like it. It's a bit aggressive with inserting newlines but it uses a simple set of rules and I think users will like it for that reason.

///
/// The main principle is that a structure can either be:
/// * expanded and contain newlines in all the designated places
/// * unexpanded and not contain any newlines in any of designated places.
Copy link
Contributor

Choose a reason for hiding this comment

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

Slightly more concise description:

/// * unexpanded and contain newlines in none of the designated places.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

@sparkprime sparkprime merged commit 7453280 into google:master Aug 1, 2017
@sbarzowski sbarzowski deleted the bracebug branch August 2, 2017 15:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants