Transcripts now output changes to the scratchfile

This commit is contained in:
Chris Penner 2024-01-04 14:12:15 -08:00
parent 407629aa5d
commit 19f58795f5
5 changed files with 33 additions and 25 deletions

View File

@ -1723,7 +1723,8 @@ handleShowDefinition outputLoc showDefinitionScope query = do
-- next update for that file (which will happen immediately) -- next update for that file (which will happen immediately)
#latestFile ?= (fp, True) #latestFile ?= (fp, True)
liftIO $ writeSource (Text.pack fp) renderedCodeText liftIO $ writeSource (Text.pack fp) renderedCodeText
Cli.respond $ LoadedDefinitionsToSourceFile fp renderedCodePretty let numDefinitions = Map.size terms + Map.size types
Cli.respond $ LoadedDefinitionsToSourceFile fp numDefinitions
when (not (null misses)) (Cli.respond (SearchTermsNotFound misses)) when (not (null misses)) (Cli.respond (SearchTermsNotFound misses))
where where
renderCodePretty pped isSourceFile isTest terms types = renderCodePretty pped isSourceFile isTest terms types =

View File

@ -254,7 +254,7 @@ data Output
DisplayDefinitions (P.Pretty P.ColorText) DisplayDefinitions (P.Pretty P.ColorText)
| -- Like `DisplayDefinitions`, but the definitions are already rendered. `Nothing` means they were output to the terminal. | -- Like `DisplayDefinitions`, but the definitions are already rendered. `Nothing` means they were output to the terminal.
DisplayDefinitionsString !(Maybe FilePath) !(P.Pretty P.ColorText {- rendered definitions -}) DisplayDefinitionsString !(Maybe FilePath) !(P.Pretty P.ColorText {- rendered definitions -})
| LoadedDefinitionsToSourceFile FilePath (P.Pretty P.ColorText) | LoadedDefinitionsToSourceFile FilePath Int
| TestIncrementalOutputStart PPE.PrettyPrintEnv (Int, Int) TermReferenceId | TestIncrementalOutputStart PPE.PrettyPrintEnv (Int, Int) TermReferenceId
| TestIncrementalOutputEnd PPE.PrettyPrintEnv (Int, Int) TermReferenceId Bool {- True if success, False for Failure -} | TestIncrementalOutputEnd PPE.PrettyPrintEnv (Int, Int) TermReferenceId Bool {- True if success, False for Failure -}
| TestResults | TestResults

View File

@ -267,7 +267,9 @@ run verbosity dir stanzas codebase runtime sbRuntime nRuntime config ucmVersion
Just accessToken -> Just accessToken ->
\_codeserverID -> pure $ Right accessToken \_codeserverID -> pure $ Right accessToken
seedRef <- newIORef (0 :: Int) seedRef <- newIORef (0 :: Int)
inputQueue <- Q.newIO -- Queue of Stanzas and Just index, or Nothing if the stanza was programmatically generated
-- e.g. a unison-file update by a command like 'edit'
inputQueue <- Q.newIO @(Stanza, Maybe Int)
cmdQueue <- Q.newIO cmdQueue <- Q.newIO
unisonFiles <- newIORef Map.empty unisonFiles <- newIORef Map.empty
out <- newIORef mempty out <- newIORef mempty
@ -275,7 +277,7 @@ run verbosity dir stanzas codebase runtime sbRuntime nRuntime config ucmVersion
allowErrors <- newIORef False allowErrors <- newIORef False
hasErrors <- newIORef False hasErrors <- newIORef False
mStanza <- newIORef Nothing mStanza <- newIORef Nothing
traverse_ (atomically . Q.enqueue inputQueue) (stanzas `zip` [1 :: Int ..]) traverse_ (atomically . Q.enqueue inputQueue) (stanzas `zip` (Just <$> [1 :: Int ..]))
let patternMap = let patternMap =
Map.fromList $ Map.fromList $
validInputs validInputs
@ -395,7 +397,7 @@ run verbosity dir stanzas codebase runtime sbRuntime nRuntime config ucmVersion
liftIO (output "```ucm\n") liftIO (output "```ucm\n")
atomically . Q.enqueue cmdQueue $ Nothing atomically . Q.enqueue cmdQueue $ Nothing
let sourceName = fromMaybe "scratch.u" filename let sourceName = fromMaybe "scratch.u" filename
liftIO $ writeSourceFile sourceName txt liftIO $ writeSourceFile False sourceName txt
pure $ Left (UnisonFileChanged sourceName txt) pure $ Left (UnisonFileChanged sourceName txt)
API apiRequests -> do API apiRequests -> do
liftIO (output "```api\n") liftIO (output "```api\n")
@ -426,8 +428,11 @@ run verbosity dir stanzas codebase runtime sbRuntime nRuntime config ucmVersion
let f = Cli.LoadSuccess <$> readUtf8 (Text.unpack name) let f = Cli.LoadSuccess <$> readUtf8 (Text.unpack name)
in f <|> pure Cli.InvalidSourceNameError in f <|> pure Cli.InvalidSourceNameError
writeSourceFile :: ScratchFileName -> Text -> IO () writeSourceFile :: Bool -> ScratchFileName -> Text -> IO ()
writeSourceFile fp contents = do writeSourceFile programmaticUpdate fp contents = do
when programmaticUpdate $ do
let fenceDescription = "unison:added-by-ucm " <> fp
atomically (Q.undequeue inputQueue (UnprocessedFence fenceDescription contents, Nothing))
liftIO (modifyIORef' unisonFiles (Map.insert fp contents)) liftIO (modifyIORef' unisonFiles (Map.insert fp contents))
print :: Output.Output -> IO () print :: Output.Output -> IO ()
@ -504,7 +509,7 @@ run verbosity dir stanzas codebase runtime sbRuntime nRuntime config ucmVersion
pure (Parser.uniqueBase32Namegen (Random.drgNewSeed (Random.seedFromInteger (fromIntegral i)))), pure (Parser.uniqueBase32Namegen (Random.drgNewSeed (Random.seedFromInteger (fromIntegral i)))),
isTranscript = True, -- we are running a transcript isTranscript = True, -- we are running a transcript
loadSource = loadPreviousUnisonBlock, loadSource = loadPreviousUnisonBlock,
writeSource = writeSourceFile, writeSource = writeSourceFile True,
notify = print, notify = print,
notifyNumbered = printNumbered, notifyNumbered = printNumbered,
runtime, runtime,

View File

@ -760,16 +760,14 @@ notifyUser dir = \case
[prettyReadRemoteNamespaceWith absurd baseNS, prettyPath' squashedPath] [prettyReadRemoteNamespaceWith absurd baseNS, prettyPath' squashedPath]
<> "to push the changes." <> "to push the changes."
] ]
LoadedDefinitionsToSourceFile fp code -> LoadedDefinitionsToSourceFile fp numDefinitions ->
pure $ pure $
P.callout "☝️" $ P.callout "☝️" $
P.lines P.lines
[ P.wrap $ "I added these definitions to the top of " <> fromString fp, [ P.wrap $ "I added " <> P.shown @Int numDefinitions <> " definitions to the top of " <> fromString fp,
"",
P.indentN 2 code,
"", "",
P.wrap $ P.wrap $
"You can edit them there, then do" "You can edit them there, then run"
<> makeExample' IP.update <> makeExample' IP.update
<> "to replace the definitions currently in this namespace." <> "to replace the definitions currently in this namespace."
] ]

View File

@ -45,29 +45,33 @@ mytest = [Ok "ok"]
☝️ ☝️
I added these definitions to the top of /private/tmp/scratch.u I added 2 definitions to the top of /private/tmp/scratch.u
bar : Nat You can edit them there, then run `update` to replace the
bar = 456
foo : Nat
foo = 123
You can edit them there, then do `update` to replace the
definitions currently in this namespace. definitions currently in this namespace.
.> edit mytest .> edit mytest
☝️ ☝️
I added these definitions to the top of /private/tmp/scratch.u I added 1 definitions to the top of /private/tmp/scratch.u
test> mytest = [Ok "ok"] You can edit them there, then run `update` to replace the
You can edit them there, then do `update` to replace the
definitions currently in this namespace. definitions currently in this namespace.
``` ```
```unison:added-by-ucm /private/tmp/scratch.u
test> mytest = [Ok "ok"]
```
```unison:added-by-ucm /private/tmp/scratch.u
bar : Nat
bar = 456
foo : Nat
foo = 123
```
```ucm ```ucm
.> edit missing .> edit missing