<commit_before>-- This is the elm file responsible for returning the completed code box given
-- the Model and the appropriate dimensions.

module CodeBox (interpretAceEvents, packageModel, tripRender,
                AceMessage, AceCodeBoxInfo, initAceCodeBoxInfo) where

import Graphics.Element as GE
import InterfaceModel as Model exposing (Event, sampleModel, events)

-- So we can check on the installSaveState update
import InterfaceStorage exposing (installSaveState)

import Task exposing (Task)

import Dict exposing (Dict)

-- So we can crash correctly
import Debug

type alias AceCodeBoxInfo = 
    { code : String 
    , cursorPos : Model.AcePos
    , manipulable : Bool
    , selections : List Model.Range
    , highlights : List Model.Highlight
    , bounce : Bool
    }

type alias AceMessage = { evt : String 
                        , strArg  : String 
                        , cursorArg : Model.AcePos
                        , selectionArg : List Model.Range
                        } 

-- An initial AceCodeBoxInfo for the foldp
-- Doesn't actually get sent over the port
initAceCodeBoxInfo =
  ( { code = sampleModel.code
    , cursorPos = sampleModel.codeBoxInfo.cursorPos
    , manipulable = True
    , selections = sampleModel.codeBoxInfo.selections
    , highlights = sampleModel.codeBoxInfo.highlights
    , bounce = True
    }
  , []
  )

interpretAceEvents : AceMessage -> Event
interpretAceEvents amsg = case amsg.evt of
    "AceCodeUpdate" -> Model.UpdateModel <|
        \m -> { m | code <- amsg.strArg
                  , codeBoxInfo <- { cursorPos = amsg.cursorArg
                                   , selections = amsg.selectionArg
                                   , highlights = m.codeBoxInfo.highlights
                                   }
              }
    "Rerender" -> Model.UpdateModel <| \m -> { m | code <- m.code }
    "LoadedFromError" -> Model.UpdateModel <| recoverFromError amsg.strArg
    "init" -> Model.Noop
    _ -> Debug.crash "Malformed update sent to Elm"

-- Puts us in the correct state if we recovered from an error, which we find out
-- about from the JS that also happens to load Ace.
-- Maybe we should split this out into a different Elm/JS file?
recoverFromError : String -> Model.Model -> Model.Model
recoverFromError offendingCode fresh = 
    { fresh | code <- offendingCode
            , editingMode <- Just offendingCode
    }

-- The number of times that we defensively rerender the codebox on codebox
-- clobbering updates. Determined experimentally.
-- We shouldn't have to do this. For some reason the Elm runtime will rerender
-- parts of the page *after* sending signals out to ports.
-- Note that each one of these won't necessarily trigger a DOM copy/replacement;
-- it only will for each of the times that Elm clobbers it.
rerenderCount : Int
rerenderCount = 3

packageModel : (Model.Model, Event) -> (AceCodeBoxInfo, List Bool) -> 
                    (AceCodeBoxInfo, List Bool)
packageModel (model, evt) (lastBox, rerenders) = 
    let manipulable = case (model.mode, model.editingMode) of
            (Model.SaveDialog _, _) -> False
            (_, Nothing) -> False
            _           -> True
        rerender = tripRender evt rerenders
    in 
      ( { code = model.code 
        , cursorPos = model.codeBoxInfo.cursorPos <commit_msg>Added caption (that gets cut off :P)<commit_after>-- This is the elm file responsible for returning the completed code box given
-- the Model and the appropriate dimensions.

module CodeBox (interpretAceEvents, packageModel, tripRender,
                AceMessage, AceCodeBoxInfo, initAceCodeBoxInfo) where

import Graphics.Element as GE
import InterfaceModel as Model exposing (Event, sampleModel, events)

-- So we can check on the installSaveState update
import InterfaceStorage exposing (installSaveState)

import Task exposing (Task)

import Dict exposing (Dict)

-- So we can crash correctly
import Debug

type alias AceCodeBoxInfo = 
    { code : String 
    , cursorPos : Model.AcePos
    , manipulable : Bool
    , selections : List Model.Range
    , highlights : List Model.Highlight
    , bounce : Bool
    }

type alias AceMessage = { evt : String 
                        , strArg  : String 
                        , cursorArg : Model.AcePos
                        , selectionArg : List Model.Range
                        } 

-- An initial AceCodeBoxInfo for the foldp
-- Doesn't actually get sent over the port
initAceCodeBoxInfo =
  ( { code = sampleModel.code
    , cursorPos = sampleModel.codeBoxInfo.cursorPos
    , manipulable = True
    , selections = sampleModel.codeBoxInfo.selections
    , highlights = sampleModel.codeBoxInfo.highlights
    , bounce = True
    }
  , []
  )

interpretAceEvents : AceMessage -> Event
interpretAceEvents amsg = case amsg.evt of
    "AceCodeUpdate" -> Model.UpdateModel <|
        \m -> { m | code <- amsg.strArg
                  , codeBoxInfo <- { cursorPos = amsg.cursorArg
                                   , selections = amsg.selectionArg
                                   , highlights = m.codeBoxInfo.highlights
                                   }
              }
    "Rerender" -> Model.UpdateModel <| \m -> { m | code <- m.code }
    "LoadedFromError" -> Model.UpdateModel <| recoverFromError amsg.strArg
    "init" -> Model.Noop
    _ -> Debug.crash "Malformed update sent to Elm"

-- Puts us in the correct state if we recovered from an error, which we find out
-- about from the JS that also happens to load Ace.
-- Maybe we should split this out into a different Elm/JS file?
recoverFromError : String -> Model.Model -> Model.Model
recoverFromError offendingCode fresh = 
    { fresh | code <- offendingCode
            , editingMode <- Just offendingCode
            , caption <- Just <| Model.LangError <| "Runtime Error!\n" ++
                "You likely provided the wrong number of arguments to a\n" ++
                "function or referenced an undefined expression name."
    }

-- The number of times that we defensively rerender the codebox on codebox
-- clobbering updates. Determined experimentally.
-- We shouldn't have to do this. For some reason the Elm runtime will rerender
-- parts of the page *after* sending signals out to ports.
-- Note that each one of these won't necessarily trigger a DOM copy/replacement;
-- it only will for each of the times that Elm clobbers it.
rerenderCount : Int
rerenderCount = 3

packageModel : (Model.Model, Event) -> (AceCodeBoxInfo, List Bool) -> 
                    (AceCodeBoxInfo, List Bool)
packageModel (model, evt) (lastBox, rerenders) = 
    let manipulable = case (model.mode, model.editingMode) of
            (Model.SaveDialog _, _) -> False
            (_, Nothing) -> False
            _           -> True
        rerender = tripRender evt rerenders
    in 
      ( { code = model.code 
        , cursorPos = model.codeBoxInfo.cursorPos 
{'fraction_non_alphanumeric': 0.07896261953057085, 'fraction_numerical': 0.0002897710808461316, 'mean_word_length': 2.3060344827586206, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7009131', 'n_tokens_mistral': 1948, 'n_tokens_neox': 1822, 'n_words': 656}