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

Improve formatting of the print function #192

Open
JeanJPNM opened this issue Jul 10, 2023 · 2 comments
Open

Improve formatting of the print function #192

JeanJPNM opened this issue Jul 10, 2023 · 2 comments
Labels
enhancement New feature or request
Milestone

Comments

@JeanJPNM
Copy link
Collaborator

JeanJPNM commented Jul 10, 2023

When using print inside indented contexts (functions, if statements, loops, etc) it is not possible to correctly format the template string without breaking the surrounding indentation:

function printVec({ x, y }) {
  print`
    Vector2 {
      x: ${x},
      y: ${y}
    }`
}

printVec({ x: "[x]", y: "[y]" })

Prints:


    Vector2 {
      x: [x],
      y: [y]
    }

It is possible to see that the most common intent with this code would be to print the message without the leading new line and the extra indentation. To achieve this, one would have to rewrite the code as:

function printVec({ x, y }) {
  print`Vector2 {
  x: ${x},
  y: ${y}
}`
}

printVec({ x: "[x]", y: "[y]" })
printFlush()

I propose changing the print function to automatically format template strings by:

  • Removing the first leading newline
  • Deindenting lines based on the indentation of the least indented line.

However, this change may break existing code but I consider it to be worth it.

Edit:

The compiler can't tell the difference between these two examples, so the first would not behave as expected:

print`\n(${Vars.thisx}, ${Vars.thisy})`;
print`
(${Vars.thisx}, ${Vars.thisy})`
@JeanJPNM JeanJPNM added the enhancement New feature or request label Jul 10, 2023
@JeanJPNM
Copy link
Collaborator Author

Seems like the best approach is creating a printf function that only takes a string template as a parameter and has consistent formatting rules

@JeanJPNM JeanJPNM added this to the v0.8 milestone Nov 6, 2023
@JeanJPNM JeanJPNM modified the milestones: v0.8, v0.6 Feb 9, 2024
@JeanJPNM
Copy link
Collaborator Author

Implementing String.dedent from this proposal might prove more benefitial, as it would work with other functions such as concat.

function printPoint({ x, y }) {
  String.dedent(print)`
    Point {
      x: ${x},
      y: ${y}
    }`;
}

printPoint(Vars.this);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant