Skip to content
This repository has been archived by the owner on Oct 7, 2020. It is now read-only.

Fix more code actions in windows #1399

Merged
merged 4 commits into from
Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions src/Haskell/Ide/Engine/Plugin/GhcMod.hs
Original file line number Diff line number Diff line change
Expand Up @@ -304,24 +304,18 @@ extractRenamableTerms msg
| "ot in scope:" `T.isInfixOf` msg = extractSuggestions msg
| otherwise = []
where
extractSuggestions = map getEnclosed
extractSuggestions = map Hie.extractTerm
. concatMap singleSuggestions
. filter isKnownSymbol
. T.lines
singleSuggestions = T.splitOn "), " -- Each suggestion is comma delimited
isKnownSymbol t = " (imported from" `T.isInfixOf` t || " (line " `T.isInfixOf` t
getEnclosed' b e = T.dropWhile (== b)
. T.dropWhileEnd (== e)
. T.dropAround (\c -> c /= b && c /= e)
getEnclosed txt = case getEnclosed' '‘' '’' txt of
"" -> getEnclosed' '`' '\'' txt -- Needed for windows
enc -> enc

extractRedundantImport :: T.Text -> Maybe T.Text
extractRedundantImport msg =
if ("The import of " `T.isPrefixOf` firstLine || "The qualified import of " `T.isPrefixOf` firstLine)
&& " is redundant" `T.isSuffixOf` firstLine
then Just $ T.init $ T.tail $ T.dropWhileEnd (/= '’') $ T.dropWhile (/= '‘') firstLine
then Just $ Hie.extractTerm firstLine
else Nothing
where
firstLine = case T.lines msg of
Expand Down Expand Up @@ -394,13 +388,10 @@ extractMissingSignature msg = extractSignature <$> stripMessageStart msg
extractSignature = T.strip

extractUnusedTerm :: T.Text -> Maybe T.Text
extractUnusedTerm msg = extractTerm <$> stripMessageStart msg
extractUnusedTerm msg = Hie.extractTerm <$> stripMessageStart msg
where
stripMessageStart = T.stripPrefix "Defined but not used:"
. T.strip
extractTerm = T.dropWhile (== '‘')
. T.dropWhileEnd (== '’')
. T.dropAround (\c -> c /= '‘' && c /= '’')

-- ---------------------------------------------------------------------

Expand Down
22 changes: 12 additions & 10 deletions src/Haskell/Ide/Engine/Plugin/HsImport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -483,17 +483,19 @@ extractImportableTerm dirtyMsg = do
$ T.unlines
$ map T.strip
$ T.lines
$ T.replace "* " "" -- Needed for Windows
$ T.replace "• " "" dirtyMsg

extractedTerm = asum
[ importMsg
>>= T.stripPrefix "Variable not in scope: "
>>= \name -> Just (name, Import Symbol)
, importMsg
>>= T.stripPrefix "Not in scope: type constructor or class ‘"
>>= \name -> Just (T.init name, Import Type)
, importMsg
>>= T.stripPrefix "Data constructor not in scope: "
>>= \name -> Just (name, Import Constructor)]
extractTerm prefix symTy =
importMsg
>>= T.stripPrefix prefix
>>= \name -> Just (name, Import symTy)

extractType b =
extractTerm ("Not in scope: type constructor or class " <> b) Type

extractedTerm = asum
[ extractTerm "Variable not in scope: " Symbol
, extractType "‘"
, extractType "`" -- Needed for windows
, extractTerm "Data constructor not in scope: " Constructor]
6 changes: 3 additions & 3 deletions src/Haskell/Ide/Engine/Plugin/Package.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module Haskell.Ide.Engine.Plugin.Package where
import Haskell.Ide.Engine.MonadTypes
import qualified Haskell.Ide.Engine.Plugin.Hoogle as Hoogle
import Haskell.Ide.Engine.PluginUtils
import Haskell.Ide.Engine.Support.HieExtras as Hie
import GHC.Generics
import GHC.Exts
import Control.Lens
Expand Down Expand Up @@ -338,11 +339,10 @@ codeActionProvider plId docId _ context = do
-- | Extract a module name from an error message.
extractModuleName :: T.Text -> Maybe Package
extractModuleName msg
| T.isPrefixOf "Could not find module " msg = Just $ T.tail $ T.init nameAndQuotes
| T.isPrefixOf "Could not load module " msg = Just $ T.tail $ T.init nameAndQuotes
| T.isPrefixOf "Could not find module " msg = Just $ Hie.extractTerm line
| T.isPrefixOf "Could not load module " msg = Just $ Hie.extractTerm line
| otherwise = Nothing
where line = head $ T.lines msg
nameAndQuotes = T.dropWhileEnd (/= '’') $ T.dropWhile (/= '‘') line

-- Example error messages
{- GHC 8.6.2 error message is
Expand Down
13 changes: 13 additions & 0 deletions src/Haskell/Ide/Engine/Support/HieExtras.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module Haskell.Ide.Engine.Support.HieExtras
, getSymbolsAtPoint
, getReferencesInDoc
, getModule
, extractTerm
, findDef
, findTypeDef
, showName
Expand Down Expand Up @@ -230,6 +231,18 @@ getModule df n = do
let pkg = showName . packageName <$> lookupPackage df uid
return (pkg, T.pack $ moduleNameString $ moduleName m)

-- | Extract a term from a compiler message.
-- It looks for terms delimited between '‘' and '’' falling back to '`' and '\''
-- (the used ones in Windows systems).
extractTerm :: T.Text -> T.Text
extractTerm txt =
case extract '‘' '’' txt of
"" -> extract '`' '\'' txt -- Needed for windows
term -> term
where extract b e = T.dropWhile (== b)
. T.dropWhileEnd (== e)
. T.dropAround (\c -> c /= b && c /= e)

-- ---------------------------------------------------------------------

-- | Return the type definition of the symbol at the given position.
Expand Down