今までまり必要性がなかったため放置していた LSP(lsp-mode) を導入します。(typescript, tsx を編集する機会が増えたので)
1. 言語サーバーのインストール
まず、TypeScript と言語サーバーをグローバルにインストールします。
npm install -g typescript typescript-language-server
インストール確認:
typescript-language-server --version tsc --version
2. typescript-language-server への PATH の設定
lsp-mode から typescript-language-server が起動されます。 そのため typescript-language-server への場所に PATH を通しておく必要があります。 init.el などに以下を追加します。
(when (eq system-type 'windows-nt)
(let* ((userprofile (getenv "USERPROFILE"))
(volta-bin (expand-file-name "AppData/Local/Volta/bin" userprofile)))
(when (file-directory-p volta-bin)
(add-to-list 'exec-path volta-bin)
(setenv "PATH"
(concat volta-bin ";" (getenv "PATH"))))))
3. LSP Mode の設定
;; lsp-mode の基本設定
(use-package lsp-mode
:init
(setq lsp-keymap-prefix "C-c l")
:hook ((typescript-ts-mode . lsp)
(tsx-ts-mode . lsp)
(lsp-mode . lsp-enable-which-key-integration))
:commands lsp
:config
;; パフォーマンス設定
(setq lsp-idle-delay 0.5
lsp-log-io nil
lsp-completion-provider :capf
lsp-headerline-breadcrumb-enable nil)
;; Windows特有の設定
(setq lsp-enable-file-watchers nil) ;; パフォーマンス向上
)
;; lsp-ui の設定(UIの改善)
(use-package lsp-ui
:commands lsp-ui-mode
:config
(setq lsp-ui-doc-enable t
lsp-ui-doc-position 'at-point
lsp-ui-doc-delay 0.5
lsp-ui-sideline-enable t
lsp-ui-sideline-show-hover t))
4. TypeScript Mode の設定
(use-package typescript-ts-mode
:mode (("\\.ts\\'" . typescript-ts-mode)
("\\.tsx\\'" . tsx-ts-mode))
:config
(setq typescript-ts-mode-indent-offset 2))