2018-04-26 19:43:50 +00:00
|
|
|
|
;;; funcs.el --- react layer funcs file for Spacemacs. -*- lexical-binding: t -*-
|
2017-01-02 03:45:23 +00:00
|
|
|
|
;;
|
2022-05-31 16:12:58 +00:00
|
|
|
|
;; Copyright (c) 2012-2022 Sylvain Benner & Contributors
|
2017-01-02 03:45:23 +00:00
|
|
|
|
;;
|
|
|
|
|
;; Author: Muneeb Shaikh <muneeb@reversehack.in>
|
|
|
|
|
;; URL: https://github.com/syl20bnr/spacemacs
|
|
|
|
|
;;
|
|
|
|
|
;; This file is not part of GNU Emacs.
|
|
|
|
|
;;
|
2021-03-24 03:31:44 +00:00
|
|
|
|
;; This program is free software; you can redistribute it and/or modify
|
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
;;
|
|
|
|
|
;; This program is distributed in the hope that it will be useful,
|
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
;;
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
|
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
2017-01-02 03:45:23 +00:00
|
|
|
|
|
2018-04-26 19:43:50 +00:00
|
|
|
|
|
|
|
|
|
;; Backend
|
|
|
|
|
(defun spacemacs//react-setup-backend ()
|
|
|
|
|
"Conditionally setup react backend."
|
|
|
|
|
(pcase javascript-backend
|
2021-03-18 03:29:20 +00:00
|
|
|
|
('tern (spacemacs/tern-setup-tern))
|
|
|
|
|
('tide (spacemacs//tide-setup))
|
|
|
|
|
('lsp (spacemacs//react-setup-lsp))))
|
2018-04-26 19:43:50 +00:00
|
|
|
|
|
|
|
|
|
(defun spacemacs//react-setup-company ()
|
|
|
|
|
"Conditionally setup company based on backend."
|
2021-03-18 03:29:20 +00:00
|
|
|
|
(when (eq javascript-backend 'tide)
|
|
|
|
|
(spacemacs//tide-setup-company 'rjsx-mode)))
|
2018-04-26 19:43:50 +00:00
|
|
|
|
|
2019-04-13 07:12:06 +00:00
|
|
|
|
(defun spacemacs//react-setup-next-error-fn ()
|
|
|
|
|
"If the `syntax-checking' layer is enabled, disable `rjsx-mode''s
|
|
|
|
|
`next-error-function', and let `flycheck' handle any errors."
|
|
|
|
|
(when (configuration-layer/layer-used-p 'syntax-checking)
|
|
|
|
|
(setq-local next-error-function nil)))
|
2018-04-26 19:43:50 +00:00
|
|
|
|
|
|
|
|
|
;; LSP
|
|
|
|
|
(defun spacemacs//react-setup-lsp ()
|
|
|
|
|
"Setup lsp backend."
|
|
|
|
|
(if (configuration-layer/layer-used-p 'lsp)
|
2019-04-13 07:12:06 +00:00
|
|
|
|
(progn
|
2021-03-18 03:29:20 +00:00
|
|
|
|
(unless javascript-lsp-linter
|
2020-09-18 18:31:30 +00:00
|
|
|
|
(setq-local lsp-diagnostics-provider :none))
|
2021-06-06 20:25:00 +00:00
|
|
|
|
(lsp-deferred))
|
2018-05-24 02:12:30 +00:00
|
|
|
|
(message "`lsp' layer is not installed, please add `lsp' layer to your dotfile.")))
|
2018-04-26 19:43:50 +00:00
|
|
|
|
|
|
|
|
|
|
2018-05-19 05:15:04 +00:00
|
|
|
|
;; Emmet
|
|
|
|
|
(defun spacemacs/react-emmet-mode ()
|
|
|
|
|
"Activate `emmet-mode' and configure it for local buffer."
|
|
|
|
|
(emmet-mode)
|
|
|
|
|
(setq-local emmet-expand-jsx-className? t))
|
|
|
|
|
|
2018-05-19 05:16:18 +00:00
|
|
|
|
|
2018-04-26 19:43:50 +00:00
|
|
|
|
;; Others
|
2021-03-18 03:29:20 +00:00
|
|
|
|
(defun spacemacs//javascript-jsx-file-p ()
|
|
|
|
|
"Enable rjsx mode by using magic-mode-alist."
|
2021-04-04 17:21:46 +00:00
|
|
|
|
(when buffer-file-name
|
2021-04-19 17:26:19 +00:00
|
|
|
|
(and (member (file-name-extension buffer-file-name) '("js" "jsx"))
|
2021-03-18 03:29:20 +00:00
|
|
|
|
(re-search-forward "\\(^\\s-*import React\\|\\( from \\|require(\\)[\"']react\\)"
|
|
|
|
|
magic-mode-regexp-match-limit t)
|
|
|
|
|
(save-excursion
|
|
|
|
|
(goto-char (match-beginning 1))
|
|
|
|
|
(let ((sexp (syntax-ppss)))
|
|
|
|
|
;; not inside string or comment
|
|
|
|
|
(not (or (nth 3 sexp)
|
|
|
|
|
(nth 4 sexp))))))))
|
2018-06-02 00:34:48 +00:00
|
|
|
|
|
|
|
|
|
(defun spacemacs//react-setup-yasnippet ()
|
|
|
|
|
(yas-activate-extra-mode 'js-mode))
|
2019-10-09 15:26:12 +00:00
|
|
|
|
|
|
|
|
|
;; Format
|
2019-11-22 19:54:19 +00:00
|
|
|
|
(defun spacemacs//react-fmt-before-save-hook ()
|
2019-10-09 15:26:12 +00:00
|
|
|
|
(add-hook 'before-save-hook 'spacemacs/javascript-format t t))
|