Skip to content

Commit 4e904c8

Browse files
committed
Only compare line numbers when checking cursor presence
1 parent 1791df8 commit 4e904c8

2 files changed

Lines changed: 18 additions & 13 deletions

File tree

ghcide-test/exe/CompletionTests.hs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -200,18 +200,17 @@ localCompletionTests = [
200200
,("abcde", CompletionItemKind_Function, "abcde", True, False, Nothing)
201201
],
202202
testSessionEmpty "incomplete entries" $ do
203-
let src a = "data Data = " <> a
204-
doc <- createDoc "A.hs" "haskell" $ src "AAA"
203+
let src a = a <> " = aaa"
204+
doc <- createDoc "A.hs" "haskell" $ src "aaa"
205205
void $ waitForTypecheck doc
206-
let editA rhs =
207-
changeDoc doc [TextDocumentContentChangeEvent . InR . TextDocumentContentChangeWholeDocument $ src rhs]
208-
editA "AAAA"
206+
let editA rhs = changeDoc doc [TextDocumentContentChangeEvent . InR . TextDocumentContentChangeWholeDocument $ src rhs]
207+
editA "aaaa"
209208
void $ waitForTypecheck doc
210-
editA "AAAAA"
209+
editA "aaaaa"
211210
void $ waitForTypecheck doc
212211

213-
compls <- getCompletions doc (Position 0 15)
214-
liftIO $ filter ("AAA" `T.isPrefixOf`) (mapMaybe _insertText compls) @?= ["AAAAA"]
212+
compls <- getCompletions doc (Position 0 11)
213+
liftIO $ filter ("aaa" `T.isPrefixOf`) (mapMaybe _insertText compls) @?= ["aaaaa"]
215214
pure (),
216215
completionTest
217216
"polymorphic record dot completion"
@@ -345,10 +344,10 @@ otherCompletionTests = [
345344
T.unlines
346345
[ "module A where",
347346
"import B",
348-
"memb"
347+
"3 = memb"
349348
]
350349
_ <- waitForDiagnostics
351-
compls <- getCompletions docA $ Position 2 4
350+
compls <- getCompletions docA $ Position 2 7
352351
let compls' = [txt | CompletionItem {_insertText = Just txt, ..} <- compls, _label == "member"]
353352
liftIO $ take 1 compls' @?= ["member"],
354353

ghcide/src/Development/IDE/Plugin/Completions/Context.hs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,9 @@ getContext (ContextMap chunks) query =
155155
searchChunks :: Bool -> [ContextChunk] -> ([ContextGroup], ContextResult)
156156
searchChunks _ [] = ([], mempty)
157157
searchChunks firstChunk (Chunk cLo cHi group contextOf : rest)
158-
| -- query is past this chunk
159-
qLo > cHi = searchChunks False rest
158+
| -- query is past this chunk (line-only comparison so cursors
159+
-- past the last column on the final line still match)
160+
_line qLo > _line cHi = searchChunks False rest
160161
| -- query is before this chunk
161162
qHi < cLo = (if firstChunk then [HeaderGroup] else [], mempty)
162163
-- this chunk is relevant, emit the group and all relevant intervals
@@ -209,8 +210,13 @@ contextual context shouldStop query s =
209210
dominates :: Range -> Range -> Bool
210211
dominates (Range s e) (Range qs qe) = s <= qs && qe <= e
211212

213+
-- | A query range is outside a source range if it ends before the source
214+
-- starts, or it starts on a line after the source ends.
215+
-- We intentionally compare only lines (not columns) for the trailing
216+
-- boundary so that a cursor past the last token on a line still falls
217+
-- inside the node occupying that line.
212218
outside :: Range -> Range -> Bool
213-
outside (Range ps pe) (Range qs qe) = pe < qs || ps > qe
219+
outside (Range ps pe) (Range qs qe) = pe < qs || _line ps > _line qe
214220

215221
instance Pretty Context where
216222
pretty = \case

0 commit comments

Comments
 (0)