Skip to content

Commit

Permalink
react: refactor
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
lebensterben authored and duianto committed Apr 4, 2021
1 parent 215b634 commit 58243c6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 34 deletions.
41 changes: 18 additions & 23 deletions layers/+frameworks/react/funcs.el
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@
(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))))
('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."
(pcase javascript-backend
(`tide (spacemacs//tide-setup-company 'rjsx-mode))))
(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
Expand All @@ -46,7 +46,7 @@
"Setup lsp backend."
(if (configuration-layer/layer-used-p 'lsp)
(progn
(when (not javascript-lsp-linter)
(unless javascript-lsp-linter
(setq-local lsp-diagnostics-provider :none))
(lsp))
(message "`lsp' layer is not installed, please add `lsp' layer to your dotfile.")))
Expand All @@ -60,23 +60,18 @@


;; Others
(defun spacemacs//react-inside-string-q ()
"Returns non-nil if inside string, else nil.
Result depends on syntax table's string quote character."
(let ((result (nth 3 (syntax-ppss))))
result))

(defun spacemacs//react-inside-comment-q ()
"Returns non-nil if inside comment, else nil.
Result depends on syntax table's comment character."
(let ((result (nth 4 (syntax-ppss))))
result))

(defun spacemacs//react-inside-string-or-comment-q ()
"Return non-nil if point is inside string, documentation string or a comment.
If optional argument P is present, test this instead of point."
(or (spacemacs//react-inside-string-q)
(spacemacs//react-inside-comment-q)))
(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))
Expand Down
13 changes: 2 additions & 11 deletions layers/+frameworks/react/packages.el
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,8 @@
(use-package rjsx-mode
:defer t
:init
;; enable rjsx mode by using magic-mode-alist
(defun +javascript-jsx-file-p ()
(and buffer-file-name
(or (equal (file-name-extension buffer-file-name) "js")
(equal (file-name-extension buffer-file-name) "jsx"))
(re-search-forward "\\(^\\s-*import React\\|\\( from \\|require(\\)[\"']react\\)"
magic-mode-regexp-match-limit t)
(progn (goto-char (match-beginning 1))
(not (spacemacs//react-inside-string-or-comment-q)))))

(add-to-list 'magic-mode-alist (cons #'+javascript-jsx-file-p 'rjsx-mode))

(add-to-list 'magic-mode-alist (cons #'spacemacs//javascript-jsx-file-p 'rjsx-mode))

;; setup rjsx backend
(add-hook 'rjsx-mode-local-vars-hook #'spacemacs//react-setup-backend)
Expand Down

0 comments on commit 58243c6

Please sign in to comment.