cursor-dirforest (empty) → 0.0.0.0
raw patch · 5 files changed · show whitespace changes · browse cursor-dirforest-0.0.0.0
+734/−0 lines, 5 filesdep +basedep +containersdep +cursorsetup-changed
Dependencies added: base, containers, cursor, deepseq, dirforest, filepath, microlens, path, text, validity, validity-path
Files
- Setup.hs +2/−0
- cursor-dirforest.cabal +43/−0
- src/Cursor/DirForest.hs +411/−0
- src/Cursor/FileOrDir.hs +119/−0
- src/Cursor/Simple/DirForest.hs +159/−0
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ cursor-dirforest.cabal view
@@ -0,0 +1,43 @@+cabal-version: 1.12++-- This file has been generated from package.yaml by hpack version 0.38.3.+--+-- see: https://github.com/sol/hpack+--+-- hash: 03fd5b7113479fd1b95960afaebd6763f70fdb58935b143e86232ecf81ddab04++name: cursor-dirforest+version: 0.0.0.0+synopsis: Cursors for typed directory forests+homepage: https://github.com/NorfairKing/cursor-dirforest#readme+bug-reports: https://github.com/NorfairKing/cursor-dirforest/issues+license: MIT+build-type: Simple++source-repository head+ type: git+ location: https://github.com/NorfairKing/cursor-dirforest++library+ exposed-modules:+ Cursor.DirForest+ Cursor.FileOrDir+ Cursor.Simple.DirForest+ other-modules:+ Paths_cursor_dirforest+ hs-source-dirs:+ src+ ghc-options: -Wall -fwarn-redundant-constraints+ build-depends:+ base >=4.7 && <5+ , containers+ , cursor+ , deepseq+ , dirforest+ , filepath+ , microlens+ , path+ , text+ , validity+ , validity-path+ default-language: Haskell2010
+ src/Cursor/DirForest.hs view
@@ -0,0 +1,411 @@+{-# LANGUAGE DeriveFunctor #-}+{-# LANGUAGE DeriveGeneric #-}+{-# LANGUAGE LambdaCase #-}+{-# LANGUAGE QuasiQuotes #-}+{-# LANGUAGE RankNTypes #-}+{-# LANGUAGE ScopedTypeVariables #-}++module Cursor.DirForest+ ( -- * Types+ DirForestCursor (..),+ FileOrDirCursor (..),+ FileOrDir (..),++ -- * Construction and deconstruction++ -- ** Make+ makeDirForestCursor,++ -- ** Rebuild+ rebuildDirForestCursor,+ isTopLevel,++ -- *** Helper+ dirForestCursorPrepareForMovement,++ -- ** Lenses+ dirForestCursorForestCursorL,+ dirForestCursorSelectedL,++ -- ** Fold+ foldDirForestCursor,++ -- * Query+ dirForestCursorSelected,++ -- * Movements+ dirForestCursorSelectPrevOnSameLevel,+ dirForestCursorSelectNextOnSameLevel,+ dirForestCursorSelectFirstOnSameLevel,+ dirForestCursorSelectLastOnSameLevel,+ dirForestCursorSelectPrevTree,+ dirForestCursorSelectNextTree,+ dirForestCursorSelectFirstTree,+ dirForestCursorSelectLastTree,+ dirForestCursorSelectPrev,+ dirForestCursorSelectNext,+ dirForestCursorSelectFirst,+ dirForestCursorSelectLast,+ dirForestCursorSelectFirstChild,+ dirForestCursorSelectLastChild,+ dirForestCursorSelectParent,+ dirForestCursorSelectPrevChar,+ dirForestCursorSelectNextChar,++ -- * Edits+ dirForestCursorDeleteCurrent,+ dirForestCursorStartNew,+ dirForestCursorStartNewBelowAtStart,+ dirForestCursorStartNewBelowAtEnd,+ dirForestCursorStopNew,+ dirForestCursorInsertChar,+ dirForestCursorAppendChar,+ dirForestCursorRemoveChar,+ dirForestCursorDeleteChar,+ dirForestCursorCompleteToDir,+ dirForestCursorCompleteToDir',+ dirForestCursorCompleteToFile,+ dirForestCursorCompleteToFile',++ -- * Collapsing++ -- ** One level+ dirForestCursorOpen,+ dirForestCursorClose,+ dirForestCursorToggle,++ -- ** Recursively+ dirForestCursorOpenRecursively,+ dirForestCursorToggleRecursively,+ )+where++import Control.Applicative+import Control.DeepSeq+import Cursor.FileOrDir+import Cursor.Forest+import Cursor.List.NonEmpty+import Cursor.Text+import Cursor.Tree+import Cursor.Types+import Data.DirForest (DirForest (..), DirTree (..))+import Data.Functor.Identity+import qualified Data.List.NonEmpty as NE+import qualified Data.Map as M+import Data.Maybe+import Data.Tree+import Data.Validity+import GHC.Generics (Generic)+import Lens.Micro+import Path+import Path.Internal+import qualified System.FilePath as FP++-- | A cursor for a dirforest.+--+-- This cursor is necessarily nonempty.+-- If you need a dirforestcursor that can be empty, wrap it in a Maybe+--+-- A user can look at any file or directory in the forest.+--+-- Internally this cursor is represented as a forest cursor of (either file (with contents) or directory (without contents)).+-- This means that files must not have children.+newtype DirForestCursor a b = DirForestCursor {dirForestCursorForestCursor :: ForestCursor (FileOrDirCursor a) (FileOrDir b)}+ deriving (Show, Eq, Generic)++instance (Validity a, Validity b) => Validity (DirForestCursor a b) where+ validate dfc =+ mconcat+ [ genericValidate dfc,+ decorate "The forest cursor" $+ let fc = dirForestCursorForestCursor dfc+ in decorate "The nonempty cursor" $+ let nec = forestCursorListCursor fc+ goTree :: Tree (FileOrDir s) -> Validation+ goTree (Node a f) =+ mconcat+ [ declare "If the node is a file, then the forest below is empty" $+ case a of+ FodFile _ _ -> case f of+ [] -> True+ _ -> False+ FodDir _ -> True,+ decorate "The Forest below" $ goForest f+ ]+ goForest :: Forest (FileOrDir s) -> Validation+ goForest f = decorateList f goTree+ goCTree :: CTree (FileOrDir s) -> Validation+ goCTree (CNode a cf) =+ mconcat+ [ declare "If the node is a file, then the forest below is empty" $+ case a of+ FodFile _ _ -> case cf of+ EmptyCForest -> True+ _ -> False+ FodDir _ -> True,+ decorate "The CForest below" $ goCForest cf+ ]+ goCForest :: CForest (FileOrDir s) -> Validation+ goCForest = \case+ EmptyCForest -> mempty+ ClosedForest net -> decorateList (NE.toList net) goTree+ OpenForest nect -> decorateList (NE.toList nect) goCTree+ in mconcat+ [ decorate "The prev trees" $ mconcat $ map goCTree $ nonEmptyCursorPrev nec,+ decorate "The current tree" $+ let tc = nonEmptyCursorCurrent nec+ goTreeAbove :: TreeAbove (FileOrDir s) -> Validation+ goTreeAbove ta =+ mconcat+ [ decorate "The treeAboveLefts" $ decorateList (treeAboveLefts ta) goCTree,+ decorate "The treeAboveRights" $ decorateList (treeAboveRights ta) goCTree,+ declare "The treeAboveNode is a dir" $ case treeAboveNode ta of+ FodDir _ -> True+ _ -> False,+ decorate "The treeAboveAbove recursively" $ maybe mempty goTreeAbove $ treeAboveAbove ta+ ]+ in mconcat+ [ decorate "The treeAbove" $ maybe mempty goTreeAbove $ treeAbove tc,+ declare "If the currently selected node is a file, then the forest below is empty" $+ case treeCurrent tc of+ InProgress _ -> True+ Existent (FodFile _ _) -> case treeBelow tc of+ EmptyCForest -> True+ _ -> False+ Existent (FodDir _) -> True,+ decorate "The treeBelow" $ goCForest $ treeBelow tc+ ],+ decorate "The next trees" $ mconcat $ map goCTree $ nonEmptyCursorNext nec+ -- TODO the names need to be unique+ ]+ ]++instance (NFData a, NFData b) => NFData (DirForestCursor a b)++dirForestCursorForestCursorL :: Lens' (DirForestCursor a b) (ForestCursor (FileOrDirCursor a) (FileOrDir b))+dirForestCursorForestCursorL = lens dirForestCursorForestCursor $ \dfc mc -> dfc {dirForestCursorForestCursor = mc}++-- | The selected 'FileOrDir'.+--+-- Note that its path will only be the last piece, not the entire path.+-- If you need the entire piece, see 'dirForestCursorSelected' instead.+dirForestCursorSelectedL :: Lens' (DirForestCursor a b) (FileOrDirCursor a)+dirForestCursorSelectedL = dirForestCursorForestCursorL . forestCursorSelectedTreeL . treeCursorCurrentL++-- | Make a 'DirForestCursor'.+--+-- This will fail if the dirforest is empty.+makeDirForestCursor :: (b -> a) -> DirForest b -> Maybe (DirForestCursor a b)+makeDirForestCursor func = fmap (DirForestCursor . makeForestCursor (fmap func . makeFileOrDirCursor) . NE.map makeCTree) . NE.nonEmpty . toForest+ where+ toForest :: DirForest b -> Forest (FileOrDir b)+ toForest = goDF+ where+ goDF :: DirForest b -> Forest (FileOrDir b)+ goDF = map (uncurry goDT) . M.toList . unDirForest+ goDT :: FilePath -> DirTree b -> Tree (FileOrDir b)+ goDT f dt = case dt of+ NodeFile v -> Node (FodFile (Path f) v) []+ NodeDir df -> Node (FodDir (Path $ FP.addTrailingPathSeparator f)) (goDF df)++dirForestCursorPrepareForMovement :: (b -> a) -> DirForestCursor a b -> DeleteOrUpdate (ForestCursor (FileOrDir a) (FileOrDir b))+dirForestCursorPrepareForMovement g dfc =+ fmap (mapForestCursor (fromJust . rebuildFileOrDirCursor) id)+ $ ( case dfc ^. dirForestCursorSelectedL of+ InProgress _ -> forestCursorRemoveElem (fmap g . makeFileOrDirCursor)+ Existent _ -> Updated+ )+ $ dirForestCursorForestCursor dfc++rebuildDirForestCursor :: (a -> b) -> (b -> a) -> DirForestCursor a b -> DeleteOrUpdate (DirForest b)+rebuildDirForestCursor f g = fmap (fromForest . NE.toList . NE.map rebuildCTree . rebuildForestCursor (fmap f)) . dirForestCursorPrepareForMovement g+ where+ fromForest :: Forest (FileOrDir b) -> DirForest b+ fromForest = goF+ where+ goF :: Forest (FileOrDir b) -> DirForest b+ goF = DirForest . M.fromList . map goT+ goT :: Tree (FileOrDir b) -> (FilePath, DirTree b)+ goT (Node fod forest) = case fod of+ FodFile rf v -> (fromRelFile rf, NodeFile v)+ FodDir rd -> (FP.dropTrailingPathSeparator $ fromRelDir rd, NodeDir $ goF forest)++foldDirForestCursor :: ([CTree (FileOrDir b)] -> TreeCursor (FileOrDirCursor a) (FileOrDir b) -> [CTree (FileOrDir b)] -> c) -> DirForestCursor a b -> c+foldDirForestCursor func (DirForestCursor fc) = foldForestCursor func fc++dirForestCursorSelected :: DirForestCursor a b -> (Path Rel Dir, FileOrDirCursor a)+dirForestCursorSelected dfc =+ let tc = dfc ^. dirForestCursorForestCursorL . forestCursorSelectedTreeL+ goMAbove :: (Path Rel Dir, c) -> Maybe (TreeAbove (FileOrDir b)) -> (Path Rel Dir, c)+ goMAbove fod = \case+ Nothing -> fod+ Just ta -> goAbove fod ta+ goAbove :: (Path Rel Dir, c) -> TreeAbove (FileOrDir b) -> (Path Rel Dir, c)+ goAbove (d, fod) ta = case treeAboveNode ta of+ FodFile _ _ -> goMAbove (d, fod) (treeAboveAbove ta) -- Should never happen. Valid DirForestCursors disallow this+ FodDir rp -> goMAbove (rp </> d, fod) (treeAboveAbove ta)+ in goMAbove ([reldir|./|], treeCurrent tc) (treeAbove tc)++doMovementF :: forall a b f. (Functor f) => (b -> a) -> (ForestCursor (FileOrDir a) (FileOrDir b) -> f (ForestCursor (FileOrDir a) (FileOrDir b))) -> DirForestCursor a b -> DeleteOrUpdate (f (DirForestCursor a b))+doMovementF g movementFunc =+ fmap+ ( fmap (DirForestCursor . mapForestCursor makeFileOrDirCursor id)+ . movementFunc+ )+ . dirForestCursorPrepareForMovement g++doMovement :: forall a b. (b -> a) -> (ForestCursor (FileOrDir a) (FileOrDir b) -> ForestCursor (FileOrDir a) (FileOrDir b)) -> DirForestCursor a b -> DeleteOrUpdate (DirForestCursor a b)+doMovement g func = fmap runIdentity . doMovementF g (Identity . func)++dirForestCursorSelectPrevOnSameLevel :: (a -> b) -> (b -> a) -> DirForestCursor a b -> DeleteOrUpdate (Maybe (DirForestCursor a b))+dirForestCursorSelectPrevOnSameLevel f g = doMovementF g (forestCursorSelectPrevOnSameLevel (fmap f) (fmap g))++dirForestCursorSelectNextOnSameLevel :: (a -> b) -> (b -> a) -> DirForestCursor a b -> DeleteOrUpdate (Maybe (DirForestCursor a b))+dirForestCursorSelectNextOnSameLevel f g = doMovementF g $ forestCursorSelectNextOnSameLevel (fmap f) (fmap g)++dirForestCursorSelectFirstOnSameLevel :: (a -> b) -> (b -> a) -> DirForestCursor a b -> DeleteOrUpdate (DirForestCursor a b)+dirForestCursorSelectFirstOnSameLevel f g = doMovement g $ forestCursorSelectFirstOnSameLevel (fmap f) (fmap g)++dirForestCursorSelectLastOnSameLevel :: (a -> b) -> (b -> a) -> DirForestCursor a b -> DeleteOrUpdate (DirForestCursor a b)+dirForestCursorSelectLastOnSameLevel f g = doMovement g $ forestCursorSelectLastOnSameLevel (fmap f) (fmap g)++dirForestCursorSelectPrevTree :: (a -> b) -> (b -> a) -> DirForestCursor a b -> DeleteOrUpdate (Maybe (DirForestCursor a b))+dirForestCursorSelectPrevTree f g = doMovementF g $ forestCursorSelectPrevTreeCursor (fmap f) (fmap g)++dirForestCursorSelectNextTree :: (a -> b) -> (b -> a) -> DirForestCursor a b -> DeleteOrUpdate (Maybe (DirForestCursor a b))+dirForestCursorSelectNextTree f g = doMovementF g $ forestCursorSelectNextTreeCursor (fmap f) (fmap g)++dirForestCursorSelectFirstTree :: (a -> b) -> (b -> a) -> DirForestCursor a b -> DeleteOrUpdate (DirForestCursor a b)+dirForestCursorSelectFirstTree f g = doMovement g $ forestCursorSelectFirstTreeCursor (fmap f) (fmap g)++dirForestCursorSelectLastTree :: (a -> b) -> (b -> a) -> DirForestCursor a b -> DeleteOrUpdate (DirForestCursor a b)+dirForestCursorSelectLastTree f g = doMovement g $ forestCursorSelectLastTreeCursor (fmap f) (fmap g)++dirForestCursorSelectPrev :: (a -> b) -> (b -> a) -> DirForestCursor a b -> DeleteOrUpdate (Maybe (DirForestCursor a b))+dirForestCursorSelectPrev f g = doMovementF g $ forestCursorSelectPrev (fmap f) (fmap g)++dirForestCursorSelectNext :: (a -> b) -> (b -> a) -> DirForestCursor a b -> DeleteOrUpdate (Maybe (DirForestCursor a b))+dirForestCursorSelectNext f g = doMovementF g $ forestCursorSelectNext (fmap f) (fmap g)++dirForestCursorSelectFirst :: (a -> b) -> (b -> a) -> DirForestCursor a b -> DeleteOrUpdate (DirForestCursor a b)+dirForestCursorSelectFirst f g = doMovement g $ forestCursorSelectFirst (fmap f) (fmap g)++dirForestCursorSelectLast :: (a -> b) -> (b -> a) -> DirForestCursor a b -> DeleteOrUpdate (DirForestCursor a b)+dirForestCursorSelectLast f g = doMovement g $ forestCursorSelectLast (fmap f) (fmap g)++dirForestCursorSelectFirstChild :: (a -> b) -> (b -> a) -> DirForestCursor a b -> DeleteOrUpdate (Maybe (DirForestCursor a b))+dirForestCursorSelectFirstChild f g = doMovementF g $ forestCursorSelectBelowAtStart (fmap f) (fmap g)++dirForestCursorSelectLastChild :: (a -> b) -> (b -> a) -> DirForestCursor a b -> DeleteOrUpdate (Maybe (DirForestCursor a b))+dirForestCursorSelectLastChild f g = doMovementF g $ forestCursorSelectBelowAtEnd (fmap f) (fmap g)++dirForestCursorSelectParent :: (a -> b) -> (b -> a) -> DirForestCursor a b -> DeleteOrUpdate (Maybe (DirForestCursor a b))+dirForestCursorSelectParent f g = doMovementF g $ forestCursorSelectAbove (fmap f) (fmap g)++dirForestCursorSelectPrevChar :: DirForestCursor a b -> Maybe (DirForestCursor a b)+dirForestCursorSelectPrevChar = dirForestCursorSelectedL fileOrDirCursorSelectPrevChar++dirForestCursorSelectNextChar :: DirForestCursor a b -> Maybe (DirForestCursor a b)+dirForestCursorSelectNextChar = dirForestCursorSelectedL fileOrDirCursorSelectNextChar++dirForestCursorDeleteCurrent :: (b -> a) -> DirForestCursor a b -> DeleteOrUpdate (DirForestCursor a b)+dirForestCursorDeleteCurrent g = dirForestCursorForestCursorL $ forestCursorDeleteSubTree (fmap g . makeFileOrDirCursor)++dirForestCursorStartNew :: forall a b. (a -> b) -> (b -> a) -> Maybe (DirForestCursor a b) -> Maybe (DirForestCursor a b)+dirForestCursorStartNew f g =+ let newNode = InProgress emptyTextCursor+ new = DirForestCursor $ singletonForestCursor newNode+ in \case+ Nothing -> Just new+ Just dfc -> case dfc ^. dirForestCursorSelectedL of+ InProgress _ -> Nothing+ Existent _ ->+ Just $ case dirForestCursorPrepareForMovement g dfc of+ Deleted -> new+ Updated fc ->+ DirForestCursor $+ forestCursorAppendNodeSingleAndSelect (fmap f . fromJust . rebuildFileOrDirCursor) newNode $+ mapForestCursor+ Existent+ id+ fc++dirForestCursorStartNewBelowAtStart :: forall a b. (a -> b) -> (b -> a) -> DirForestCursor a b -> Maybe (DirForestCursor a b)+dirForestCursorStartNewBelowAtStart f g dfc = case dfc ^. dirForestCursorSelectedL of+ InProgress _ -> Nothing+ Existent (FodFile _ _) -> Nothing+ Existent (FodDir _) -> case dirForestCursorPrepareForMovement g dfc of+ Deleted -> Nothing -- Should not happen+ Updated fc ->+ Just $+ DirForestCursor $+ forestCursorAddChildNodeSingleToNodeAtStartAndSelect (fmap f . fromJust . rebuildFileOrDirCursor) (InProgress emptyTextCursor) $+ mapForestCursor+ Existent+ id+ fc++dirForestCursorStartNewBelowAtEnd :: forall a b. (a -> b) -> (b -> a) -> DirForestCursor a b -> Maybe (DirForestCursor a b)+dirForestCursorStartNewBelowAtEnd f g dfc = case dfc ^. dirForestCursorSelectedL of+ InProgress _ -> Nothing+ Existent (FodFile _ _) -> Nothing+ Existent (FodDir _) -> case dirForestCursorPrepareForMovement g dfc of+ Deleted -> Nothing -- Should not happen+ Updated fc ->+ Just $+ DirForestCursor $+ forestCursorAddChildNodeSingleToNodeAtEndAndSelect (fmap f . fromJust . rebuildFileOrDirCursor) (InProgress emptyTextCursor) $+ mapForestCursor+ Existent+ id+ fc++dirForestCursorStopNew :: (b -> a) -> DirForestCursor a b -> Maybe (DeleteOrUpdate (DirForestCursor a b))+dirForestCursorStopNew g dfc = case dfc ^. dirForestCursorSelectedL of+ InProgress _ -> Just $ dirForestCursorDeleteCurrent g dfc+ _ -> Nothing++dirForestCursorInsertChar :: Char -> DirForestCursor a b -> Maybe (DirForestCursor a b)+dirForestCursorInsertChar c = dirForestCursorSelectedL $ fileOrDirCursorInsertChar c++dirForestCursorAppendChar :: Char -> DirForestCursor a b -> Maybe (DirForestCursor a b)+dirForestCursorAppendChar c = dirForestCursorSelectedL $ fileOrDirCursorAppendChar c++dirForestCursorRemoveChar :: (b -> a) -> DirForestCursor a b -> Maybe (DeleteOrUpdate (DirForestCursor a b))+dirForestCursorRemoveChar g dfc = Updated <$> dirForestCursorSelectedL (dullMDelete . fileOrDirCursorRemoveChar) dfc <|> dirForestCursorStopNew g dfc++dirForestCursorDeleteChar :: (b -> a) -> DirForestCursor a b -> Maybe (DeleteOrUpdate (DirForestCursor a b))+dirForestCursorDeleteChar g dfc = Updated <$> dirForestCursorSelectedL (dullMDelete . fileOrDirCursorDeleteChar) dfc <|> dirForestCursorStopNew g dfc++dirForestCursorCompleteToDir :: DirForestCursor a b -> Maybe (Path Rel Dir, DirForestCursor a b)+dirForestCursorCompleteToDir = dirForestCursorCompleteToDir' pure++dirForestCursorCompleteToDir' :: (Path Rel Dir -> Maybe (Path Rel Dir)) -> DirForestCursor a b -> Maybe (Path Rel Dir, DirForestCursor a b)+dirForestCursorCompleteToDir' func dfc = do+ (rd, fodc) <- fileOrDirCursorCompleteToDir' func $ dfc ^. dirForestCursorSelectedL+ let dfc' = dfc & dirForestCursorSelectedL .~ fodc+ pure (rd, dfc')++dirForestCursorCompleteToFile :: a -> DirForestCursor a b -> Maybe (Path Rel File, DirForestCursor a b)+dirForestCursorCompleteToFile = dirForestCursorCompleteToFile' pure++dirForestCursorCompleteToFile' :: (Path Rel File -> Maybe (Path Rel File)) -> a -> DirForestCursor a b -> Maybe (Path Rel File, DirForestCursor a b)+dirForestCursorCompleteToFile' func a dfc = do+ (rf, fodc) <- fileOrDirCursorCompleteToFile' func a $ dfc ^. dirForestCursorSelectedL+ let dfc' = dfc & dirForestCursorSelectedL .~ fodc+ pure (rf, dfc')++dirForestCursorOpen :: DirForestCursor a b -> Maybe (DirForestCursor a b)+dirForestCursorOpen = dirForestCursorForestCursorL forestCursorOpenCurrentForest++dirForestCursorClose :: DirForestCursor a b -> Maybe (DirForestCursor a b)+dirForestCursorClose = dirForestCursorForestCursorL forestCursorCloseCurrentForest++dirForestCursorToggle :: DirForestCursor a b -> Maybe (DirForestCursor a b)+dirForestCursorToggle = dirForestCursorForestCursorL forestCursorToggleCurrentForest++dirForestCursorOpenRecursively :: DirForestCursor a b -> Maybe (DirForestCursor a b)+dirForestCursorOpenRecursively = dirForestCursorForestCursorL forestCursorOpenCurrentForestRecursively++dirForestCursorToggleRecursively :: DirForestCursor a b -> Maybe (DirForestCursor a b)+dirForestCursorToggleRecursively = dirForestCursorForestCursorL forestCursorToggleCurrentForestRecursively
+ src/Cursor/FileOrDir.hs view
@@ -0,0 +1,119 @@+{-# LANGUAGE DeriveFunctor #-}+{-# LANGUAGE DeriveGeneric #-}+{-# LANGUAGE LambdaCase #-}+{-# LANGUAGE QuasiQuotes #-}++module Cursor.FileOrDir where++import Control.DeepSeq+import Cursor.Text+import Cursor.Types+import qualified Data.Text as T+import Data.Validity+import Data.Validity.Path ()+import GHC.Generics (Generic)+import Path++data FileOrDirCursor a+ = Existent (FileOrDir a)+ | InProgress TextCursor+ deriving (Show, Eq, Generic, Functor)++instance (Validity a) => Validity (FileOrDirCursor a)++instance (NFData a) => NFData (FileOrDirCursor a)++makeFileOrDirCursor :: FileOrDir a -> FileOrDirCursor a+makeFileOrDirCursor = Existent++rebuildFileOrDirCursor :: FileOrDirCursor a -> Maybe (FileOrDir a)+rebuildFileOrDirCursor = \case+ Existent fod -> Just fod+ InProgress _ -> Nothing++fileOrDirCursorInsertChar :: Char -> FileOrDirCursor a -> Maybe (FileOrDirCursor a)+fileOrDirCursorInsertChar c = \case+ Existent _ -> Nothing+ InProgress tc -> InProgress <$> textCursorInsert c tc++fileOrDirCursorAppendChar :: Char -> FileOrDirCursor a -> Maybe (FileOrDirCursor a)+fileOrDirCursorAppendChar c = \case+ Existent _ -> Nothing+ InProgress tc -> InProgress <$> textCursorAppend c tc++fileOrDirCursorRemoveChar :: FileOrDirCursor a -> Maybe (DeleteOrUpdate (FileOrDirCursor a))+fileOrDirCursorRemoveChar = \case+ Existent _ -> Nothing+ InProgress tc -> fmap InProgress <$> textCursorRemove tc++fileOrDirCursorDeleteChar :: FileOrDirCursor a -> Maybe (DeleteOrUpdate (FileOrDirCursor a))+fileOrDirCursorDeleteChar = \case+ Existent _ -> Nothing+ InProgress tc -> fmap InProgress <$> textCursorDelete tc++fileOrDirCursorSelectPrevChar :: FileOrDirCursor a -> Maybe (FileOrDirCursor a)+fileOrDirCursorSelectPrevChar = \case+ Existent _ -> Nothing+ InProgress tc -> InProgress <$> textCursorSelectPrev tc++fileOrDirCursorSelectNextChar :: FileOrDirCursor a -> Maybe (FileOrDirCursor a)+fileOrDirCursorSelectNextChar = \case+ Existent _ -> Nothing+ InProgress tc -> InProgress <$> textCursorSelectNext tc++fileOrDirCursorCompleteToDir :: FileOrDirCursor a -> Maybe (Path Rel Dir, FileOrDirCursor a)+fileOrDirCursorCompleteToDir = fileOrDirCursorCompleteToDir' pure++fileOrDirCursorCompleteToDir' :: (Path Rel Dir -> Maybe (Path Rel Dir)) -> FileOrDirCursor a -> Maybe (Path Rel Dir, FileOrDirCursor a)+fileOrDirCursorCompleteToDir' func = \case+ Existent _ -> Nothing+ InProgress tc -> case completeTextCursorToDir tc of+ Nothing -> Nothing+ Just rd ->+ if isTopLevel rd+ then do+ rd' <- func rd+ if isTopLevel rd'+ then pure (rd', Existent $ FodDir rd')+ else Nothing+ else Nothing++completeTextCursorToDir :: TextCursor -> Maybe (Path Rel Dir)+completeTextCursorToDir = parseRelDir . T.unpack . rebuildTextCursor++fileOrDirCursorCompleteToFile :: a -> FileOrDirCursor a -> Maybe (Path Rel File, FileOrDirCursor a)+fileOrDirCursorCompleteToFile = fileOrDirCursorCompleteToFile' pure++fileOrDirCursorCompleteToFile' :: (Path Rel File -> Maybe (Path Rel File)) -> a -> FileOrDirCursor a -> Maybe (Path Rel File, FileOrDirCursor a)+fileOrDirCursorCompleteToFile' func a = \case+ Existent _ -> Nothing+ InProgress tc -> case completeTextCursorToFile tc of+ Nothing -> Nothing+ Just rf ->+ if isTopLevel rf+ then do+ rf' <- func rf+ if isTopLevel rf'+ then Just (rf', Existent $ FodFile rf' a)+ else Nothing+ else Nothing++completeTextCursorToFile :: TextCursor -> Maybe (Path Rel File)+completeTextCursorToFile = parseRelFile . T.unpack . rebuildTextCursor++data FileOrDir a = FodFile (Path Rel File) a | FodDir (Path Rel Dir)+ deriving (Show, Eq, Generic, Functor)++instance (Validity a) => Validity (FileOrDir a) where+ validate fod =+ mconcat+ [ genericValidate fod,+ declare "The path is toplevel" $ case fod of+ FodFile rf _ -> isTopLevel rf+ FodDir rd -> isTopLevel rd+ ]++instance (NFData a) => NFData (FileOrDir a)++isTopLevel :: Path Rel t -> Bool+isTopLevel p_ = parent p_ == [reldir|./|]
+ src/Cursor/Simple/DirForest.hs view
@@ -0,0 +1,159 @@+module Cursor.Simple.DirForest+ ( -- * Types+ DirForestCursor,+ DFC.dirForestCursorForestCursor,+ DFC.FileOrDir (..),++ -- * Construction and deconstruction++ -- ** Make+ makeDirForestCursor,++ -- ** Rebuild+ rebuildDirForestCursor,+ DFC.isTopLevel,++ -- *** Helper+ dirForestCursorPrepareForMovement,++ -- ** Lenses+ DFC.dirForestCursorForestCursorL,+ DFC.dirForestCursorSelectedL,++ -- ** Fold+ DFC.foldDirForestCursor,++ -- * Query+ DFC.dirForestCursorSelected,++ -- * Movements+ dirForestCursorSelectPrevOnSameLevel,+ dirForestCursorSelectNextOnSameLevel,+ dirForestCursorSelectFirstOnSameLevel,+ dirForestCursorSelectLastOnSameLevel,+ dirForestCursorSelectPrevTree,+ dirForestCursorSelectNextTree,+ dirForestCursorSelectFirstTree,+ dirForestCursorSelectLastTree,+ dirForestCursorSelectPrev,+ dirForestCursorSelectNext,+ dirForestCursorSelectFirst,+ dirForestCursorSelectLast,+ dirForestCursorSelectFirstChild,+ dirForestCursorSelectLastChild,+ dirForestCursorSelectParent,+ DFC.dirForestCursorSelectPrevChar,+ DFC.dirForestCursorSelectNextChar,++ -- * Edits+ dirForestCursorDeleteCurrent,+ dirForestCursorStartNew,+ dirForestCursorStartNewBelowAtStart,+ dirForestCursorStartNewBelowAtEnd,+ dirForestCursorStopNew,+ DFC.dirForestCursorInsertChar,+ DFC.dirForestCursorAppendChar,+ dirForestCursorRemoveChar,+ dirForestCursorDeleteChar,+ DFC.dirForestCursorCompleteToDir,+ DFC.dirForestCursorCompleteToDir',+ DFC.dirForestCursorCompleteToFile,+ DFC.dirForestCursorCompleteToFile',++ -- * Collapsing++ -- ** One level+ DFC.dirForestCursorOpen,+ DFC.dirForestCursorClose,+ DFC.dirForestCursorToggle,++ -- ** Recursively+ DFC.dirForestCursorOpenRecursively,+ DFC.dirForestCursorToggleRecursively,+ )+where++import qualified Cursor.DirForest as DFC+import Cursor.Simple.Forest+import Cursor.Types+import Data.DirForest (DirForest (..))++type DirForestCursor a = DFC.DirForestCursor a a++-- | Make a 'DirForestCursor'.+--+-- This will fail if the dirforest is empty.+makeDirForestCursor :: DirForest a -> Maybe (DirForestCursor a)+makeDirForestCursor = DFC.makeDirForestCursor id++dirForestCursorPrepareForMovement :: DirForestCursor a -> DeleteOrUpdate (ForestCursor (DFC.FileOrDir a))+dirForestCursorPrepareForMovement = DFC.dirForestCursorPrepareForMovement id++rebuildDirForestCursor :: DirForestCursor a -> DeleteOrUpdate (DirForest a)+rebuildDirForestCursor = DFC.rebuildDirForestCursor id id++dirForestCursorSelectPrevOnSameLevel :: DirForestCursor a -> DeleteOrUpdate (Maybe (DirForestCursor a))+dirForestCursorSelectPrevOnSameLevel = DFC.dirForestCursorSelectPrevOnSameLevel id id++dirForestCursorSelectNextOnSameLevel :: DirForestCursor a -> DeleteOrUpdate (Maybe (DirForestCursor a))+dirForestCursorSelectNextOnSameLevel = DFC.dirForestCursorSelectNextOnSameLevel id id++dirForestCursorSelectFirstOnSameLevel :: DirForestCursor a -> DeleteOrUpdate (DirForestCursor a)+dirForestCursorSelectFirstOnSameLevel = DFC.dirForestCursorSelectFirstOnSameLevel id id++dirForestCursorSelectLastOnSameLevel :: DirForestCursor a -> DeleteOrUpdate (DirForestCursor a)+dirForestCursorSelectLastOnSameLevel = DFC.dirForestCursorSelectLastOnSameLevel id id++dirForestCursorSelectPrevTree :: DirForestCursor a -> DeleteOrUpdate (Maybe (DirForestCursor a))+dirForestCursorSelectPrevTree = DFC.dirForestCursorSelectPrevTree id id++dirForestCursorSelectNextTree :: DirForestCursor a -> DeleteOrUpdate (Maybe (DirForestCursor a))+dirForestCursorSelectNextTree = DFC.dirForestCursorSelectNextTree id id++dirForestCursorSelectFirstTree :: DirForestCursor a -> DeleteOrUpdate (DirForestCursor a)+dirForestCursorSelectFirstTree = DFC.dirForestCursorSelectFirstTree id id++dirForestCursorSelectLastTree :: DirForestCursor a -> DeleteOrUpdate (DirForestCursor a)+dirForestCursorSelectLastTree = DFC.dirForestCursorSelectLastTree id id++dirForestCursorSelectPrev :: DirForestCursor a -> DeleteOrUpdate (Maybe (DirForestCursor a))+dirForestCursorSelectPrev = DFC.dirForestCursorSelectPrev id id++dirForestCursorSelectNext :: DirForestCursor a -> DeleteOrUpdate (Maybe (DirForestCursor a))+dirForestCursorSelectNext = DFC.dirForestCursorSelectNext id id++dirForestCursorSelectFirst :: DirForestCursor a -> DeleteOrUpdate (DirForestCursor a)+dirForestCursorSelectFirst = DFC.dirForestCursorSelectFirst id id++dirForestCursorSelectLast :: DirForestCursor a -> DeleteOrUpdate (DirForestCursor a)+dirForestCursorSelectLast = DFC.dirForestCursorSelectLast id id++dirForestCursorSelectFirstChild :: DirForestCursor a -> DeleteOrUpdate (Maybe (DirForestCursor a))+dirForestCursorSelectFirstChild = DFC.dirForestCursorSelectFirstChild id id++dirForestCursorSelectLastChild :: DirForestCursor a -> DeleteOrUpdate (Maybe (DirForestCursor a))+dirForestCursorSelectLastChild = DFC.dirForestCursorSelectLastChild id id++dirForestCursorSelectParent :: DirForestCursor a -> DeleteOrUpdate (Maybe (DirForestCursor a))+dirForestCursorSelectParent = DFC.dirForestCursorSelectParent id id++dirForestCursorDeleteCurrent :: DirForestCursor a -> DeleteOrUpdate (DirForestCursor a)+dirForestCursorDeleteCurrent = DFC.dirForestCursorDeleteCurrent id++dirForestCursorStartNew :: Maybe (DirForestCursor a) -> Maybe (DirForestCursor a)+dirForestCursorStartNew = DFC.dirForestCursorStartNew id id++dirForestCursorStartNewBelowAtStart :: DirForestCursor a -> Maybe (DirForestCursor a)+dirForestCursorStartNewBelowAtStart = DFC.dirForestCursorStartNewBelowAtStart id id++dirForestCursorStartNewBelowAtEnd :: DirForestCursor a -> Maybe (DirForestCursor a)+dirForestCursorStartNewBelowAtEnd = DFC.dirForestCursorStartNewBelowAtEnd id id++dirForestCursorStopNew :: DirForestCursor a -> Maybe (DeleteOrUpdate (DirForestCursor a))+dirForestCursorStopNew = DFC.dirForestCursorStopNew id++dirForestCursorRemoveChar :: DirForestCursor a -> Maybe (DeleteOrUpdate (DirForestCursor a))+dirForestCursorRemoveChar = DFC.dirForestCursorRemoveChar id++dirForestCursorDeleteChar :: DirForestCursor a -> Maybe (DeleteOrUpdate (DirForestCursor a))+dirForestCursorDeleteChar = DFC.dirForestCursorDeleteChar id