Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP - some syntax highlighting working
- Loading branch information
Showing
9 changed files
with
281 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,68 @@ | ||
module Halogen.Monaco where | ||
|
||
import Prelude | ||
|
||
import Data.Lens (view) | ||
import Data.Maybe (Maybe(..)) | ||
import Debug.Trace (trace) | ||
import Effect.Class (class MonadEffect, liftEffect) | ||
import Halogen (HalogenM, RefLabel(..)) | ||
import Halogen as H | ||
import Halogen.HTML (HTML, div) | ||
import Halogen.HTML.Properties (class_, ref) | ||
import Monaco (Monaco) | ||
import Monaco as Monaco | ||
import Monaco.Marlowe as MM | ||
|
||
type State | ||
= { editor :: Maybe Monaco } | ||
|
||
data Query a | ||
= Q a | ||
|
||
data Action | ||
= Init | ||
|
||
data Message | ||
= Initialized | ||
|
||
monacoComponent :: forall m. MonadEffect m => H.Component HTML Query Unit Message m | ||
monacoComponent = | ||
H.mkComponent | ||
{ initialState: const { editor: Nothing } | ||
, render | ||
, eval: | ||
H.mkEval | ||
{ handleAction | ||
, handleQuery | ||
, initialize: Just Init | ||
, receive: const Nothing | ||
, finalize: Nothing | ||
} | ||
} | ||
|
||
render :: forall p i. State -> HTML p i | ||
render state = | ||
div | ||
[ ref $ H.RefLabel "monacoEditor" | ||
, class_ $ H.ClassName "monaco-editor-container" | ||
] | ||
[] | ||
|
||
handleAction :: forall slots m. MonadEffect m => Action -> HalogenM State Action slots Message m Unit | ||
handleAction Init = do | ||
m <- liftEffect Monaco.getMonaco | ||
maybeElement <- H.getHTMLElementRef (RefLabel "monacoEditor") | ||
case maybeElement of | ||
Just element -> do | ||
trace element \_ -> pure unit | ||
liftEffect $ Monaco.registerLanguage m MM.languageExtensionPoint | ||
_ <- liftEffect $ Monaco.create m element (view MM._id MM.languageExtensionPoint) | ||
liftEffect $ Monaco.setMonarchTokensProvider m (view MM._id MM.languageExtensionPoint) MM.monarchLanguage | ||
_ <- H.modify (const { editor: Just m }) | ||
pure unit | ||
Nothing -> pure unit | ||
H.raise Initialized | ||
|
||
handleQuery :: forall a input m. Query a -> HalogenM State Action input Message m (Maybe a) | ||
handleQuery (Q next) = pure $ Just next |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
module Monaco.Marlowe where | ||
|
||
import Prelude | ||
|
||
import Data.Lens (Lens') | ||
import Data.Lens.Iso.Newtype (_Newtype) | ||
import Data.Lens.Record (prop) | ||
import Data.Maybe (Maybe(..)) | ||
import Data.Newtype as Newtype | ||
import Data.String.Regex.Flags (noFlags) | ||
import Data.String.Regex.Unsafe (unsafeRegex) | ||
import Data.Symbol (SProxy(..)) | ||
import Data.Tuple (Tuple(..)) | ||
import Data.Tuple.Nested ((/\)) | ||
import Foreign.Object as Object | ||
import Monaco (LanguageExtensionPoint(..), MonarchLanguage(..), MonarchLanguageBracket(..), default, simpleRule, simpleRuleCases, simpleRuleWithAction, simpleRuleWithLog) | ||
|
||
languageExtensionPoint :: LanguageExtensionPoint | ||
languageExtensionPoint = LanguageExtensionPoint { id: "marlowe" } | ||
|
||
_id :: Lens' LanguageExtensionPoint String | ||
_id = _Newtype <<< prop (SProxy :: SProxy "id") | ||
|
||
monarchLanguage :: MonarchLanguage | ||
monarchLanguage = | ||
let | ||
tokenizer = | ||
Object.fromFoldable | ||
[ "root" | ||
/\ [ simpleRuleCases (unsafeRegex "[A-Z][a-z$]*" noFlags) [ Tuple "@keywords" "keyword" ] | ||
, simpleRule (unsafeRegex "[ \\t\\r\\n]+" noFlags) "white" | ||
-- TODO: monaco version has /"([^"\\]|\\.)*$/ not sure exactly what this is | ||
, simpleRuleWithLog (unsafeRegex "\"*$" noFlags) "string.invalid" "string.invalid" | ||
, simpleRuleWithAction (unsafeRegex "\"" noFlags) "string.quote" "@string" | ||
, simpleRule (unsafeRegex "[()]" noFlags) "@brackets" | ||
] | ||
, "string" | ||
/\ [ simpleRule (unsafeRegex """[^\\"]+""" noFlags) "string" | ||
, simpleRuleWithAction (unsafeRegex "\"" noFlags) "string" "@pop" | ||
] | ||
] | ||
|
||
brackets = Just [ MonarchLanguageBracket { open: "(", close: ")", token: "delimiter.parenthesis" } ] | ||
|
||
keywords = Just [ "Close", "If" ] | ||
|
||
lang r = r { tokenizer = tokenizer, brackets = brackets, defaultToken = Just "invalid", keywords = keywords } | ||
in | ||
Newtype.over MonarchLanguage lang default |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.