Yiエディタでtagsを読み込む
yi.hsの設定処理(ConfigM a
)でタグ(TagTable
)を読み込むようにしてみます。
Vimでいう、これ。
.vimrc
" Reference tags of ctags
let &tags = join([
\ 'tags',
\ '.git/tags',
\ '../.git/tags',
\ '../../.git/tags',
\ '../../../.git/tags',
\ '../../../../.git/tags'
\], ',')
ConfigM a
は以下のyiの起動用関数によって実行されます。
- configMain :: Config -> ConfigM () -> IO ()
またはrunConfigM
と以下を組み合わせることでも実現できます。
- runConfigM :: ConfigM a -> StateT Config IO a
- flip execStateT defaultConfig . runConfigM :: ConfigM a -> IO Config
- yi :: Config -> IO ()
- startEditor :: Config -> Maybe Editor -> IO ()
TagTable
はタグファイルとタグ情報のまとまりです。
以下の関数によって読み込み、設定されます。
- importTagTable :: FilePath -> IO TagTable
- setTags :: TagTable -> EditorM ()
ConfigM aにTagTableを設定する
Config
値コンストラクタの第4引数(initialActions
に位置する値)にsetTags x
を設定します。
ConfigM ()
内でinitialActionsA
というLens関数を使います。
- initialActionsA :: Lens' Config [Action]
- (.=) :: MonadState s m => ASetter s s a b -> b -> m ()
ConfigM ()
はMonadState Config ()
なので(initialActionsA .=)
に[Action]
を渡すことで
MonadState Config
の文脈のConfig
値を更新することができます。
Config
- initialActions :: [Action]
実装
-- Find ctag file and Load it
findTagTable :: IO (Maybe TagTable)
findTagTable = do
mayTagFile <- findTagFile maxDepth
case mayTagFile of
Nothing -> return Nothing
Just tagFile -> Just <$> importTagTable tagFile
where
maxDepth = 5
findTagFile :: Int -> IO (Maybe FilePath)
findTagFile 0 = return Nothing
findTagFile depth = do
let parentsSuffix = foldl' (++) "" . replicate (maxDepth - depth) $ "/.."
currentDir <- (++) <$> getCurrentDirectory <*> pure parentsSuffix
x <- sequence [ doesFileExist $ currentDir ++ "/tags"
, doesFileExist $ currentDir ++ "/.git/tags" ]
case x of
[True, _] -> return . Just $ currentDir ++ "/tags"
[_, True] -> return . Just $ currentDir ++ "/.git/tags"
_ -> findTagFile $ depth - 1
myConfig :: Maybe TagTable -> ConfigM ()
myConfig mayTagTable = do
configureHaskellMode
configureMiscModes
configureMyVim
case mayTagTable of
Nothing -> return ()
Just tagTable -> do
let actions = [ EditorA $ setTags tagTable ]
initialActionsA .= actions
以下のように使用します。 (ただしyi.hsでなくMain.hs版)
Main.hs
main :: IO ()
main = do
mayTagTable <- findTagTable
config <- flip execStateT defaultConfig . runConfigM $ myConfig mayTagTable
startEditor config Nothing
参考ページ
追伸
プリパラ見ながら書きました。