summaryrefslogtreecommitdiff
path: root/utils/notes-util/Notes.hs
diff options
context:
space:
mode:
authorMatthew Pickering <matthewtpickering@gmail.com>2022-02-03 17:15:18 +0000
committerMatthew Pickering <matthewtpickering@gmail.com>2022-02-07 08:42:17 +0000
commitf84d2b90f9ae3768b16a9f49c013917cb2485014 (patch)
tree56044a7d07d73c3eca7f603dd1c966f044741fe3 /utils/notes-util/Notes.hs
parent6fa4d7d67bf1ace294a4c27dd2114e39b1251988 (diff)
downloadhaskell-wip/notes-linter.tar.gz
Add suggestion mode to notes-utilwip/notes-linter
Diffstat (limited to 'utils/notes-util/Notes.hs')
-rw-r--r--utils/notes-util/Notes.hs27
1 files changed, 25 insertions, 2 deletions
diff --git a/utils/notes-util/Notes.hs b/utils/notes-util/Notes.hs
index f27b483536..cf267d8d67 100644
--- a/utils/notes-util/Notes.hs
+++ b/utils/notes-util/Notes.hs
@@ -11,6 +11,9 @@ import qualified Data.Text.Encoding as T
import qualified Data.Map.Strict as M
import qualified Data.Set as S
import System.Directory (doesFileExist)
+import Data.Array
+import Data.List (sortBy)
+import Data.Ord
data SrcLoc = SrcLoc { fileName :: FilePath
, row :: !Int
@@ -148,14 +151,34 @@ fileNotes fname = do
}
else return mempty
-brokenNoteRefs :: NoteDb -> [NoteRef]
+brokenNoteRefs :: NoteDb -> [(NoteRef, NoteDef)]
brokenNoteRefs db =
- [ ref
+ [ (ref, best_match)
| (_fname, refs) <- M.toList (noteRefs db)
, ref <- S.toList refs
, Nothing <- pure $ M.lookup (noteRefName ref) (noteDefs db)
+ , let best_match = bestLev (show (noteRefName ref)) (concatMap S.toList (M.elems (noteDefs db)))
]
+bestLev :: String -> [NoteDef] -> NoteDef
+bestLev x ds = head $ sortBy (comparing (\d -> lev x (show (noteDefName d)))) ds
+
+
+lev:: (Eq a) => [a] -> [a] -> Int
+lev xs ys = levMemo ! (n, m)
+ where levMemo = array ((0,0),(n,m)) [((i,j),lev' i j) | i <- [0..n], j <- [0..m]]
+ n = length xs
+ m = length ys
+ xa = listArray (1, n) xs
+ ya = listArray (1, m) ys
+ lev' 0 v = v
+ lev' u 0 = u
+ lev' u v
+ | xa ! u == ya ! v = levMemo ! (u-1, v-1)
+ | otherwise = 1 + minimum [levMemo ! (u, v-1),
+ levMemo ! (u-1, v),
+ levMemo ! (u-1, v-1)]
+
unreferencedNotes :: NoteDb -> S.Set NoteDef
unreferencedNotes db =
fold $ noteDefs db `M.withoutKeys` referencedNotes