spacemacs/layers/+frameworks/react/funcs.el
Lucius Hu 58243c67f8 react: refactor
- Refactored functions
  - Several functions are inlined and removed from global namespace
- Moved function from `packages.el` to `func.el`
- Replaced `(when (not foo) bar)` with `(unless foo bar)`
- Replaced unnecessary backquote construct with simple quotation
- Replaced `pcase` form with only one-arm with `when` or `unless` form
2021-04-04 12:48:21 +02:00

82 lines
2.9 KiB
EmacsLisp
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

;;; funcs.el --- react layer funcs file for Spacemacs. -*- lexical-binding: t -*-
;;
;; Copyright (c) 2012-2021 Sylvain Benner & Contributors
;;
;; Author: Muneeb Shaikh <muneeb@reversehack.in>
;; URL: https://github.com/syl20bnr/spacemacs
;;
;; This file is not part of GNU Emacs.
;;
;; 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/>.
;; Backend
(defun spacemacs//react-setup-backend ()
"Conditionally setup react backend."
(pcase javascript-backend
('tern (spacemacs/tern-setup-tern))
('tide (spacemacs//tide-setup))
('lsp (spacemacs//react-setup-lsp))))
(defun spacemacs//react-setup-company ()
"Conditionally setup company based on backend."
(when (eq javascript-backend 'tide)
(spacemacs//tide-setup-company 'rjsx-mode)))
(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)))
;; LSP
(defun spacemacs//react-setup-lsp ()
"Setup lsp backend."
(if (configuration-layer/layer-used-p 'lsp)
(progn
(unless javascript-lsp-linter
(setq-local lsp-diagnostics-provider :none))
(lsp))
(message "`lsp' layer is not installed, please add `lsp' layer to your dotfile.")))
;; Emmet
(defun spacemacs/react-emmet-mode ()
"Activate `emmet-mode' and configure it for local buffer."
(emmet-mode)
(setq-local emmet-expand-jsx-className? t))
;; Others
(defun spacemacs//javascript-jsx-file-p ()
"Enable rjsx mode by using magic-mode-alist."
(when-let* ((buffer-file-name))
(and (memq (file-name-extension buffer-file-name) '("js" "jsx"))
(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))))))))
(defun spacemacs//react-setup-yasnippet ()
(yas-activate-extra-mode 'js-mode))
;; Format
(defun spacemacs//react-fmt-before-save-hook ()
(add-hook 'before-save-hook 'spacemacs/javascript-format t t))