Skip to content

Commit 6e9eba0

Browse files
authored
Merge pull request #106 from mdibaiee/already-compiled
Re-render compilation view only if recently compiled the output
2 parents 71acf04 + 4fce673 commit 6e9eba0

File tree

6 files changed

+31
-35
lines changed

6 files changed

+31
-35
lines changed

src/common/HaskellDo/Compilation/State.hs

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ initialState = State
3434
, compilationError = "No project has been loaded yet, try opening one?"
3535
, projectPath = ""
3636
, workingFile = "src/Main.hs"
37+
, dirtyCompile = True
3738
}
3839

3940
lastProjectFile :: FilePath
@@ -92,7 +93,7 @@ buildOutput state = do
9293
System.ExitFailure _ ->
9394
return state { compiledOutput = "Compiling" }
9495
System.ExitSuccess ->
95-
return state { compiledOutput = preprocessOutput out, compilationError = "" }
96+
return state { compiledOutput = preprocessOutput out, compilationError = "", dirtyCompile = True }
9697

9798

9899
preprocessOutput :: String -> String

src/common/HaskellDo/Compilation/Types.hs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ data State = State
2020
, compilationError :: String
2121
, projectPath :: String
2222
, workingFile :: String
23+
, dirtyCompile :: Bool
2324
} deriving (Read, Show)
2425

2526
data Action

src/common/HaskellDo/Compilation/View.hs

+8-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
module HaskellDo.Compilation.View where
1717

1818
import Control.Monad.IO.Class
19-
import Control.Monad (unless)
19+
import Control.Monad (when)
2020
import Prelude hiding (div, id)
2121

2222
import AxiomUtils
@@ -41,10 +41,10 @@ errorDisplay state
4141
$ code (compilationError state)
4242

4343
updateDisplays :: State -> Widget ()
44-
updateDisplays state = do
45-
highlighted <- liftIO alreadyHighlighted
46-
unless highlighted $ Ulmus.newWidget "outputDisplay" (outputDisplay state)
47-
Ulmus.newWidget "errorDisplay" (errorDisplay state)
48-
liftIO $ activateScriptTags "#output-frame"
49-
liftIO $ setHeightFromElement ".error-placeholder" "#errorDisplay"
50-
liftIO highlightCode
44+
updateDisplays state =
45+
when (dirtyCompile state) $ do
46+
Ulmus.newWidget "outputDisplay" (outputDisplay state)
47+
Ulmus.newWidget "errorDisplay" (errorDisplay state)
48+
liftIO $ activateScriptTags "#output-frame"
49+
liftIO $ setHeightFromElement ".error-placeholder" "#errorDisplay"
50+
liftIO highlightCode

src/common/HaskellDo/State.hs

+19-10
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import qualified HaskellDo.Compilation.Types as Compilation
3232
import qualified HaskellDo.Toolbar.State as Toolbar
3333
import qualified HaskellDo.Toolbar.Types as Toolbar
3434
import qualified Foreign.JQuery as JQuery
35-
import qualified Foreign.Highlight as Highlight
3635

3736
initialAppState :: AppState
3837
initialAppState = AppState
@@ -42,7 +41,19 @@ initialAppState = AppState
4241
}
4342

4443
update :: Action -> AppState -> Cloud AppState
45-
update (CodeMirrorAction action) appState = do
44+
update action appState = _preUpdate action appState >>= _update action >>= _postUpdate action
45+
46+
_preUpdate :: Action -> AppState -> Cloud AppState
47+
_preUpdate _ appState = do
48+
let cs = compilationState appState
49+
newCS = cs { Compilation.dirtyCompile = False }
50+
return appState { compilationState = newCS }
51+
52+
_postUpdate :: Action -> AppState -> Cloud AppState
53+
_postUpdate _ = return
54+
55+
_update :: Action -> AppState -> Cloud AppState
56+
_update (CodeMirrorAction action) appState = do
4657
newCodeMirrorState <- CodeMirror.update action (codeMirrorState appState)
4758
let newContent = CodeMirror.content newCodeMirrorState
4859
_ <- atRemote $ Compilation.update
@@ -53,12 +64,10 @@ update (CodeMirrorAction action) appState = do
5364
{ codeMirrorState = newCodeMirrorState
5465
}
5566
if compileShortcutPressed
56-
then do
57-
localIO Highlight.askForHighlight
58-
update (ToolbarAction Toolbar.Compile) newState
67+
then update (ToolbarAction Toolbar.Compile) newState
5968
else return newState
6069

61-
update (ToolbarAction Toolbar.Compile) appState = do
70+
_update (ToolbarAction Toolbar.Compile) appState = do
6271
let tbState = toolbarState appState
6372
if Toolbar.projectOpened tbState
6473
then do
@@ -74,7 +83,7 @@ update (ToolbarAction Toolbar.Compile) appState = do
7483
localIO Toolbar.shakeErrorDisplay
7584
return appState
7685

77-
update (ToolbarAction Toolbar.LoadProject) appState = do
86+
_update (ToolbarAction Toolbar.LoadProject) appState = do
7887
localIO $ JQuery.hide "#dependencyMessage"
7988
let tbState = toolbarState appState
8089
let cmpState = compilationState appState
@@ -116,7 +125,7 @@ update (ToolbarAction Toolbar.LoadProject) appState = do
116125
localIO $ JQuery.show "#errorDisplay" -- Show it after they finished
117126
return newState
118127

119-
update (ToolbarAction Toolbar.LoadPackageYaml) appState = do
128+
_update (ToolbarAction Toolbar.LoadPackageYaml) appState = do
120129
let projectPath = Compilation.projectPath (compilationState appState)
121130
contents <- atRemote $ localIO $ readFile (projectPath </> "package.yaml")
122131
let tbState = toolbarState appState
@@ -125,7 +134,7 @@ update (ToolbarAction Toolbar.LoadPackageYaml) appState = do
125134
_ <- Toolbar.update Toolbar.LoadPackageYaml tbState
126135
return appState { toolbarState = tbState' }
127136

128-
update (ToolbarAction Toolbar.SavePackage) appState = do
137+
_update (ToolbarAction Toolbar.SavePackage) appState = do
129138
let projectPath = Compilation.projectPath (compilationState appState)
130139
let tbState = toolbarState appState
131140
atRemote $ localIO $ writeFile (projectPath </> "package.yaml") (Toolbar.projectConfig tbState)
@@ -135,7 +144,7 @@ update (ToolbarAction Toolbar.SavePackage) appState = do
135144
localIO $ JQuery.hide "#dependencyMessage"
136145
return newState
137146

138-
update (ToolbarAction action) appState = do
147+
_update (ToolbarAction action) appState = do
139148
newToolbarState <- Toolbar.update action (toolbarState appState)
140149
let cs = compilationState appState
141150
let newCompilationState = cs

src/ghc-specific/Foreign/Highlight.hs

-6
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,3 @@ module Foreign.Highlight where
1717

1818
highlightCode :: IO ()
1919
highlightCode = return ()
20-
21-
askForHighlight :: IO ()
22-
askForHighlight = return ()
23-
24-
alreadyHighlighted :: IO Bool
25-
alreadyHighlighted = return True

src/ghcjs-specific/Foreign/Highlight.hs

+1-10
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,5 @@
1515
-}
1616
module Foreign.Highlight where
1717

18-
foreign import javascript unsafe "if (!alreadyHighlighted){ \
19-
setTimeout(function() {$('.haskell').each(function(i, block){ hljs.highlightBlock(block);}) }, 0); \
20-
alreadyHighlighted = true; \
21-
}"
18+
foreign import javascript unsafe "setTimeout(function() {$('.haskell').each(function(i, block){ hljs.highlightBlock(block);}) }, 0);"
2219
highlightCode :: IO ()
23-
24-
foreign import javascript unsafe "alreadyHighlighted = false;"
25-
askForHighlight :: IO ()
26-
27-
foreign import javascript unsafe "$r = alreadyHighlighted;"
28-
alreadyHighlighted :: IO Bool

0 commit comments

Comments
 (0)