ログ日記

作業ログと日記とメモ

haskell-language-server の設定2

https://n314.hatenablog.com/entry/2021/07/24/173834 の続き。

emacs lsp-mode "disconnected"」で検索した https://github.com/emacs-lsp/lsp-mode/issues/905 ここのコメントに M-x lsp してログを見ろと書いていたので、そのようにする。

lsp-haskell stack "Cradle requires ghc but couldn't find it"

という警告が出ていた。
というかそもそもEmacs起動時に

Server lsp-haskell:27768/starting exited with status exit(check corresponding stderr buffer for details). Do you want to restart it? (y or n)

というエラーも出ていた。

検索して出てきた
haskell-language-server+emacsでのハマりどころ2020年夏
このページを見てもよく分からなかったけど、そういえば hie.yaml が必要だったとどこかに書いていた記憶がある。
手動で作ってうまくいかなくて消したんだった。


hie.yamlでもう一度調べる。
(廃止) VS Code と haskell-ide-engine で Haskell 開発環境を構築する
Haskell環境構築2020簡易版 (macOS, Linux向け) - LugendrePublic

stack install implicit-hie
gen-hie > hie.yaml

そして stackプロジェクトのMain.hsを起動。

emacs app/Main.hs &

おおお、いけたじゃん!


あとは修正と設定。

Error running timer ‘lsp--on-idle’: (wrong-type-argument integerp 9.223372036854776e+18)

というエラーが出るのを回避する。
https://github.com/emacs-lsp/lsp-mode/issues/2435

(setq lsp-headerline-breadcrumb-enable nil)
Error while checking syntax automatically: (error "Keyword argument :end-line not one of (:buffer :checker :filename :line :column :message :level :id :group)")

というエラーが出るのでflycheckをバージョンアップ。



型を自動で挿入したいので

(defun lsp-haskell-execute-code-action-add-signature ()
  "Execute code action of add signature.
Add the type signature that GHC infers to the function located below the point."
  (interactive)
  (let ((action (seq-find
                 (lambda (e) (string-prefix-p "add signature" (lsp:code-action-title e)))
                 (lsp-code-actions-at-point))))
    (if action
        (lsp-execute-code-action action)
      (message "I can't find add signature action for this point"))))
https://www.ncaq.net/2021/06/25/18/20/30/

これを何かのキーに割り当てる。

オプションで

{-# OPTIONS_GHC -Wmissing-signatures #-}

を設定してwarningが出るようにしておく必要があるっぽい。


あとは定義ジャンプ。
Emacs で Language Server Protocol を使ってみる :: プログラマになりたい人生 — プログラマになりたいおじさんの日記



右に色々出たりコードに線が出るけど、まだあまり意味が分かっていない。
Fold とか Unfold は何なんだろう…?IDEで関数を閉じたりする?haskell-modeでは使わない?
Use pointとかreduceとかは押したら文字が削られる…。
How do you use (un)folding? · Issue #418 · haskell/haskell-language-server · GitHub
よく分からん…。
他にも、haskellではなくてlsp-modeのreadmeを見た方がいいかも。



7/31 追記:

Main.hs is not part of any project. Select action:

i==>Import project root path/to/project/
I==>Import project by selecting root directory interactively.
d==>Do not ask again for the current project by adding /path/to/project to lsp-session-folders-blacklist
D==>Do not ask again for the current project by selecting ignore path interactively.
n==>Do nothing: ask again when opening other files from the current project.

というメッセージがミニバッファに出る場合は i を押す。
一瞬で消えたりするので、他のキーを触らずに i を押す。M-x lsp でもう一度見れる。

もしかして以前やったときはこのメッセージを見逃していたのかもしれない。


あと、他のファイルに飛べない?
xref-find-definitions で Main.hs から Lib.hs に飛べない。

と思ったけど、Lib.hs と同じ階層にFoo.hsを作ったらそこには飛べる。
stack new したプロジェクトの構成が分かってないのが問題か…。

Haskellのstackによるプロジェクトについて - Qiita
コメントも参考になる。

Stackでやる最速Haskell Hello world! (GHCのインストール付き!) - Qiita
チュートリアル