vital.vim icon indicating copy to clipboard operation
vital.vim copied to clipboard

RFC: Add a special module method to check if the module is available

Open lambdalisue opened this issue 6 years ago • 8 comments

Summary

Add '_vital_healthcheck' method which will be called when the module is imported and raise an error with a reason when the module does not work on the running Vim.

Detail

I would like to make modules which requires some requriments. In that case, the module might not work on some Vim and I would like to skip the tests.

To achive that, I made a helper module import function like

https://github.com/lambdalisue/vital-Whisky/blob/master/autoload/healthcheck/vital.vim

However, it contains a dependency to the vim-themis just for testing.

So it would be nice if vital.vim itself has similar special method which is automatically called when the module is imported and raise an error with the reason. With that new feature, vim-themis or its harmonic plugin can wrap vital#xxx#import and skip with a reason raised when the module is not available.

What do you think? Esp, @thinca

lambdalisue avatar Jan 23 '18 15:01 lambdalisue

返り値で neovim が使えるかどうか判断するとしたら

  • どんな構造の値を投げるのか
  • エラーでない値は何なのか
    • v:null は Vim 8 からなので使えない

を決める必要がありそうです。 もしくはエラーの時だけ :throw してしまえば2番目のエラーでない値を決める必要は無くなりそう。

tyru avatar Feb 27 '18 05:02 tyru

返り値で neovim が使えるかどうか判断するとしたら

neovim 以外にも様々な理由でモジュールは利用不可になるため、返り値では判断できないと思っています。 返り値で返すべきは「なぜ、利用できないのか?」の理由が良いと思います。

個人的には自動的に呼ばれる関数があり、その関数でエラーを投げたら利用不可くらいのゆるい感じがいいなかと思います。

lambdalisue avatar Feb 27 '18 23:02 lambdalisue

neovim 以外にも様々な理由でモジュールは利用不可になるため、返り値では判断できないと思っています。 返り値で返すべきは「なぜ、利用できないのか?」の理由が良いと思います。

理由を :throw する、という事でしょうか?(s/返り値で返すべき/例外で返すべき/ ?)

ゆるい感じ

自分もそんな感じでいいと思います。

あと投げる値の構造は個人的には文字列でいいと思います(他に含める情報が思い当たらない)。

tyru avatar Feb 28 '18 04:02 tyru

ただ例外を投げるとなるとそもそも _vital_healthcheck って新しく追加する必要がない気がします。 _vital_created_vital_loaded で例外投げれば使用不可能だと判断する、で良いような(autoload/vital/_vital.vim の修正のみで良い)。

tyru avatar Feb 28 '18 04:02 tyru

良さそう > 例外投げるならわざわざ確認用メソッドではなくload時で的な

ujihisa avatar Feb 28 '18 14:02 ujihisa

特殊な関数 _vital_healthcheck (仮) は、利用不可な理由(文字列)の配列を返す。 vital loader はこの関数をチェックし、利用不可であれば例外を投げる。 が、良さそう。

thinca avatar Mar 01 '18 10:03 thinca

名前案の第一候補: ~get_unavailable_reasons()~ reject_reasons()

ujihisa avatar Mar 01 '18 10:03 ujihisa

名前候補 _vital_validate でいいのではと思ってきた

lambdalisue avatar Jun 14 '18 09:06 lambdalisue