New layer tern extracted from javascript layer

This commit is contained in:
syl20bnr 2018-05-17 23:34:24 -04:00
parent 387a19530d
commit abf3df4ad7
10 changed files with 149 additions and 88 deletions

View File

@ -20,7 +20,6 @@
- [[#refactoring-js2-refactor][Refactoring (js2-refactor)]]
- [[#formatting-web-beautify][Formatting (web-beautify)]]
- [[#documentation-js-doc][Documentation (js-doc)]]
- [[#auto-complete-and-documentation-tern][Auto-complete and documentation (tern)]]
- [[#repl-skewer-mode][REPL (skewer-mode)]]
* Description
@ -79,31 +78,7 @@ Backend can be chosen on a per project basis using directory local variables
* Backends
** Tern
Install =tern= to use the auto-completion and documentation features:
#+BEGIN_SRC sh
$ npm install -g tern
#+END_SRC
To make tern re-use the server across multiple different editing sessions (thus
creating multiple =.tern-port= files for each document you have open [[http://ternjs.net/doc/manual.html][see here
for more details]]):
The variables provided below can be set when adding the =javascript=
configuration layer, as detailed in [[https://github.com/syl20bnr/spacemacs/blob/develop/doc/DOCUMENTATION.org#setting-configuration-layers-variables][this]] section of the documentation.
#+BEGIN_SRC emacs-lisp
(javascript :variables javascript-disable-tern-port-files nil)
#+END_SRC
Windows users may need to set the variable =tern-command= in order for emacs to
locate and launch tern server successfully. See [[https://github.com/syl20bnr/spacemacs/issues/5733][this open issue for more
details]]. The variable can be set when adding the =javascript= configuration
layer:
#+BEGIN_SRC emacs-lisp
(setq-default dotspacemacs-configuration-layers '(
(javascript :variables tern-command '("node" "/path/to/tern/bin/tern")))
#+END_SRC
See [[file:+tools/tern/README.org][tern layer]] documentation.
** Language Server Protocol
You just have to install javascript and typescript language server packages:
@ -178,7 +153,7 @@ doing this [[https://stackoverflow.com/questions/9679932#comment33532258_9683472
** Refactoring (js2-refactor)
Bindings should match the plain emacs assignments.
| Key Binding | Description |
| Key Binding | Descripti using ternon |
|---------------+----------------------------------------------------------------------------------------------------------------|
| ~SPC m k~ | deletes to the end of the line, but does not cross semantic boundaries |
| ~SPC m r 3 i~ | converts ternary operator to if-statement |
@ -227,17 +202,6 @@ You can check more [[https://github.com/mooz/js-doc/][here]]
| ~SPC m r d t~ | insert tag to comment |
| ~SPC m r d h~ | show list of available jsdoc tags |
** Auto-complete and documentation (tern)
| Key Binding | Description |
|---------------+------------------------------------------------------------------------------------------|
| ~SPC m C-g~ | brings you back to last place you were when you pressed M-.. |
| ~SPC m g g~ | jump to the definition of the thing under the cursor |
| ~SPC m g G~ | jump to definition for the given name |
| ~SPC m h d~ | find docs of the thing under the cursor. Press again to open the associated URL (if any) |
| ~SPC m h t~ | find the type of the thing under the cursor |
| ~SPC m r r V~ | rename variable under the cursor using tern |
** REPL (skewer-mode)
| Key Binding | Description |

View File

@ -13,12 +13,6 @@
(spacemacs|define-jump-handlers js2-mode)
(defvar javascript-disable-tern-port-files t
"Stops tern from creating tern port files.")
(defvar javascript-backend 'tern
"The backend to use for IDE features. Possible values are `tern'
and `lsp'.")
(defvar tern-command '("tern")
"Path to the tern server. Used when `javascript-backend' is set to `tern'.")

View File

@ -15,42 +15,15 @@
(defun spacemacs//javascript-setup-backend ()
"Conditionally setup javascript backend."
(pcase javascript-backend
(`tern (spacemacs//javascript-setup-tern))
(`tern (spacemacs/tern-setup-tern))
(`lsp (spacemacs//javascript-setup-lsp))))
(defun spacemacs//javascript-setup-company ()
"Conditionally setup company based on backend."
(pcase javascript-backend
(`tern (spacemacs//javascript-setup-tern-company))
(`tern (spacemacs/tern-setup-tern-company 'js2-mode))
(`lsp (spacemacs//javascript-setup-lsp-company))))
;; tern
(defun spacemacs//javascript-setup-tern ()
"Setup tern backend."
(tern-mode))
(defun spacemacs//javascript-setup-tern-company ()
"Setup tern auto-completion."
(spacemacs|add-company-backends
:backends company-tern
:modes js2-mode
:append-hooks nil
:call-hooks t)
(company-mode))
(defun spacemacs//set-tern-key-bindings (mode)
"Set the key bindings for tern and the given MODE."
(add-to-list (intern (format "spacemacs-jump-handlers-%S" mode))
'(tern-find-definition :async t))
(spacemacs/set-leader-keys-for-major-mode mode
"rrV" 'tern-rename-variable
"hd" 'tern-get-docs
"gG" 'tern-find-definition-by-name
(kbd "C-g") 'tern-pop-find-definition
"ht" 'tern-get-type))
;; lsp

View File

@ -9,4 +9,4 @@
;;
;;; License: GPLv3
(configuration-layer/declare-layers '(node json))
(configuration-layer/declare-layers '(json node tern))

View File

@ -13,7 +13,6 @@
'(
add-node-modules-path
company
(company-tern :requires company)
counsel-gtags
evil-matchit
flycheck
@ -46,10 +45,6 @@
(defun javascript/post-init-company ()
(add-hook 'js2-mode-local-vars-hook #'spacemacs//javascript-setup-company))
(defun javascript/init-company-tern ()
(use-package company-tern
:defer t))
(defun javascript/post-init-flycheck ()
(spacemacs/enable-flycheck 'js2-mode))
@ -200,15 +195,8 @@
"sR" 'spacemacs/skewer-eval-region-and-focus
"ss" 'skewer-repl))))
(defun javascript/init-tern ()
(use-package tern
:defer t
:config
(progn
(spacemacs|hide-lighter tern-mode)
(when javascript-disable-tern-port-files
(add-to-list 'tern-command "--no-port-file" 'append))
(spacemacs//set-tern-key-bindings 'js2-mode))))
(defun javascript/post-init-tern ()
(add-to-list 'tern--key-bindings-modes 'js2-mode))
(defun javascript/init-web-beautify ()
(use-package web-beautify

View File

@ -0,0 +1,65 @@
#+TITLE: Tern layer
[[file:img/tern.png]]
* Table of Contents :TOC_4_gh:noexport:
- [[#description][Description]]
- [[#features][Features:]]
- [[#install][Install]]
- [[#configuration][Configuration]]
- [[#custom-tern-command][Custom tern command]]
- [[#re-use-the-server-across-multiple-different-editing-sessions][Re-use the server across multiple different editing sessions]]
- [[#key-bindings][Key Bindings]]
* Description
This layer adds support for [[http://ternjs.net/][tern]] stand-alone code-analysis engine for
JavaScript.
** Features:
- TODO: list the feature of tern supported by tern package
* Install
To use this configuration layer, add it to your =~/.spacemacs=. You will need to
add =tern= to the existing =dotspacemacs-configuration-layers= list in this
file.
You can install =tern= globally with the following command:
#+BEGIN_SRC sh
$ npm install -g tern
#+END_SRC
* Configuration
** Custom tern command
Windows users may need to set the variable =tern-command= in order for emacs to
locate and launch tern server successfully. See [[https://github.com/syl20bnr/spacemacs/issues/5733][this open issue for more
details]]. The variable can be set by adding the =tern= layer with this
configuration layer:
#+BEGIN_SRC emacs-lisp
(tern :variables tern-command '("node" "/path/to/tern/bin/tern"))
#+END_SRC
Note: =tern-command= must be a list of strings.
** Re-use the server across multiple different editing sessions
By default =tern= is setup to not create multiple =.tern-port= files in your
repository.
To make tern re-use the server across multiple different editing sessions (thus
creating multiple =.tern-port= files for each document you have open [[http://ternjs.net/doc/manual.html][see here
for more details]]) set the variable =tern-disable-port-files= to nil:
#+BEGIN_SRC emacs-lisp
(tern :variables tern-disable-port-files nil)
#+END_SRC
* Key Bindings
| Key Binding | Description |
|---------------+------------------------------------------------------------------------------------------|
| ~SPC m C-g~ | brings you back to last place you were when you pressed M-.. |
| ~SPC m g g~ | jump to the definition of the thing under the cursor |
| ~SPC m g G~ | jump to definition for the given name |
| ~SPC m h d~ | find docs of the thing under the cursor. Press again to open the associated URL (if any) |
| ~SPC m h t~ | find the type of the thing under the cursor |
| ~SPC m r r V~ | rename variable under the cursor using tern |

View File

@ -0,0 +1,16 @@
;;; config.el --- Tern Layer configuration File for Spacemacs
;;
;; Copyright (c) 2012-2018 Sylvain Benner & Contributors
;;
;; Author: Sylvain Benner <sylvain.benner@gmail.com>
;; URL: https://github.com/syl20bnr/spacemacs
;;
;; This file is not part of GNU Emacs.
;;
;;; License: GPLv3
(defvar tern-disable-port-files t
"Stops tern from creating tern port files.")
(defvar tern--key-bindings-modes nil
"List of major modes where Tern key-bindings must be defined.")

View File

@ -0,0 +1,28 @@
;;; funcs.el --- Tern Layer functions File for Spacemacs
;;
;; Copyright (c) 2012-2018 Sylvain Benner & Contributors
;;
;; Author: Sylvain Benner <sylvain.benner@gmail.com>
;; URL: https://github.com/syl20bnr/spacemacs
;;
;; This file is not part of GNU Emacs.
;;
;;; License: GPLv3
(defun spacemacs/tern-setup-tern ()
"Setup tern backend.
Must be called by a layer using tern."
(require tern)
(when tern-disable-port-files
(add-to-list 'tern-command "--no-port-file" 'append))
(tern-mode))
(defun spacemacs/tern-setup-tern-company (&rest modes)
"Setup tern auto-completion for given MODES.
Must be called by a layer using tern."
(eval `(spacemacs|add-company-backends
:backends company-tern
:modes ,@mode
:append-hooks nil
:call-hooks t))
(company-mode))

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

View File

@ -0,0 +1,33 @@
;;; packages.el --- Tern Layer packages File for Spacemacs
;;
;; Copyright (c) 2012-2018 Sylvain Benner & Contributors
;;
;; Author: Sylvain Benner <sylvain.benner@gmail.com>
;; URL: https://github.com/syl20bnr/spacemacs
;;
;; This file is not part of GNU Emacs.
;;
;;; License: GPLv3
(setq tern-packages '((company-tern :requires company)
tern))
(defun tern/init-company-tern ()
(use-package company-tern
:defer t))
(defun tern/init-tern ()
(use-package tern
:defer t
:config
(progn
(spacemacs|hide-lighter tern-mode)
(dolist (mode tern-key-bindings-modes)
(add-to-list (intern (format "spacemacs-jump-handlers-%S" mode))
'(tern-find-definition :async t))
(spacemacs/set-leader-keys-for-major-mode mode
"rrV" 'tern-rename-variable
"hd" 'tern-get-docs
"gG" 'tern-find-definition-by-name
(kbd "C-g") 'tern-pop-find-definition
"ht" 'tern-get-type)))))