|
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 as T |
| 22 | +import qualified Data.Text.Encoding as Encoding |
| 23 | +import Data.Typeable |
| 24 | +import Development.IDE as D |
| 25 | +import Development.IDE.Core.Shake (restartShakeSession) |
| 26 | +import qualified Development.IDE.Core.Shake as Shake |
20 | 27 | import GHC.Generics
|
21 |
| -import Ide.PluginUtils |
| 28 | +import qualified Ide.Plugin.Cabal.Diag as Diag |
| 29 | +import qualified Ide.Plugin.Cabal.LicenseSuggest as LicenseSuggest |
| 30 | +import qualified Ide.Plugin.Cabal.Parse as Parse |
| 31 | +import Ide.Plugin.Config (Config) |
22 | 32 | import Ide.Types
|
| 33 | +import Language.LSP.Server (LspM) |
23 | 34 | import Language.LSP.Types
|
| 35 | +import qualified Language.LSP.Types as LSP |
| 36 | +import qualified Language.LSP.VFS as VFS |
24 | 37 |
|
25 |
| - |
26 |
| -newtype Log = LogText T.Text deriving Show |
| 38 | +data Log |
| 39 | + = LogText T.Text |
| 40 | + | LogShake Shake.Log deriving Show |
27 | 41 |
|
28 | 42 | instance Pretty Log where
|
29 | 43 | pretty = \case
|
30 |
| - LogText log -> pretty log |
| 44 | + LogShake log' -> pretty log' |
| 45 | + LogText log' -> pretty log' |
31 | 46 |
|
32 | 47 | descriptor :: Recorder (WithPriority Log) -> PluginId -> PluginDescriptor IdeState
|
33 | 48 | descriptor recorder plId = (defaultCabalPluginDescriptor plId)
|
34 |
| - { pluginHandlers = mkPluginHandler STextDocumentCodeLens (codeLens recorder) |
35 |
| - } |
| 49 | + { pluginRules = exampleRules recorder |
| 50 | + , pluginHandlers = mkPluginHandler STextDocumentCodeAction licenseSuggest |
| 51 | + , pluginNotificationHandlers = mconcat |
| 52 | + [ mkPluginNotificationHandler LSP.STextDocumentDidOpen $ |
| 53 | + \ide vfs _ (DidOpenTextDocumentParams TextDocumentItem{_uri,_version}) -> liftIO $ do |
| 54 | + whenUriFile _uri $ \file -> do |
| 55 | + logDebug (ideLogger ide) $ "Opened text document: " <> getUri _uri |
| 56 | + join $ atomically $ Shake.recordDirtyKeys (shakeExtras ide) GetModificationTime [file] |
| 57 | + restartShakeSession (shakeExtras ide) (VFSModified vfs) (fromNormalizedFilePath file ++ " (opened)") [Shake.mkDelayedAction "cabal parse open" Info $ void $ use Example file] |
| 58 | + join $ Shake.shakeEnqueue (shakeExtras ide) $ Shake.mkDelayedAction "cabal parse modified" Info $ void $ use Example file |
36 | 59 |
|
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 [] |
51 |
| - where |
52 |
| - log = logWith recorder |
53 |
| --- --------------------------------------------------------------------- |
| 60 | + , mkPluginNotificationHandler LSP.STextDocumentDidChange $ |
| 61 | + \ide vfs _ (DidChangeTextDocumentParams VersionedTextDocumentIdentifier{_uri} _) -> liftIO $ do |
| 62 | + whenUriFile _uri $ \file -> do |
| 63 | + logDebug (ideLogger ide) $ "Modified text document: " <> getUri _uri |
| 64 | + logDebug (ideLogger ide) $ "VFS State: " <> T.pack (show vfs) |
| 65 | + join $ atomically $ Shake.recordDirtyKeys (shakeExtras ide) GetModificationTime [file] |
| 66 | + restartShakeSession (shakeExtras ide) (VFSModified vfs) (fromNormalizedFilePath file ++ " (modified)") [Shake.mkDelayedAction "cabal parse modified" Info $ void $ use Example file] |
| 67 | + join $ Shake.shakeEnqueue (shakeExtras ide) $ Shake.mkDelayedAction "cabal parse modified" Info $ void $ use Example file |
54 | 68 |
|
55 |
| -data AddTodoParams = AddTodoParams |
56 |
| - { file :: Uri -- ^ Uri of the file to add the pragma to |
57 |
| - , todoText :: T.Text |
| 69 | + , mkPluginNotificationHandler LSP.STextDocumentDidSave $ |
| 70 | + \ide vfs _ (DidSaveTextDocumentParams TextDocumentIdentifier{_uri} _) -> liftIO $ do |
| 71 | + whenUriFile _uri $ \file -> do |
| 72 | + logDebug (ideLogger ide) $ "Saved text document: " <> getUri _uri |
| 73 | + join $ atomically $ Shake.recordDirtyKeys (shakeExtras ide) GetModificationTime [file] |
| 74 | + restartShakeSession (shakeExtras ide) (VFSModified vfs) (fromNormalizedFilePath file ++ " (saved)") [Shake.mkDelayedAction "cabal parse saved" Info $ void $ use Example file] |
| 75 | + join $ Shake.shakeEnqueue (shakeExtras ide) $ Shake.mkDelayedAction "cabal parse modified" Info $ void $ use Example file |
| 76 | + |
| 77 | + , mkPluginNotificationHandler LSP.STextDocumentDidClose $ |
| 78 | + \ide vfs _ (DidCloseTextDocumentParams TextDocumentIdentifier{_uri}) -> liftIO $ do |
| 79 | + whenUriFile _uri $ \file -> do |
| 80 | + let msg = "Closed text document: " <> getUri _uri |
| 81 | + logDebug (ideLogger ide) msg |
| 82 | + join $ atomically $ Shake.recordDirtyKeys (shakeExtras ide) GetModificationTime [file] |
| 83 | + restartShakeSession (shakeExtras ide) (VFSModified vfs) (fromNormalizedFilePath file ++ " (closed)") [Shake.mkDelayedAction "cabal parse closed" Info $ void $ use Example file] |
| 84 | + join $ Shake.shakeEnqueue (shakeExtras ide) $ Shake.mkDelayedAction "cabal parse modified" Info $ void $ use Example file |
| 85 | + ] |
58 | 86 | }
|
59 |
| - deriving (Show, Eq, Generic, ToJSON, FromJSON) |
| 87 | + where |
| 88 | + whenUriFile :: Uri -> (NormalizedFilePath -> IO ()) -> IO () |
| 89 | + whenUriFile uri act = whenJust (LSP.uriToFilePath uri) $ act . toNormalizedFilePath' |
| 90 | + licenseSuggest |
| 91 | + :: IdeState |
| 92 | + -> PluginId |
| 93 | + -> CodeActionParams |
| 94 | + -> LspM Config (Either ResponseError (ResponseResult 'TextDocumentCodeAction)) |
| 95 | + licenseSuggest _ _ (CodeActionParams _ _ (TextDocumentIdentifier uri) _range CodeActionContext{_diagnostics=List diags}) = |
| 96 | + pure $ Right $ List $ catMaybes $ map (fmap InR . LicenseSuggest.licenseErrorAction uri) diags |
| 97 | + |
| 98 | +data Example = Example |
| 99 | + deriving (Eq, Show, Typeable, Generic) |
| 100 | +instance Hashable Example |
| 101 | +instance NFData Example |
| 102 | + |
| 103 | +type instance RuleResult Example = () |
| 104 | + |
| 105 | +exampleRules :: Recorder (WithPriority Log) -> Rules () |
| 106 | +exampleRules recorder = do |
| 107 | + define (cmapWithPrio LogShake recorder) $ \Example file -> do |
| 108 | + t <- use GetModificationTime file |
| 109 | + logWith recorder Debug $ LogText $ "Parse: " <> T.pack (show file) <> " " <> T.pack (show t) |
| 110 | + mVirtualFile <- Shake.getVirtualFile file |
| 111 | + contents <- case mVirtualFile of |
| 112 | + Just vfile -> pure $ Encoding.encodeUtf8 $ VFS.virtualFileText vfile |
| 113 | + Nothing -> do |
| 114 | + liftIO $ BS.readFile $ fromNormalizedFilePath file |
| 115 | + |
| 116 | + _pm <- liftIO $ Parse.parseCabalFileContents contents |
| 117 | + liftIO $ log' Debug $ LogText $ T.pack $ "Parsed file: " <> fromNormalizedFilePath file <> ". Result: " <> show _pm |
| 118 | + let diagLst = case _pm of |
| 119 | + (_, Left (_, pErrorNE)) -> |
| 120 | + NE.toList $ NE.map (Diag.errorDiag file) pErrorNE |
| 121 | + _ -> [] |
| 122 | + logWith recorder Debug $ LogText $ "Diagnostics: " <> T.pack (show diagLst) |
| 123 | + return (diagLst, Just ()) |
| 124 | + where |
| 125 | + log' = logWith recorder |
0 commit comments