Skip to content

Commit

Permalink
correct path, indefinite search(?)
Browse files Browse the repository at this point in the history
  • Loading branch information
VenInf committed Aug 13, 2024
1 parent a49ecea commit 2ff597a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
30 changes: 20 additions & 10 deletions plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ import Distribution.PackageDescription (Benchmark (..),
library,
unUnqualComponentName)
import Distribution.PackageDescription.Configuration (flattenPackageDescription)
import Distribution.Utils.Path (getSymbolicPath)
import System.Directory (doesFileExist)
import System.FilePath ((</>), takeDirectory)
import Distribution.Utils.Generic (safeHead)

data Log
= LogModificationTime NormalizedFilePath FileVersion
Expand Down Expand Up @@ -325,23 +329,25 @@ gotoDefinition ideState _ msgParam = do
let mModuleNames = CabalFields.getModulesNames <$> mCabalFields
mModuleName = find (isModuleName cursorText) =<< mModuleNames
case mModuleName of
Nothing -> traceShowM ("NOT A MODULE")
Nothing -> pure $ InR $ InR Null
Just (mBuildTargetNames, moduleName) -> do
traceShowM ("IS A MODULE", moduleName, "at", mBuildTargetNames)
mGPD <- liftIO $ runAction "cabal.GPD" ideState $ useWithStale ParseCabalFile $ toNormalizedFilePath filePath
case mGPD of
Nothing -> traceShowM ("failed to get GPD")
Nothing -> pure $ InR $ InR Null
Just (gpd, _) -> do
let debug = map (lookupBuildTargetPackageDescription
(flattenPackageDescription gpd))
mBuildTargetNames
traceShowM ("debug is", debug)
let buildInfos = foldMap (lookupBuildTargetPackageDescription
(flattenPackageDescription gpd))
mBuildTargetNames
traceShowM ("buildInfos is", buildInfos)
traceShowM ("Found hsSourceDirs", map hsSourceDirs buildInfos)
pure $ InR $ InR Null
sourceDirs = map getSymbolicPath $ concatMap hsSourceDirs buildInfos
potentialPaths = map (\dir -> takeDirectory filePath </> dir </> toHaskellFile moduleName) sourceDirs
traceShowM ("potentialPaths", potentialPaths)
allPaths <- liftIO $ filterM doesFileExist potentialPaths
traceShowM ("allPaths", allPaths)
let locations = map (\pth -> Location (filePathToUri pth) (mkRange 0 0 0 0)) allPaths
traceShowM ("locations", locations)
case safeHead locations of
Nothing -> pure $ InR $ InR Null
Just location -> pure $ InL $ Definition $ InL location
where
cursor = Types.lspPositionToCabalPosition (msgParam ^. JL.position)
uri = msgParam ^. JL.textDocument . JL.uri
Expand Down Expand Up @@ -390,6 +396,10 @@ gotoDefinition ideState _ msgParam = do
if T.pack (unUnqualComponentName benchmarkName) == buildTargetName
then Just benchmarkBuildInfo
else Nothing

toHaskellFile :: T.Text -> FilePath
toHaskellFile moduleName = foldl1 (</>) (map T.unpack $ T.splitOn "." moduleName) ++ ".hs"

-- ----------------------------------------------------------------
-- Cabal file of Interest rules and global variable
-- ----------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
module Ide.Plugin.Cabal.Completion.CabalFields (findStanzaForColumn, getSectionsWithModules, getModulesNames, findFieldSection, findTextWord, findFieldLine, getOptionalSectionName, getAnnotation, getFieldName, onelineSectionArgs, getFieldEndPosition, getSectionArgEndPosition, getNameEndPosition, getFieldLineEndPosition, getFieldLSPRange) where

import qualified Data.ByteString as BS
import Data.List (find)
import Data.List.Extra (groupSort)
import Data.List.NonEmpty (NonEmpty)
import qualified Data.List.NonEmpty as NE
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import Data.Tuple (swap)
import qualified Distribution.Fields as Syntax
import qualified Distribution.Parsec.Position as Syntax
import Ide.Plugin.Cabal.Completion.Types
import qualified Data.ByteString as BS
import Data.List (find)
import Ide.Plugin.Cabal.Completion.Types
( cabalPositionToLSPPosition, FieldContext(None), StanzaContext )
import qualified Language.LSP.Protocol.Types as LSP
import Data.List.Extra (groupSort)
import Data.Bifunctor (second)
import Data.Tuple (swap)

-- ----------------------------------------------------------------
-- Cabal-syntax utilities I don't really want to write myself
Expand Down Expand Up @@ -152,9 +152,9 @@ getModulesNames fields = map swap $ groupSort rawModuleTargetPairs
getSectionModuleNames (Syntax.Section _ secArgs fields) = map (, getArgsName secArgs) $ concatMap getFieldModuleNames fields
getSectionModuleNames _ = []

getArgsName [] = Nothing -- only a main library can have no name
getArgsName [] = Nothing -- only a main library can have no name
getArgsName [Syntax.SecArgName _ name] = Just $ T.decodeUtf8 name
getArgsName _ = Nothing -- impossible to have multiple names for a build target
getArgsName _ = Nothing -- impossible to have multiple names for a build target

getFieldModuleNames field@(Syntax.Field _ modules) = if getFieldName field == T.pack "exposed-modules" ||
getFieldName field == T.pack "other-modules"
Expand Down

0 comments on commit 2ff597a

Please sign in to comment.