mirror of
https://github.com/joshuaclayton/unused.git
synced 2024-09-11 04:45:28 +03:00
Add flag to filter by likelihood
This commit is contained in:
parent
cb6eb12af4
commit
ee1c4cd0f6
23
app/Main.hs
23
app/Main.hs
@ -3,13 +3,14 @@ module Main where
|
|||||||
import Options.Applicative
|
import Options.Applicative
|
||||||
import System.IO (hSetBuffering, BufferMode(NoBuffering), stdout)
|
import System.IO (hSetBuffering, BufferMode(NoBuffering), stdout)
|
||||||
import Unused.Parser (parseLines)
|
import Unused.Parser (parseLines)
|
||||||
import Unused.Types (ParseResponse)
|
import Unused.Types (ParseResponse, RemovalLikelihood(..))
|
||||||
import Unused.ResponseFilter (withOneOccurrence, withOneFile)
|
import Unused.ResponseFilter (withOneOccurrence, withOneFile, withLikelihoods)
|
||||||
import Unused.CLI (SearchRunner(..), executeSearch, printParseError, printSearchResults, resetScreen)
|
import Unused.CLI (SearchRunner(..), executeSearch, printParseError, printSearchResults, resetScreen)
|
||||||
|
|
||||||
data Options = Options
|
data Options = Options
|
||||||
{ oSearchRunner :: SearchRunner
|
{ oSearchRunner :: SearchRunner
|
||||||
, oAllOccurrencesAndFiles :: Bool
|
, oAllOccurrencesAndFiles :: Bool
|
||||||
|
, oLikelihoods :: [RemovalLikelihood]
|
||||||
}
|
}
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
@ -40,6 +41,7 @@ optionFilters o =
|
|||||||
where
|
where
|
||||||
filters =
|
filters =
|
||||||
[ if oAllOccurrencesAndFiles o then id else withOneOccurrence . withOneFile
|
[ if oAllOccurrencesAndFiles o then id else withOneOccurrence . withOneFile
|
||||||
|
, withLikelihoods $ oLikelihoods o
|
||||||
]
|
]
|
||||||
|
|
||||||
parseOptions :: Parser Options
|
parseOptions :: Parser Options
|
||||||
@ -47,6 +49,7 @@ parseOptions =
|
|||||||
Options
|
Options
|
||||||
<$> parseSearchRunner
|
<$> parseSearchRunner
|
||||||
<*> parseDisplayAllMatches
|
<*> parseDisplayAllMatches
|
||||||
|
<*> parseLikelihoods
|
||||||
|
|
||||||
parseSearchRunner :: Parser SearchRunner
|
parseSearchRunner :: Parser SearchRunner
|
||||||
parseSearchRunner =
|
parseSearchRunner =
|
||||||
@ -60,3 +63,19 @@ parseDisplayAllMatches = switch $
|
|||||||
short 'a'
|
short 'a'
|
||||||
<> long "all"
|
<> long "all"
|
||||||
<> help "Display all files and occurrences"
|
<> help "Display all files and occurrences"
|
||||||
|
|
||||||
|
parseLikelihoods :: Parser [RemovalLikelihood]
|
||||||
|
parseLikelihoods = many $
|
||||||
|
parseLikelihood <$> parseLikelihoodOption
|
||||||
|
|
||||||
|
parseLikelihood :: String -> RemovalLikelihood
|
||||||
|
parseLikelihood "high" = High
|
||||||
|
parseLikelihood "medium" = Medium
|
||||||
|
parseLikelihood "low" = Low
|
||||||
|
parseLikelihood _ = Unknown
|
||||||
|
|
||||||
|
parseLikelihoodOption :: Parser String
|
||||||
|
parseLikelihoodOption = strOption $
|
||||||
|
short 'l'
|
||||||
|
<> long "likelihood"
|
||||||
|
<> help "[Allows multiple] [Allowed values: high, medium, low] Display results based on likelihood"
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
module Unused.ResponseFilter
|
module Unused.ResponseFilter
|
||||||
( withOneFile
|
( withOneFile
|
||||||
, withOneOccurrence
|
, withOneOccurrence
|
||||||
|
, withLikelihoods
|
||||||
, oneFile
|
, oneFile
|
||||||
, oneOccurence
|
, oneOccurence
|
||||||
, isClassOrModule
|
, isClassOrModule
|
||||||
@ -22,9 +23,16 @@ withOneOccurrence = applyFilter (const oneOccurence)
|
|||||||
oneOccurence :: TermResults -> Bool
|
oneOccurence :: TermResults -> Bool
|
||||||
oneOccurence = (== 1) . trTotalOccurrences
|
oneOccurence = (== 1) . trTotalOccurrences
|
||||||
|
|
||||||
|
withLikelihoods :: [RemovalLikelihood] -> ParseResponse -> ParseResponse
|
||||||
|
withLikelihoods [] = id
|
||||||
|
withLikelihoods l = applyFilter (const $ includesLikelihood l)
|
||||||
|
|
||||||
oneFile :: TermResults -> Bool
|
oneFile :: TermResults -> Bool
|
||||||
oneFile = (== 1) . trTotalFiles
|
oneFile = (== 1) . trTotalFiles
|
||||||
|
|
||||||
|
includesLikelihood :: [RemovalLikelihood] -> TermResults -> Bool
|
||||||
|
includesLikelihood l = (`elem` l) . trRemovalLikelihood
|
||||||
|
|
||||||
isClassOrModule :: TermResults -> Bool
|
isClassOrModule :: TermResults -> Bool
|
||||||
isClassOrModule = matchRegex "^[A-Z]" . trTerm
|
isClassOrModule = matchRegex "^[A-Z]" . trTerm
|
||||||
|
|
||||||
|
@ -23,7 +23,7 @@ data TermResults = TermResults
|
|||||||
, trRemovalLikelihood :: RemovalLikelihood
|
, trRemovalLikelihood :: RemovalLikelihood
|
||||||
} deriving Show
|
} deriving Show
|
||||||
|
|
||||||
data RemovalLikelihood = High | Medium | Low deriving Show
|
data RemovalLikelihood = High | Medium | Low | Unknown deriving (Eq, Show)
|
||||||
|
|
||||||
type TermMatchSet = Map.Map String TermResults
|
type TermMatchSet = Map.Map String TermResults
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user