diff --git a/layers/+lang/typescript/README.org b/layers/+lang/typescript/README.org index acb1f75c0..d5d09424f 100644 --- a/layers/+lang/typescript/README.org +++ b/layers/+lang/typescript/README.org @@ -27,12 +27,15 @@ This layer provides: - Imenu - linting - tsx mode +- formatting * Install ** Pre-requisites You will need =node.js v0.12.0= or greater -If you want linting run: =npm instell -g typescript= =npm install -g tslint= +If you want linting run: =npm install -g typescript= =npm install -g tslint= + +If you want formatting run: =npm install -g typescript-formatter= For best results, make sure that the =auto-completion= (company) and =html= layers are enabled. @@ -45,17 +48,32 @@ file. (setq-default dotspacemacs-configuration-layers '(typescript)) #+END_SRC -or if you don't need linting. +If you don't need linting, you can use: #+BEGIN_SRC emacs-lisp (setq-default dotspacemacs-configuration-layers '( - (typescript :variables - typescript-use-tslint nil)) + (typescript :variables + typescript-use-tslint nil))) + +#+END_SRC + +If you need formatting on save: + +#+BEGIN_SRC emacs-lisp +(setq-default dotspacemacs-configuration-layers '( + (typescript :variables + typescript-fmt-on-save t))) + #+END_SRC ** Notes -This layer uses [[https://github.com/ananthakumaran/tide][tide]] and [[https://github.com/Simplify/flycheck-typescript-tslint/][flycheck-typescript-tslint]] +This layer uses: +- [[https://github.com/ananthakumaran/tide][tide]] +- [[https://github.com/Simplify/flycheck-typescript-tslint][flycheck-typescript-tslint]] +- [[https://github.com/vvakame/typescript-formatter][typescript-formatter]] + +*Those tools use configuration files. You can learn more in their documentations.* Make sure to add [[https://github.com/Microsoft/TypeScript/wiki/tsconfig.json][tsconfig.json]] in the project root folder. @@ -69,19 +87,20 @@ Currently tsserver doesn't pickup tsconfig.json file changes. You might need to | Key Binding | Description | |-------------+----------------------------------| -| ~SPC g b~ | jump back | -| ~SPC g g~ | jump to entity's definition | -| ~SPC g t~ | jump to entity's type definition | -| ~SPC g u~ | references | -| ~SPC h h~ | documentation at point | -| ~SPC r r~ | rename symbol | -| ~SPC s r~ | restart server | +| ~SPC m =~ | Reformat the buffer | +| ~SPC m g b~ | jump back | +| ~SPC m g g~ | jump to entity's definition | +| ~SPC m g t~ | jump to entity's type definition | +| ~SPC m g u~ | references | +| ~SPC m h h~ | documentation at point | +| ~SPC m r r~ | rename symbol | +| ~SPC m s r~ | restart server | ** Reference Major Mode -| Key Binding | Description | -|-------------+----------------------------------| -| ~C-j~ | find previous reference | -| ~C-k~ | find next reference | -| ~C-l~ | goto reference | +| Key Binding | Description | +|-------------+-------------------------| +| ~C-j~ | find previous reference | +| ~C-k~ | find next reference | +| ~C-l~ | goto reference | diff --git a/layers/+lang/typescript/config.el b/layers/+lang/typescript/config.el index 0701ed44e..0f79dedcc 100644 --- a/layers/+lang/typescript/config.el +++ b/layers/+lang/typescript/config.el @@ -1,2 +1,5 @@ (defvar typescript-use-tslint t - "Use flycheck linter if not nil") + "Use flycheck linter if not nil.") + +(defvar typescript-fmt-on-save nil + "Run formatter on buffer save.") diff --git a/layers/+lang/typescript/packages.el b/layers/+lang/typescript/packages.el index f1ccfc379..3a60d419e 100644 --- a/layers/+lang/typescript/packages.el +++ b/layers/+lang/typescript/packages.el @@ -9,8 +9,9 @@ ;; ;;; License: GPLv3 -(setq typescript-packages '(tide - flycheck-typescript-tslint +(setq typescript-packages '(flycheck-typescript-tslint + tide + typescript-mode web-mode)) (defun typescript/init-tide () @@ -71,6 +72,48 @@ (when (configuration-layer/package-usedp 'company) (company-mode-on)))))))) +(defun typescript/init-typescript-mode () + (use-package typescript-mode + :defer t + :commands (typescript/format-buffer) + :config (progn + (defun typescript/format-buffer () + "Format buffer with tsfmt." + (interactive) + (if (executable-find "tsfmt") + (let* ((tmpfile (make-temp-file "~fmt-tmp" nil ".ts")) + (coding-system-for-read 'utf-8) + (coding-system-for-write 'utf-8) + (outputbuf (get-buffer-create "*~fmt-tmp.ts*"))) + (unwind-protect + (progn + (with-current-buffer outputbuf (erase-buffer)) + (write-region nil nil tmpfile) + (if (zerop (apply 'call-process "tsfmt" nil outputbuf nil (list tmpfile))) + (let ((p (point))) + (save-excursion + (with-current-buffer (current-buffer) + (erase-buffer) + (insert-buffer-substring outputbuf))) + (goto-char p) + (message "formatted.") + (kill-buffer outputbuf)) + (progn + (message "Formatting failed!") + (display-buffer outputbuf))) + (progn + (delete-file tmpfile))))) + (message "tsfmt not found. Run \"npm install -g typescript-formatter\""))) + + (spacemacs/set-leader-keys-for-major-mode 'typescript-mode "=" 'typescript/format-buffer) + + (when typescript-fmt-on-save + + (defun typescript/before-save-hook () + (add-hook 'before-save-hook 'typescript/format-buffer t t)) + + (add-hook 'typescript-mode-hook 'typescript/before-save-hook))))) + (when typescript-use-tslint (defun typescript/init-flycheck-typescript-tslint () (use-package flycheck-typescript-tslint