Skip to content

Commit 095d271

Browse files
committed
Refactor Cabal Completion module structure
Add Types and FilePathCompletion modules
1 parent ae0f7b9 commit 095d271

File tree

6 files changed

+401
-367
lines changed

6 files changed

+401
-367
lines changed

plugins/hls-cabal-plugin/hls-cabal-plugin.cabal

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ library
2828
Ide.Plugin.Cabal
2929
Ide.Plugin.Cabal.Diagnostics
3030
Ide.Plugin.Cabal.Completions
31+
Ide.Plugin.Cabal.FilepathCompletions
3132
Ide.Plugin.Cabal.LicenseSuggest
3233
Ide.Plugin.Cabal.Parse
34+
Ide.Plugin.Cabal.Types
35+
3336

3437
build-depends:
3538
, base >=4.12 && <5

plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal.hs

Lines changed: 65 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import qualified Ide.Plugin.Cabal.Completions as Completions
3434
import qualified Ide.Plugin.Cabal.Diagnostics as Diagnostics
3535
import qualified Ide.Plugin.Cabal.LicenseSuggest as LicenseSuggest
3636
import qualified Ide.Plugin.Cabal.Parse as Parse
37+
import qualified Ide.Plugin.Cabal.Types as Types
3738
import Ide.Types
3839
import qualified Language.LSP.Protocol.Lens as JL
3940
import qualified Language.LSP.Protocol.Message as LSP
@@ -49,8 +50,8 @@ data Log
4950
| LogDocSaved Uri
5051
| LogDocClosed Uri
5152
| LogFOI (HashMap NormalizedFilePath FileOfInterestStatus)
52-
| LogCompletionContext Completions.Context Position
53-
| LogCompletions Completions.Log
53+
| LogCompletionContext Types.Context Position
54+
| LogCompletions Types.Log
5455
deriving (Show)
5556

5657
instance Pretty Log where
@@ -68,62 +69,65 @@ instance Pretty Log where
6869
"Closed text document:" <+> pretty (getUri uri)
6970
LogFOI files ->
7071
"Set files of interest to:" <+> viaShow files
71-
LogCompletionContext context position->
72-
"Determined completion context:" <+> viaShow context
73-
<+> "for cursor position:" <+> viaShow position
72+
LogCompletionContext context position ->
73+
"Determined completion context:"
74+
<+> viaShow context
75+
<+> "for cursor position:"
76+
<+> viaShow position
7477
LogCompletions logs -> pretty logs
7578

7679
descriptor :: Recorder (WithPriority Log) -> PluginId -> PluginDescriptor IdeState
77-
descriptor recorder plId = (defaultCabalPluginDescriptor plId)
78-
{ pluginRules = cabalRules recorder
79-
, pluginHandlers = mconcat
80-
[ mkPluginHandler LSP.SMethod_TextDocumentCodeAction licenseSuggestCodeAction
81-
, mkPluginHandler LSP.SMethod_TextDocumentCompletion $ completion recorder
82-
]
83-
, pluginNotificationHandlers = mconcat
84-
[ mkPluginNotificationHandler LSP.SMethod_TextDocumentDidOpen $
85-
\ide vfs _ (DidOpenTextDocumentParams TextDocumentItem{_uri,_version}) -> liftIO $ do
86-
whenUriFile _uri $ \file -> do
87-
log' Debug $ LogDocOpened _uri
88-
addFileOfInterest recorder ide file Modified{firstOpen=True}
89-
restartCabalShakeSession (shakeExtras ide) vfs file "(opened)"
90-
91-
, mkPluginNotificationHandler LSP.SMethod_TextDocumentDidChange $
92-
\ide vfs _ (DidChangeTextDocumentParams VersionedTextDocumentIdentifier{_uri} _) -> liftIO $ do
93-
whenUriFile _uri $ \file -> do
94-
log' Debug $ LogDocModified _uri
95-
addFileOfInterest recorder ide file Modified{firstOpen=False}
96-
restartCabalShakeSession (shakeExtras ide) vfs file "(changed)"
97-
98-
, mkPluginNotificationHandler LSP.SMethod_TextDocumentDidSave $
99-
\ide vfs _ (DidSaveTextDocumentParams TextDocumentIdentifier{_uri} _) -> liftIO $ do
100-
whenUriFile _uri $ \file -> do
101-
log' Debug $ LogDocSaved _uri
102-
addFileOfInterest recorder ide file OnDisk
103-
restartCabalShakeSession (shakeExtras ide) vfs file "(saved)"
104-
105-
, mkPluginNotificationHandler LSP.SMethod_TextDocumentDidClose $
106-
\ide vfs _ (DidCloseTextDocumentParams TextDocumentIdentifier{_uri}) -> liftIO $ do
107-
whenUriFile _uri $ \file -> do
108-
log' Debug $ LogDocClosed _uri
109-
deleteFileOfInterest recorder ide file
110-
restartCabalShakeSession (shakeExtras ide) vfs file "(closed)"
111-
]
112-
}
113-
where
114-
log' = logWith recorder
115-
116-
whenUriFile :: Uri -> (NormalizedFilePath -> IO ()) -> IO ()
117-
whenUriFile uri act = whenJust (uriToFilePath uri) $ act . toNormalizedFilePath'
118-
119-
-- | Helper function to restart the shake session, specifically for modifying .cabal files.
120-
-- No special logic, just group up a bunch of functions you need for the base
121-
-- Notification Handlers.
122-
--
123-
-- To make sure diagnostics are up to date, we need to tell shake that the file was touched and
124-
-- needs to be re-parsed. That's what we do when we record the dirty key that our parsing
125-
-- rule depends on.
126-
-- Then we restart the shake session, so that changes to our virtual files are actually picked up.
80+
descriptor recorder plId =
81+
(defaultCabalPluginDescriptor plId)
82+
{ pluginRules = cabalRules recorder
83+
, pluginHandlers =
84+
mconcat
85+
[ mkPluginHandler LSP.SMethod_TextDocumentCodeAction licenseSuggestCodeAction
86+
, mkPluginHandler LSP.SMethod_TextDocumentCompletion $ completion recorder
87+
]
88+
, pluginNotificationHandlers =
89+
mconcat
90+
[ mkPluginNotificationHandler LSP.SMethod_TextDocumentDidOpen $
91+
\ide vfs _ (DidOpenTextDocumentParams TextDocumentItem{_uri, _version}) -> liftIO $ do
92+
whenUriFile _uri $ \file -> do
93+
log' Debug $ LogDocOpened _uri
94+
addFileOfInterest recorder ide file Modified{firstOpen = True}
95+
restartCabalShakeSession (shakeExtras ide) vfs file "(opened)"
96+
, mkPluginNotificationHandler LSP.SMethod_TextDocumentDidChange $
97+
\ide vfs _ (DidChangeTextDocumentParams VersionedTextDocumentIdentifier{_uri} _) -> liftIO $ do
98+
whenUriFile _uri $ \file -> do
99+
log' Debug $ LogDocModified _uri
100+
addFileOfInterest recorder ide file Modified{firstOpen = False}
101+
restartCabalShakeSession (shakeExtras ide) vfs file "(changed)"
102+
, mkPluginNotificationHandler LSP.SMethod_TextDocumentDidSave $
103+
\ide vfs _ (DidSaveTextDocumentParams TextDocumentIdentifier{_uri} _) -> liftIO $ do
104+
whenUriFile _uri $ \file -> do
105+
log' Debug $ LogDocSaved _uri
106+
addFileOfInterest recorder ide file OnDisk
107+
restartCabalShakeSession (shakeExtras ide) vfs file "(saved)"
108+
, mkPluginNotificationHandler LSP.SMethod_TextDocumentDidClose $
109+
\ide vfs _ (DidCloseTextDocumentParams TextDocumentIdentifier{_uri}) -> liftIO $ do
110+
whenUriFile _uri $ \file -> do
111+
log' Debug $ LogDocClosed _uri
112+
deleteFileOfInterest recorder ide file
113+
restartCabalShakeSession (shakeExtras ide) vfs file "(closed)"
114+
]
115+
}
116+
where
117+
log' = logWith recorder
118+
119+
whenUriFile :: Uri -> (NormalizedFilePath -> IO ()) -> IO ()
120+
whenUriFile uri act = whenJust (uriToFilePath uri) $ act . toNormalizedFilePath'
121+
122+
{- | Helper function to restart the shake session, specifically for modifying .cabal files.
123+
No special logic, just group up a bunch of functions you need for the base
124+
Notification Handlers.
125+
126+
To make sure diagnostics are up to date, we need to tell shake that the file was touched and
127+
needs to be re-parsed. That's what we do when we record the dirty key that our parsing
128+
rule depends on.
129+
Then we restart the shake session, so that changes to our virtual files are actually picked up.
130+
-}
127131
restartCabalShakeSession :: ShakeExtras -> VFS.VFS -> NormalizedFilePath -> String -> IO ()
128132
restartCabalShakeSession shakeExtras vfs file actionMsg = do
129133
join $ atomically $ Shake.recordDirtyKeys shakeExtras GetModificationTime [file]
@@ -190,12 +194,12 @@ kick = do
190194
-- Code Actions
191195
-- ----------------------------------------------------------------
192196

193-
licenseSuggestCodeAction
194-
:: IdeState
195-
-> PluginId
196-
-> CodeActionParams
197-
-> LspM Config (Either LSP.ResponseError (LSP.MessageResult 'LSP.Method_TextDocumentCodeAction))
198-
licenseSuggestCodeAction _ _ (CodeActionParams _ _ (TextDocumentIdentifier uri) _range CodeActionContext{_diagnostics=diags}) =
197+
licenseSuggestCodeAction ::
198+
IdeState ->
199+
PluginId ->
200+
CodeActionParams ->
201+
LspM Config (Either LSP.ResponseError (LSP.MessageResult 'LSP.Method_TextDocumentCodeAction))
202+
licenseSuggestCodeAction _ _ (CodeActionParams _ _ (TextDocumentIdentifier uri) _range CodeActionContext{_diagnostics = diags}) =
199203
pure $ Right $ InL $ diags >>= (fmap InR . LicenseSuggest.licenseErrorAction uri)
200204

201205
-- ----------------------------------------------------------------

0 commit comments

Comments
 (0)