Using vs code, how to get scala format to work and format my code?

喜你入骨 提交于 2020-05-30 02:46:08

问题


I have the scala format plugin in my multi project sbt repository.

addSbtPlugin("org.scalameta"    % "sbt-scalafmt"        % "2.3.2")

So in the sbt console if I run scalafmt it works fine

My build.sbt has:

scalafmtOnCompile := true

If I do a ~compile in sbt or just compile manually, it doesn't format my code during compilation.

What is wrong with my setup?

Also, running scalafmt works but it doesn't format my .scala files in /project like my Dependencies.scala file. Why is it ignoring these files?

I am using VS Code with metals also as my IDE.


回答1:


Scalafmt seeems to work in VSC with Metals out-of-the-box on my machine

Metals automatically uses Scalafmt to respond to formatting requests from the editor, according to the configuration defined in .scalafmt.conf... if there is no .scalafmt.conf, upon receiving the first format request Metals will create the .scalafmt.conf file for you.

Simply execute Format Document command from Command Palette in VSC and it should format according to .scalafmt.conf. If you would like to continuously format and compile on every source change and scalafmtOnCompile is not working, then try executing in sbt

~scalafmt;compile

however format on compile is discouraged as per documentation

This option is discouraged since it messes up undo buffers in the editor and it slows down compilation. It is recommended to use "format on save" in the editor instead.



来源:https://stackoverflow.com/questions/61060140/using-vs-code-how-to-get-scala-format-to-work-and-format-my-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!