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

Kotlin script files (.kts) #35

Closed
russellw opened this issue May 6, 2020 · 10 comments
Closed

Kotlin script files (.kts) #35

russellw opened this issue May 6, 2020 · 10 comments

Comments

@russellw
Copy link

russellw commented May 6, 2020

ktfmt looks good so far! Only deficiency I have found is that it doesn't work on script (.kts) files. Any chance of adding this?

@cgrushko
Copy link
Contributor

cgrushko commented May 6, 2020

Looks like ktfmt is parsing the file as if it were a .kt file, expecting a top-level declaration.

$ cat > ~/temp/foo.kts
println("Called with args:")
args.forEach {
    println("- $it")
}

$ java -jar ktfmt-0.13-SNAPSHOT-jar-with-dependencies.jar ~/temp/foo.kts
~/temp/foo.kts:1:1: error: Expecting a top level declaration

Something inside Parser.kt causes kotlin-compiler to expect a .kt file instead of .kts.
KotlinFileType.INSTANCE looks suspicious, but there's no kts equivalent.

@cgrushko cgrushko added the good first issue Good for newcomers label May 12, 2020
@cgrushko cgrushko removed the good first issue Good for newcomers label Jun 2, 2020
@cgrushko
Copy link
Contributor

cgrushko commented Jun 2, 2020

Had to rollback the changes that added support for scripts, so reopening.

@cgrushko cgrushko reopened this Jun 2, 2020
@jdemeulenaere
Copy link

Are there any news regarding support for Kotlin Script files? It would be very useful to us as we are currently using ktlint but we would like to migrate to ktfmt for both normal and script Kotlin files.

@cgrushko
Copy link
Contributor

@jdemeulenaere glad you find ktfmt useful :)
Script support was added in c2cf421, but unfortunately had to be rolled back because it hurt preserving some newlines, as well as newlines between package and the rest of the file when there are no imports. We'll get to it soon-ish, I hope.

facebook-github-bot pushed a commit that referenced this issue Jan 4, 2021
Summary:
The formatting is inherited from non-kts files, so each statement is surrounded by newlines.

Follow-up: only force newlines between certain constrcuts, such as classes.

This unblocks #35 .

Reviewed By: strulovich

Differential Revision: D25743859

fbshipit-source-id: 8dbda2866ea50eb4a73d94a72d2b7f10618a4cd0
@cgrushko cgrushko changed the title Kotlin script files Kotlin script files (.kts) Jan 4, 2021
@cgrushko
Copy link
Contributor

cgrushko commented Jan 4, 2021

There's now preliminary support in the main branch. It'll do undesirable things like surrounding every statement in newlines, but at least it ktfmt no longer chokes on kts files.

@JavierSegoviaCordoba
Copy link
Contributor

Is there a snapshot for it?

@cgrushko
Copy link
Contributor

cgrushko commented Jan 6, 2021

I uploaded a snapshot to

<dependency>
  <groupId>com.facebook</groupId>
  <artifactId>ktfmt</artifactId>
  <version>0.20-SNAPSHOT</version>
</dependency>

see https://stackoverflow.com/questions/7715321/how-to-download-snapshot-version-from-maven-snapshot-repository for hwo to add the OSSRH snapshot repo to your Maven repo list.

@MathieuLorber
Copy link

Hi, everything seems to be ok now, with just a small problem : formatting scripts starting by a hashbang will fail
#!/usr/bin/env kscript
Is there any chance to support it ?

@ZacSweers
Copy link

@cgrushko would you be open to a PR for the hashbang support via just peeking the first line and skipping it if it is a hashbang?

@cgrushko
Copy link
Contributor

@ZacSweers was just implemented in d71487b :) next weekly release should pick it up.

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

No branches or pull requests

6 participants