Skip to content

fix: some comments#7

Open
line-o wants to merge 2 commits intomainfrom
fix/some-comments
Open

fix: some comments#7
line-o wants to merge 2 commits intomainfrom
fix/some-comments

Conversation

@line-o
Copy link
Collaborator

@line-o line-o commented May 22, 2025

Normalize indent on multi-line comments and fill trailing one-line comments.

@line-o line-o force-pushed the fix/some-comments branch from 8ada206 to 41c7b81 Compare May 22, 2025 06:20
@line-o line-o requested a review from DrRataplan May 22, 2025 06:20
const [first, ...rest] = lines
const trimmed = rest.map(line => {
const t = line.trim()
const alignment = t.startsWith(':') ? ' ' : ' '
Copy link
Owner

Choose a reason for hiding this comment

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

I think prettier has some tooling for this that also handles tabs vs spaces. Called align or something

Copy link
Owner

Choose a reason for hiding this comment

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

Looking at this again, I am thinking two things:

100% for reformating and aligning docblocks. Since this is an opinionated formatter we should align with xqdoc. So docblocks shaped like this:

(:~
 : My comment
 :)

For normal block comments like you'd use when commenting out something, we should output it directly:

let $x := "bla"
(: let $_ :=
  util:log(
    "INFO",
    "The value for $x at this point is " ||
      $x ||
      " some more code to go over the 80 character limit"
  ) :)
return $x

As opposed to aligning all this to the start:

let $x := "bla"
(: let $_ :=
    util:log(
    "INFO",
    "The value for $x at this point is " ||
    $x ||
    " some more code to go over the 80 character limit"
    ) :)
  return $x

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yep, that sounds much better.
the colon alignment is already done in this PR but it will break the verbatim output.
What to do in mixed cases?

(:~
 : It started as a XQDoc 

but then I lost interest
:)

Copy link
Owner

@DrRataplan DrRataplan May 23, 2025

Choose a reason for hiding this comment

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

playground

/*
Block comment
   AA
end
*/

/*
 * Not really anything...
 * But still indented
 */

/**
 * A full JSDoc
 * All the lines
 */

/**
 * It started as a JSDoc
But then I stopped caring
* And then I cared again!
    * A markdown list?
      * With nesting?
 */

@line-o
Copy link
Collaborator Author

line-o commented May 22, 2025

line-o added 2 commits May 24, 2025 10:49
- renamed the workflow "Node.js CI" to Test which makes it clearer what it does
- add semantic-release configuration and GHA workflow, this replaces npm-publish.yml
- add commtlint configuration allowing longer lines in commit message bodies
@line-o line-o force-pushed the fix/some-comments branch from 41c7b81 to f1ff76f Compare May 24, 2025 09:07
@line-o
Copy link
Collaborator Author

line-o commented May 24, 2025

oops ... let me remove the other PR first

@github-actions
Copy link

Visit the preview URL for this PR (updated for commit f1ff76f):

https://prettier-plugin-xquery-demo--pr7-fix-some-comments-bj8pklhz.web.app

(expires Sat, 31 May 2025 09:08:23 GMT)

🔥 via Firebase Hosting GitHub Action 🌎

Sign: 05f844b4fde774e8f22615c0409beb01474e9378

@DrRataplan
Copy link
Owner

DrRataplan commented May 27, 2025

Let's do the following:

  • For single-line comments ((: I am single line :) ), ensure leading space and trailing space for the content.
    • Do:
      • (: I am single line :)
    • Do not:
      • (:Too Close!:)
  • For multi-line comments without leading : for all the lines of the content, ensure leading and trailing space for the content but trim spaces on the end of the line.
    • Do:
let $x := "a" 
(: let $_ := $x || "B"
let $_ := util:log("info", "The value of X is " || $x) :)
return $x
  • Don't:
let $x := "a" 
(: let $_ := $x || "B"<space><space><space>
let $_ := util:log("info", "The value of X is " || $x) :)
return $x
  • For block comments, if all the lines start with a :, align the : with spaces (<space>:<space>) and start and end with a newline. Finally, trim trailing spaces from all lines.
    • Do:
(:
 : I am multiline blocky comment
 :)
  • Do not:
* (: I am multiline blocky comment
 : But I should not start with a newline
 :)
let $x := "A" (: I am multiline blocky comment
 : But I should not start with a newline
 :)
return $x
(:~
:I am multiline blocky comment
:But I am too cramped because my lines do not start with `<space>:<space>`
:)
(:~
: I am multiline blocky comment
: But I end without a trailing newline :)
(:~<space><space><space>
: I am multiline blocky comment<space><space>
: But I have trailing spaces<space>
:)
  • For docblock comments (aka xqdoc) that start with a ~, enforce leading aligned <space>:<space> for all lines and start and end with a newline. Finally, trim trailing spaces from all lines.
    • Do:
(:~
 : I am a docblock
 :)
  • Do not:
(:~ I am a docblock
: But I should not start with a newline
:)
(:~
  I am a docblock
: But I do not always start with a `:`
:)
(:~
: I am a docblock
: But I end without a trailing newline :)
(:~
:I am a docblock
:But I am too cramped because my lines do not start with `<space>:<space>`
:)

I would personally implement this is in a separate file to make unit testing it easier and prevent the main.ts from filling up too much. It's starting to become a set of relatively complicated rules.

@bwbohl
Copy link

bwbohl commented Jun 3, 2025

First, many thanks for this great tool @DrRataplan. It still has some way to go, but it’s the best option I've found so far for good XQuery formatting. I tested this PR on some of my XQueries and was quite pleased with how it handled inline documentation comments. What I discovered, though, is that it sometimes adds extra space before a comment starting on a newline and which had no indentation before, both for some arbitrary comments and XqDoc comments.

@DrRataplan
Copy link
Owner

Hey @bwbohl,

Thank you for the kind words! Agreed, not done yet!

Do you have a clear example of where it does this? I would love to fix it, or at least make sure it's fixed as part of this PR! Could you open an issue for it?

Kind regards,

Martin

@bwbohl
Copy link

bwbohl commented Jun 4, 2025

Do you have a clear example of where it does this? I would love to fix it, or at least make sure it's fixed as part of this PR! Could you open an issue for it?

I tried formatting the XQLs in https://github.com/Edirom/Edirom-Online-Backend/tree/develop/data/xql

For XQdoc comments, it seems to happen if they are preceded by comments or comments and empty lines, e.g.:

https://github.com/Edirom/Edirom-Online-Backend/blob/2048f74a76dd2f978e6490837899062d7bd01a0d/data/xql/getAnnotationInfos.xql#L33-L36

@bwbohl
Copy link

bwbohl commented Jun 4, 2025

@bwbohl
Copy link

bwbohl commented Jun 4, 2025

Oh, this one is good, because it has both cases with added extra space before the comment:
https://github.com/Edirom/Edirom-Online-Backend/blob/2048f74a76dd2f978e6490837899062d7bd01a0d/data/xql/getEditions.xql

line 6 and line 12

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