packages feed

zwirn-0.2.3.1: app/zwirnmill/Editor/Config.hs

module Editor.Config where

import Conferer.Config ((/.))
import Conferer.FromConfig (FromConfig (..), fetchFromConfig)
import Data.Maybe (fromMaybe)
import qualified Data.Text as T
import Data.Text.Zipper (textZipper)
import Editor.Core (EditorState (..), esTabWidth, esZipper, newEditor)
import Editor.File (loadFile)
import Lens.Micro

data EditorConfig
  = EditorConfig {editorConfigTabWidth :: Int, editorConfigPath :: Maybe FilePath, editorConfigContent :: Maybe T.Text}
  deriving (Eq, Show)

instance FromConfig EditorConfig where
  fromConfig key configSource = do
    r <- fetchFromConfig (key /. "tabwidth") configSource
    p <- fetchFromConfig (key /. "path") configSource
    cont <- fetchFromConfig (key /. "content") configSource
    return (EditorConfig (fromMaybe 4 r) p cont)

editorFromConfig :: EditorConfig -> IO EditorState
editorFromConfig (EditorConfig tw Nothing Nothing) = return $ newEditor & esTabWidth .~ tw
editorFromConfig (EditorConfig tw Nothing (Just c)) =
  return $
    newEditor
      & esTabWidth .~ tw
      & esZipper .~ textZipper (T.lines c) Nothing
editorFromConfig (EditorConfig tw (Just path) _) = do
  contents <- readFile path
  let es = loadFile path (T.pack contents)
  return $ es & esTabWidth .~ tw