kcl-lang / kcl

KCL Programming Language (CNCF Sandbox Project). https://kcl-lang.io
https://kcl-lang.io
Apache License 2.0
1.61k stars 112 forks source link

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

Closed Peefy closed 1 month ago

Peefy commented 1 year ago

Enhancement

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

import path.to.package
config = package.Config {}
import path.to.package

config = package.Config {}
a = 1
b = 2

c = 3

d = 4
a = 1
b = 2

c = 3

d = 4
config = {
     key = "value"
} # To prevent format to `config = {key = "value"}`
a = ["a"
"b"
]

Before format

f = lambda x {
    x
}(1)

After format

f = lambda x {
    x

}(1)

to be discussed:

amyXia1994 commented 1 year ago

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 commented 11 months ago

@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 commented 11 months ago

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):

(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

Peefy commented 6 months ago

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