|
| 1 | +#+TITLE: Starter Kit Scala |
| 2 | +#+OPTIONS: toc:nil num:nil ^:nil |
| 3 | + |
| 4 | +This is part of the [[file:starter-kit.org][Emacs Starter Kit]]. |
| 5 | + |
| 6 | +* Starter Kit Scala |
| 7 | + :PROPERTIES: |
| 8 | + :results: silent |
| 9 | + :END: |
| 10 | + |
| 11 | +Support for developing in Scala under Emacs. |
| 12 | + |
| 13 | +** Scala-mode2 |
| 14 | + :PROPERTIES: |
| 15 | + :tangle: yes |
| 16 | + :END: |
| 17 | +This is a new scala major mode for emacs 24. It is a complete rewrite |
| 18 | +based on the Scala Language Specification 2.9. |
| 19 | + |
| 20 | +The mode intends to provide the basic emacs support, including: |
| 21 | + |
| 22 | +- indenting of code, comments and multi-line strings |
| 23 | +- motion commands |
| 24 | +- highlighting |
| 25 | + |
| 26 | +Currently the indenting of code has been finalized. Highlighting is |
| 27 | +under work. No scala specific motion commands have been added, but |
| 28 | +standard emacs motions work of course. |
| 29 | + |
| 30 | +*** Install scala-mode2 |
| 31 | +Install scala-mode2 from MELPA (M-x package-install RET scala-mode2) |
| 32 | +#+begin_src emacs-lisp |
| 33 | + (starter-kit-install-if-needed 'scala-mode2) |
| 34 | +#+end_src |
| 35 | + |
| 36 | +*** Customise scala-mode2 |
| 37 | +#+begin_src emacs-lisp |
| 38 | + (add-hook 'scala-mode-hook '(lambda () |
| 39 | + |
| 40 | + ;; Bind the 'newline-and-indent' command to RET (aka 'enter'). This |
| 41 | + ;; is normally also available as C-j. The 'newline-and-indent' |
| 42 | + ;; command has the following functionality: 1) it removes trailing |
| 43 | + ;; whitespace from the current line, 2) it create a new line, and 3) |
| 44 | + ;; indents it. An alternative is the |
| 45 | + ;; 'reindent-then-newline-and-indent' command. |
| 46 | + (local-set-key (kbd "RET") 'newline-and-indent) |
| 47 | + |
| 48 | + ;; Alternatively, bind the 'newline-and-indent' command and |
| 49 | + ;; 'scala-indent:insert-asterisk-on-multiline-comment' to RET in |
| 50 | + ;; order to get indentation and asterisk-insertion within multi-line |
| 51 | + ;; comments. |
| 52 | + (local-set-key (kbd "RET") |
| 53 | + '(lambda () |
| 54 | + (interactive) |
| 55 | + (newline-and-indent) |
| 56 | + (scala-indent:insert-asterisk-on-multiline-comment))) |
| 57 | + |
| 58 | + ;; Bind the backtab (shift tab) to |
| 59 | + ;; 'scala-indent:indent-with-reluctant-strategy command. This is usefull |
| 60 | + ;; when using the 'eager' mode by default and you want to "outdent" a |
| 61 | + ;; code line as a new statement. |
| 62 | + (local-set-key (kbd "<backtab>") 'scala-indent:indent-with-reluctant-strategy))) |
| 63 | +#+end_src |
| 64 | + |
| 65 | +** sbt-mode |
| 66 | + :PROPERTIES: |
| 67 | + :tangle: yes |
| 68 | + :END: |
| 69 | + |
| 70 | +An emacs mode for interacting with sbt, scala console (aka REPL) and |
| 71 | +sbt projects. |
| 72 | + |
| 73 | +The mode provides basic functionality required for successfully |
| 74 | +interacting with sbt from emacs. The core functionality includes: |
| 75 | + |
| 76 | +- interacting with sbt shell and scala console |
| 77 | +- compiling code and navigating to errors |
| 78 | +- finding things in code |
| 79 | + |
| 80 | +*** Install sbt-mode |
| 81 | +Install sbt-mode2 from MELPA (M-x package-install RET sbt-mode) |
| 82 | + |
| 83 | +#+begin_src emacs-lisp |
| 84 | + (starter-kit-install-if-needed 'sbt-mode) |
| 85 | +#+end_src |
| 86 | +*** Customise sbt-mode |
| 87 | +To work efficiently with sbt-mode, you should customize these |
| 88 | +variables. |
| 89 | + |
| 90 | +- sbt:program-name - the name of the sbt executable, defaults to |
| 91 | +sbt. Note: this variable is best configured throught the emacs |
| 92 | +customization menu (M-x customize-variable RET sbt:program-name) or |
| 93 | +set globally. You can not set it with the mode hook. |
| 94 | + |
| 95 | +- grep-find-ignored-directories - directories not to include in |
| 96 | +searches. You should add the target directory and maybe remove many of |
| 97 | +the directories related to arcane version control tools that you will |
| 98 | +not have anyway. |
| 99 | + |
| 100 | +- grep-find-ignored-files - a list of file patterns to ignore in searches. |
| 101 | +You may also want to add a mode-hook to you .emacs file that alters |
| 102 | +key-bindings and some settings. |
| 103 | + |
| 104 | +#+begin_src emacs-lisp |
| 105 | + (add-hook 'sbt-mode-hook '(lambda () |
| 106 | + ;; compilation-skip-threshold tells the compilation minor-mode |
| 107 | + ;; which type of compiler output can be skipped. 1 = skip info |
| 108 | + ;; 2 = skip info and warnings. |
| 109 | + (setq compilation-skip-threshold 1) |
| 110 | + |
| 111 | + ;; Bind C-a to 'comint-bol when in sbt-mode. This will move the |
| 112 | + ;; cursor to just after prompt. |
| 113 | + (local-set-key (kbd "C-a") 'comint-bol) |
| 114 | + |
| 115 | + ;; Bind M-RET to 'comint-accumulate. This will allow you to add |
| 116 | + ;; more than one line to scala console prompt before sending it |
| 117 | + ;; for interpretation. It will keep your command history cleaner. |
| 118 | + (local-set-key (kbd "M-RET") 'comint-accumulate))) |
| 119 | +#+end_src |
| 120 | + |
| 121 | +Besides customizing sbt-mode, you might also want to add some |
| 122 | +customizations to your scala-mode2 key-bindings. The following two |
| 123 | +commands are good to have in some easily accessible key position. |
| 124 | + |
| 125 | +#+begin_src emacs-lisp |
| 126 | + (add-hook 'scala-mode-hook '(lambda () |
| 127 | + ;; sbt-find-definitions is a command that tries to find (with grep) |
| 128 | + ;; the definition of the thing at point. |
| 129 | + (local-set-key (kbd "M-.") 'sbt-find-definitions) |
| 130 | + |
| 131 | + ;; use emacs M-x next-error to navigate errors |
| 132 | + (local-set-key (kbd "M-'") 'next-error) |
| 133 | + ;; use sbt-run-previous-command to re-compile your code after changes |
| 134 | + (local-set-key (kbd "C-x '") 'sbt-run-previous-command))) |
| 135 | +#+end_src |
| 136 | +** Ensime |
| 137 | +ENSIME is the ENhanced Scala Interaction Mode for Emacs. It provides |
| 138 | +many features that are commonly found only in IDEs, such as live |
| 139 | +error-checking, symbol inspection, package/type browsing, and basic |
| 140 | +refactoring. |
| 141 | + |
| 142 | +*** Install Ensime |
| 143 | +#+begin_src emacs-lisp |
| 144 | + (starter-kit-install-if-needed 'ensime) |
| 145 | +#+end_src |
| 146 | + |
| 147 | +** Yasnippet |
| 148 | + :PROPERTIES: |
| 149 | + :tangle: yes |
| 150 | + :END: |
| 151 | +Provide templates for many standard operations. |
| 152 | + |
| 153 | +*** Install Yasnippet |
| 154 | +#+begin_src emacs-lisp |
| 155 | + (starter-kit-install-if-needed 'yasnippet-bundle 'yasnippet) |
| 156 | +#+end_src |
| 157 | + |
| 158 | +*** Configure Yasnippet |
| 159 | +#+begin_src emacs-lisp |
| 160 | + (add-hook 'scala-mode-hook '(lambda () |
| 161 | + (yas/minor-mode-on))) |
| 162 | +#+end_src |
| 163 | + |
| 164 | +** Whitespace |
| 165 | + :PROPERTIES: |
| 166 | + :tangle: yes |
| 167 | + :END: |
| 168 | + |
| 169 | +Emacs has a very nice minor mode for highlighting bad whitespace and |
| 170 | +removing any unwanted whitespace when you save a file. To use it, |
| 171 | +uncomment the expression below |
| 172 | +*** Configure Whitespace |
| 173 | + |
| 174 | +#+begin_src emacs-lisp |
| 175 | + (add-hook 'scala-mode-hook '(lambda () |
| 176 | + ;;(require 'whitespace) |
| 177 | + ;; clean-up whitespace at save |
| 178 | + (make-local-variable 'before-save-hook) |
| 179 | + (add-hook 'before-save-hook 'whitespace-cleanup) |
| 180 | + |
| 181 | + ;; turn on highlight. To configure what is highlighted, customize |
| 182 | + ;; the *whitespace-style* variable. A sane set of things to |
| 183 | + ;; highlight is: face, tabs, trailing |
| 184 | + (whitespace-mode))) |
| 185 | +#+end_src |
0 commit comments