Skip to content

Commit 6906a94

Browse files
committed
Merge branch 'master' into cabal-go-to-modules-definition
2 parents 39b4389 + d32d3fa commit 6906a94

File tree

27 files changed

+674
-6
lines changed

27 files changed

+674
-6
lines changed

.github/actions/setup-build/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ runs:
3131
sudo chown -R $USER /usr/local/.ghcup
3232
shell: bash
3333

34-
- uses: haskell-actions/[email protected].5
34+
- uses: haskell-actions/[email protected].6
3535
id: HaskEnvSetup
3636
with:
3737
ghc-version : ${{ inputs.ghc }}

cabal.project

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ packages:
77
./hls-plugin-api
88
./hls-test-utils
99

10+
-- Only keep this until https://github.com/Bodigrim/cabal-add/issues/7
11+
-- is resolved
12+
source-repository-package
13+
type: git
14+
location: https://github.com/Bodigrim/cabal-add.git
15+
tag: 8c004e2a4329232f9824425f5472b2d6d7958bbd
16+
1017
index-state: 2024-06-29T00:00:00Z
1118

1219
tests: True

haskell-language-server.cabal

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ library hls-cabal-plugin
245245
Ide.Plugin.Cabal.Definition
246246
Ide.Plugin.Cabal.FieldSuggest
247247
Ide.Plugin.Cabal.LicenseSuggest
248+
Ide.Plugin.Cabal.CabalAdd
248249
Ide.Plugin.Cabal.Orphans
249250
Ide.Plugin.Cabal.Outline
250251
Ide.Plugin.Cabal.Parse
@@ -271,6 +272,12 @@ library hls-cabal-plugin
271272
, transformers
272273
, unordered-containers >=0.2.10.0
273274
, containers
275+
, cabal-add
276+
, process
277+
, aeson
278+
, Cabal
279+
, pretty
280+
274281
hs-source-dirs: plugins/hls-cabal-plugin/src
275282

276283
test-suite hls-cabal-plugin-tests
@@ -281,6 +288,7 @@ test-suite hls-cabal-plugin-tests
281288
hs-source-dirs: plugins/hls-cabal-plugin/test
282289
main-is: Main.hs
283290
other-modules:
291+
CabalAdd
284292
Completer
285293
Context
286294
Definition
@@ -298,6 +306,7 @@ test-suite hls-cabal-plugin-tests
298306
, lens
299307
, lsp-types
300308
, text
309+
, hls-plugin-api
301310

302311
-----------------------------
303312
-- class plugin

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

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{-# LANGUAGE OverloadedStrings #-}
55
{-# LANGUAGE TypeFamilies #-}
66

7-
module Ide.Plugin.Cabal (descriptor, Log (..)) where
7+
module Ide.Plugin.Cabal (descriptor, haskellInteractionDescriptor, Log (..)) where
88

99
import Control.Concurrent.Strict
1010
import Control.DeepSeq
@@ -50,6 +50,9 @@ import qualified Language.LSP.Protocol.Message as LSP
5050
import Language.LSP.Protocol.Types
5151
import qualified Language.LSP.VFS as VFS
5252

53+
import qualified Data.Text ()
54+
import qualified Ide.Plugin.Cabal.CabalAdd as CabalAdd
55+
5356
data Log
5457
= LogModificationTime NormalizedFilePath FileVersion
5558
| LogShake Shake.Log
@@ -60,6 +63,7 @@ data Log
6063
| LogFOI (HashMap NormalizedFilePath FileOfInterestStatus)
6164
| LogCompletionContext Types.Context Position
6265
| LogCompletions Types.Log
66+
| LogCabalAdd CabalAdd.Log
6367
deriving (Show)
6468

6569
instance Pretty Log where
@@ -83,6 +87,25 @@ instance Pretty Log where
8387
<+> "for cursor position:"
8488
<+> pretty position
8589
LogCompletions logs -> pretty logs
90+
LogCabalAdd logs -> pretty logs
91+
92+
-- | Some actions with cabal files originate from haskell files.
93+
-- This descriptor allows to hook into the diagnostics of haskell source files, and
94+
-- allows us to provide code actions and commands that interact with `.cabal` files.
95+
haskellInteractionDescriptor :: Recorder (WithPriority Log) -> PluginId -> PluginDescriptor IdeState
96+
haskellInteractionDescriptor recorder plId =
97+
(defaultPluginDescriptor plId "Provides the cabal-add code action in haskell files")
98+
{ pluginHandlers =
99+
mconcat
100+
[ mkPluginHandler LSP.SMethod_TextDocumentCodeAction cabalAddCodeAction
101+
]
102+
, pluginCommands = [PluginCommand CabalAdd.cabalAddCommand "add a dependency to a cabal file" (CabalAdd.command cabalAddRecorder)]
103+
, pluginRules = pure ()
104+
, pluginNotificationHandlers = mempty
105+
}
106+
where
107+
cabalAddRecorder = cmapWithPrio LogCabalAdd recorder
108+
86109

87110
descriptor :: Recorder (WithPriority Log) -> PluginId -> PluginDescriptor IdeState
88111
descriptor recorder plId =
@@ -280,6 +303,32 @@ fieldSuggestCodeAction recorder ide _ (CodeActionParams _ _ (TextDocumentIdentif
280303
pure $ FieldSuggest.fieldErrorAction uri fieldName completionTexts _range
281304

282305

306+
cabalAddCodeAction :: PluginMethodHandler IdeState 'LSP.Method_TextDocumentCodeAction
307+
cabalAddCodeAction state plId (CodeActionParams _ _ (TextDocumentIdentifier uri) _ CodeActionContext{_diagnostics=diags}) = do
308+
maxCompls <- fmap maxCompletions . liftIO $ runAction "cabal.cabal-add" state getClientConfigAction
309+
let suggestions = take maxCompls $ concatMap CabalAdd.hiddenPackageSuggestion diags
310+
case suggestions of
311+
[] -> pure $ InL []
312+
_ ->
313+
case uriToFilePath uri of
314+
Nothing -> pure $ InL []
315+
Just haskellFilePath -> do
316+
mbCabalFile <- liftIO $ CabalAdd.findResponsibleCabalFile haskellFilePath
317+
case mbCabalFile of
318+
Nothing -> pure $ InL []
319+
Just cabalFilePath -> do
320+
verTxtDocId <- lift $ pluginGetVersionedTextDoc $ TextDocumentIdentifier (filePathToUri cabalFilePath)
321+
mbGPD <- liftIO $ runAction "cabal.cabal-add" state $ useWithStale ParseCabalFile $ toNormalizedFilePath cabalFilePath
322+
case mbGPD of
323+
Nothing -> pure $ InL []
324+
Just (gpd, _) -> do
325+
actions <- liftIO $ CabalAdd.addDependencySuggestCodeAction plId verTxtDocId
326+
suggestions
327+
haskellFilePath cabalFilePath
328+
gpd
329+
pure $ InL $ fmap InR actions
330+
331+
283332
-- ----------------------------------------------------------------
284333
-- Cabal file of Interest rules and global variable
285334
-- ----------------------------------------------------------------

0 commit comments

Comments
 (0)