What was missing was an API to invoke it from JavaScript, so I rolled-up my sleeves and wrote one. ![]() ![]() There’s nothing inherently JVM-specific about it, so it can work just fine in Node.js. Scalafmt already cross-compiles to Scala.js, and - if you think about it - it makes sense: its core functionality is to manipulate a string (the original source code) and return another string (the formatted source code). What can we do to make this faster and self-contained? Second attempt: scalafmt… in JS? Formatting a file while editing is an operation that should be cheap, especially if you enable it on save (like VSCode allows). Don’t get me wrong: Scalafmt is not slow per se, but spinning up a JVM for formatting a single file incurs in a very high overhead. it requires installing scalafmt and having it available on the user's path, making the extension not self-contained.While this approach is very simple and it works, it also has a few downsides: Something like scalafmt -std-out /path/to/file. First attempt: invoking the scalafmt CLIĪfter understanding the basics of VSCode extensions, my first naive attempt was to simply “shell out” and invoke a local scalafmt process to format the file in the active text editor. Some people use Sublime, some people Emacs, some other Vim, but there’s a good group of people using Visual Studio Code, a solid rich editor by Microsoft, which features also a good support for Ensime.Īs many other modern editors, Visual Studio Code supports extensions, and they are written in TypeScript, a typed superset of JavaScript. No one at buildo has a taste for IDEs, so IntelliJ is not on the table (too bad, since it already had a Scalafmt plugin!). The second one is trickier: the editor is a personal choice and when talking about editors for Scala there’s usually little agreement. The first requirement is easy to fulfill: we can write a CI task that runs scalafmt -test and it's done. the editor should allow the developer to format a file while working on it.the CI should check that everything is properly formatted. ![]() While discussing its adoption we identified a few non-negotiable requirements: ![]() Recently at buildo we grew tired of formatting our Scala code by hand and we started looking into Scalafmt.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |