Skip to content

Commit 3420cf3

Browse files
authored
Merge pull request #5404 from unisonweb/24-10-09-edit-add-to-fold
2 parents b1ac7ba + 7cdf99a commit 3420cf3

File tree

25 files changed

+437
-209
lines changed

25 files changed

+437
-209
lines changed

unison-cli/src/Unison/Cli/Monad.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ data Env = Env
164164
generateUniqueName :: IO Parser.UniqueName,
165165
-- | How to load source code.
166166
loadSource :: SourceName -> IO LoadSourceResult,
167-
-- | How to write source code.
168-
writeSource :: SourceName -> Text -> IO (),
167+
-- | How to write source code. Bool = make new fold?
168+
writeSource :: SourceName -> Text -> Bool -> IO (),
169169
-- | What to do with output for the user.
170170
notify :: Output -> IO (),
171171
-- | What to do with numbered output for the user.

unison-cli/src/Unison/Cli/Pretty.hs

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ module Unison.Cli.Pretty
3737
prettySharePath,
3838
prettyShareURI,
3939
prettySlashProjectBranchName,
40+
prettyTerm,
4041
prettyTermName,
42+
prettyType,
4143
prettyTypeName,
4244
prettyTypeResultHeader',
4345
prettyTypeResultHeaderFull',
@@ -47,14 +49,11 @@ module Unison.Cli.Pretty
4749
prettyWriteRemoteNamespace,
4850
shareOrigin,
4951
unsafePrettyTermResultSigFull',
50-
prettyTermDisplayObjects,
51-
prettyTypeDisplayObjects,
5252
)
5353
where
5454

5555
import Control.Lens hiding (at)
5656
import Control.Monad.Writer (Writer, runWriter)
57-
import Data.List qualified as List
5857
import Data.Map qualified as Map
5958
import Data.Set qualified as Set
6059
import Data.Time (UTCTime)
@@ -92,7 +91,6 @@ import Unison.HashQualified qualified as HQ
9291
import Unison.HashQualifiedPrime qualified as HQ'
9392
import Unison.LabeledDependency as LD
9493
import Unison.Name (Name)
95-
import Unison.Name qualified as Name
9694
import Unison.NameSegment (NameSegment)
9795
import Unison.NameSegment.Internal qualified as NameSegment
9896
import Unison.Parser.Ann (Ann)
@@ -102,10 +100,9 @@ import Unison.PrettyPrintEnv.Names qualified as PPE
102100
import Unison.PrettyPrintEnv.Util qualified as PPE
103101
import Unison.PrettyPrintEnvDecl qualified as PPED
104102
import Unison.Project (ProjectAndBranch (..), ProjectName, Semver (..))
105-
import Unison.Reference (Reference, TermReferenceId)
103+
import Unison.Reference (Reference)
106104
import Unison.Reference qualified as Reference
107105
import Unison.Referent (Referent)
108-
import Unison.Referent qualified as Referent
109106
import Unison.Server.SearchResultPrime qualified as SR'
110107
import Unison.ShortHash (ShortHash)
111108
import Unison.Symbol (Symbol)
@@ -439,34 +436,6 @@ prettyUnisonFile ppe uf@(UF.UnisonFileId datas effects terms watches) =
439436
rd = Reference.DerivedId
440437
hqv v = HQ.unsafeFromVar v
441438

442-
prettyTypeDisplayObjects ::
443-
PPED.PrettyPrintEnvDecl ->
444-
(Map Reference (DisplayObject () (DD.Decl Symbol Ann))) ->
445-
[P.Pretty SyntaxText]
446-
prettyTypeDisplayObjects pped types =
447-
types
448-
& Map.toList
449-
& map (\(ref, dt) -> (PPE.typeName unsuffixifiedPPE ref, ref, dt))
450-
& List.sortBy (\(n0, _, _) (n1, _, _) -> Name.compareAlphabetical n0 n1)
451-
& map (prettyType pped)
452-
where
453-
unsuffixifiedPPE = PPED.unsuffixifiedPPE pped
454-
455-
prettyTermDisplayObjects ::
456-
PPED.PrettyPrintEnvDecl ->
457-
Bool ->
458-
(TermReferenceId -> Bool) ->
459-
(Map Reference.TermReference (DisplayObject (Type Symbol Ann) (Term Symbol Ann))) ->
460-
[P.Pretty SyntaxText]
461-
prettyTermDisplayObjects pped isSourceFile isTest terms =
462-
terms
463-
& Map.toList
464-
& map (\(ref, dt) -> (PPE.termName unsuffixifiedPPE (Referent.Ref ref), ref, dt))
465-
& List.sortBy (\(n0, _, _) (n1, _, _) -> Name.compareAlphabetical n0 n1)
466-
& map (\t -> prettyTerm pped isSourceFile (fromMaybe False . fmap isTest . Reference.toId $ (t ^. _2)) t)
467-
where
468-
unsuffixifiedPPE = PPED.unsuffixifiedPPE pped
469-
470439
prettyTerm ::
471440
PPED.PrettyPrintEnvDecl ->
472441
Bool {- whether we're printing to a source-file or not. -} ->

unison-cli/src/Unison/Codebase/Editor/HandleInput.hs

Lines changed: 13 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import Control.Monad.State qualified as State
1414
import Data.Foldable qualified as Foldable
1515
import Data.List qualified as List
1616
import Data.List.Extra (nubOrd)
17-
import Data.List.NonEmpty (NonEmpty)
1817
import Data.List.NonEmpty qualified as Nel
1918
import Data.Map qualified as Map
2019
import Data.Set qualified as Set
@@ -85,7 +84,7 @@ import Unison.Codebase.Editor.HandleInput.Reflogs qualified as Reflogs
8584
import Unison.Codebase.Editor.HandleInput.ReleaseDraft (handleReleaseDraft)
8685
import Unison.Codebase.Editor.HandleInput.Run (handleRun)
8786
import Unison.Codebase.Editor.HandleInput.RuntimeUtils qualified as RuntimeUtils
88-
import Unison.Codebase.Editor.HandleInput.ShowDefinition (showDefinitions)
87+
import Unison.Codebase.Editor.HandleInput.ShowDefinition (handleShowDefinition)
8988
import Unison.Codebase.Editor.HandleInput.TermResolution (resolveMainRef)
9089
import Unison.Codebase.Editor.HandleInput.Tests qualified as Tests
9190
import Unison.Codebase.Editor.HandleInput.Todo (handleTodo)
@@ -616,7 +615,7 @@ loop e = do
616615
DisplayI outputLoc namesToDisplay -> do
617616
traverse_ (displayI outputLoc) namesToDisplay
618617
ShowDefinitionI outputLoc showDefinitionScope query -> handleShowDefinition outputLoc showDefinitionScope query
619-
EditNamespaceI paths -> handleEditNamespace LatestFileLocation paths
618+
EditNamespaceI paths -> handleEditNamespace (LatestFileLocation AboveFold) paths
620619
FindShallowI pathArg -> handleLs pathArg
621620
FindI isVerbose fscope ws -> handleFindI isVerbose fscope ws input
622621
StructuredFindI _fscope ws -> handleStructuredFindI ws
@@ -763,25 +762,25 @@ loop e = do
763762
Nothing -> do
764763
Cli.respond DebugFuzzyOptionsNoResolver
765764
DebugFormatI -> do
766-
Cli.Env {writeSource, loadSource} <- ask
765+
env <- ask
767766
void $ runMaybeT do
768767
(filePath, _) <- MaybeT Cli.getLatestFile
769768
pf <- lift Cli.getLatestParsedFile
770769
tf <- lift Cli.getLatestTypecheckedFile
771770
names <- lift Cli.currentNames
772771
let buildPPED uf tf =
773772
let names' = (fromMaybe mempty $ (UF.typecheckedToNames <$> tf) <|> (UF.toNames <$> uf)) `Names.shadowing` names
774-
in pure (PPED.makePPED (PPE.hqNamer 10 names') (PPE.suffixifyByHashName names'))
773+
in pure (PPED.makePPED (PPE.hqNamer 10 names') (PPE.suffixifyByHashName names'))
775774
let formatWidth = 80
776775
currentPath <- lift $ Cli.getCurrentPath
777776
updates <- MaybeT $ Format.formatFile buildPPED formatWidth currentPath pf tf Nothing
778777
source <-
779-
liftIO (loadSource (Text.pack filePath)) >>= \case
778+
liftIO (env.loadSource (Text.pack filePath)) >>= \case
780779
Cli.InvalidSourceNameError -> lift $ Cli.returnEarly $ Output.InvalidSourceName filePath
781780
Cli.LoadError -> lift $ Cli.returnEarly $ Output.SourceLoadFailed filePath
782781
Cli.LoadSuccess contents -> pure contents
783782
let updatedSource = Format.applyTextReplacements updates source
784-
liftIO $ writeSource (Text.pack filePath) updatedSource
783+
liftIO $ env.writeSource (Text.pack filePath) updatedSource True
785784
DebugDumpNamespacesI -> do
786785
let seen h = State.gets (Set.member h)
787786
set h = State.modify (Set.insert h)
@@ -1264,50 +1263,6 @@ handleDependents hq = do
12641263
Cli.setNumberedArgs . map SA.HashQualified $ types <> terms
12651264
Cli.respond (ListDependents ppe lds types terms)
12661265

1267-
-- | Handle a @ShowDefinitionI@ input command, i.e. `view` or `edit`.
1268-
handleShowDefinition :: OutputLocation -> ShowDefinitionScope -> NonEmpty (HQ.HashQualified Name) -> Cli ()
1269-
handleShowDefinition outputLoc showDefinitionScope query = do
1270-
Cli.Env {codebase} <- ask
1271-
hqLength <- Cli.runTransaction Codebase.hashLength
1272-
let hasAbsoluteQuery = any (any Name.isAbsolute) query
1273-
(names, unbiasedPPED) <- case (hasAbsoluteQuery, showDefinitionScope) of
1274-
-- TODO: We should instead print each definition using the names from its project-branch root.
1275-
(True, _) -> do
1276-
root <- Cli.getCurrentProjectRoot
1277-
let root0 = Branch.head root
1278-
let names = Names.makeAbsolute $ Branch.toNames root0
1279-
let pped = PPED.makePPED (PPE.hqNamer 10 names) (suffixify names)
1280-
pure (names, pped)
1281-
(_, ShowDefinitionGlobal) -> do
1282-
-- TODO: Maybe rewrite to be properly global
1283-
root <- Cli.getCurrentProjectRoot
1284-
let root0 = Branch.head root
1285-
let names = Names.makeAbsolute $ Branch.toNames root0
1286-
let pped = PPED.makePPED (PPE.hqNamer 10 names) (suffixify names)
1287-
pure (names, pped)
1288-
(_, ShowDefinitionLocal) -> do
1289-
currentNames <- Cli.currentNames
1290-
let pped = PPED.makePPED (PPE.hqNamer 10 currentNames) (suffixify currentNames)
1291-
pure (currentNames, pped)
1292-
let pped = PPED.biasTo (mapMaybe HQ.toName (toList query)) unbiasedPPED
1293-
Backend.DefinitionResults terms types misses <- do
1294-
let nameSearch = NameSearch.makeNameSearch hqLength names
1295-
Cli.runTransaction (Backend.definitionsByName codebase nameSearch includeCycles Names.IncludeSuffixes (toList query))
1296-
showDefinitions outputLoc pped terms types misses
1297-
where
1298-
suffixify =
1299-
case outputLoc of
1300-
ConsoleLocation -> PPE.suffixifyByHash
1301-
FileLocation _ -> PPE.suffixifyByHashName
1302-
LatestFileLocation -> PPE.suffixifyByHashName
1303-
1304-
-- `view`: don't include cycles; `edit`: include cycles
1305-
includeCycles =
1306-
case outputLoc of
1307-
ConsoleLocation -> Backend.DontIncludeCycles
1308-
FileLocation _ -> Backend.IncludeCycles
1309-
LatestFileLocation -> Backend.IncludeCycles
1310-
13111266
-- todo: compare to `getHQTerms` / `getHQTypes`. Is one universally better?
13121267
resolveHQToLabeledDependencies :: HQ.HashQualified Name -> Cli (Set LabeledDependency)
13131268
resolveHQToLabeledDependencies = \case
@@ -1355,17 +1310,17 @@ doDisplay outputLoc names tm = do
13551310
rendered <- DisplayValues.displayTerm pped loadTerm loadTypeOfTerm' evalTerm loadDecl tm
13561311
mayFP <- case outputLoc of
13571312
ConsoleLocation -> pure Nothing
1358-
FileLocation path -> Just <$> Directory.canonicalizePath path
1359-
LatestFileLocation -> traverse Directory.canonicalizePath $ fmap fst (loopState ^. #latestFile) <|> Just "scratch.u"
1313+
FileLocation path _ -> Just <$> Directory.canonicalizePath path
1314+
LatestFileLocation _ -> traverse Directory.canonicalizePath $ fmap fst (loopState ^. #latestFile) <|> Just "scratch.u"
13601315
whenJust mayFP \fp -> do
13611316
liftIO $ prependFile fp (Text.pack . P.toPlain 80 $ rendered)
13621317
Cli.respond $ DisplayRendered mayFP rendered
13631318
where
13641319
suffixify =
13651320
case outputLoc of
13661321
ConsoleLocation -> PPE.suffixifyByHash
1367-
FileLocation _ -> PPE.suffixifyByHashName
1368-
LatestFileLocation -> PPE.suffixifyByHashName
1322+
FileLocation _ _ -> PPE.suffixifyByHashName
1323+
LatestFileLocation _ -> PPE.suffixifyByHashName
13691324

13701325
prependFile :: FilePath -> Text -> IO ()
13711326
prependFile filePath txt = do
@@ -1475,7 +1430,7 @@ doCompile profile native output main = do
14751430
outf
14761431
| native = output
14771432
| otherwise = output <> ".uc"
1478-
copts = Runtime.defaultCompileOpts { Runtime.profile = profile }
1433+
copts = Runtime.defaultCompileOpts {Runtime.profile = profile}
14791434
whenJustM
14801435
( liftIO $
14811436
Runtime.compileTo theRuntime copts codeLookup ppe ref outf
@@ -1661,8 +1616,8 @@ displayI outputLoc hq = do
16611616
suffixify =
16621617
case outputLoc of
16631618
ConsoleLocation -> PPE.suffixifyByHash
1664-
FileLocation _ -> PPE.suffixifyByHashName
1665-
LatestFileLocation -> PPE.suffixifyByHashName
1619+
FileLocation _ _ -> PPE.suffixifyByHashName
1620+
LatestFileLocation _ -> PPE.suffixifyByHashName
16661621

16671622
docsI :: Name -> Cli ()
16681623
docsI src = do

unison-cli/src/Unison/Codebase/Editor/HandleInput/FindAndReplace.hs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ import Unison.Var qualified as Var
5252

5353
handleStructuredFindReplaceI :: HQ.HashQualified Name -> Cli ()
5454
handleStructuredFindReplaceI rule = do
55-
Cli.Env {writeSource} <- ask
55+
env <- ask
5656
uf0 <- Cli.expectLatestParsedFile
5757
let (prepare, uf, finish) = UF.prepareRewrite uf0
5858
(ppe, _ns, rules) <- lookupRewrite InvalidStructuredFindReplace prepare rule
@@ -67,7 +67,7 @@ handleStructuredFindReplaceI rule = do
6767
#latestTypecheckedFile .= Just (Left . snd $ uf')
6868
let msg = "| Rewrote using: "
6969
let rendered = Text.pack . P.toPlain 80 $ renderRewrittenFile ppe msg uf'
70-
liftIO $ writeSource (Text.pack dest) rendered
70+
liftIO $ env.writeSource (Text.pack dest) rendered True
7171
Cli.respond $ OutputRewrittenFile dest vs
7272

7373
handleStructuredFindI :: HQ.HashQualified Name -> Cli ()
@@ -116,13 +116,13 @@ handleTextFindI allowLib tokens = do
116116
results0 <- traverse ok results
117117
let results = Alphabetical.sortAlphabetically [hq | (hq, True) <- results0]
118118
Cli.setNumberedArgs $ map SA.HashQualified results
119-
Cli.respond (ListTextFind allowLib results)
119+
Cli.respond (ListTextFind allowLib results)
120120
where
121121
tokensTxt = Text.pack <$> tokens
122-
containsTokens tm =
122+
containsTokens tm =
123123
hasAll . join $ ABT.find txts tm
124-
where
125-
hasAll txts = all (\tok -> any (\haystack -> Text.isInfixOf tok haystack) txts) tokensTxt
124+
where
125+
hasAll txts = all (\tok -> any (\haystack -> Text.isInfixOf tok haystack) txts) tokensTxt
126126
txts (Term.Text' haystack) = ABT.Found [haystack]
127127
txts (Term.Nat' haystack) = ABT.Found [Text.pack (show haystack)]
128128
txts (Term.Int' haystack) = ABT.Found [Text.pack (show haystack)]

unison-cli/src/Unison/Codebase/Editor/HandleInput/Merge2.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ doMerge info = do
318318

319319
blob5 <-
320320
maybeBlob5 & onNothing do
321-
Cli.Env {writeSource} <- ask
321+
env <- ask
322322
(_temporaryBranchId, temporaryBranchName) <-
323323
HandleInput.Branch.createBranch
324324
info.description
@@ -336,7 +336,7 @@ doMerge info = do
336336
Cli.getLatestFile <&> \case
337337
Nothing -> "scratch.u"
338338
Just (file, _) -> file
339-
liftIO $ writeSource (Text.pack scratchFilePath) (Text.pack $ Pretty.toPlain 80 blob3.unparsedFile)
339+
liftIO $ env.writeSource (Text.pack scratchFilePath) (Text.pack $ Pretty.toPlain 80 blob3.unparsedFile) True
340340
done (Output.MergeFailure scratchFilePath mergeSourceAndTarget temporaryBranchName)
341341

342342
Cli.runTransaction (Codebase.addDefsToCodebase env.codebase blob5.file)

0 commit comments

Comments
 (0)