Skip to content

Commit 7e7836a

Browse files
authored
Merge pull request #5545 from unisonweb/25-01-15-lsp-merge-bug
bugfix: call lspCheckForChanges in more places, such as after a failed merge
2 parents 0a98cc9 + 47abc53 commit 7e7836a

File tree

5 files changed

+30
-21
lines changed

5 files changed

+30
-21
lines changed

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

+16-13
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ module Unison.Cli.Monad
5050
runTransactionWithRollback,
5151
runTransactionWithRollback2,
5252

53-
-- * Internal
54-
setMostRecentProjectPath,
55-
5653
-- * Misc types
5754
LoadSourceResult (..),
5855
)
@@ -169,6 +166,8 @@ data Env = Env
169166
generateUniqueName :: IO Parser.UniqueName,
170167
-- | How to load source code.
171168
loadSource :: SourceName -> IO LoadSourceResult,
169+
-- | Notify the LSP that this ProjectPathIds might be different from the last (e.g. on branch update, switch, etc).
170+
lspCheckForChanges :: PP.ProjectPathIds -> IO (),
172171
-- | How to write source code. Bool = make new fold?
173172
writeSource :: SourceName -> Text -> Bool -> IO (),
174173
-- | What to do with output for the user.
@@ -388,19 +387,24 @@ getProjectPathIds = do
388387

389388
cd :: Path.Absolute -> Cli ()
390389
cd path = do
390+
env <- ask
391391
pp <- getProjectPathIds
392392
let newPP = pp & PP.absPath_ .~ path
393-
setMostRecentProjectPath newPP
393+
runTransaction (Codebase.setCurrentProjectPath newPP)
394394
#projectPathStack %= NonEmpty.cons newPP
395+
liftIO (env.lspCheckForChanges newPP)
395396

396397
switchProject :: ProjectAndBranch ProjectId ProjectBranchId -> Cli ()
397398
switchProject pab@(ProjectAndBranch projectId branchId) = do
398-
Env {codebase} <- ask
399+
env <- ask
399400
let newPP = PP.ProjectPath projectId branchId Path.absoluteEmpty
400401
#projectPathStack %= NonEmpty.cons newPP
401-
runTransaction $ do Q.setMostRecentBranch projectId branchId
402-
setMostRecentProjectPath newPP
403-
liftIO $ Codebase.preloadProjectBranch codebase pab
402+
runTransaction do
403+
Q.setMostRecentBranch projectId branchId
404+
Codebase.setCurrentProjectPath newPP
405+
liftIO do
406+
Codebase.preloadProjectBranch env.codebase pab
407+
env.lspCheckForChanges newPP
404408

405409
-- | Pop the latest path off the stack, if it's not the only path in the stack.
406410
--
@@ -411,14 +415,13 @@ popd = do
411415
case List.NonEmpty.uncons (projectPathStack state) of
412416
(_, Nothing) -> pure False
413417
(_, Just paths) -> do
414-
setMostRecentProjectPath (List.NonEmpty.head paths)
418+
let path = List.NonEmpty.head paths
419+
runTransaction (Codebase.setCurrentProjectPath path)
415420
State.put state {projectPathStack = paths}
421+
env <- ask
422+
liftIO (env.lspCheckForChanges path)
416423
pure True
417424

418-
setMostRecentProjectPath :: PP.ProjectPathIds -> Cli ()
419-
setMostRecentProjectPath loc =
420-
runTransaction $ Codebase.setCurrentProjectPath loc
421-
422425
respond :: Output -> Cli ()
423426
respond output = do
424427
Env {notify} <- ask

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

+11-4
Original file line numberDiff line numberDiff line change
@@ -433,18 +433,25 @@ updateAndStepAt reason projectBranch updates steps = do
433433

434434
updateProjectBranchRoot :: ProjectBranch -> Text -> (Branch IO -> Cli (Branch IO, r)) -> Cli r
435435
updateProjectBranchRoot projectBranch reason f = do
436-
Cli.Env {codebase} <- ask
436+
env <- ask
437437
Cli.time "updateProjectBranchRoot" do
438438
old <- getProjectBranchRoot projectBranch
439439
(new, result) <- f old
440440
when (old /= new) do
441-
liftIO $ Codebase.putBranch codebase new
442-
Cli.runTransaction $ do
441+
liftIO $ Codebase.putBranch env.codebase new
442+
Cli.runTransaction do
443443
-- TODO: If we transactionally check that the project branch hasn't changed while we were computing the new
444444
-- branch, and if it has, abort the transaction and return an error, then we can
445445
-- remove the single UCM per codebase restriction.
446446
causalHashId <- Q.expectCausalHashIdByCausalHash (Branch.headHash new)
447-
Q.setProjectBranchHead reason (projectBranch ^. #projectId) (projectBranch ^. #branchId) causalHashId
447+
Q.setProjectBranchHead reason projectBranch.projectId projectBranch.branchId causalHashId
448+
-- The input to this function isn't necessarily the *current* project branch, which is what LSP cares about. But
449+
-- it might be! There's no harm in unconditionally notifying the LSP that the current project branch may have
450+
-- changed, but it is slightly more efficient for us to just do the == comparison here (since otherwise the LSP
451+
-- would have to dig around in the database before confirming whether there's a change).
452+
projectPathIds <- Cli.getProjectPathIds
453+
when ((projectBranch.projectId, projectBranch.branchId) == (projectPathIds.project, projectPathIds.branch)) do
454+
liftIO (env.lspCheckForChanges projectPathIds)
448455
pure result
449456

450457
updateProjectBranchRoot_ :: ProjectBranch -> Text -> (Branch IO -> Branch IO) -> Cli ()

unison-cli/src/Unison/Codebase/Transcript/Runner.hs

+1
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ run isTest verbosity dir codebase runtime sbRuntime nRuntime ucmVersion baseURL
487487
i <- atomicModifyIORef' seedRef \i -> let !i' = i + 1 in (i', i)
488488
pure (Parser.uniqueBase32Namegen (Random.drgNewSeed (Random.seedFromInteger (fromIntegral i)))),
489489
loadSource = loadPreviousUnisonBlock,
490+
lspCheckForChanges = \_ -> pure (),
490491
writeSource,
491492
notify = print,
492493
notifyNumbered = printNumbered,

unison-cli/src/Unison/CommandLine/Main.hs

+1-3
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ main dir welcome ppIds initialInputs runtime sbRuntime nRuntime codebase serverB
232232
codebase,
233233
credentialManager,
234234
loadSource = loadSourceFile,
235+
lspCheckForChanges,
235236
writeSource,
236237
generateUniqueName = Parser.uniqueBase32Namegen <$> Random.getSystemDRG,
237238
notify,
@@ -252,9 +253,6 @@ main dir welcome ppIds initialInputs runtime sbRuntime nRuntime codebase serverB
252253
-- Handle inputs until @HaltRepl@, staying in the loop on Ctrl+C or synchronous exception.
253254
let loop0 :: Cli.LoopState -> IO ()
254255
loop0 s0 = do
255-
-- It's always possible the previous command changed the branch head, so tell the LSP to check if the current
256-
-- path or project has changed.
257-
lspCheckForChanges (NEL.head $ Cli.projectPathStack s0)
258256
let step = do
259257
input <- awaitInput s0
260258
(!result, resultState) <- Cli.runCli env s0 (HandleInput.loop input)

unison-cli/src/Unison/Main.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ main version = do
306306
currentPP <- Codebase.runTransaction theCodebase do
307307
PP.toIds <$> Codebase.expectCurrentProjectPath
308308
changeSignal <- Signal.newSignalIO (Just currentPP)
309-
let lspCheckForChanges pp = Signal.writeSignalIO changeSignal pp
309+
let lspCheckForChanges = Signal.writeSignalIO changeSignal
310310
-- Unfortunately, the windows IO manager on GHC 8.* is prone to just hanging forever
311311
-- when waiting for input on handles, so if we listen for LSP connections it will
312312
-- prevent UCM from shutting down properly. Hopefully we can re-enable LSP on

0 commit comments

Comments
 (0)