Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MonadFail proposal #1108

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions yi-core/src/Yi/Buffer/Misc.hs
Original file line number Diff line number Diff line change
Expand Up @@ -449,9 +449,10 @@ runBufferFull w b f =
MarkSet { insMark = MarkValue 0 Forward,
selMark = MarkValue 0 Backward, -- sel
fromMark = MarkValue 0 Backward } -- from
else do
Just mrks <- uses winMarksA (M.lookup $ wkey (b ^. lastActiveWindowA))
forM mrks getMarkValueB
else uses winMarksA (M.lookup $ wkey (b ^. lastActiveWindowA))
>>= \case
Just mrks -> forM mrks getMarkValueB
Nothing -> error "runBufferFull: no marks from previous window"
newMrks <- forM newMarkValues newMarkB
winMarksA %= M.insert (wkey w) newMrks
lastActiveWindowA .= w
Expand Down Expand Up @@ -869,9 +870,10 @@ getInsMark :: BufferM Mark
getInsMark = insMark <$> askMarks

askMarks :: BufferM WinMarks
askMarks = do
Just !ms <- getMarks =<< ask
return ms
askMarks =
ask >>= getMarks >>= \case
Just !ms -> return ms
Nothing -> error "askMarks failed"

getMarkB :: Maybe String -> BufferM Mark
getMarkB m = do
Expand Down
4 changes: 3 additions & 1 deletion yi-core/src/Yi/Buffer/Region.hs
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,15 @@ inclusiveRegionB r =
-- a list of small regions form this block region.
blockifyRegion :: Region -> BufferM [Region]
blockifyRegion r = savingPointB $ do
[lowCol, highCol] <- sort <$> mapM colOf [regionStart r, regionEnd r]
(lowCol, highCol) <- curry sortTuple <$> colOf (regionStart r) <*> colOf (regionEnd r)
startLine <- lineOf $ regionStart r
endLine <- lineOf $ regionEnd r
when (startLine > endLine) $ fail "blockifyRegion: impossible"
mapM (\line -> mkRegion <$> pointOfLineColB line lowCol
<*> pointOfLineColB line (1 + highCol))
[startLine..endLine]
where
sortTuple (a,b) = if a < b then (a,b) else (b,a)

-- | Joins lines in the region with a single space, skipping any empty
-- lines.
Expand Down
10 changes: 6 additions & 4 deletions yi-core/src/Yi/Hoogle.hs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,12 @@ hoogle = do
wordRegion <- regionOfB unitWord
word <- readRegionB wordRegion
return (wordRegion, word)
((modl,fun):_) <- io $ hoogleFunModule word

withCurrentBuffer $ replaceRegionB wordRegion fun
return modl
funMods <- io $ hoogleFunModule word
case funMods of
((modl,fun):_) -> do
withCurrentBuffer $ replaceRegionB wordRegion fun
return modl
_ -> error "hoogle search failed"

-- | Call out to 'hoogleRaw', and print inside the Minibuffer the results of
-- searching Hoogle with the word at point.
Expand Down
8 changes: 6 additions & 2 deletions yi-core/src/Yi/Interact.hs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ module Yi.Interact
accepted
) where

import qualified Control.Monad.Fail as Fail
import Control.Applicative (Alternative ((<|>), empty))
import Control.Arrow (first)
import Lens.Micro.Platform (_1, _2, view)
Expand All @@ -80,7 +81,7 @@ import qualified Data.Text as T (Text, append, pack)
-- Classes

-- | Abstraction of monadic interactive processes
class (Eq w, Monad m, Alternative m, Applicative m, MonadPlus m) => MonadInteract m w e | m -> w e where
class (Eq w, Monad m, Alternative m, Applicative m, MonadPlus m,Fail.MonadFail m) => MonadInteract m w e | m -> w e where
write :: w -> m ()
-- ^ Outputs a result.
eventBounds :: Ord e => Maybe e -> Maybe e -> m e
Expand Down Expand Up @@ -124,9 +125,12 @@ instance Alternative (I ev w) where
empty = Fails
(<|>) = Plus

instance Fail.MonadFail (I event w) where
fail _ = Fails

instance Monad (I event w) where
return = Returns
fail _ = Fails
fail = Fail.fail
(>>=) = Binds

instance Eq w => MonadPlus (I event w) where
Expand Down
4 changes: 3 additions & 1 deletion yi-core/src/Yi/Rectangle.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ getRectangle :: BufferM (Region, Int, Int)
getRectangle = do
r <- getSelectRegionB
extR <- unitWiseRegion Line r
[lowCol,highCol] <- sort <$> mapM colOf [regionStart r, regionEnd r]
(lowCol,highCol) <- curry sortTuple <$> colOf (regionStart r) <*> colOf (regionEnd r)
return (extR, lowCol, highCol)
where
sortTuple (a,b) = if a < b then (a,b) else (b,a)

-- | Split text at the boundaries given
multiSplit :: [Int] -> R.YiString -> [R.YiString]
Expand Down
65 changes: 35 additions & 30 deletions yi-core/src/Yi/Search.hs
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,12 @@ isearchDelE = do
_ -> return () -- if the searched string is empty, don't try to remove chars from it.

isearchHistory :: Int -> EditorM ()
isearchHistory delta = do
Isearch ((current,_p0,_dir):_) <- getEditorDyn
h <- historyMoveGen iSearch delta (return current)
isearchFunE (const h)
isearchHistory delta =
getEditorDyn >>= \case
Isearch ((current,_p0,_dir):_) -> do
h <- historyMoveGen iSearch delta (return current)
isearchFunE (const h)
_ -> error "isearchHistory: invalid isearch state encountered"

isearchPrevE :: EditorM ()
isearchPrevE = isearchNext0 Backward
Expand All @@ -310,34 +312,37 @@ isearchNextE :: EditorM ()
isearchNextE = isearchNext0 Forward

isearchNext0 :: Direction -> EditorM ()
isearchNext0 newDir = do
Isearch ((current,_p0,_dir):_rest) <- getEditorDyn
if T.null current
then isearchHistory 1
else isearchNext newDir

isearchNext0 newDir =
getEditorDyn >>= \case
Isearch ((current,_p0,_dir):_rest) ->
if T.null current
then isearchHistory 1
else isearchNext newDir
_ -> error "isearchNext0: invalid isearch state encountered"

isearchNext :: Direction -> EditorM ()
isearchNext direction = do
Isearch ((current, p0, _dir) : rest) <- getEditorDyn
withCurrentBuffer $ moveTo (regionStart p0 + startOfs)
mp <- withCurrentBuffer $
regexB direction (makeISearch current)
case mp of
[] -> do
endPoint <- withCurrentBuffer $ do
moveTo (regionEnd p0) -- revert to offset we were before.
sizeB
printMsg "isearch: end of document reached"
let wrappedOfs = case direction of
Forward -> mkRegion 0 0
Backward -> mkRegion endPoint endPoint
putEditorDyn $ Isearch ((current,wrappedOfs,direction):rest) -- prepare to wrap around.
(p:_) -> do
withCurrentBuffer $
moveTo (regionEnd p)
printMsg $ "I-search: " <> current
putEditorDyn $ Isearch ((current,p,direction):rest)
isearchNext direction =
getEditorDyn >>= \case
Isearch ((current, p0, _dir) : rest) -> do
withCurrentBuffer $ moveTo (regionStart p0 + startOfs)
mp <- withCurrentBuffer $
regexB direction (makeISearch current)
case mp of
[] -> do
endPoint <- withCurrentBuffer $ do
moveTo (regionEnd p0) -- revert to offset we were before.
sizeB
printMsg "isearch: end of document reached"
let wrappedOfs = case direction of
Forward -> mkRegion 0 0
Backward -> mkRegion endPoint endPoint
putEditorDyn $ Isearch ((current,wrappedOfs,direction):rest) -- prepare to wrap around.
(p:_) -> do
withCurrentBuffer $
moveTo (regionEnd p)
printMsg $ "I-search: " <> current
putEditorDyn $ Isearch ((current,p,direction):rest)
_ -> error "isearchNext: invalid isearch state encountered"
where startOfs = case direction of
Forward -> 1
Backward -> -1
Expand Down