Skip to content
Merged
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
37 changes: 3 additions & 34 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,21 @@ module Main (
main,
) where

import Control.Exception
import Core.Node (Node)
import Core.NodeCursor (newCursor)
import Data.Functor (($>))
import Data.Text (Text)
import Data.Text.Lazy.Encoding (encodeUtf8)
import Formatting (RuleSet, formatNode, newRuleSet)
import GHC.IO.Exception (IOErrorType (NoSuchThing), IOException (IOError))
import Parsing.DSL (parseDSL)
import Formatting (RuleSet, formatNode)
import Formatting.Config
import IOUtils
import Parsing.Jbeam (parseNodes)
import System.Environment (getArgs)
import Transformation (transform)

import Data.ByteString.Lazy qualified as BL (
ByteString,
readFile,
toStrict,
writeFile,
)
import Data.List qualified as L (uncons)
import Data.Text qualified as T (append, pack)
import Data.Text.IO qualified as TIO (putStrLn)
import Data.Text.Lazy qualified as TL (fromStrict)

Expand All @@ -46,28 +40,3 @@ processNodes nodes formattingConfig =
. formatNode formattingConfig newCursor
. transform
$ nodes

readFormattingConfig :: IO RuleSet
readFormattingConfig = do
contents <- tryReadFile [NoSuchThing] "rules.jbfl"
case contents >>= parseDSL . BL.toStrict of
Right rs -> pure rs
Left err -> TIO.putStrLn err $> newRuleSet

ioErrorMsg
:: [IOErrorType]
-> Either IOException BL.ByteString
-> Either Text BL.ByteString
ioErrorMsg noerrs (Left (IOError _ ioe_type _ ioe_desc _ filename)) =
if ioe_type `notElem` noerrs
then Left $ maybe "" appendColon filename `T.append` T.pack ioe_desc
else Right ""
where
appendColon f = T.pack f `T.append` ": "
ioErrorMsg _ (Right valid) = Right valid

tryReadFile :: [IOErrorType] -> FilePath -> IO (Either Text BL.ByteString)
tryReadFile noerrs fp = do
possiblyContent <-
try (BL.readFile fp) :: IO (Either IOException BL.ByteString)
pure $ ioErrorMsg noerrs possiblyContent
34 changes: 17 additions & 17 deletions examples/ast/jbfl/complex.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ RuleSet
( fromList
[ AnyObjectKey
, Selector
( ObjectKey "flexbodies" )
( ObjectKey "nodes" )
, AnyArrayIndex
, Selector
( ArrayIndex 0 )
]
)
, fromList
[
( SomeKey NoComplexNewLine
, SomeProperty NoComplexNewLine True
( SomeKey PadAmount
, SomeProperty PadAmount 8
)
]
)
Expand All @@ -23,12 +25,17 @@ RuleSet
, Selector
( ObjectKey "nodes" )
, AnyArrayIndex
, AnyArrayIndex
]
)
, fromList
[
( SomeKey NoComplexNewLine
, SomeProperty NoComplexNewLine True
( SomeKey PadAmount
, SomeProperty PadAmount 6
)
,
( SomeKey PadDecimals
, SomeProperty PadDecimals 3
)
]
)
Expand All @@ -37,19 +44,14 @@ RuleSet
( fromList
[ AnyObjectKey
, Selector
( ObjectKey "nodes" )
, AnyArrayIndex
( ObjectKey "flexbodies" )
, AnyArrayIndex
]
)
, fromList
[
( SomeKey PadAmount
, SomeProperty PadAmount 6
)
,
( SomeKey PadDecimals
, SomeProperty PadDecimals 3
( SomeKey NoComplexNewLine
, SomeProperty NoComplexNewLine True
)
]
)
Expand All @@ -60,14 +62,12 @@ RuleSet
, Selector
( ObjectKey "nodes" )
, AnyArrayIndex
, Selector
( ArrayIndex 0 )
]
)
, fromList
[
( SomeKey PadAmount
, SomeProperty PadAmount 8
( SomeKey NoComplexNewLine
, SomeProperty NoComplexNewLine True
)
]
)
Expand Down
22 changes: 11 additions & 11 deletions examples/ast/jbfl/minimal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ RuleSet
( fromList
[ AnyObjectKey
, Selector
( ObjectKey "flexbodies" )
( ObjectKey "nodes" )
, AnyArrayIndex
, AnyArrayIndex
]
)
, fromList
[
( SomeKey NoComplexNewLine
, SomeProperty NoComplexNewLine True
( SomeKey PadAmount
, SomeProperty PadAmount 8
)
,
( SomeKey PadDecimals
, SomeProperty PadDecimals 3
)
]
)
Expand All @@ -21,19 +26,14 @@ RuleSet
( fromList
[ AnyObjectKey
, Selector
( ObjectKey "nodes" )
, AnyArrayIndex
( ObjectKey "flexbodies" )
, AnyArrayIndex
]
)
, fromList
[
( SomeKey PadAmount
, SomeProperty PadAmount 8
)
,
( SomeKey PadDecimals
, SomeProperty PadDecimals 3
( SomeKey NoComplexNewLine
, SomeProperty NoComplexNewLine True
)
]
)
Expand Down
11 changes: 11 additions & 0 deletions jbeam-edit.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ license-file: LICENSE
build-type: Simple
extra-source-files:
README.org
data-files:
examples/jbfl/complex.jbfl
examples/jbfl/minimal.jbfl

source-repository head
type: git
Expand All @@ -33,7 +36,9 @@ library
Core.NodeCursor
Core.NodePath
Formatting
Formatting.Config
Formatting.Rules
IOUtils
Parsing.Common
Parsing.Common.ErrorMessage
Parsing.Common.Helpers
Expand All @@ -54,6 +59,8 @@ library
base >=4.7 && <5
, bytestring
, containers
, directory
, filepath
, foldable1-classes-compat
, megaparsec
, scientific
Expand All @@ -77,6 +84,8 @@ executable jbeam-edit
base >=4.7 && <5
, bytestring
, containers
, directory
, filepath
, foldable1-classes-compat
, jbeam-edit
, megaparsec
Expand All @@ -102,6 +111,7 @@ executable jbeam-edit-dump-ast
, bytestring
, containers
, directory
, filepath
, foldable1-classes-compat
, jbeam-edit
, megaparsec
Expand Down Expand Up @@ -137,6 +147,7 @@ test-suite jbeam-edit-test
, bytestring
, containers
, directory
, filepath
, foldable1-classes-compat
, hspec
, hspec-megaparsec
Expand Down
8 changes: 6 additions & 2 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ license: BSD-3-Clause
author: webdevred
maintainer: example@example.com
copyright: 2025 webdevred
data-files: examples/jbfl/*.jbfl

extra-source-files:
- README.org
Expand All @@ -28,6 +29,8 @@ dependencies:
- text
- scientific
- foldable1-classes-compat
- directory
- filepath

default-extensions: [OverloadedStrings, ImportQualifiedPost]

Expand All @@ -44,6 +47,7 @@ ghc-options:

library:
source-dirs: src
generated-other-modules: Paths_jbeam_edit

flags:
dump-ast:
Expand All @@ -67,12 +71,12 @@ executables:
main: Main.hs
source-dirs: tools/dump_ast
ghc-options: [-threaded, -rtsopts, -with-rtsopts=-N]
dependencies: [directory, pretty-simple, jbeam-edit]
dependencies: [pretty-simple, jbeam-edit]

tests:
jbeam-edit-test:
main: Spec.hs
source-dirs: test
ghc-options: [-threaded, -rtsopts, -with-rtsopts=-N]
dependencies: [jbeam-edit, directory, hspec, hspec-megaparsec]
dependencies: [jbeam-edit, hspec, hspec-megaparsec]
build-tools: [hspec-discover]
48 changes: 48 additions & 0 deletions src/Formatting/Config.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module Formatting.Config (readFormattingConfig) where

import Control.Monad (when)
import Data.Functor (($>))
import Formatting.Rules
import GHC.IO.Exception (IOErrorType (NoSuchThing))
import IOUtils
import Parsing.DSL (parseDSL)
import Paths_jbeam_edit
import System.Directory
import System.FilePath ((</>))

import Data.ByteString.Lazy qualified as BL (
toStrict,
)
import Data.Text.IO qualified as TIO (putStrLn)

data ConfigType = MinimalConfig | ComplexConfig deriving (Show)

getJbflSourcePath :: ConfigType -> FilePath
getJbflSourcePath MinimalConfig = "examples" </> "jbfl" </> "minimal.jbfl"
getJbflSourcePath ComplexConfig = "examples" </> "jbfl" </> "complex.jbfl"

getConfigDir :: IO FilePath
getConfigDir = getXdgDirectory XdgConfig "jbeam_edit"

copyConfigFile :: FilePath -> ConfigType -> IO ()
copyConfigFile destDir configType = do
createDirectoryIfMissing True destDir
source <- getDataFileName (getJbflSourcePath configType)
copyFile source (destDir </> "rules.jbfl")

appendRuleFilename :: FilePath -> FilePath
appendRuleFilename configDir = configDir </> "rules.jbfl"

createRuleFileIfDoesNotExist :: FilePath -> IO ()
createRuleFileIfDoesNotExist configDir =
doesFileExist (appendRuleFilename configDir)
>>= (`when` copyConfigFile configDir MinimalConfig) . not

readFormattingConfig :: IO RuleSet
readFormattingConfig = do
configDir <- getConfigDir
createRuleFileIfDoesNotExist configDir
contents <- tryReadFile [NoSuchThing] (appendRuleFilename configDir)
case contents >>= parseDSL . BL.toStrict of
Right rs -> pure rs
Left err -> TIO.putStrLn err $> newRuleSet
21 changes: 18 additions & 3 deletions src/Formatting/Rules.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,39 @@ import Core.NodePath (NodeSelector (..))
import Data.Function (on)
import Data.List (find)
import Data.Map (Map)
import Data.Ord (Down (..))
import Data.Sequence (Seq (..))
import Data.Text (Text)
import Data.Type.Equality ((:~:) (Refl))

import Core.NodeCursor qualified as NC
import Data.Map qualified as M
import Data.Sequence qualified as Seq (null)
import Data.Sequence qualified as Seq (length, null)
import Data.Text qualified as T

data NodePatternSelector
= AnyObjectKey
| AnyArrayIndex
| Selector NodeSelector
deriving (Eq, Ord, Read, Show)
deriving (Eq, Read, Show)

instance Ord NodePatternSelector where
compare a b = compare (rank a) (rank b)
where
rank :: NodePatternSelector -> (Int, Maybe NodeSelector)
rank AnyArrayIndex = (2, Nothing)
rank AnyObjectKey = (1, Nothing)
rank (Selector s) = (0, Just s)

newtype NodePattern
= NodePattern (Seq NodePatternSelector)
deriving stock (Eq, Ord, Read, Show)
deriving stock (Eq, Read, Show)

instance Ord NodePattern where
compare (NodePattern a) (NodePattern b) =
case on compare (Down . Seq.length) a b of
EQ -> compare a b
c -> c

data PropertyKey a where
NoComplexNewLine :: PropertyKey Bool
Expand Down
Loading