conf-javascript.el 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. ;;; Code:
  2. (require 'js2-mode)
  3. (require 'js2-refactor)
  4. (require 'js2-imenu-extras)
  5. (require 'xref-js2)
  6. (require 'web-beautify)
  7. (require 'rainbow-delimiters)
  8. (require 'company-tern)
  9. ;; Control
  10. (defconst distopico:jshint-regexp
  11. (concat "\\`" (regexp-quote ".jshintrc") "\\'"))
  12. (defconst distopico:eslint-regexp
  13. (concat "\\`" (regexp-quote ".eslintrc") "\\(\\.\\(cjs|js\\|ya?ml\\|json\\)\\)?\\'"))
  14. ;; Base config
  15. ;; TODO: some cool stuff: https://github.com/foretagsplatsen/emacs-js
  16. (setq js-indent-level 4
  17. js-indent-first-init "dynamic"
  18. ;; js-indent-align-list-continuation nil ;; TODO: align parameter to next line
  19. js2-basic-offset 4
  20. js2-skip-preprocessor-directives t
  21. js2-include-node-externs t
  22. js2-include-browser-externs t
  23. js2-highlight-level 3
  24. js2-move-point-on-right-click t
  25. ;; Let js2-mode warn some errors
  26. js2-mode-show-parse-errors t
  27. js2-mode-show-strict-warnings t
  28. js2-strict-trailing-comma-warning nil
  29. js2-strict-missing-semi-warning nil
  30. ;; js2-bounce-indent-p t TODO: works for better indentation?
  31. js2-strict-inconsistent-return-warning nil)
  32. (add-to-list 'auto-mode-alist '("\\.js$" . js2-mode))
  33. (add-to-list 'interpreter-mode-alist '("node" . js2-mode))
  34. (add-to-list 'interpreter-mode-alist '("javascript" . js2-mode))
  35. (custom-set-faces
  36. '(js2-highlight-vars-face ((t (:background "royal blue" :foreground "white")))))
  37. ;; unbind keys
  38. (define-key tern-mode-keymap (kbd "M-.") nil)
  39. (define-key tern-mode-keymap (kbd "M-,") nil)
  40. ;; Custom keys
  41. (js2r-add-keybindings-with-prefix "C-c C-m")
  42. (define-key js2-mode-map (kbd "M-.") 'xref-find-definitions)
  43. (define-key js2-mode-map (kbd "M-,") 'xref-pop-marker-stack)
  44. (define-key js2-mode-map (kbd "C-c ci") 'js-doc-insert-function-doc)
  45. (define-key js2-mode-map (kbd "C-c cf") 'js-doc-insert-file-doc)
  46. (define-key js2-mode-map (kbd "C-c cs") 'js-doc-insert-function-doc-snippet)
  47. (define-key js2-mode-map "@" 'js-doc-insert-tag)
  48. ;; Beautify with js
  49. (define-key js2-mode-map (kbd "C-c C-b f") 'web-beautify-js)
  50. ;; Functions
  51. (defun distopico:js2-mode-hook ()
  52. "The js2-mode hook."
  53. (js2-imenu-extras-mode t)
  54. (js2-imenu-extras-setup)
  55. (tern-mode t)
  56. (js2-refactor-mode t)
  57. (add-hook 'xref-backend-functions #'xref-js2-xref-backend nil t)
  58. (distopico:js-common-setup))
  59. (defun distopico:js2-company-setup ()
  60. "Setup JavaScript company/code completion back-ends.
  61. All the inherits `company-yasnippet' back-end."
  62. (distopico:backend-with-yas
  63. '(company-semantic company-files
  64. (company-dabbrev-code company-gtags company-tern)
  65. (company-capf company-dabbrev company-keywords))))
  66. ;;;###autoload
  67. (defun distopico:js-common-setup ()
  68. "Common setup to JS and JSX."
  69. ;; Add company backend for js
  70. (setq-local company-backends (distopico:js2-company-setup))
  71. ;; Add node_modules to exec path
  72. (distopico:add-node-modules-path)
  73. ;; TODO: migrate to flymake
  74. ;; Default checker
  75. ;; Set default jshint
  76. ;; Enable checker by project
  77. ;; (when (distopico:locate-parent-file distopico:eslint-regexp)
  78. ;; ;; enable eslint
  79. )
  80. ;;;###autoload
  81. (defun distopico:bin-from-node-modules ()
  82. "Get executable node_modules by sub-package."
  83. (interactive)
  84. (let* ((bin-target "node_modules/.bin")
  85. (bin-parent (locate-dominating-file
  86. (or (buffer-file-name) default-directory)
  87. bin-target))
  88. (bin-path (and bin-parent
  89. (expand-file-name bin-target bin-parent))))
  90. bin-path))
  91. ;;;###autoload
  92. (defun distopico:add-node-modules-path ()
  93. "Add `node_modules' in current project to exec path."
  94. (interactive)
  95. ;; TODO: make async
  96. (let ((node-path (shell-command-to-string "npm bin"))
  97. (modules-bin (distopico:bin-from-node-modules)))
  98. (when node-path
  99. (progn
  100. (make-local-variable 'exec-path)
  101. (add-to-list 'exec-path (string-trim node-path))
  102. (when modules-bin
  103. (add-to-list 'exec-path (string-trim modules-bin)))
  104. (message "added node_modules to exec-path")))))
  105. ;; Hooks
  106. (add-hook 'js2-mode-hook #'distopico:js2-mode-hook)
  107. (provide 'conf-javascript)