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
cristianoc merged 21 commits into
rescript-lang:masterfrom
zheyz:keep-comments
Sep 26, 2022
Merged

Unify comment formatting inside the empty blocks#647
cristianoc merged 21 commits into
rescript-lang:masterfrom
zheyz:keep-comments

Conversation

@zheyz
Copy link
Copy Markdown
Contributor

@zheyz zheyz 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 */
]

// Ptyp_poly
type fn = {f: /* c0 */ 'a /* c1 */ 'b /* c2 */. /* c3 */ string /* c4 */}

type fn = {/* comment1 */
Copy link
Copy Markdown
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
 }

} // after

// Pexp_record with empty record
let user = /* before */ {/* comment1 */
Copy link
Copy Markdown
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
 }

Comment thread src/res_comments_table.ml Outdated
@cknitt
Copy link
Copy Markdown
Member

cknitt commented Sep 22, 2022

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

@zheyz zheyz marked this pull request as draft September 22, 2022 09:44
@zheyz zheyz 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
Copy Markdown
Contributor Author

@zheyz zheyz 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
Copy Markdown
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
Copy Markdown
Contributor

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

@zheyz
Copy link
Copy Markdown
Contributor Author

zheyz commented Sep 23, 2022

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

@cristianoc
Copy link
Copy Markdown
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
Copy Markdown
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.

@zheyz
Copy link
Copy Markdown
Contributor Author

zheyz commented Sep 24, 2022

That sounds reasonable, will fix it later.

@zheyz zheyz requested a review from IwanKaramazow September 24, 2022 11:34
Comment thread src/res_printer.ml Outdated
Doc.rbrace;
])
let doc = printCommentsInside cmtTbl modType.pmty_loc in
if Doc.isNil doc then
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

Comment thread src/res_printer.ml Outdated
loop false expr

let hasCommentsInside tbl loc =
match Hashtbl.find tbl.CommentTable.inside loc with
Copy link
Copy Markdown
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
Copy Markdown
Contributor

@ah-yu just ping when this is ready.

@zheyz
Copy link
Copy Markdown
Contributor Author

zheyz commented Sep 26, 2022

@cristianoc done

@cristianoc
Copy link
Copy Markdown
Contributor

@cristianoc done

Thanks a lot for contributing this!

@cristianoc cristianoc merged commit 19c7071 into rescript-lang:master Sep 26, 2022
@zheyz zheyz 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