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

Add scala-lsp and metals support to scala #12234

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.develop
Original file line number Diff line number Diff line change
Expand Up @@ -2428,6 +2428,7 @@ Other:
- Evilify =ensime= search in insert/normal mode (thanks to Diego Alvarez)
- Remove duplicated code (thanks to Tetsuro Takemoto)
- Added ENSIME jump handlers (thanks to Joao Azevedo)
- Added scala-lsp and metals as an additional backend for scala
**** Scheme
- Added missing =parinfer= package declaration (thanks to Kalle Lindqvist)
- Update install docs for Chicken 5 changes (thanks to Grant Shangreaux)
Expand Down
41 changes: 36 additions & 5 deletions layers/+lang/scala/README.org
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
- [[#description][Description]]
- [[#features][Features:]]
- [[#layer-installation][Layer Installation]]
- [[#ensime][Ensime]]
- [[#backends][Backends]]
- [[#ensime][Ensime]]
- [[#metals][Metals]]
- [[#scalastyle][Scalastyle]]
- [[#use-java-doc-style][Use Java doc-style]]
- [[#automatically-show-the-type-of-the-symbol-under-the-cursor][Automatically show the type of the symbol under the cursor]]
Expand All @@ -22,6 +24,7 @@ This layer adds support for the Scala language to Spacemacs.

** Features:
- Syntax highlighting
- Support for language backend. Either using [[https://ensime.github.io/][ENSIME]] or [[https://scalameta.org/metals/][Metals]]
- Auto-completion
- Syntax-checking
- Refactoring
Expand All @@ -31,7 +34,6 @@ This layer adds support for the Scala language to Spacemacs.
- Eldoc integration
- Test execution directly from Emacs
- Automatic replacement of ASCII arrows with unicode ones
- Automatic starting/stopping of [[https://ensime.github.io/][ENSIME]] IDE server

* Layer Installation
To use this configuration layer, add it to your =~/.spacemacs=. You will need to
Expand All @@ -46,7 +48,20 @@ version (Stable). Please add the following lines to =dotspacemacs/user-init=:
(add-to-list 'package-pinned-packages '(ensime . "melpa-stable"))
#+END_SRC

* Ensime
* Backends
The currently supported language backends are:
- scala-ensime (default)
- scala-metals

To set your choice of backend, configure the layer variable =scala-backend=.

#+BEGIN_SRC emacs-lisp
(setq-default dotspacemacs-configuration-layers
'((scala :variables scala-backend 'scala-ensime))) ;or 'scala-metals
#+END_SRC


** Ensime
[[https://ensime.github.io/][ENSIME]] provides IDE-like features, such as refactoring, incremental compilation
and project-wide type-checking.

Expand All @@ -57,6 +72,22 @@ list) to generate these files.
Installation instructions and usage can be found in the =Java= layer
[[https://github.com/syl20bnr/spacemacs/tree/develop/layers/%2Blang/java/README.org#ensime][README.org]].

** Metals
kryptt marked this conversation as resolved.
Show resolved Hide resolved
Currently, you must manually install the metals server. It is possible to do so via coursier; the latest version can be built using the following commands:

#+BEGIN_SRC bash
./coursier bootstrap \
--java-opt -Xss4m \
--java-opt -Xms100m \
--java-opt -Dmetals.client=emacs \
org.scalameta:metals_2.12:0.5.1 \
-r bintray:scalacenter/releases \
-r sonatype:snapshots \
-o /usr/local/bin/metals-emacs -f
#+END_SRC

You will then have the common LSP keybindings; see http://develop.spacemacs.org/layers/+tools/lsp/README.html#key-bindings for more details.

* Scalastyle
[[http://www.scalastyle.org/][Scalastyle]] provides style-checking and linting. The Emacs functionality is
provided by Flycheck.
Expand Down Expand Up @@ -128,12 +159,12 @@ the ascii arrows back.
#+END_SRC

* Auto-start
If you prefer to have Ensime start when you load a scala file, you can enable it
If you prefer to have the backend start when you load a scala file, you can enable it
with

#+BEGIN_SRC emacs-lisp
(setq-default dotspacemacs-configuration-layers '(
(scala :variables scala-auto-start-ensime t)))
(scala :variables scala-auto-start-backend t)))
#+END_SRC

* Key bindings
Expand Down
11 changes: 9 additions & 2 deletions layers/+lang/scala/config.el
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,12 @@
(defvar scala-use-unicode-arrows nil
"If non-nil then `->`, `=>` and `<-` are replaced with unicode arrows.")

(defvar scala-auto-start-ensime nil
"If non nil then ensime will be started when a scala file is opened.")
(defconst scala-backends '(scala-ensime scala-metals)
"Backend server implementation to enable advanced IDE language features")

(defvar scala-backend 'scala-ensime
"Backend used to trigger IDE language features.
`scala-ensime' or `scala-metals' are currently supported")

(defvar scala-auto-start-backend nil
"If non nil then ensime/metals will be started when a scala file is opened.")
duianto marked this conversation as resolved.
Show resolved Hide resolved
12 changes: 12 additions & 0 deletions layers/+lang/scala/funcs.el
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,21 @@
(spacemacs//java-setup-ensime)
(add-to-list 'spacemacs-jump-handlers-scala-mode 'ensime-edit-definition))

(defun spacemacs//scala-setup-metals ()
"Setup LSP metals for Scala."
(setq-local lsp-prefer-flymake nil))

(defun spacemacs//scala-disable-flycheck-scala ()
(push 'scala flycheck-disabled-checkers))

(defun spacemacs//scala-backend-ensime-p ()
"Return true if the selected backend is ensime"
duianto marked this conversation as resolved.
Show resolved Hide resolved
(eq scala-backend 'scala-ensime))

(defun spacemacs//scala-backend-metals-p ()
"Return true if the selected backend is metals"
duianto marked this conversation as resolved.
Show resolved Hide resolved
(eq scala-backend 'scala-metals))

(defun spacemacs/scala-join-line ()
"Adapt `scala-indent:join-line' to behave more like evil's line join.

Expand Down
Binary file added layers/+lang/scala/img/scalameta.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion layers/+lang/scala/layers.el
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
;;; License: GPLv3


(configuration-layer/declare-layer 'java)
(configuration-layer/declare-layers '(lsp java))
37 changes: 31 additions & 6 deletions layers/+lang/scala/packages.el
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@

(setq scala-packages
'(
eldoc
ensime
flycheck
flyspell
(lsp-scala :requires lsp-mode)
ggtags
counsel-gtags
helm-gtags
Expand All @@ -25,7 +23,7 @@
))

(defun scala/post-init-eldoc ()
(when scala-enable-eldoc
(when (and scala-enable-eldoc (spacemacs//scala-backend-ensime-p))
(add-hook 'scala-mode-hook #'spacemacs//java-setup-ensime-eldoc)))

(defun scala/pre-init-ensime ()
Expand All @@ -35,10 +33,11 @@
(defun scala/post-init-ensime ()
(use-package ensime
:defer t
:if (spacemacs//scala-backend-ensime-p)
:init
(progn
(add-hook 'scala-mode-hook #'spacemacs//scala-setup-ensime)
(when scala-auto-start-ensime
(when scala-auto-start-backend
(add-hook 'scala-mode-hook 'spacemacs//ensime-maybe-start)))
:config
(progn
Expand All @@ -52,7 +51,8 @@
;; Don't use scala checker if ensime mode is active, since it provides
;; better error checking.
(with-eval-after-load 'flycheck
(add-hook 'ensime-mode-hook 'spacemacs//scala-disable-flycheck-scala)))
(add-hook 'ensime-mode-hook 'spacemacs//scala-disable-flycheck-scala)
(add-hook 'lsp-scala-)))
kryptt marked this conversation as resolved.
Show resolved Hide resolved

(defun scala/post-init-flyspell ()
(spell-checking/add-flyspell-hook 'scala-mode)
Expand All @@ -68,6 +68,13 @@
(defun scala/init-sbt-mode ()
(use-package sbt-mode
:defer t
:config
;; WORKAROUND: https://github.com/ensime/emacs-sbt-mode/issues/31
;; allows for using SPACE in the minibuffer
(substitute-key-definition
'minibuffer-complete-word
'self-insert-command
minibuffer-local-completion-map)
:init (spacemacs/set-leader-keys-for-major-mode 'scala-mode
"b." 'sbt-hydra
"bb" 'sbt-command)))
Expand All @@ -81,6 +88,15 @@
(add-to-list 'completion-ignored-extensions ext)))
:config
(progn

;; Ensure only one of metals and ensime is loaded
(unless (spacemacs//scala-backend-ensime-p)
(progn
(fmakunbound 'ensime)
(remove-hook 'after-change-functions 'ensime-after-change-function)
(remove-hook 'window-configuration-change-hook
'ensime-show-left-margin-hook)))

;; Automatically insert asterisk in a comment when enabled
(defun scala/newline-and-indent-with-asterisk ()
(interactive)
Expand Down Expand Up @@ -139,6 +155,15 @@ If it's part of a left arrow (`<-'),replace it with the unicode arrow."
scala-indent:default-run-on-strategy
scala-indent:operator-strategy))))

(defun scala/init-lsp-scala ()
(use-package lsp-scala
:after scala-mode
:demand t
:if (spacemacs//scala-backend-metals-p)
:config
(add-hook 'scala-mode-local-vars-hook #'spacemacs//scala-setup-metals)
:hook ((scala-mode) . lsp)))

(defun scala/post-init-ggtags ()
(add-hook 'scala-mode-local-vars-hook #'spacemacs/ggtags-mode-enable))

Expand Down