Skip to content

Commit 5cc3906

Browse files
committed
formatting and docs
1 parent 2ff597a commit 5cc3906

File tree

2 files changed

+85
-27
lines changed

2 files changed

+85
-27
lines changed

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

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,20 @@ import Development.IDE.Graph (Key,
3232
import qualified Development.IDE.Plugin.Completions.Logic as Ghcide
3333
import Development.IDE.Types.Shake (toKey)
3434
import qualified Distribution.Fields as Syntax
35+
import Distribution.PackageDescription (Benchmark (..),
36+
BuildInfo (..),
37+
Executable (..),
38+
ForeignLib (..),
39+
Library (..),
40+
LibraryName (LMainLibName, LSubLibName),
41+
PackageDescription (..),
42+
TestSuite (..),
43+
library,
44+
unUnqualComponentName)
45+
import Distribution.PackageDescription.Configuration (flattenPackageDescription)
3546
import qualified Distribution.Parsec.Position as Syntax
47+
import Distribution.Utils.Generic (safeHead)
48+
import Distribution.Utils.Path (getSymbolicPath)
3649
import GHC.Generics
3750
import Ide.Plugin.Cabal.Completion.CabalFields as CabalFields
3851
import qualified Ide.Plugin.Cabal.Completion.Completer.Types as CompleterTypes
@@ -52,23 +65,9 @@ import qualified Language.LSP.Protocol.Lens as JL
5265
import qualified Language.LSP.Protocol.Message as LSP
5366
import Language.LSP.Protocol.Types
5467
import qualified Language.LSP.VFS as VFS
55-
56-
import Debug.Trace
57-
import Distribution.PackageDescription (Benchmark (..),
58-
BuildInfo (..),
59-
Executable (..),
60-
ForeignLib (..),
61-
Library (..),
62-
LibraryName (LMainLibName, LSubLibName),
63-
PackageDescription (..),
64-
TestSuite (..),
65-
library,
66-
unUnqualComponentName)
67-
import Distribution.PackageDescription.Configuration (flattenPackageDescription)
68-
import Distribution.Utils.Path (getSymbolicPath)
69-
import System.Directory (doesFileExist)
70-
import System.FilePath ((</>), takeDirectory)
71-
import Distribution.Utils.Generic (safeHead)
68+
import System.Directory (doesFileExist)
69+
import System.FilePath (takeDirectory,
70+
(</>))
7271

7372
data Log
7473
= LogModificationTime NormalizedFilePath FileVersion
@@ -302,11 +301,12 @@ fieldSuggestCodeAction recorder ide _ (CodeActionParams _ _ (TextDocumentIdentif
302301

303302
-- | CodeActions for going to definitions.
304303
--
305-
-- Provides a CodeAction for going to a definition when clicking on an identifier.
304+
-- Provides a CodeAction for going to a definition when clicking on an identifier
305+
-- and clicking on exposed-module or other-module field.
306306
-- The definition is found by traversing the sections and comparing their name to
307-
-- the clicked identifier.
307+
-- the clicked identifier. If it's not in sections it attempts to find it in module names.
308308
--
309-
-- TODO: Support more definitions than sections.
309+
-- TODO: Resolve more cases for go-to definition.
310310
gotoDefinition :: PluginMethodHandler IdeState LSP.Method_TextDocumentDefinition
311311
gotoDefinition ideState _ msgParam = do
312312
case uriToFilePath' uri of
@@ -340,13 +340,10 @@ gotoDefinition ideState _ msgParam = do
340340
mBuildTargetNames
341341
sourceDirs = map getSymbolicPath $ concatMap hsSourceDirs buildInfos
342342
potentialPaths = map (\dir -> takeDirectory filePath </> dir </> toHaskellFile moduleName) sourceDirs
343-
traceShowM ("potentialPaths", potentialPaths)
344343
allPaths <- liftIO $ filterM doesFileExist potentialPaths
345-
traceShowM ("allPaths", allPaths)
346344
let locations = map (\pth -> Location (filePathToUri pth) (mkRange 0 0 0 0)) allPaths
347-
traceShowM ("locations", locations)
348-
case safeHead locations of
349-
Nothing -> pure $ InR $ InR Null
345+
case safeHead locations of -- We assume there could be only one source location
346+
Nothing -> pure $ InR $ InR Null
350347
Just location -> pure $ InL $ Definition $ InL location
351348
where
352349
cursor = Types.lspPositionToCabalPosition (msgParam ^. JL.position)

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

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import qualified Data.Text.Encoding as T
1010
import Data.Tuple (swap)
1111
import qualified Distribution.Fields as Syntax
1212
import qualified Distribution.Parsec.Position as Syntax
13-
import Ide.Plugin.Cabal.Completion.Types
14-
( cabalPositionToLSPPosition, FieldContext(None), StanzaContext )
13+
import Ide.Plugin.Cabal.Completion.Types (FieldContext (None),
14+
StanzaContext,
15+
cabalPositionToLSPPosition)
1516
import qualified Language.LSP.Protocol.Types as LSP
1617

1718
-- ----------------------------------------------------------------
@@ -142,6 +143,34 @@ getOptionalSectionName (x:xs) = case x of
142143
type BuildTargetName = T.Text
143144
type ModuleName = T.Text
144145

146+
-- | Given a cabal AST returns pairs of all respective target names
147+
-- and the module name bounded to them. If a target is a main library gives
148+
-- @Nothing@, otherwise @Just target-name@
149+
--
150+
-- Examples of input cabal files and the outputs:
151+
--
152+
-- * Target is a main library module:
153+
--
154+
-- > library
155+
-- > exposed-modules:
156+
-- > MyLib
157+
--
158+
-- * @getModulesNames@ output:
159+
--
160+
-- > [([Nothing], "MyLib")]
161+
--
162+
-- * Same module names in different targets:
163+
--
164+
-- > test-suite first-target
165+
-- > other-modules:
166+
-- > Config
167+
-- > test-suite second-target
168+
-- > other-modules:
169+
-- > Config
170+
--
171+
-- * @getModulesNames@ output:
172+
--
173+
-- > [([Just "first-target", Just "second-target"], "Config")]
145174
getModulesNames :: [Syntax.Field any] -> [([Maybe BuildTargetName], ModuleName)]
146175
getModulesNames fields = map swap $ groupSort rawModuleTargetPairs
147176
where
@@ -162,6 +191,38 @@ getModulesNames fields = map swap $ groupSort rawModuleTargetPairs
162191
else []
163192
getFieldModuleNames _ = []
164193

194+
-- | Trims a given cabal AST leaving only targets and their
195+
-- @exposed-modules@ and @other-modules@ sections.
196+
--
197+
-- For examle:
198+
--
199+
-- * Given a cabal file like this:
200+
--
201+
-- > library
202+
-- > import: extra
203+
-- > hs-source-dirs: source/directory
204+
-- > ...
205+
-- > exposed-modules:
206+
-- > Importaint.Exposed.Module
207+
-- > other-modules:
208+
-- > Importaint.Other.Module
209+
-- >
210+
-- > test-suite tests
211+
-- > type: type
212+
-- > build-tool-depends: tool
213+
-- > other-modules:
214+
-- > Importaint.Other.Module
215+
--
216+
-- * @getSectionsWithModules@ gives output:
217+
--
218+
-- > library
219+
-- > exposed-modules:
220+
-- > Importaint.Exposed.Module
221+
-- > other-modules:
222+
-- > Importaint.Other.Module
223+
-- > test-suite tests
224+
-- > other-modules:
225+
-- > Importaint.Other.Module
165226
getSectionsWithModules :: [Syntax.Field any] -> [Syntax.Field any]
166227
getSectionsWithModules fields = concatMap go fields
167228
where

0 commit comments

Comments
 (0)