astrale-sharp / typstfmt

Apache License 2.0
253 stars 25 forks source link

Please make an option to prevent arguments from folding #134

Closed ivaquero closed 11 months ago

ivaquero commented 11 months ago

[x] I have tested with the master version of typstfmt and not just typst-lsp

Describe the bug In many cases, I add new lines after each argument for convenience (when the argument list is long, it will become difficult to read and edit in one line). However, typstfmt deletes the new lines and makes the argument list fold.

To Reproduce Steps to reproduce the behavior:

typstfmt article.type

The tested string

#let csv1 = csv("data/stl-cont.csv")
#figure(
  tablex(
    columns: 8,
    align: center,
    inset: 4pt,
    ..csv1.flatten(),
  ),
  caption: "容器操作",
  supplement: [表]
)

the result after

#let csv1 = csv("data/stl-cont.csv")
#figure(
  tablex(columns: 8, align: center, inset: 4pt, ..csv1.flatten()),
  caption: "容器操作",
  supplement: [表],
)
astrale-sharp commented 11 months ago

Hey there again :)

Maybe you should try to find a value for max_line_length that suits you better

However I agree that I have the same problem but there's no simple solutions

If you feel adventurous you can even try using experimental_args_breaking_consecutive = true

ivaquero commented 11 months ago

Okay, thanks!

To add more description, I think the ideal effect is like what the Python formatter Black does. And another Python formatter Ruff (written in Rust) also implement the same feature.

Could this bring some inspiration ? https://github.com/astral-sh/ruff/tree/main/crates/ruff_python_formatter/src

astrale-sharp commented 11 months ago

What's the feature exactly we are talking about? always preventing arguments from folding? (very easy to do)

ivaquero commented 11 months ago

What's the feature exactly we are talking about? always preventing arguments from folding? (very easy to do)

Yes, you are right.

By the way, experimental_args_breaking_consecutive = true didn't solve this problem.

astrale-sharp commented 11 months ago

It's almost done, one remaining question, should all params like (Args | Params | Dict | Array | Destructuring | Parenthesized) be prevented from breaking or only actual Params and Args?

ivaquero commented 11 months ago

Thank you for your effort!

I think all params-like things.

More specifically, it's better to have separate controls for params-like things (needs new lines) and content items (e.g. items in a table() object, don't need new lines).

For example, there's a big table (5x10), it will occupy too many lines (50 lines).

Like a table in my doc

Before formatting, I make 3 items in a line on purpose

#align(center)[#table(
  columns: 3,
  align: center,
  inset: 4pt,
  [执行函数举例], [阶], [非正式术语],
  [$12$], [$O(1)$], [常数阶],
  [$a n + b$], [$O(n)$], [线性阶],
  [$a n^2 + b n + c$], [$O(n^2)$], [平方阶],
  [$a n^3 + b n^2 + c n + d$], [$O(n^3)$], [立方阶],
  [$2^n$], [$O(2^n)$], [指数阶],
  [$a log n + b$], [$O(log n)$], [对数阶],
  [$a n + b n log n + c$], [$O(n log n)$], [$n log n$ 阶],
)
]

After formatting, it becomes hard to maintain.

#align(center)[#table(
    columns: 3,
    align: center,
    inset: 4pt,
    [执行函数举例],
    [阶],
    [非正式术语],
    [$12$],
    [$O(1)$],
    [常数阶],
    [$a n + b$],
    [$O(n)$],
    [线性阶],
    [$a n^2 + b n + c$],
    [$O(n^2)$],
    [平方阶],
    [$a n^3 + b n^2 + c n + d$],
    [$O(n^3)$],
    [立方阶],
    [$2^n$],
    [$O(2^n)$],
    [指数阶],
    [$a log n + b$],
    [$O(log n)$],
    [对数阶],
    [$a n + b n log n + c$],
    [$O(n log n)$],
    [$n log n$ 阶],
  )
]
astrale-sharp commented 11 months ago

For this result we really need to improve our table management or experimental_args_breaking_consecutive,

I'm afraid just disabling breaking will make for a very long one line table call

astrale-sharp commented 11 months ago

I just pushed the change to #135, could you test the changes there and tell me if it would solve your problem?

ivaquero commented 11 months ago

Hi @astrale-sharp, thanks for your work. I will test it, but I will show you the result one or two days later.

ivaquero commented 11 months ago

Hi, @astrale-sharp, I just find a computer, how to build the tool? I am not familiar with rust, sorry. I cloned the branch config... What is the next step? cargo build

astrale-sharp commented 11 months ago

You could run cat article.typ | cargo run this would output a formatted version of article.typ in the terminal :) It shouldn't be slow to compile either! Don't hesitate if you have more questions !

ivaquero commented 11 months ago

The result is that the update didn't prevent params from folding. However, its behavior changes a little.

In structures like this, the argument list has not been folded, though the table items unfolded except the 1st line.

#align(center)[#table(
    columns: 2,
    align: center,
    inset: 4pt,
    [时间], [复杂度],
    [只有常数项],
    [$O(1)$],
    [顺序结构],
    [加法运算],
    [循环结构],
    [乘法运算],
    [分支结构],
    [取最大值],
  )
]

For structures like below, the result is the same.

#let csv1 = csv("data/stl-cont.csv")
#figure(
  tablex(columns: 8, align: center, inset: 4pt, ..csv1.flatten()),
  caption: "容器操作",
  supplement: [表],
)
astrale-sharp commented 11 months ago

Setting the new prevent_breaking_params = true

I get

#align(center)[#table(columns: 2, align: center, inset: 4pt, [时间], [复杂度], [只有常数项], [$O(1)$], [顺序结构], [加法运算], [循环结构], [乘法运算], [分支结构], [取最大值])
]
astrale-sharp commented 11 months ago

I improved experimental_args_breaking and now for your snippet I get this result

#align(center)[#table(
    columns: 3, align: center, inset: 4pt, [执行函数举例],
    [阶], [非正式术语], [$12$], [$O(1)$],
    [常数阶], [$a n + b$], [$O(n)$], [线性阶],
    [$a n^2 + b n + c$], [$O(n^2)$], [平方阶], [$a n^3 + b n^2 + c n + d$],
    [$O(n^3)$], [立方阶], [$2^n$], [$O(2^n)$],
    [指数阶], [$a log n + b$], [$O(log n)$], [对数阶],
    [$a n + b n log n + c$], [$O(n log n)$], [$n log n$ 阶],
  )
]

Which is pretty good

astrale-sharp commented 11 months ago

You can test it on the same config_no_param_break branch (same as in the PR)

ivaquero commented 11 months ago

Sure!

ivaquero commented 11 months ago

I tried the latest one update, but it did nothing. (I tried both experimental_args_breaking_consecutive on and off.)

astrale-sharp commented 11 months ago

I tried the latest one update, but it did nothing. (I tried both experimental_args_breaking_consecutive on and off.)

You should probably investigate what config file is being used, is it the global one? is it the local one? is it the default one?

We should add an option to print (with --verbose) which config file is being used

astrale-sharp commented 11 months ago

You should get the same snippet I do if you have experimental_args_breaking

ivaquero commented 11 months ago

Okay, I will test again tonight.

ivaquero commented 11 months ago

I got the same result as what you did.

Setting

experimental_args_breaking_consecutive = true
prevent_breaking_params                = false

Result

== 基本规则

#h(2em) 判断一个算法的效率时,往往只需要关注操作数量的最高次项,在没有特殊说明时,算法的时间复杂度均是指最坏时间复杂度。

#align(center)[#table(
    columns: 2, align: center, inset: 4pt, [时间],
    [复杂度], [只有常数项], [$O(1)$], [顺序结构],
    [加法运算], [循环结构], [乘法运算], [分支结构],
    [取最大值],
  )
]

== 常见时间复杂度

$ O(1) < O(log n) < O(n) < O(n log n) < O(n^2) < O(n^3) < O(2^n) < O(n excl) < O(n^n) $

#figure(table(
  columns: 3, align: center, inset: 4pt, [执行函数举例],
  [阶], [非正式术语], [$12$], [$O(1)$],
  [常数阶], [$a n + b$], [$O(n)$], [线性阶],
  [$a n^2 + b n + c$], [$O(n^2)$], [平方阶], [$a n^3 + b n^2 + c n + d$],
  [$O(n^3)$], [立方阶], [$2^n$], [$O(2^n)$],
  [指数阶], [$a log n + b$], [$O(log n)$], [对数阶],
  [$a n + b n log n + c$], [$O(n log n)$], [$n log n$ 阶],
), caption: [], supplement: [表])
astrale-sharp commented 11 months ago

Sooo where are we on this? I'm a bit lost

ivaquero commented 11 months ago

The latest update is close to the expected result

astrale-sharp commented 11 months ago

I don't get the last point exactly, would you mind explaining it again, like I'm a 5 year old? :innocent:

ivaquero commented 11 months ago

Hi, thanks for your reply.

The following table has 3 columns (as appointed by columns: 3). For convenience, I made the items 3 in a row.

First I expected the formatter not to change this layout.

For a further request, I wish it can make 4 items in a row when I use columns: 4, and n items in a row when I use columns: n.

#align(center)[#table(
  columns: 3,
  align: center,
  inset: 4pt,
  [执行函数举例], [阶], [非正式术语],
  [$12$], [$O(1)$], [常数阶],
  [$a n + b$], [$O(n)$], [线性阶],
  [$a n^2 + b n + c$], [$O(n^2)$], [平方阶],
  [$a n^3 + b n^2 + c n + d$], [$O(n^3)$], [立方阶],
  [$2^n$], [$O(2^n)$], [指数阶],
  [$a log n + b$], [$O(log n)$], [对数阶],
  [$a n + b n log n + c$], [$O(n log n)$], [$n log n$ 阶],
)
astrale-sharp commented 11 months ago

Yea, I agree that's extremely desirable and we already have an issue for it, I'd like to make it working in not too long but also don't have all the time in the world right now,

If you consider the first issue done with (which seems like the case) why don't we go ahead and close this one and discuss the other feature here https://github.com/astrale-sharp/typstfmt/issues/61

ivaquero commented 11 months ago

Sure!