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

File events and sanity checks #64 #65

Open
wants to merge 2 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions rasa-ext-files/rasa-ext-files.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ library
hs-source-dirs: src
exposed-modules:
Rasa.Ext.Files
Rasa.Ext.Files.Internal.Events

build-depends: base >= 4.8 && < 5
, rasa
Expand All @@ -27,6 +28,9 @@ library
, text
, mtl
, yi-rope
, filepath
, directory

default-language: Haskell2010

default-extensions:
Expand Down
37 changes: 28 additions & 9 deletions rasa-ext-files/src/Rasa/Ext/Files.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
module Rasa.Ext.Files
( files
, save
-- Events
, onFileLoaded
, dispatchFileLoaded
, FileLoaded(..), Extension(..)
) where

import qualified Data.Text.IO as TIO
Expand All @@ -22,15 +26,20 @@ import Rasa.Ext
import Rasa.Ext.Views
import Rasa.Ext.Cmd

import Rasa.Ext.Files.Internal.Events

import qualified System.FilePath as FilePath
import System.Directory

-- | Stores filename
data FileInfo =
FileInfo (Maybe String)
FileInfo (Maybe FilePath)
deriving (Typeable, Show, Eq)

instance Default FileInfo where
def = FileInfo Nothing

type Filename = String
type Filename = FilePath

-- | Stores File status; Clean means all changes are saved
data FileStatus =
Expand Down Expand Up @@ -96,18 +105,28 @@ save = do
setFileStatus Clean

-- | Set the filename
setFilename :: String -> BufAction ()
setFilename :: FilePath -> BufAction ()
setFilename fname = setBufExt $ FileInfo (Just fname)

-- | Add a buffer for a file
addFile :: String -> Y.YiString -> App ()
addFile fname txt = do
newBuf <- addBuffer txt
bufDo_ newBuf (setFilename fname)
addFile :: FilePath -> Y.YiString -> App ()
addFile fname txt =
do
newBuf <- addBuffer txt
dispatchFileLoaded $ extOf fname newBuf
bufDo_ newBuf (setFilename fname)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should set the filename first THEN dispatch the event probably :) Remember that even handlers are processed synchronously in-line by default

where
extOf f buf = FileLoaded (safeExt f) buf
safeExt f = case FilePath.takeExtensions f of
ext -> Extension $ ext
"" -> UnknownExtension

-- | Load files from command line
loadFiles :: App ()
loadFiles = do
fileNames <- liftIO getArgs
fileTexts <- liftIO $ traverse TIO.readFile fileNames
mapM_ (uncurry addFile) $ zip fileNames (Y.fromText <$> fileTexts)
-- Silently filter out invalid filenames or unexisting files for now
validFileNames <- return $ filter FilePath.isValid fileNames
existingFileNames <- liftIO $ filterM doesFileExist validFileNames
fileTexts <- liftIO $ traverse TIO.readFile existingFileNames
mapM_ (uncurry addFile) $ zip existingFileNames (Y.fromText <$> fileTexts)
18 changes: 18 additions & 0 deletions rasa-ext-files/src/Rasa/Ext/Files/Internal/Events.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Rasa.Ext.Files.Internal.Events where

import Rasa.Ext

import Control.Monad

data Extension = Extension String | UnknownExtension deriving (Eq)

data FileLoaded
= FileLoaded Extension BufRef
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the FileLoaded event should probably just pass the whole filepath and the consumer can pull the extension out themselves if they like.


-- | Trigger an 'App' on a 'Keypress'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keypress -> FileLoaded event😛

onFileLoaded :: (FileLoaded -> App result) -> App ListenerId
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't plan to use the result for anything I'd prefer we just set it to () for clarity purposes.

onFileLoaded actionF = addListener (void <$> actionF)

-- | Dispatch a 'Keypress' event.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copy pasta 'FileLoaded event' 😋

dispatchFileLoaded :: FileLoaded -> App ()
dispatchFileLoaded = dispatchEvent