Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Unify comment formatting inside the empty blocks #647

Merged
merged 21 commits into from
Sep 26, 2022

Conversation

ah-yu
Copy link
Contributor

@ah-yu ah-yu commented Sep 22, 2022

Unify the format of comments inside the empty block

// before
let foo = [// hi
]

let foo = [//hi
/* hi */]

// now -- same as prettier
let foo = [
  // hi
]

let foo = [
  // hi
 /* hi */
]

@@ -28,6 +28,10 @@ type t = /* c0 */ string /* c1 */ as 'x // after
// Ptyp_poly
type fn = {f: /* c0 */ 'a /* c1 */ 'b /* c2 */. /* c3 */ string /* c4 */}

type fn = {/* comment1 */
Copy link
Contributor

@IwanKaramazow IwanKaramazow Sep 22, 2022

Choose a reason for hiding this comment

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

Can we tweak the output to look more like the input:

type fn = {
   /* comment1 */
   // comment2
 }

@@ -107,6 +107,11 @@ let user = /* before */ {
/* c4 */ age /* c5 */: /* c6 */ 31 /* c7 */,
} // after

// Pexp_record with empty record
let user = /* before */ {/* comment1 */
Copy link
Contributor

Choose a reason for hiding this comment

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

The comments inside look a bit suboptimal, can we go for:

{
   /* comment1 */
   // comment2
 }

@cknitt
Copy link
Member

cknitt commented Sep 22, 2022

Also, I just fixed CI, please rebase on latest master to get CI to run.

@ah-yu ah-yu marked this pull request as draft September 22, 2022 09:44
@ah-yu ah-yu changed the title fix(formatter): keep comments in empty record && don't break empty records Unify comment formatting inside the empty blocks Sep 23, 2022
<A
// Comment
/>
<A>
Copy link
Contributor Author

@ah-yu ah-yu Sep 23, 2022

Choose a reason for hiding this comment

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

Could we keep tags open if these are comments inside? @cknitt
I found this change was introduced in e913c4c

Copy link
Member

Choose a reason for hiding this comment

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

Fine with me! That would make it easier to add code after the comment anyway.

@cristianoc
Copy link
Contributor

Has the scope of this PR changed?
There seem to be differences outside empty records.

@ah-yu
Copy link
Contributor Author

ah-yu commented Sep 23, 2022

Yes. Now this PR is for adjusting the comments format inside an empty block(list, record, etc)

@cristianoc
Copy link
Contributor

Yes. Now this PR is for adjusting the comments format inside an empty block(list, record, etc)

Would you add an entry in the CHANGELOG.md as this is user-visible.
@IwanKaramazow any more comments?

@cristianoc cristianoc marked this pull request as ready for review September 24, 2022 07:08
Copy link
Contributor

@IwanKaramazow IwanKaramazow left a comment

Choose a reason for hiding this comment

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

If a user wrote [/* a */], can we keep it on one line? Similar for the other "empty nodes".

We tend to look at the style of the author: did he write it over multiple lines or not?

  • If it is on one line, keep it on one line (unless it breaks the column width).
  • If it is written over multiple lines, force break it over multiple lines.

@ah-yu
Copy link
Contributor Author

ah-yu commented Sep 24, 2022

That sounds reasonable, will fix it later.

@ah-yu ah-yu requested a review from IwanKaramazow September 24, 2022 11:34
Doc.rbrace;
])
let doc = printCommentsInside cmtTbl modType.pmty_loc in
if Doc.isNil doc then
Copy link
Contributor

Choose a reason for hiding this comment

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

This does the job but looks like tight coupling at a distance.
When is it that printCommentsInside currently produces Nil, and can we get hold of that information instead (e.g. return an optional).

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, in an ideal world, we shouldn't inspect the contents of a doc to make formatting decisions. The information we go off is the AST (+ comments).

@ah-yu is the isNil absolutely necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

isNil is only used in the development phase, then I could do a quick test without too many code changes.

Forgot to clean it up and will do it later.

@@ -113,6 +113,12 @@ let hasNestedJsxOrMoreThanOneChild expr =
in
loop false expr

let hasCommentsInside tbl loc =
match Hashtbl.find tbl.CommentTable.inside loc with
Copy link
Contributor

Choose a reason for hiding this comment

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

Can use find_opt

@cristianoc
Copy link
Contributor

@ah-yu just ping when this is ready.

@ah-yu
Copy link
Contributor Author

ah-yu commented Sep 26, 2022

@cristianoc done

@cristianoc
Copy link
Contributor

@cristianoc done

Thanks a lot for contributing this!

@cristianoc cristianoc merged commit 19c7071 into rescript-lang:master Sep 26, 2022
@ah-yu ah-yu deleted the keep-comments branch September 26, 2022 04:55
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants