kcl icon indicating copy to clipboard operation
kcl copied to clipboard

[Enhancement][Track] Formatting tools with KCL Code style specifications

Open Peefy opened this issue 1 year ago • 4 comments

Enhancement

Spec: https://kcl-lang.io/docs/reference/lang/spec/codestyle

  • [X] The import statement and the following code are left blank on one line, leave additional line spacing between schema definitions and imports. fixed in #792
  • Before formatting
import path.to.package
config = package.Config {}
  • After formatting
import path.to.package

config = package.Config {}
  • [X] For the union type, leave space between all types e.g., "Alice" | "Bob" instead of "Alice"|"Bob": fixed in #790
  • [ ] Sometimes do not format out user-reserved blank lines: which does not mean that to preserve all user-written blank lines (for example, typically only one blank line should be kept between statements, and there shouldn't be any blank lines between entries in list/dict/schema expressions).
  • Before formatting
a = 1
b = 2

c = 3


d = 4
  • After formatting
a = 1
b = 2

c = 3

d = 4
  • [ ] For the attribute list of list/dict/schema, keep the user's line breaks.
config = {
     key = "value"
} # To prevent format to `config = {key = "value"}`
  • [x] Wrong error recovery for list/dict
a = ["a"
"b"
]
  • [x] Unexpected lambda expression empty line.

Before format

f = lambda x {
    x
}(1)

After format

f = lambda x {
    x

}(1)
  • [X] the index signatures in schema stmt are always formatted before attributes: https://github.com/kcl-lang/kcl/issues/789 code spec need to be updated, tacked in https://github.com/kcl-lang/kcl-lang.io/issues/183

to be discussed:

  • Support the alternatives to align code vertically or not: https://github.com/kcl-lang/kcl/issues/708

Peefy avatar Jul 31 '23 02:07 Peefy

Hi @Peefy, could you please describe this requirement more detailed? I didn't get it precisely 😶‍🌫️

The import statement and the following code are left blank on one line

amyXia1994 avatar Sep 12 '23 09:09 amyXia1994

@Peefy I‘ve added some test cases to clarify the expected formatting behaviors on the issues raised above. ptal and verify the golden files. #786

And here are some ambiguities:

Do not format out user reserved blank lines

This statement is kind of ambiguous -- I believe this sentence is not referring to preserving all user-written blank lines (for example, typically only one blank line should be kept between statements, and there shouldn't be any blank lines between entries in list/dict/schema expressions). Could you please clarify the specific cases when blank lines should be preserved?

The import statement and the following code are left blank on one line

Is this statement corresponding to the rule "Break one blank line between different statements e.g., import, schema and expression statements."? If so, I think this is exactly the same as another case to fix that you provide: "Leave additional line spacing between schema definitions and imports"

It would be helpful if you could provide some code examples (or you can directly add some file.input and file.golden).

amyXia1994 avatar Oct 18 '23 05:10 amyXia1994

cc @NeverRaR , @Peefy , @i-zhen

Here are some cases that the formatting behavior depends on the CST(which records the original token infos such as parenthesis, comments, whitespace and line breaks):

  • empty lines between stmts should be formatted based on the user's line feed behavior: a. No empty lines between stmts should be kept as it is. b. multiple empty lines between stmts should be formatted to one empty line.
  • whether to write line breaks between each entry in the list/dict/schema expr: a. no line breaks between entries should be kept as it is. b. line breaks between entries should be formatted to one line break.

(the code samples could be found in this issue's initial content)

and the tracking issue of the context of the (Lossless/Describing/Concrete) Syntax Tree

amyXia1994 avatar Oct 18 '23 12:10 amyXia1994

Related Impl: https://github.com/kcl-lang/tree-sitter-kcl

Peefy avatar Mar 27 '24 03:03 Peefy