on master help sign in
start with def name when the first REPL lines are a type annotation

Say you type: > incr : Int -> Int Now when you press enter, it is going to prefill the next line like this: > incr : Int -> Int | incr This should be nice in most cases. If someone wants a multi-line type annotation, they can either (1) end it with a -> to make sure the type annotation appears incomplete or (2) just press backspace and keep going with their type.

commit243dcc6ba11c8627a69dae25f53a6c1474c2675f authorEvan Czaplicki <evancz@users.noreply.github.com> dateTue 16 Jul 2019, 14:56 (7 years ago) parentf3e09a8
1 file changed+44−9
terminal/src/Repl.hs+44−9
terminal/src/Repl.hs+44−9
@@ -43,6 +43,9 @@ import qualified Parse.Expression as PE
4343import qualified Parse.Declaration as PD
4444import qualified Parse.Module as PM
4545import qualified Parse.Primitives as P
46+import qualified Parse.Space as PS
47+import qualified Parse.Type as PT
48+import qualified Parse.Variable as PV
4649import Parse.Primitives (Row, Col)
4750import qualified Reporting
4851import qualified Reporting.Annotation as A
@@ -172,12 +175,12 @@ read =
172175 in
173176 case categorize lines of
174177 Done input -> return input
175− Continue -> readMore lines
178+ Continue p -> readMore lines p
176179
177180
178−readMore :: Lines -> Repl.InputT M Input
179−readMore previousLines =
180− do input <- Repl.getInputLineWithInitial "| " (" ","")
181+readMore :: Lines -> Prefill -> Repl.InputT M Input
182+readMore previousLines prefill =
183+ do input <- Repl.getInputLineWithInitial "| " (renderPrefill prefill, "")
181184 case input of
182185 Nothing ->
183186 return Skip
@@ -188,7 +191,22 @@ readMore previousLines =
188191 in
189192 case categorize lines of
190193 Done input -> return input
191− Continue -> readMore lines
194+ Continue p -> readMore lines p
195+
196+
197+data Prefill
198+ = Indent
199+ | DefStart N.Name
200+
201+
202+renderPrefill :: Prefill -> String
203+renderPrefill lineStart =
204+ case lineStart of
205+ Indent ->
206+ " "
207+
208+ DefStart name ->
209+ N.toChars name ++ " "
192210
193211
194212
@@ -241,7 +259,7 @@ getFirstLine (Lines x xs) =
241259
242260data CategorizedInput
243261 = Done Input
244− | Continue
262+ | Continue Prefill
245263
246264
247265categorize :: Lines -> CategorizedInput
@@ -270,14 +288,14 @@ ifFail :: Lines -> Input -> CategorizedInput
270288ifFail lines input =
271289 if endsWithBlankLine lines
272290 then Done input
273− else Continue
291+ else Continue Indent
274292
275293
276294ifDone :: Lines -> Input -> CategorizedInput
277295ifDone lines input =
278296 if isSingleLine lines || endsWithBlankLine lines
279297 then Done input
280− else Continue
298+ else Continue Indent
281299
282300
283301attemptDeclOrExpr :: Lines -> CategorizedInput
@@ -311,7 +329,9 @@ attemptDeclOrExpr lines =
311329 if exprPosition >= declPosition then
312330 ifFail lines (Expr src)
313331 else
314− ifFail lines (Decl "ERR" src)
332+ case P.fromByteString annotation (\_ _ -> ()) src of
333+ Right name -> Continue (DefStart name)
334+ Left () -> ifFail lines (Decl "ERR" src)
315335
316336
317337startsWithColon :: Lines -> Bool
@@ -361,6 +381,21 @@ toDeclPosition src decl r c =
361381 (row, col)
362382
363383
384+annotation :: P.Parser () N.Name
385+annotation =
386+ let
387+ err _ _ = ()
388+ err_ _ _ _ = ()
389+ in
390+ do name <- PV.lower err
391+ PS.chompAndCheckIndent err_ err
392+ P.word1 0x3A {-:-} err
393+ PS.chompAndCheckIndent err_ err
394+ (_, _) <- P.specialize err_ PT.expression
395+ PS.checkFreshLine err
396+ return name
397+
398+
364399
365400-- STATE
366401