diff --git a/modules/lang/javascript/README.org b/modules/lang/javascript/README.org index dfc55ebbe..e184bbb0e 100644 --- a/modules/lang/javascript/README.org +++ b/modules/lang/javascript/README.org @@ -2,7 +2,7 @@ This module adds Javascript support. -+ Code completion (tern) ++ Code completion (tide) + REPL support (nodejs-repl) + Refactoring commands (js2-refactor) + Syntax checking (flycheck) @@ -11,11 +11,10 @@ This module adds Javascript support. + Jump-to-definitions and references support (xref) * Table of Contents :TOC: -- [[#install][Install]] - - [[#node--npm][Node & NPM]] - - [[#dependencies][Dependencies]] -- [[#appendix][Appendix]] - - [[#commands][Commands]] +- [[Install][Install]] + - [[Node & NPM][Node & NPM]] +- [[Appendix][Appendix]] + - [[Commands][Commands]] * Install ** Node & NPM @@ -31,13 +30,6 @@ brew install node sudo pacman --needed --noconfirm -S nodejs npm #+END_SRC -** Dependencies -This module optionally requires ~tern~ for code completion. - -#+BEGIN_SRC sh -npm -g install tern -#+END_SRC - * Appendix ** Commands | command | key / ex command | description | diff --git a/modules/lang/javascript/autoload.el b/modules/lang/javascript/autoload.el index cfc42d65c..25dbc801d 100644 --- a/modules/lang/javascript/autoload.el +++ b/modules/lang/javascript/autoload.el @@ -74,3 +74,19 @@ Run this for any buffer you want to skewer." (if skewer-css-mode (skewer-css-mode -1)) (if skewer-html-mode (skewer-html-mode -1))))) +;;;###autoload +(defun +javascript|add-node-modules-path () + "Search the current buffer's parent directories for `node_modules/.bin`. +If it's found, then add it to the `exec-path'." + (interactive) + (if-let* ((root (locate-dominating-file + (or (buffer-file-name) default-directory) + "node_modules")) + (path (expand-file-name "node_modules/.bin/" root))) + (progn + (make-local-variable 'exec-path) + (cl-pushnew path exec-path :test #'string=) + (when doom-debug-mode + (message "Added %s to exec-path" path))) + (when doom-debug-mode + (message "node_modules not found in %s" root)))) diff --git a/modules/lang/javascript/config.el b/modules/lang/javascript/config.el index 7a9dbe7f1..69a261bce 100644 --- a/modules/lang/javascript/config.el +++ b/modules/lang/javascript/config.el @@ -11,7 +11,8 @@ js2-mode-show-parse-errors nil js2-mode-show-strict-warnings nil) - (add-hook! 'js2-mode-hook #'(flycheck-mode rainbow-delimiters-mode)) + (add-hook! 'js2-mode-hook + #'(flycheck-mode rainbow-delimiters-mode +javascript|add-node-modules-path)) (set! :repl 'js2-mode #'+javascript/repl) (set! :electric 'js2-mode :chars '(?\} ?\) ?.)) @@ -45,6 +46,17 @@ :n "S" #'+javascript/skewer-this-buffer)) +(def-package! tide + :hook (js2-mode . tide-setup) + :config + (set! :company 'js2-mode 'company-tide) + (set! :lookup 'js2-mode + :definition #'tide-jump-to-definition + :references #'tide-references + :documentation #'tide-documentation-at-point) + (add-hook! 'tide-mode-hook #'(eldoc-mode tide-hl-identifier-mode))) + + ;; A find-{definition,references} backend for js2-mode. NOTE The xref API is ;; unstable and may break with an Emacs update. (def-package! xref-js2 @@ -95,20 +107,6 @@ ("Reformat buffer (eslint_d)" :exec eslintd-fix :region nil :when (fboundp 'eslintd-fix))) :prompt "Refactor: ")) - -(def-package! tern - :hook (js2-mode . tern-mode) - :config - (advice-add #'tern-project-dir :override #'doom-project-root)) - - -(def-package! company-tern - :when (featurep! :completion company) - :after tern - :config - (set! :company-backend 'js2-mode '(company-tern))) - - (def-package! rjsx-mode :commands rjsx-mode :mode "\\.jsx$" @@ -132,6 +130,8 @@ "<" nil "C-d" nil) (add-hook! rjsx-mode + + #'(flycheck-mode set-up-tide-mode add-node-modules-path rainbow-delimiters-mode) ;; jshint doesn't really know how to deal with jsx (push 'javascript-jshint flycheck-disabled-checkers))) diff --git a/modules/lang/javascript/packages.el b/modules/lang/javascript/packages.el index 22358eb9a..5571459e6 100644 --- a/modules/lang/javascript/packages.el +++ b/modules/lang/javascript/packages.el @@ -8,14 +8,11 @@ (package! js2-refactor) (package! rjsx-mode) (package! nodejs-repl) -(package! tern) (package! web-beautify) +(package! tide) (package! skewer-mode) (package! eslintd-fix) -(when (featurep! :completion company) - (package! company-tern)) - (when (featurep! :feature lookup) (package! xref-js2))