Skip to content

Commit 9d1c75a

Browse files
authored
Merge pull request #4691 from unisonweb/lsp/auto-fmt-flag
Disable lsp-formatting by default, opt in via --lsp-format flag
2 parents 36fdcd8 + 712a9ad commit 9d1c75a

File tree

3 files changed

+40
-21
lines changed

3 files changed

+40
-21
lines changed

unison-cli/src/Unison/LSP.hs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
{-# LANGUAGE RecordWildCards #-}
44
{-# LANGUAGE TypeOperators #-}
55

6-
module Unison.LSP where
6+
module Unison.LSP
7+
( spawnLsp,
8+
LspFormattingConfig (..),
9+
)
10+
where
711

812
import Colog.Core (LogAction (LogAction))
913
import Colog.Core qualified as Colog
@@ -50,12 +54,15 @@ import Unison.Symbol
5054
import UnliftIO
5155
import UnliftIO.Foreign (Errno (..), eADDRINUSE)
5256

57+
data LspFormattingConfig = LspFormatEnabled | LspFormatDisabled
58+
deriving (Show, Eq)
59+
5360
getLspPort :: IO String
5461
getLspPort = fromMaybe "5757" <$> lookupEnv "UNISON_LSP_PORT"
5562

5663
-- | Spawn an LSP server on the configured port.
57-
spawnLsp :: Codebase IO Symbol Ann -> Runtime Symbol -> STM CausalHash -> STM (Path.Absolute) -> IO ()
58-
spawnLsp codebase runtime latestRootHash latestPath =
64+
spawnLsp :: LspFormattingConfig -> Codebase IO Symbol Ann -> Runtime Symbol -> STM CausalHash -> STM (Path.Absolute) -> IO ()
65+
spawnLsp lspFormattingConfig codebase runtime latestRootHash latestPath =
5966
ifEnabled . TCP.withSocketsDo $ do
6067
lspPort <- getLspPort
6168
UnliftIO.handleIO (handleFailure lspPort) $ do
@@ -75,7 +82,7 @@ spawnLsp codebase runtime latestRootHash latestPath =
7582
-- different un-saved state for the same file.
7683
initVFS $ \vfs -> do
7784
vfsVar <- newMVar vfs
78-
void $ runServerWith lspServerLogger lspClientLogger clientInput clientOutput (serverDefinition vfsVar codebase runtime scope latestRootHash latestPath)
85+
void $ runServerWith lspServerLogger lspClientLogger clientInput clientOutput (serverDefinition lspFormattingConfig vfsVar codebase runtime scope latestRootHash latestPath)
7986
where
8087
handleFailure :: String -> IOException -> IO ()
8188
handleFailure lspPort ioerr =
@@ -101,21 +108,22 @@ spawnLsp codebase runtime latestRootHash latestPath =
101108
Nothing -> when (not onWindows) runServer
102109

103110
serverDefinition ::
111+
LspFormattingConfig ->
104112
MVar VFS ->
105113
Codebase IO Symbol Ann ->
106114
Runtime Symbol ->
107115
Ki.Scope ->
108116
STM CausalHash ->
109117
STM (Path.Absolute) ->
110118
ServerDefinition Config
111-
serverDefinition vfsVar codebase runtime scope latestRootHash latestPath =
119+
serverDefinition lspFormattingConfig vfsVar codebase runtime scope latestRootHash latestPath =
112120
ServerDefinition
113121
{ defaultConfig = defaultLSPConfig,
114122
configSection = "unison",
115123
parseConfig = Config.parseConfig,
116124
onConfigChange = Config.updateConfig,
117125
doInitialize = lspDoInitialize vfsVar codebase runtime scope latestRootHash latestPath,
118-
staticHandlers = lspStaticHandlers,
126+
staticHandlers = lspStaticHandlers lspFormattingConfig,
119127
interpretHandler = lspInterpretHandler,
120128
options = lspOptions
121129
}
@@ -154,16 +162,16 @@ lspDoInitialize vfsVar codebase runtime scope latestRootHash latestPath lspConte
154162
pure $ Right $ env
155163

156164
-- | LSP request handlers that don't register/unregister dynamically
157-
lspStaticHandlers :: ClientCapabilities -> Handlers Lsp
158-
lspStaticHandlers _capabilities =
165+
lspStaticHandlers :: LspFormattingConfig -> ClientCapabilities -> Handlers Lsp
166+
lspStaticHandlers lspFormattingConfig _capabilities =
159167
Handlers
160-
{ reqHandlers = lspRequestHandlers,
168+
{ reqHandlers = lspRequestHandlers lspFormattingConfig,
161169
notHandlers = lspNotificationHandlers
162170
}
163171

164172
-- | LSP request handlers
165-
lspRequestHandlers :: SMethodMap (ClientMessageHandler Lsp 'Msg.Request)
166-
lspRequestHandlers =
173+
lspRequestHandlers :: LspFormattingConfig -> SMethodMap (ClientMessageHandler Lsp 'Msg.Request)
174+
lspRequestHandlers lspFormattingConfig =
167175
mempty
168176
& SMM.insert Msg.SMethod_TextDocumentHover (mkHandler hoverHandler)
169177
& SMM.insert Msg.SMethod_TextDocumentCodeAction (mkHandler codeActionHandler)
@@ -172,9 +180,15 @@ lspRequestHandlers =
172180
& SMM.insert Msg.SMethod_TextDocumentFoldingRange (mkHandler foldingRangeRequest)
173181
& SMM.insert Msg.SMethod_TextDocumentCompletion (mkHandler completionHandler)
174182
& SMM.insert Msg.SMethod_CompletionItemResolve (mkHandler completionItemResolveHandler)
175-
& SMM.insert Msg.SMethod_TextDocumentFormatting (mkHandler formatDocRequest)
176-
& SMM.insert Msg.SMethod_TextDocumentRangeFormatting (mkHandler formatRangeRequest)
183+
& addFormattingHandlers
177184
where
185+
addFormattingHandlers handlers =
186+
case lspFormattingConfig of
187+
LspFormatEnabled ->
188+
handlers
189+
& SMM.insert Msg.SMethod_TextDocumentFormatting (mkHandler formatDocRequest)
190+
& SMM.insert Msg.SMethod_TextDocumentRangeFormatting (mkHandler formatRangeRequest)
191+
LspFormatDisabled -> handlers
178192
defaultTimeout = 10_000 -- 10s
179193
mkHandler ::
180194
forall m.

unison-cli/unison/ArgParse.hs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import Text.Read (readMaybe)
5959
import Unison.Codebase.Path qualified as Path
6060
import Unison.Codebase.Path.Parse qualified as Path
6161
import Unison.CommandLine.Types (ShouldWatchFiles (..))
62+
import Unison.LSP (LspFormattingConfig (..))
6263
import Unison.PrettyTerminal qualified as PT
6364
import Unison.Server.CodebaseServer (CodebaseServerOpts (..))
6465
import Unison.Server.CodebaseServer qualified as Server
@@ -117,7 +118,8 @@ data Command
117118
-- | Options shared by sufficiently many subcommands.
118119
data GlobalOptions = GlobalOptions
119120
{ codebasePathOption :: Maybe CodebasePathOption,
120-
exitOption :: ShouldExit
121+
exitOption :: ShouldExit,
122+
lspFormattingConfig :: LspFormattingConfig
121123
}
122124
deriving (Show, Eq)
123125

@@ -259,12 +261,10 @@ globalOptionsParser = do
259261
-- ApplicativeDo
260262
codebasePathOption <- codebasePathParser <|> codebaseCreateParser
261263
exitOption <- exitParser
264+
lspFormattingConfig <- lspFormattingParser
262265

263266
pure
264-
GlobalOptions
265-
{ codebasePathOption = codebasePathOption,
266-
exitOption = exitOption
267-
}
267+
GlobalOptions {codebasePathOption, exitOption, lspFormattingConfig}
268268

269269
codebasePathParser :: Parser (Maybe CodebasePathOption)
270270
codebasePathParser = do
@@ -291,6 +291,11 @@ exitParser = flag DoNotExit Exit (long "exit" <> help exitHelp)
291291
where
292292
exitHelp = "Exit repl after the command."
293293

294+
lspFormattingParser :: Parser LspFormattingConfig
295+
lspFormattingParser = flag LspFormatDisabled LspFormatEnabled (long "lsp-format" <> help lspFormatHelp)
296+
where
297+
lspFormatHelp = "[Experimental] Enable formatting of source files via LSP."
298+
294299
versionOptionParser :: String -> String -> Parser (a -> a)
295300
versionOptionParser progName version =
296301
infoOption (progName <> " version: " <> version) (short 'v' <> long "version" <> help "Show version")

unison-cli/unison/Main.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ where
1515
import ArgParse
1616
( CodebasePathOption (..),
1717
Command (Init, Launch, PrintVersion, Run, Transcript),
18-
GlobalOptions (GlobalOptions, codebasePathOption, exitOption),
18+
GlobalOptions (..),
1919
IsHeadless (Headless, WithCLI),
2020
RunSource (..),
2121
ShouldExit (DoNotExit, Exit),
@@ -120,7 +120,7 @@ main = do
120120
progName <- getProgName
121121
-- hSetBuffering stdout NoBuffering -- cool
122122
(renderUsageInfo, globalOptions, command) <- parseCLIArgs progName (Text.unpack Version.gitDescribeWithDate)
123-
let GlobalOptions {codebasePathOption = mCodePathOption, exitOption = exitOption} = globalOptions
123+
let GlobalOptions {codebasePathOption = mCodePathOption, exitOption, lspFormattingConfig} = globalOptions
124124
withConfig mCodePathOption \config -> do
125125
currentDir <- getCurrentDirectory
126126
case command of
@@ -293,7 +293,7 @@ main = do
293293
-- prevent UCM from shutting down properly. Hopefully we can re-enable LSP on
294294
-- Windows when we move to GHC 9.*
295295
-- https://gitlab.haskell.org/ghc/ghc/-/merge_requests/1224
296-
void . Ki.fork scope $ LSP.spawnLsp theCodebase runtime (readTVar rootCausalHashVar) (readTVar pathVar)
296+
void . Ki.fork scope $ LSP.spawnLsp lspFormattingConfig theCodebase runtime (readTVar rootCausalHashVar) (readTVar pathVar)
297297
Server.startServer (Backend.BackendEnv {Backend.useNamesIndex = False}) codebaseServerOpts sbRuntime theCodebase $ \baseUrl -> do
298298
case exitOption of
299299
DoNotExit -> do

0 commit comments

Comments
 (0)