Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
fendor authored and Jana Chadt committed Oct 8, 2022
1 parent 3af4ccf commit 307ce71
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import Control.Monad.IO.Class
import qualified Data.ByteString as BS
import Data.Hashable
import qualified Data.List.NonEmpty as NE
import Data.Maybe (catMaybes)
import Data.Maybe (mapMaybe)
import qualified Data.Text.Encoding as Encoding
import Data.Typeable
import Development.IDE as D
Expand Down Expand Up @@ -69,33 +69,25 @@ descriptor recorder plId = (defaultCabalPluginDescriptor plId)
\ide vfs _ (DidOpenTextDocumentParams TextDocumentItem{_uri,_version}) -> liftIO $ do
whenUriFile _uri $ \file -> do
log' Debug $ LogDocOpened _uri
join $ atomically $ Shake.recordDirtyKeys (shakeExtras ide) GetModificationTime [file]
restartShakeSession (shakeExtras ide) (VFSModified vfs) (fromNormalizedFilePath file ++ " (opened)") []
join $ Shake.shakeEnqueue (shakeExtras ide) $ Shake.mkDelayedAction "cabal parse modified" Info $ void $ use ParseCabal file
restartCabalShakeSession ide vfs file "(opened)"

, mkPluginNotificationHandler LSP.STextDocumentDidChange $
\ide vfs _ (DidChangeTextDocumentParams VersionedTextDocumentIdentifier{_uri} _) -> liftIO $ do
whenUriFile _uri $ \file -> do
log' Debug $ LogDocModified _uri
join $ atomically $ Shake.recordDirtyKeys (shakeExtras ide) GetModificationTime [file]
restartShakeSession (shakeExtras ide) (VFSModified vfs) (fromNormalizedFilePath file ++ " (modified)") []
join $ Shake.shakeEnqueue (shakeExtras ide) $ Shake.mkDelayedAction "cabal parse modified" Info $ void $ use ParseCabal file
restartCabalShakeSession ide vfs file "(changed)"

, mkPluginNotificationHandler LSP.STextDocumentDidSave $
\ide vfs _ (DidSaveTextDocumentParams TextDocumentIdentifier{_uri} _) -> liftIO $ do
whenUriFile _uri $ \file -> do
log' Debug $ LogDocSaved _uri
join $ atomically $ Shake.recordDirtyKeys (shakeExtras ide) GetModificationTime [file]
restartShakeSession (shakeExtras ide) (VFSModified vfs) (fromNormalizedFilePath file ++ " (saved)") []
join $ Shake.shakeEnqueue (shakeExtras ide) $ Shake.mkDelayedAction "cabal parse modified" Info $ void $ use ParseCabal file
restartCabalShakeSession ide vfs file "(saved)"

, mkPluginNotificationHandler LSP.STextDocumentDidClose $
\ide vfs _ (DidCloseTextDocumentParams TextDocumentIdentifier{_uri}) -> liftIO $ do
whenUriFile _uri $ \file -> do
log' Debug $ LogDocClosed _uri
join $ atomically $ Shake.recordDirtyKeys (shakeExtras ide) GetModificationTime [file]
restartShakeSession (shakeExtras ide) (VFSModified vfs) (fromNormalizedFilePath file ++ " (closed)") []
join $ Shake.shakeEnqueue (shakeExtras ide) $ Shake.mkDelayedAction "cabal parse modified" Info $ void $ use ParseCabal file
restartCabalShakeSession ide vfs file "(closed)"
]
}
where
Expand All @@ -104,6 +96,15 @@ descriptor recorder plId = (defaultCabalPluginDescriptor plId)
whenUriFile :: Uri -> (NormalizedFilePath -> IO ()) -> IO ()
whenUriFile uri act = whenJust (LSP.uriToFilePath uri) $ act . toNormalizedFilePath'

-- | Helper function to restart the shake session, specifically for modifying .cabal files.
-- No special logic, just group up a bunch of functions you need for the base
-- Notification Handlers.
restartCabalShakeSession :: IdeState -> VFS.VFS -> NormalizedFilePath -> String -> IO ()
restartCabalShakeSession ide vfs file actionMsg = do
join $ atomically $ Shake.recordDirtyKeys (shakeExtras ide) GetModificationTime [file]
restartShakeSession (shakeExtras ide) (VFSModified vfs) (fromNormalizedFilePath file ++ " " ++ actionMsg) []
join $ Shake.shakeEnqueue (shakeExtras ide) $ Shake.mkDelayedAction "cabal parse modified" Info $ void $ use ParseCabal file

-- ----------------------------------------------------------------
-- Plugin Rules
-- ----------------------------------------------------------------
Expand Down Expand Up @@ -150,4 +151,4 @@ licenseSuggestCodeAction
-> CodeActionParams
-> LspM Config (Either ResponseError (ResponseResult 'TextDocumentCodeAction))
licenseSuggestCodeAction _ _ (CodeActionParams _ _ (TextDocumentIdentifier uri) _range CodeActionContext{_diagnostics=List diags}) =
pure $ Right $ List $ catMaybes $ map (fmap InR . LicenseSuggest.licenseErrorAction uri) diags
pure $ Right $ List $ mapMaybe (fmap InR . LicenseSuggest.licenseErrorAction uri) diags

0 comments on commit 307ce71

Please sign in to comment.