From abf3df4ad79500d4f3a5b10da32c7955cfc855d0 Mon Sep 17 00:00:00 2001 From: syl20bnr Date: Thu, 17 May 2018 23:34:24 -0400 Subject: [PATCH] New layer tern extracted from javascript layer --- layers/+lang/javascript/README.org | 40 +---------------- layers/+lang/javascript/config.el | 6 --- layers/+lang/javascript/funcs.el | 31 +------------ layers/+lang/javascript/layers.el | 2 +- layers/+lang/javascript/packages.el | 16 +------ layers/+tools/tern/README.org | 65 ++++++++++++++++++++++++++++ layers/+tools/tern/config.el | 16 +++++++ layers/+tools/tern/funcs.el | 28 ++++++++++++ layers/+tools/tern/img/tern.png | Bin 0 -> 2583 bytes layers/+tools/tern/packages.el | 33 ++++++++++++++ 10 files changed, 149 insertions(+), 88 deletions(-) create mode 100644 layers/+tools/tern/README.org create mode 100644 layers/+tools/tern/config.el create mode 100644 layers/+tools/tern/funcs.el create mode 100644 layers/+tools/tern/img/tern.png create mode 100644 layers/+tools/tern/packages.el diff --git a/layers/+lang/javascript/README.org b/layers/+lang/javascript/README.org index 961edb187..172dc5e12 100644 --- a/layers/+lang/javascript/README.org +++ b/layers/+lang/javascript/README.org @@ -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 | diff --git a/layers/+lang/javascript/config.el b/layers/+lang/javascript/config.el index 9b38c864e..14d24bcc7 100644 --- a/layers/+lang/javascript/config.el +++ b/layers/+lang/javascript/config.el @@ -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'.") diff --git a/layers/+lang/javascript/funcs.el b/layers/+lang/javascript/funcs.el index 68985facd..433d51b74 100644 --- a/layers/+lang/javascript/funcs.el +++ b/layers/+lang/javascript/funcs.el @@ -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 diff --git a/layers/+lang/javascript/layers.el b/layers/+lang/javascript/layers.el index 4dd66ec4f..ec178d44d 100644 --- a/layers/+lang/javascript/layers.el +++ b/layers/+lang/javascript/layers.el @@ -9,4 +9,4 @@ ;; ;;; License: GPLv3 -(configuration-layer/declare-layers '(node json)) +(configuration-layer/declare-layers '(json node tern)) diff --git a/layers/+lang/javascript/packages.el b/layers/+lang/javascript/packages.el index 55ad46143..58a55ddaf 100644 --- a/layers/+lang/javascript/packages.el +++ b/layers/+lang/javascript/packages.el @@ -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 diff --git a/layers/+tools/tern/README.org b/layers/+tools/tern/README.org new file mode 100644 index 000000000..441964d37 --- /dev/null +++ b/layers/+tools/tern/README.org @@ -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 | diff --git a/layers/+tools/tern/config.el b/layers/+tools/tern/config.el new file mode 100644 index 000000000..c13002179 --- /dev/null +++ b/layers/+tools/tern/config.el @@ -0,0 +1,16 @@ +;;; config.el --- Tern Layer configuration File for Spacemacs +;; +;; Copyright (c) 2012-2018 Sylvain Benner & Contributors +;; +;; Author: Sylvain Benner +;; 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.") diff --git a/layers/+tools/tern/funcs.el b/layers/+tools/tern/funcs.el new file mode 100644 index 000000000..7d8ec1c36 --- /dev/null +++ b/layers/+tools/tern/funcs.el @@ -0,0 +1,28 @@ +;;; funcs.el --- Tern Layer functions File for Spacemacs +;; +;; Copyright (c) 2012-2018 Sylvain Benner & Contributors +;; +;; Author: Sylvain Benner +;; 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)) diff --git a/layers/+tools/tern/img/tern.png b/layers/+tools/tern/img/tern.png new file mode 100644 index 0000000000000000000000000000000000000000..e50acd2131540bc415374629e39e84187d1c8f69 GIT binary patch literal 2583 zcmV+y3h4ETP)NppWNlPc4GzW#IO~}z1O8(CTLg{o`0*#X%DNKht z^vJZ(PAStSo$0itG=L#az@`L@Z(BCT7;It7hi+-LN5`lG#+Id(yu13fNWH!wHwddxC-PdKDLFnpdzbV(b(vTpEQ^82j zgy$dqVTv8I2q=0N1Pz`On8nDjPasaX3=3t%5<_B zO*W(XbYxN%e?Z|9Qh|7i&snKmrSu~kWrcatc;bNxA%4|bQLvR!^QE8_5 zk_r?q4;ETd&9*=g)KHYd^C%^O!sY&aa~h*wtuK4nZcPJdQUS4V)Dl=tuGGMPzHk)z z&SUqvgOS$j?zYaJiK#`7>MT8lqw=0V6bc?Uotl5xj;wY0Qs z+qTW=oZDFH3Rdm@)gN%J{_@y#faN(M$*xM$d?8RI-#}ZnBtzf~fiWb8BZ!~n{A|=8 z;cyh8FqFb$FN~h|4~D?<^i&!}54q%Wxi)Ux7z%}khK4F0d*Y=%Ke;{|obZPuyda8+ zK5nV^kwJr3W*aRuX`u-VP3G(ARTc(KcOhV1gZ}n^`a4hBp4+RjSEqLpiJF?4+P{B) zV`HNziVyDksl`zzWt3DdC=6S)bMa*`%99wP!B7fAHTc{+d3&L?wFX>K1RQItRt z5<|=sVWfx!TfyP)ZwL)uAal$04V$wz?S?U>VzWC6-01&o$o2H5O^l%(t;6*z= zKp8Uxt9Ns?Tpc4$H2(hO+Fc)w2wW7w@T5ahSdTOq%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|_Bd>6{c!xK%d1AdlM^onvx zK`!5@u|Mr7h!uUBS_jS!gMRPKmr`85vNr$4+LBl^%Yje&+;aDMDJ`YAG!$lqIsWja ztAFdg3BvEjrYc3aROc8c1K|UI>6`jrn +;; 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)))))