Open7

neovim の startup 時に読み込まれるファイル

futsuuufutsuuu

大体の流れ

引数とかは適当に端折ってます

bool did_source_packages;

void load_plugins(void) {
  if (!loadplugins) {
    /*
    - :set noloadplugins
    - nvim -u NONE
    - nvim --noplugin など
    */
    return;
  }

  char *rtp_copy = p_rtp;

  if (!did_source_packages) {
    add_pack_start_dirs();
  }

  // loading rtp plugins
  source_in_path_vim_lua(rtp_copy, "plugin/**/*", NOAFTER);

  // loading packages
  if (!did_source_packages) {
    load_start_packages();
  }

  // loading after plugins
  source_runtime_vim_lua("plugin/**/*", AFTER);
}
futsuuufutsuuu
static bool source_callback_vim_lua(char **fname) {
  // *.vim、*.lua の順に読み込む
}
static int load_pack_plugin(bool opt, char *fname) {
  // パッケージの plugin/ にあるファイル、
  // opt パッケージは ftdetect も一緒に読み込む
  // fname から
  // - plugin/**/*
  // - ftdetect/*
  // にマッチしたファイルを探し、
  // source_callback_vim_lua() に渡す
}

void load_start_packages(void) {
  did_source_packages = true;
  // 全てのパッケージの start/ にあるプラグインを読み込む
  // &packpath から
  // - pack/*/start/*
  // - start/*
  // にマッチするディレクトリを探し、
  // load_pack_plugin(false, )に渡す
}

load_start_packages():packloadall でも呼び出される

futsuuufutsuuu
int source_in_path_vim_lua(char *path, char *name) {
  // path から name にマッチするファイルを探し、
  // source_callback_vim_lua() に渡す
}

int source_runtime_vim_lua(char *name) {
  // source_in_path_vim_lua の path が runtimepath になるのと同じ
}
futsuuufutsuuu
void add_pack_start_dirs(void) {
  // 全てのパッケージの start/ にあるプラグインを runtimepath に追加する
  // &packpath の全てのディレクトリをadd_pack_start_dir()に渡す
}

static bool add_pack_start_dir(char **fnames) {
  // fname に
  // - /start/*
  // - /pack/*/start/*
  // を付け、ディレクトリ(dir)が存在した場合、
  // runtimepath を {dir},p_rtp にする
  // {dir}/after が存在する場合は
  // runtimepath を {dir},p_rtp,{dir}/after にする
}
futsuuufutsuuu

最初に読み込まれるプラグイン

  1. プラグイン
  2. after プラグイン
  3. start パッケージのプラグイン
  4. start パッケージの after プラグイン

読み込まれるタイミング

-- :packloadall が呼ばれると 3 が読み込まれ、did_source_packages が true になる

-- 1, 2 のみが含まれる runtimepath を保存しておく
local rtp_copy = runtimepath

-- 3, 4 を runtimepath に追加する
if not did_source_packages then
  for path in packpath do
    for dir in "{path}/start/*" do
      runtimepath:prepend(dir)
      if exist("{dir}/after") then
        runtimepath:append("{dir}/after")
      end
    end
    for dir in "{path}/pack/*/start/*" do
      runtimepath:prepend(dir)
      if exist("{dir}/after") then
        runtimepath:append("{dir}/after")
      end
    end
  end
end

-- 1 を読み込む
for path in rtp_copy do
  if not path:find("after/?$") then
    "{path}/plugin/**/*.vim"
    "{path}/plugin/**/*.lua"
  end
end

-- 3 を読み込む
if not did_source_packages then
  did_source_packages = true
  for path in packpath do
    "{path}/pack/*/start/*/plugin/**/*.vim"
    "{path}/pack/*/start/*/plugin/**/*.lua"

    "{path}/start/*/plugin/**/*.vim"
    "{path}/start/*/plugin/**/*.lua"
  end
end

-- 2, 4 を読み込む
for path in runtimepath do
  if path:find("after/?$") then
    "{path}/plugin/**/*.vim"
    "{path}/plugin/**/*.lua"
  end
end
futsuuufutsuuu
if (!vimrc_none || params.clean) {
  // ftplugin.vim と indent.vim を読み込む
  // ftplugin で定義された設定を init.vim 内の autocmd FileType で
  // 上書きできるようにするため、init.vim よりも先に実行する
  filetype_plugin_enable();
}

// init.vim
source_startup_scripts();

if (!vimrc_none || params.clean) {
  // filetype on の場合、filetype.lua を読み込む
  filetype_maybe_enable();
  // syntax on の場合、syntax/synax.vim を読み込む
  syn_maybe_enable();
}

// ここでプラグインが読み込まれる
load_plugins();

つまり、noloadplugins なプラグインマネージャは filetype.lua、syntax/syntax.vim を読み込んでからプラグインを処理する必要がある

futsuuufutsuuu

「*.vim、*.lua を読み込む」というのは、内部的には :source と同じ(do_source(...))。
つまり、デフォルトプラグインを読み込むときにも、SourceCmdSourcePostSourcePre が発火する。