 
 ;; Typescript
 
-(defun setup-tide-mode ()
-  (interactive)
-  (tide-setup)
-  (flycheck-mode +1)
-  (setq flycheck-check-syntax-automatically '(save mode-enabled))
-  (eldoc-mode +1)
-  (tide-hl-identifier-mode +1)
-  (company-mode +1))
-
-(defun use-eslint-from-node-modules ()
-  (let* ((root (locate-dominating-file
-                (or (buffer-file-name) default-directory)
-                "node_modules"))
-         (eslint
-          (and root
-               (expand-file-name "node_modules/.bin/eslint"
-                                 root))))
-    (when (and eslint (file-executable-p eslint))
-      (setq-local flycheck-javascript-eslint-executable eslint))))
+(defun seanmacs/bin-from-node-modules (bin)
+  "Return the full path if BIN exists in dominating node modules
+dir. Return nil otherwise."
+  (let* ((dir (or (projectile-project-root)
+                  (buffer-file-name)
+                  default-directory))
+         (root (locate-dominating-file dir "node_modules"))
+         (rel-path (format "node_modules/.bin/%s" bin))
+         (path (and root (expand-file-name rel-path root))))
+    (when (and path (file-executable-p path))
+      path)))
 
 (use-package tide
   :straight t
-  :after (web-mode company flycheck)
-  :config)
+  :defer t
+  :commands tide-setup)
 
 (use-package prettier-js
   :straight t
-  :after (web-mode))
+  :defer t
+  :init
+  (defun seanmacs/use-node-modules-prettier ()
+    (when-let ((prettier (seanmacs/bin-from-node-modules "prettier")))
+      (setq-local prettier-js-command prettier)))
+  :hook ((prettier-js-mode . seanmacs/use-node-modules-prettier)))
 
 (use-package web-mode
   :straight t

  :straight t
  :defer t
  :config
  (setq rust-format-on-save t)
  (setq lsp-rust-clippy-preference "on")
  (add-hook 'rust-mode-hook #'lsp)
  (add-hook 'rust-mode-hook #'cargo-minor-mode)
  (define-key rust-mode-map (kbd "C-c C-d") #'lsp-describe-thing-at-point))

(use-package cargo
  :straight t

        web-mode-enable-current-element-highlight t
        web-mode-enable-auto-quoting nil
        web-mode-enable-auto-indentation nil)
  :hook ((web-mode . seanmacs/setup-tide)
         (web-mode . seanmacs/use-node-modules-eslint))
  :bind (:map web-mode-map
              ("C-c C-d" . tide-documentation-at-point)))

(core/local 'web-mode-map
            "rn" 'tide-rename-symbol
