Skip to content

Commit 76f3bef

Browse files
committed
hls-notes-plugin: Allow Note definition within single line comments
1 parent 00390d7 commit 76f3bef

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

plugins/hls-notes-plugin/src/Ide/Plugin/Notes.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ findNotesInFile file recorder = do
128128
((x@(name, (char, _)):xs, m), (n, nc, c)) -> \char' ->
129129
let !c' = c + 1
130130
(!n', !nc') = if char' == '\n' then (n + 1, c') else (n, nc)
131-
p = if char == c then
131+
p@(!_, !_) = if char == c then
132132
(xs, HM.insert name (Position (uint n') (uint (char - nc'))) m)
133133
else (x:xs, m)
134134
in (p, (n', nc', c'))
@@ -137,7 +137,7 @@ findNotesInFile file recorder = do
137137
noteRefRegex, noteRegex :: Regex
138138
(noteRefRegex, noteRegex) =
139139
( mkReg ("note \\[(.+)\\]" :: String)
140-
, mkReg ("note \\[([[:print:]]+)\\][[:blank:]]*[[:space:]][[:space:]]?~~~" :: String)
140+
, mkReg ("note \\[([[:print:]]+)\\][[:blank:]]*[[:space:]](--)?[[:blank:]]*~~~" :: String)
141141
)
142142
where
143143
mkReg = makeRegexOpts (defaultCompOpt { caseSensitive = False }) defaultExecOpt

plugins/hls-notes-plugin/test/NotesTest.hs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,21 @@ gotoNoteTests = testGroup "Goto Note Definition"
2323
defs <- getDefinitions doc (Position 3 41)
2424
liftIO $ do
2525
fp <- canonicalizePath "NoteDef.hs"
26-
defs @?= InL (Definition (InR [Location (filePathToUri fp) (Range (Position 5 9) (Position 5 9))]))
26+
defs @?= InL (Definition (InR [Location (filePathToUri fp) (Range (Position 8 9) (Position 8 9))]))
27+
, testCase "liberal_format" $ runSessionWithServer def plugin testDataDir $ do
28+
doc <- openDoc "NoteDef.hs" "haskell"
29+
_ <- waitForAllProgressDone
30+
defs <- getDefinitions doc (Position 5 64)
31+
liftIO $ do
32+
fp <- canonicalizePath "NoteDef.hs"
33+
defs @?= InL (Definition (InR [Location (filePathToUri fp) (Range (Position 18 9) (Position 18 9))]))
34+
, testCase "invalid_note" $ runSessionWithServer def plugin testDataDir $ do
35+
doc <- openDoc "NoteDef.hs" "haskell"
36+
_ <- waitForAllProgressDone
37+
defs <- getDefinitions doc (Position 6 54)
38+
liftIO $ do
39+
defs @?= InL (Definition (InR []))
40+
2741
, testCase "no_note" $ runSessionWithServer def plugin testDataDir $ do
2842
doc <- openDoc "NoteDef.hs" "haskell"
2943
defs <- getDefinitions doc (Position 1 0)
@@ -36,7 +50,7 @@ gotoNoteTests = testGroup "Goto Note Definition"
3650
defs <- getDefinitions doc (Position 5 20)
3751
liftIO $ do
3852
fp <- canonicalizePath "NoteDef.hs"
39-
defs @?= InL (Definition (InR [Location (filePathToUri fp) (Range (Position 9 6) (Position 9 6))]))
53+
defs @?= InL (Definition (InR [Location (filePathToUri fp) (Range (Position 12 6) (Position 12 6))]))
4054
]
4155

4256
testDataDir :: FilePath

plugins/hls-notes-plugin/test/testdata/NoteDef.hs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ module NoteDef (foo) where
33
foo :: Int -> Int
44
foo _ = 0 -- We always return zero, see Note [Returning zero from foo]
55

6+
-- The plugin is more liberal with the note definitions, see Note [Single line comments]
7+
-- It does not work on wrong note definitions, see Note [Not a valid Note]
8+
69
{- Note [Returning zero from foo]
710
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
811
This is a big long form note, with very important info
@@ -12,3 +15,14 @@ Note [Multiple notes in comment]
1215
This is also a very common thing to do for GHC
1316
1417
-}
18+
19+
-- Note [Single line comments]
20+
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~
21+
-- GHC's notes script only allows multiline comments to define notes, but in the
22+
-- HLS codebase this single line style can be found as well.
23+
24+
{- Note [Not a valid Note]
25+
26+
~~~~~~~~~~~~
27+
The underline needs to be directly under the Note header
28+
-}

0 commit comments

Comments
 (0)