メインコンテンツまでスキップ

Emacsでscalafmtを使う

· 約1分

EmacsにMetalsを導入したのですが、ファイル保存時にscalafmtが効いてないです。

これってこういうものなのでしょうか?

ということでinit.elに自分で設定を書くことにしました。

coursierのインストール

scalafmtをインストールするにはcoursierが必要なのでインストール!

brew install coursier/formulas/coursier

そしてセットアップ!

cs setup

scalafmtのインストール

cs install scalafmt

init.elの設定

ファイルを保存するとscalafmtを実行してくれます。

(add-to-list 'exec-path (expand-file-name "~/Library/Application Support/Coursier/bin"))
(defun exec-scalafmt ()
(interactive)
(shell-command
(format "%s %s"
(shell-quote-argument (executable-find "scalafmt"))
(shell-quote-argument (expand-file-name buffer-file-name))))
(revert-buffer t t t))

(add-hook 'scala-mode-hook
(lambda ()
(add-hook 'after-save-hook 'exec-scalafmt t t)))