Skip to content

Commit 0cde028

Browse files
committed
Fix testsuite for license suggestions
1 parent f0868c0 commit 0cde028

File tree

4 files changed

+30
-18
lines changed

4 files changed

+30
-18
lines changed

ghcide/src/Text/Fuzzy/Parallel.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ filter chunkSize maxRes pattern ts extract = partialSortByAscScore maxRes perfec
9191
-- match against the pattern. Runs with default settings where
9292
-- nothing is added around the matches, as case insensitive.
9393
--
94-
-- >>> simpleFilter "vm" ["vim", "emacs", "virtual machine"]
95-
-- ["vim","virtual machine"]
94+
-- >>> simpleFilter 1000 10 "vm" ["vim", "emacs", "virtual machine"]
95+
-- [Scored {score = 4, original = "vim"},Scored {score = 4, original = "virtual machine"}]
9696
{-# INLINABLE simpleFilter #-}
9797
simpleFilter :: Int -- ^ Chunk size. 1000 works well.
9898
-> Int -- ^ Max. number of results wanted

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
module Ide.Plugin.Cabal.LicenseSuggest
77
( licenseErrorSuggestion
88
, licenseErrorAction
9+
, licenseNames
910
-- * Re-exports
1011
, T.Text
1112
, Diagnostic(..)
@@ -23,8 +24,9 @@ import Language.LSP.Types (CodeAction (CodeAction),
2324
WorkspaceEdit (WorkspaceEdit))
2425
import Text.Regex.TDFA
2526

27+
import qualified Data.List as List
2628
import Distribution.SPDX.LicenseId (licenseId)
27-
import Text.Fuzzy (simpleFilter)
29+
import qualified Text.Fuzzy.Parallel as Fuzzy
2830

2931
-- | Given a diagnostic returned by 'Ide.Plugin.Cabal.Diag.errorDiagnostic',
3032
-- if it represents an "Unknown SPDX license identifier"-error along
@@ -61,20 +63,27 @@ licenseNames :: [T.Text]
6163
licenseNames = map (T.pack . licenseId) [minBound .. maxBound]
6264

6365
-- | Given a diagnostic returned by 'Ide.Plugin.Cabal.Diag.errorDiagnostic',
64-
-- if it represents an "Unknown SPDX license identifier"-error along
65-
-- with a suggestion then return the suggestion (after the "Do you mean"-text)
66-
-- along with the incorrect identifier.
66+
-- provide possible corrections for SPDX license identifiers
67+
-- based on the list specified in Cabal.
68+
-- Results are sorted by best fit, and prefer solutions that have smaller
69+
-- length distance to the original word.
70+
--
71+
-- >>> take 2 $ licenseErrorSuggestion (T.pack "Unknown SPDX license identifier: 'BSD3'")
72+
-- [("BSD3","BSD-3-Clause"),("BSD3","BSD-3-Clause-LBNL")]
6773
licenseErrorSuggestion ::
6874
T.Text
6975
-- ^ Output of 'Ide.Plugin.Cabal.Diag.errorDiagnostic'
7076
-> [(T.Text, T.Text)]
7177
-- ^ (Original (incorrect) license identifier, suggested replacement)
72-
licenseErrorSuggestion msg = take 10 $
78+
licenseErrorSuggestion msg =
7379
(getMatch <$> msg =~~ regex) >>= \case
74-
[original] -> simpleFilter original licenseNames >>= \x -> [(original,x)]
80+
[original] ->
81+
let matches = map Fuzzy.original $ Fuzzy.simpleFilter 1000 10 original licenseNames
82+
in [(original,candidate) | candidate <- List.sortBy (lengthDistance original) matches]
7583
_ -> []
7684
where
7785
regex :: T.Text
7886
regex = "Unknown SPDX license identifier: '(.*)'"
7987
getMatch :: (T.Text, T.Text, T.Text, [T.Text]) -> [T.Text]
8088
getMatch (_, _, _, results) = results
89+
lengthDistance original x1 x2 = abs (T.length original - T.length x1) `compare` abs (T.length original - T.length x2)

plugins/hls-cabal-plugin/test/Main.hs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{-# OPTIONS_GHC -Wno-orphans #-}
33
{-# LANGUAGE DisambiguateRecordFields #-}
44
{-# LANGUAGE NamedFieldPuns #-}
5+
{-# LANGUAGE TypeOperators #-}
56
module Main
67
( main
78
) where
@@ -75,11 +76,13 @@ codeActionUnitTests = testGroup "Code Action Tests"
7576
licenseErrorSuggestion "Unknown license identifier: 'BSD3' Do you mean BSD-3-Clause?" @?= [],
7677

7778
testCase "BSD-3-Clause" $ do
78-
licenseErrorSuggestion "Unknown SPDX license identifier: 'BSD3' Do you mean BSD-3-Clause?" @?= [("BSD3", "BSD-3-Clause")],
79+
take 2 (licenseErrorSuggestion "Unknown SPDX license identifier: 'BSD3' Do you mean BSD-3-Clause?")
80+
@?= [("BSD3","BSD-3-Clause"),("BSD3","BSD-3-Clause-LBNL")],
7981

80-
testCase "MIT" $ do
82+
testCase "MiT" $ do
8183
-- contains no suggestion
82-
licenseErrorSuggestion "Unknown SPDX license identifier: 'MIT3'" @?= [("MIT3", "MIT")]
84+
take 2 (licenseErrorSuggestion "Unknown SPDX license identifier: 'MiT'")
85+
@?= [("MiT","MIT"),("MiT","MIT-0")]
8386
]
8487

8588
-- ------------------------------------------------------------------------
@@ -139,7 +142,7 @@ pluginTests recorder = testGroup "Plugin Tests"
139142
length diags @?= 1
140143
reduceDiag ^. J.range @?= Range (Position 3 24) (Position 4 0)
141144
reduceDiag ^. J.severity @?= Just DsError
142-
[codeAction] <- getLicenseAction "BSD-3-Clause"<$> getCodeActions doc (Range (Position 3 24) (Position 4 0))
145+
[codeAction] <- getLicenseAction "BSD-3-Clause" <$> getCodeActions doc (Range (Position 3 24) (Position 4 0))
143146
executeCodeAction codeAction
144147
contents <- documentContents doc
145148
liftIO $ contents @?= Text.unlines
@@ -154,14 +157,14 @@ pluginTests recorder = testGroup "Plugin Tests"
154157
]
155158
, runCabalTestCaseSession "Apache-2.0" recorder "" $ do
156159
doc <- openDoc "licenseCodeAction2.cabal" "cabal"
157-
diags <- waitForDiagnosticsFromSource doc "parsing"
160+
diags <- waitForDiagnosticsFromSource doc "cabal"
158161
-- test if it supports typos in license name, here 'apahe'
159-
reduceDiag <- liftIO $ inspectDiagnostic diags ["Unknown SPDX license identifier: 'apahe'"]
162+
reduceDiag <- liftIO $ inspectDiagnostic diags ["Unknown SPDX license identifier: 'APAHE'"]
160163
liftIO $ do
161164
length diags @?= 1
162165
reduceDiag ^. J.range @?= Range (Position 3 25) (Position 4 0)
163166
reduceDiag ^. J.severity @?= Just DsError
164-
[codeAction] <- getLicenseAction "Apache-2.0"<$> getCodeActions doc (Range (Position 3 24) (Position 4 0))
167+
[codeAction] <- getLicenseAction "Apache-2.0" <$> getCodeActions doc (Range (Position 3 24) (Position 4 0))
165168
executeCodeAction codeAction
166169
contents <- documentContents doc
167170
liftIO $ contents @?= Text.unlines
@@ -177,10 +180,10 @@ pluginTests recorder = testGroup "Plugin Tests"
177180
]
178181
]
179182
where
180-
getLicenseAction :: Text.Text -> [(|?) Command CodeAction] -> [CodeAction]
183+
getLicenseAction :: Text.Text -> [Command |? CodeAction] -> [CodeAction]
181184
getLicenseAction license codeActions = do
182185
InR action@CodeAction{_title} <- codeActions
183-
guard (_title=="Replace with "<>license)
186+
guard (_title=="Replace with " <> license)
184187
pure action
185188

186189
-- ------------------------------------------------------------------------

plugins/hls-cabal-plugin/test/testdata/licenseCodeAction2.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cabal-version: 3.0
22
name: licenseCodeAction2
33
version: 0.1.0.0
4-
license: apahe
4+
license: APAHE
55

66
library
77
build-depends: base

0 commit comments

Comments
 (0)