|
1 | 1 | {-# LANGUAGE DataKinds #-}
|
2 |
| -{-# LANGUAGE DeriveAnyClass #-} |
3 | 2 | {-# LANGUAGE DeriveGeneric #-}
|
4 | 3 | {-# LANGUAGE DuplicateRecordFields #-}
|
5 | 4 | {-# LANGUAGE FlexibleContexts #-}
|
6 | 5 | {-# LANGUAGE FlexibleInstances #-}
|
7 | 6 | {-# LANGUAGE LambdaCase #-}
|
| 7 | +{-# LANGUAGE NamedFieldPuns #-} |
8 | 8 | {-# LANGUAGE OverloadedStrings #-}
|
9 |
| -{-# LANGUAGE RecordWildCards #-} |
10 |
| -{-# LANGUAGE TupleSections #-} |
11 | 9 | {-# LANGUAGE TypeFamilies #-}
|
12 |
| -{-# LANGUAGE ViewPatterns #-} |
13 | 10 |
|
14 | 11 | module Ide.Plugin.Cabal where
|
15 | 12 |
|
| 13 | +import Control.Concurrent.STM |
| 14 | +import Control.DeepSeq (NFData) |
| 15 | +import Control.Monad.Extra |
16 | 16 | import Control.Monad.IO.Class
|
17 |
| -import Data.Aeson |
18 |
| -import qualified Data.Text as T |
19 |
| -import Development.IDE as D |
| 17 | +import qualified Data.ByteString as BS |
| 18 | +import Data.Hashable |
| 19 | +import qualified Data.List.NonEmpty as NE |
| 20 | +import Data.Maybe (catMaybes) |
| 21 | +import qualified Data.Text.Encoding as Encoding |
| 22 | +import Data.Typeable |
| 23 | +import Development.IDE as D |
| 24 | +import Development.IDE.Core.Shake (restartShakeSession) |
| 25 | +import qualified Development.IDE.Core.Shake as Shake |
20 | 26 | import GHC.Generics
|
21 |
| -import Ide.PluginUtils |
| 27 | +import qualified Ide.Plugin.Cabal.Diag as Diag |
| 28 | +import qualified Ide.Plugin.Cabal.LicenseSuggest as LicenseSuggest |
| 29 | +import qualified Ide.Plugin.Cabal.Parse as Parse |
| 30 | +import Ide.Plugin.Config (Config) |
22 | 31 | import Ide.Types
|
| 32 | +import Language.LSP.Server (LspM) |
23 | 33 | import Language.LSP.Types
|
| 34 | +import qualified Language.LSP.Types as LSP |
| 35 | +import qualified Language.LSP.VFS as VFS |
24 | 36 |
|
25 |
| - |
26 |
| -newtype Log = LogText T.Text deriving Show |
| 37 | +data Log |
| 38 | + = LogModificationTime NormalizedFilePath (Maybe FileVersion) |
| 39 | + | LogDiagnostics NormalizedFilePath [FileDiagnostic] |
| 40 | + | LogShake Shake.Log |
| 41 | + deriving Show |
27 | 42 |
|
28 | 43 | instance Pretty Log where
|
29 | 44 | pretty = \case
|
30 |
| - LogText log -> pretty log |
| 45 | + LogShake log' -> pretty log' |
| 46 | + LogModificationTime nfp modTime -> |
| 47 | + "Modified:" <+> pretty (fromNormalizedFilePath nfp) <+> pretty (show modTime) |
| 48 | + LogDiagnostics nfp diags -> |
| 49 | + "Diagnostics for " <+> pretty (fromNormalizedFilePath nfp) <> ":" <+> pretty (show diags) |
31 | 50 |
|
32 | 51 | descriptor :: Recorder (WithPriority Log) -> PluginId -> PluginDescriptor IdeState
|
33 | 52 | descriptor recorder plId = (defaultCabalPluginDescriptor plId)
|
34 |
| - { pluginHandlers = mkPluginHandler STextDocumentCodeLens (codeLens recorder) |
| 53 | + { pluginRules = cabalRules recorder |
| 54 | + , pluginHandlers = mkPluginHandler STextDocumentCodeAction licenseSuggestCodeAction |
| 55 | + , pluginNotificationHandlers = mconcat |
| 56 | + [ mkPluginNotificationHandler LSP.STextDocumentDidOpen $ |
| 57 | + \ide vfs _ (DidOpenTextDocumentParams TextDocumentItem{_uri,_version}) -> liftIO $ do |
| 58 | + whenUriFile _uri $ \file -> do |
| 59 | + logDebug (ideLogger ide) $ "Opened text document: " <> getUri _uri |
| 60 | + join $ atomically $ Shake.recordDirtyKeys (shakeExtras ide) GetModificationTime [file] |
| 61 | + restartShakeSession (shakeExtras ide) (VFSModified vfs) (fromNormalizedFilePath file ++ " (opened)") [] |
| 62 | + join $ Shake.shakeEnqueue (shakeExtras ide) $ Shake.mkDelayedAction "cabal parse modified" Info $ void $ use ParseCabal file |
| 63 | + |
| 64 | + , mkPluginNotificationHandler LSP.STextDocumentDidChange $ |
| 65 | + \ide vfs _ (DidChangeTextDocumentParams VersionedTextDocumentIdentifier{_uri} _) -> liftIO $ do |
| 66 | + whenUriFile _uri $ \file -> do |
| 67 | + logDebug (ideLogger ide) $ "Modified text document: " <> getUri _uri |
| 68 | + logDebug (ideLogger ide) $ "VFS State: " <> T.pack (show vfs) |
| 69 | + join $ atomically $ Shake.recordDirtyKeys (shakeExtras ide) GetModificationTime [file] |
| 70 | + restartShakeSession (shakeExtras ide) (VFSModified vfs) (fromNormalizedFilePath file ++ " (modified)") [] |
| 71 | + join $ Shake.shakeEnqueue (shakeExtras ide) $ Shake.mkDelayedAction "cabal parse modified" Info $ void $ use ParseCabal file |
| 72 | + |
| 73 | + , mkPluginNotificationHandler LSP.STextDocumentDidSave $ |
| 74 | + \ide vfs _ (DidSaveTextDocumentParams TextDocumentIdentifier{_uri} _) -> liftIO $ do |
| 75 | + whenUriFile _uri $ \file -> do |
| 76 | + logDebug (ideLogger ide) $ "Saved text document: " <> getUri _uri |
| 77 | + join $ atomically $ Shake.recordDirtyKeys (shakeExtras ide) GetModificationTime [file] |
| 78 | + restartShakeSession (shakeExtras ide) (VFSModified vfs) (fromNormalizedFilePath file ++ " (saved)") [] |
| 79 | + join $ Shake.shakeEnqueue (shakeExtras ide) $ Shake.mkDelayedAction "cabal parse modified" Info $ void $ use ParseCabal file |
| 80 | + |
| 81 | + , mkPluginNotificationHandler LSP.STextDocumentDidClose $ |
| 82 | + \ide vfs _ (DidCloseTextDocumentParams TextDocumentIdentifier{_uri}) -> liftIO $ do |
| 83 | + whenUriFile _uri $ \file -> do |
| 84 | + let msg = "Closed text document: " <> getUri _uri |
| 85 | + logDebug (ideLogger ide) msg |
| 86 | + join $ atomically $ Shake.recordDirtyKeys (shakeExtras ide) GetModificationTime [file] |
| 87 | + restartShakeSession (shakeExtras ide) (VFSModified vfs) (fromNormalizedFilePath file ++ " (closed)") [] |
| 88 | + join $ Shake.shakeEnqueue (shakeExtras ide) $ Shake.mkDelayedAction "cabal parse modified" Info $ void $ use ParseCabal file |
| 89 | + ] |
35 | 90 | }
|
| 91 | + where |
| 92 | + whenUriFile :: Uri -> (NormalizedFilePath -> IO ()) -> IO () |
| 93 | + whenUriFile uri act = whenJust (LSP.uriToFilePath uri) $ act . toNormalizedFilePath' |
| 94 | + |
| 95 | +-- ---------------------------------------------------------------- |
| 96 | +-- Plugin Rules |
| 97 | +-- ---------------------------------------------------------------- |
36 | 98 |
|
37 |
| --- --------------------------------------------------------------------- |
38 |
| - |
39 |
| -codeLens :: Recorder (WithPriority Log) -> PluginMethodHandler IdeState TextDocumentCodeLens |
40 |
| -codeLens recorder _ideState plId CodeLensParams{_textDocument=TextDocumentIdentifier uri} = liftIO $ do |
41 |
| - log Debug $ LogText "ExampleCabal.codeLens entered (ideLogger)" |
42 |
| - case uriToFilePath' uri of |
43 |
| - Just (toNormalizedFilePath -> _filePath) -> do |
44 |
| - let |
45 |
| - title = "Add TODO Item via Code Lens" |
46 |
| - range = Range (Position 3 0) (Position 4 0) |
47 |
| - let cmdParams = AddTodoParams uri "do abc" |
48 |
| - cmd = mkLspCommand plId "codelens.todo" title (Just [toJSON cmdParams]) |
49 |
| - pure $ Right $ List [ CodeLens range (Just cmd) Nothing ] |
50 |
| - Nothing -> pure $ Right $ List [] |
| 99 | +data ParseCabal = ParseCabal |
| 100 | + deriving (Eq, Show, Typeable, Generic) |
| 101 | +instance Hashable ParseCabal |
| 102 | +instance NFData ParseCabal |
| 103 | + |
| 104 | +type instance RuleResult ParseCabal = () |
| 105 | + |
| 106 | +cabalRules :: Recorder (WithPriority Log) -> Rules () |
| 107 | +cabalRules recorder = do |
| 108 | + define (cmapWithPrio LogShake recorder) $ \ParseCabal file -> do |
| 109 | + t <- use GetModificationTime file |
| 110 | + log' Debug $ LogModificationTime file t |
| 111 | + mVirtualFile <- Shake.getVirtualFile file |
| 112 | + contents <- case mVirtualFile of |
| 113 | + Just vfile -> pure $ Encoding.encodeUtf8 $ VFS.virtualFileText vfile |
| 114 | + Nothing -> do |
| 115 | + liftIO $ BS.readFile $ fromNormalizedFilePath file |
| 116 | + |
| 117 | + pm <- liftIO $ Parse.parseCabalFileContents contents |
| 118 | + let diagLst = case pm of |
| 119 | + (pWarnings, Left (_, pErrorNE)) -> |
| 120 | + let warningDiags = fmap (Diag.warningDiag file) pWarnings |
| 121 | + errorDiags = NE.toList $ NE.map (Diag.errorDiag file) pErrorNE |
| 122 | + in warningDiags <> errorDiags |
| 123 | + _ -> [] |
| 124 | + log' Debug $ LogDiagnostics file diagLst |
| 125 | + return (diagLst, Just ()) |
51 | 126 | where
|
52 |
| - log = logWith recorder |
53 |
| --- --------------------------------------------------------------------- |
| 127 | + log' = logWith recorder |
54 | 128 |
|
55 |
| -data AddTodoParams = AddTodoParams |
56 |
| - { file :: Uri -- ^ Uri of the file to add the pragma to |
57 |
| - , todoText :: T.Text |
58 |
| - } |
59 |
| - deriving (Show, Eq, Generic, ToJSON, FromJSON) |
| 129 | +-- ---------------------------------------------------------------- |
| 130 | +-- Code Actions |
| 131 | +-- ---------------------------------------------------------------- |
| 132 | + |
| 133 | +licenseSuggestCodeAction |
| 134 | + :: IdeState |
| 135 | + -> PluginId |
| 136 | + -> CodeActionParams |
| 137 | + -> LspM Config (Either ResponseError (ResponseResult 'TextDocumentCodeAction)) |
| 138 | +licenseSuggestCodeAction _ _ (CodeActionParams _ _ (TextDocumentIdentifier uri) _range CodeActionContext{_diagnostics=List diags}) = |
| 139 | + pure $ Right $ List $ catMaybes $ map (fmap InR . LicenseSuggest.licenseErrorAction uri) diags |
0 commit comments