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

escape hatch not respected inside if statement #182

Closed
ntjess opened this issue Dec 13, 2024 · 2 comments · Fixed by #188
Closed

escape hatch not respected inside if statement #182

ntjess opened this issue Dec 13, 2024 · 2 comments · Fixed by #188

Comments

@ntjess
Copy link

ntjess commented Dec 13, 2024

Cross-posting as requested from Myriad-Dreamin/tinymist#996

According to the docs:

If you find typstyle is not working as expected, you can use // @typstyle off or /* @typstyle off */ to disable the formatter on the next node of code.

However, neither comment style prevents tinymist from applying formatting in certain circumstances.

#if true {
  // @typstyle off
  let x = (
  "A message that will be split into multiple lines to ensure that each"
  + " line does not exceed sixty characters in length. This is a common"
  + " practice to improve readability and maintainability of the code."
  )
  // @typstyle on
}

Without the if block, the escape hatch works. Inside if, it fails.

Side note: I think splitting operators to their own lines is a "favorable" default behavior compared to merging them into one line when the result would be very long

@QuadnucYard
Copy link
Collaborator

In the lastest release version of typst (not updated in tinymist yet), your code is formatted as:

#if true {
  // @typstyle off
  let x = (
    "A message that will be split into multiple lines to ensure that each"
      + " line does not exceed sixty characters in length. This is a common"
      + " practice to improve readability and maintainability of the code."
  )
  // @typstyle on
}

(Binary chain is here!)

After looking into the case, I found some defects.

Firstly, in your example, the directive // @typstyle off actually applies to the Code in the code block that wraps let. However, we do not check whether formatting Code is disabled, since we mix it into the code block in our process.
It seems that it is not the if expression but the code block that caused this bug.

Secondly, in the code below, the directive takes effect on # instead of the let-binding.

// @typstyle off
#let x = ()

Besides, @typstyle on is not a valid directive. We simply check whether the comment contains @typstyle off to decide whether to format the next expression.

Thanks for your bug report! 😄

@ntjess
Copy link
Author

ntjess commented Dec 13, 2024

I see, thanks for the detailed response!

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 a pull request may close this issue.

2 participants