はじめに

私はここ3年ほどはmolokaiというカラースキーマを使っています。

GitHub - tomasr/molokai: Molokai color scheme for Vim
Molokai color scheme for Vim. Contribute to tomasr/molokai development by creating an account on GitHub.
github.com
No Image

molokaiの素晴らしいところは、24bit-colorと256-colorであまり差がなく、鮮明に表示されるところがとても好きです。自分の好みがグレーを混ぜたようなくすんだ色よりも、ダークテーマで黒色背景と文字の色が明確に分かれているようなビビットな配色が好みなのでmolokaiはドンピシャなカラースキーマでした。

molokaiは素晴らしいカラースキームなのですが、もう少し自分好みにしたいと思っていることがありました。それがtreesitterのhighlightに対応させることです。

molokaiはtreesitterに対応していないため、なにもせずにtreesitterのhighlight=trueを設定してしまうと、今までハイライトされていなかった箇所全てが同じ色でhighlightされてしまうので、以前よりも見辛くなってしまうかもしれません。

以下の写真は、左がmolokaiのデフォルトのhighlightで右がtreesitterを有効にしたhighlightです。

sreenshot-source-code

treesitterとは

treesitterとはパーサー生成ツールであり、増分解析ライブラリです。次のことを目的に開発されています。

- あらゆるプログラミング言語を解析できる汎用性
- すべてのキーストロークに追従して解析できるほど高速な速度
- 構文エラーが存在しても、有用な解析結果を返し、堅牢であること
- 依存関係がなく、pure Cで記述されたランタイムライブラリを任意のアプリケーションに組み込むことができる

自分はneovimを使っています。neovimはnvim-treesitterというプラグインを導入することによって、treesitterを用いて構文解析することができ、それに対応したhighlightができます。

GitHub - nvim-treesitter/nvim-treesitter: Nvim Treesitter configurations and abstraction layer
Nvim Treesitter configurations and abstraction layer - GitHub - nvim-treesitter/nvim-treesitter: Nvim Treesitter configurations and abstraction layer
github.com
No Image

treesitterのクエリ(nvim-treesitterが抽象化してくれているので、vimscriptやluaからとても簡単に使えるインターフェースが用意されている)を使って、簡単にhighlightの設定を行うことができます。

追加したhighlight

以下のhighlightを追加しました。

if has('nvim-0.8')
  hi @variable    ctermfg=231
  hi @punctuation ctermfg=231
  hi @parameter   ctermfg=231
  hi @include     ctermfg=161 cterm=bold
  hi @field       ctermfg=231
  hi @property    ctermfg=231
endif

各クエリがhighlightしている箇所は以下のようになっています

クエリhighlightの対象
@variable変数
@punctuation‘{}’ ‘:’ ‘;‘など
@parameter関数のパラメータ
@includeモジュールをインクルードするためのキーワード’import’や’from’など
@fieldオブジェクトや構造体のフィールド
@propertyプロパティ。@fieldと似ている

適用後のmolokaiのhighlightは以下です。メソッドや、記号、グローバル変数などがhighlightされて表示がリッチになっています。

screenshot-after forkしたソースコードはこちらになります。

GitHub - devoc09/molokai: Molokai color scheme for Vim/neovim (supported nvim-treesitter)
Molokai color scheme for Vim/neovim (supported nvim-treesitter) - GitHub - devoc09/molokai: Molokai color scheme for Vim/neovim (supported nvim-treesitter)
github.com
No Image

おわりに

treesitterによる構文解析結果のhighlightをmolokaiに追加しました。メソッドと記号のhighlight以外はそれほどいじっていませんが、デフォルトのものよりもかなり印象が変わりました。かなり気に入っています。

自分はMacのTermianl.appを使っているので、256-colorの対応のみとなっていますが、今後iTerm2やAlacrittyなどの24bit-colorに対応したターミナルエミュレータを使うことになればそちらの対応も行おうと思っています。もしご興味があれば使ってみてください。