theam / haskell-do

:pencil2: :bar_chart: - The Haskell code editor focused on interactive development.
Apache License 2.0
352 stars 31 forks source link

Automatic format switcher #121

Open NickSeagull opened 7 years ago

NickSeagull commented 7 years ago

Right now we store the user's project as a Markdown file with some options on top so inliterate can render it properly. The thing is that this is not really efficient, because if the user were to use the project as a library on a production system, they would have to drag inliterate and all of its dependencies with them. So, we can do something "intelligent" with it:

When the user works within Haskell.do, everything gets stored in a regular (no inliterate) Lib.hs Haskell file, replacing Haskell.do blocks like this:

Everything that is text (except code) gets stored as a multiline comment, for example:

I'm text

` ` ` haskell top
im :: Code
im = undefined
` ` `

I'm text too,
but
multiline

Would get stored as:

{-I'm text-}

im :: Code
im = undefined

{-I'm text too
but
multiline-}

Same with haskell eval and haskell do blocks. These are for exploration, and in the end they are just comments on steroids that we use to explain things, the only difference that they would be stored like:

{- haskell eval
2 + 2 -}

{- haskell do
x <- readFile "foo" -}

The placement of the braces is important, because it allows us to have the same line numbers

See where this is going? Now, the user can safely remove the inliterate dependency from their project without having to worry about that (unless they are using it in their library functions of course).

So, how do we render everything? Easy, when we compile the project, we generate a Main.hs file with the format that inliterate expects. This is great because of two things:

The second point is very important, as right now the output of GHC for errors is quite random for us, because inliterate generates the boilerplate required to render everything, so this way we could parse the error strings and highlight the errors on the document.