3
3
{-# LANGUAGE RecordWildCards #-}
4
4
{-# LANGUAGE TypeOperators #-}
5
5
6
- module Unison.LSP where
6
+ module Unison.LSP
7
+ ( spawnLsp ,
8
+ LspFormattingConfig (.. ),
9
+ )
10
+ where
7
11
8
12
import Colog.Core (LogAction (LogAction ))
9
13
import Colog.Core qualified as Colog
@@ -50,12 +54,15 @@ import Unison.Symbol
50
54
import UnliftIO
51
55
import UnliftIO.Foreign (Errno (.. ), eADDRINUSE )
52
56
57
+ data LspFormattingConfig = LspFormatEnabled | LspFormatDisabled
58
+ deriving (Show , Eq )
59
+
53
60
getLspPort :: IO String
54
61
getLspPort = fromMaybe " 5757" <$> lookupEnv " UNISON_LSP_PORT"
55
62
56
63
-- | 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 =
59
66
ifEnabled . TCP. withSocketsDo $ do
60
67
lspPort <- getLspPort
61
68
UnliftIO. handleIO (handleFailure lspPort) $ do
@@ -75,7 +82,7 @@ spawnLsp codebase runtime latestRootHash latestPath =
75
82
-- different un-saved state for the same file.
76
83
initVFS $ \ vfs -> do
77
84
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)
79
86
where
80
87
handleFailure :: String -> IOException -> IO ()
81
88
handleFailure lspPort ioerr =
@@ -101,21 +108,22 @@ spawnLsp codebase runtime latestRootHash latestPath =
101
108
Nothing -> when (not onWindows) runServer
102
109
103
110
serverDefinition ::
111
+ LspFormattingConfig ->
104
112
MVar VFS ->
105
113
Codebase IO Symbol Ann ->
106
114
Runtime Symbol ->
107
115
Ki. Scope ->
108
116
STM CausalHash ->
109
117
STM (Path. Absolute ) ->
110
118
ServerDefinition Config
111
- serverDefinition vfsVar codebase runtime scope latestRootHash latestPath =
119
+ serverDefinition lspFormattingConfig vfsVar codebase runtime scope latestRootHash latestPath =
112
120
ServerDefinition
113
121
{ defaultConfig = defaultLSPConfig,
114
122
configSection = " unison" ,
115
123
parseConfig = Config. parseConfig,
116
124
onConfigChange = Config. updateConfig,
117
125
doInitialize = lspDoInitialize vfsVar codebase runtime scope latestRootHash latestPath,
118
- staticHandlers = lspStaticHandlers,
126
+ staticHandlers = lspStaticHandlers lspFormattingConfig ,
119
127
interpretHandler = lspInterpretHandler,
120
128
options = lspOptions
121
129
}
@@ -154,16 +162,16 @@ lspDoInitialize vfsVar codebase runtime scope latestRootHash latestPath lspConte
154
162
pure $ Right $ env
155
163
156
164
-- | 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 =
159
167
Handlers
160
- { reqHandlers = lspRequestHandlers,
168
+ { reqHandlers = lspRequestHandlers lspFormattingConfig ,
161
169
notHandlers = lspNotificationHandlers
162
170
}
163
171
164
172
-- | LSP request handlers
165
- lspRequestHandlers :: SMethodMap (ClientMessageHandler Lsp 'Msg.Request )
166
- lspRequestHandlers =
173
+ lspRequestHandlers :: LspFormattingConfig -> SMethodMap (ClientMessageHandler Lsp 'Msg.Request )
174
+ lspRequestHandlers lspFormattingConfig =
167
175
mempty
168
176
& SMM. insert Msg. SMethod_TextDocumentHover (mkHandler hoverHandler)
169
177
& SMM. insert Msg. SMethod_TextDocumentCodeAction (mkHandler codeActionHandler)
@@ -172,9 +180,15 @@ lspRequestHandlers =
172
180
& SMM. insert Msg. SMethod_TextDocumentFoldingRange (mkHandler foldingRangeRequest)
173
181
& SMM. insert Msg. SMethod_TextDocumentCompletion (mkHandler completionHandler)
174
182
& 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
177
184
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
178
192
defaultTimeout = 10_000 -- 10s
179
193
mkHandler ::
180
194
forall m .
0 commit comments