{-
<TEST>
{- MISSING HASH #-} -- {-# MISSING HASH #-}
<COMMENT> {- INLINE X -}
{- INLINE Y -} -- {-# INLINE Y #-}
{- INLINE[~k] f -} -- {-# INLINE[~k] f #-}
{- NOINLINE Y -} -- {-# NOINLINE Y #-}
{- UNKNOWN Y -}
<COMMENT> INLINE X
</TEST>
-}


module Hint.Comment(commentHint) where

import Hint.Type
import Data.Char
import Data.List.Extra
import Refact.Types(Refactoring(ModifyComment))
import GHC.Types.SrcLoc
import GHC.Parser.Annotation
import GHC.Util

directives :: [String]
directives :: [[Char]]
directives = [Char] -> [[Char]]
words ([Char] -> [[Char]]) -> [Char] -> [[Char]]
forall a b. (a -> b) -> a -> b
$
    [Char]
"LANGUAGE OPTIONS_GHC INCLUDE WARNING DEPRECATED MINIMAL INLINE NOINLINE INLINABLE " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++
    [Char]
"CONLIKE LINE SPECIALIZE SPECIALISE UNPACK NOUNPACK SOURCE"


commentHint :: ModuHint
commentHint :: ModuHint
commentHint Scope
_ ModuleEx
m = (Located AnnotationComment -> [Idea])
-> [Located AnnotationComment] -> [Idea]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Located AnnotationComment -> [Idea]
chk (ModuleEx -> [Located AnnotationComment]
ghcComments ModuleEx
m)
    where
        chk :: Located AnnotationComment -> [Idea]
        chk :: Located AnnotationComment -> [Idea]
chk Located AnnotationComment
comm
          | Bool
isMultiline, [Char]
"#" [Char] -> [Char] -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isSuffixOf` [Char]
s Bool -> Bool -> Bool
&& Bool -> Bool
not ([Char]
"#" [Char] -> [Char] -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isPrefixOf` [Char]
s) = [[Char] -> Located AnnotationComment -> [Char] -> Idea
grab [Char]
"Fix pragma markup" Located AnnotationComment
comm ([Char] -> Idea) -> [Char] -> Idea
forall a b. (a -> b) -> a -> b
$ Char
'#'Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
:[Char]
s]
          | Bool
isMultiline, [Char]
name [Char] -> [[Char]] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [[Char]]
directives = [[Char] -> Located AnnotationComment -> [Char] -> Idea
grab [Char]
"Use pragma syntax" Located AnnotationComment
comm ([Char] -> Idea) -> [Char] -> Idea
forall a b. (a -> b) -> a -> b
$ [Char]
"# " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char] -> [Char]
trim [Char]
s [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" #"]
               where
                 isMultiline :: Bool
isMultiline = Located AnnotationComment -> Bool
isCommentMultiline Located AnnotationComment
comm
                 s :: [Char]
s = Located AnnotationComment -> [Char]
commentText Located AnnotationComment
comm
                 name :: [Char]
name = (Char -> Bool) -> [Char] -> [Char]
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (\Char
x -> Char -> Bool
isAlphaNum Char
x Bool -> Bool -> Bool
|| Char
x Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'_') ([Char] -> [Char]) -> [Char] -> [Char]
forall a b. (a -> b) -> a -> b
$ [Char] -> [Char]
trimStart [Char]
s
        chk Located AnnotationComment
_ = []

        grab :: String -> Located AnnotationComment -> String -> Idea
        grab :: [Char] -> Located AnnotationComment -> [Char] -> Idea
grab [Char]
msg o :: Located AnnotationComment
o@(L SrcSpan
pos AnnotationComment
_) [Char]
s2 =
          let s1 :: [Char]
s1 = Located AnnotationComment -> [Char]
commentText Located AnnotationComment
o in
          Severity
-> [Char]
-> SrcSpan
-> [Char]
-> Maybe [Char]
-> [Note]
-> [Refactoring SrcSpan]
-> Idea
rawIdea Severity
Suggestion [Char]
msg SrcSpan
pos ([Char] -> [Char]
f [Char]
s1) ([Char] -> Maybe [Char]
forall a. a -> Maybe a
Just ([Char] -> Maybe [Char]) -> [Char] -> Maybe [Char]
forall a b. (a -> b) -> a -> b
$ [Char] -> [Char]
f [Char]
s2) [] [Refactoring SrcSpan]
refact
            where f :: [Char] -> [Char]
f [Char]
s = if Located AnnotationComment -> Bool
isCommentMultiline Located AnnotationComment
o then [Char]
"{-" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
s [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"-}" else [Char]
"--" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
s
                  refact :: [Refactoring SrcSpan]
refact = [SrcSpan -> [Char] -> Refactoring SrcSpan
forall a. a -> [Char] -> Refactoring a
ModifyComment (SrcSpan -> SrcSpan
toRefactSrcSpan SrcSpan
pos) ([Char] -> [Char]
f [Char]
s2)]