Skip to content

Commit d4cdec4

Browse files
committed
Fix keyword context not ignoring filecontent after cursor
Fix some tests
1 parent 6a0834b commit d4cdec4

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import System.Directory (doesDirectoryExist,
2727
import qualified System.FilePath as FP
2828
import qualified System.FilePath.Posix as Posix
2929
import qualified Text.Fuzzy.Parallel as Fuzzy
30+
import Debug.Trace (traceShowM, traceShowId)
3031

3132
{- | Takes information needed to build possible completion items
3233
and returns the list of possible completion items
@@ -165,7 +166,7 @@ getContext pos ls =
165166
-}
166167
getKeyWordContext :: Position -> [T.Text] -> Map KeyWordName a -> Maybe KeyWordContext
167168
getKeyWordContext pos ls keywords = do
168-
case lastNonEmptyLineM of
169+
case traceShowId $ lastNonEmptyLineM of
169170
Nothing -> Just None
170171
Just lastLine' -> do
171172
let (whiteSpaces, lastLine) = T.span (== ' ') lastLine'
@@ -181,8 +182,12 @@ getKeyWordContext pos ls keywords = do
181182
else Just None
182183
where
183184
lastNonEmptyLineM = do
184-
cur <- currentLineM
185-
List.find (not . T.null . T.stripEnd) $ cur : previousLines pos ls
185+
cur' <- currentLineM
186+
traceShowM ("cur line", cur')
187+
let cur = stripPartiallyWritten $ T.take (fromIntegral $ pos ^. JL.character) cur'
188+
traceShowM ("cur line before pref", cur)
189+
List.find (not . T.null . T.stripEnd)
190+
$ cur : previousLines pos ls
186191
currentLineM = ls Extra.!? (fromIntegral $ pos ^. JL.line)
187192

188193
{- | Parse the given set of lines (starting before current cursor position
@@ -219,6 +224,12 @@ previousLines pos ls = reverse $ take (fromIntegral currentLine) ls
219224
where
220225
currentLine = pos ^. JL.line
221226

227+
228+
{- | Takes a line of text and removes the last partially written word.
229+
-}
230+
stripPartiallyWritten :: T.Text -> T.Text
231+
stripPartiallyWritten = T.dropWhileEnd (\y -> (y /= ' ') && (y /= ':'))
232+
222233
{- | Takes information about the current file's file path,
223234
the current cursor position in the file
224235
and its contents; and builds a CabalCompletionItem
@@ -556,7 +567,7 @@ stanzaKeywordMap =
556567
, ("hs-source-dirs:", directoryCompleter)
557568
, ("default-extensions:", noopCompleter)
558569
, ("other-extensions:", noopCompleter)
559-
, ("default-language:", noopCompleter)
570+
, ("default-language:", constantCompleter ["GHC2021", "Haskell2010", "Haskell98"])
560571
, ("other-languages:", noopCompleter)
561572
, ("build-tool-depends:", noopCompleter)
562573
, ("buildable:", constantCompleter ["True", "False"])

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -255,17 +255,17 @@ pathCompleterTests =
255255
testDir <- getTestDir
256256
completions <- directoryCompleter $ simpleCabalCompletionContext "" testDir
257257
let insertCompletions = map itemInsert completions
258-
sort insertCompletions @?= ["dir1/", "dir2/"]
258+
sort insertCompletions @?= ["./dir1/", "./dir2/"]
259259
, testCase "Current Directory - alternative writing" $ do
260260
testDir <- getTestDir
261261
completions <- directoryCompleter $ simpleCabalCompletionContext "./" testDir
262262
let insertCompletions = map itemInsert completions
263-
sort insertCompletions @?= ["dir1/", "dir2/"]
263+
sort insertCompletions @?= ["./dir1/", "./dir2/"]
264264
, testCase "Current Directory - incomplete directory path written" $ do
265265
testDir <- getTestDir
266266
completions <- directoryCompleter $ simpleCabalCompletionContext "di" testDir
267267
let insertCompletions = map itemInsert completions
268-
sort insertCompletions @?= ["dir1/", "dir2/"]
268+
sort insertCompletions @?= ["./dir1/", "./dir2/"]
269269
, testCase "Current Directory - incomplete filepath written" $ do
270270
testDir <- getTestDir
271271
completions <- directoryCompleter $ simpleCabalCompletionContext "te" testDir
@@ -280,7 +280,7 @@ pathCompleterTests =
280280
testDir <- getTestDir
281281
completions <- directoryCompleter $ simpleCabalCompletionContext "dir2/" testDir
282282
let insertCompletions = map itemInsert completions
283-
sort insertCompletions @?= ["dir3/"]
283+
sort insertCompletions @?= ["dir2/dir3/"]
284284
, testCase "Nonexistent directory" $ do
285285
testDir <- getTestDir
286286
completions <- directoryCompleter $ simpleCabalCompletionContext "dir2/dir4/" testDir
@@ -342,19 +342,19 @@ contextTests =
342342
-- be top level without a specified keyword
343343
getContext (Position 0 0) [""] @?= Just (TopLevel, None)
344344
, testCase "Cabal version keyword - no value" $ do
345-
-- on a file, where the "cabal-version:" keyword is already written
345+
-- on a file, where the keyword is already written
346346
-- the context should still be toplevel but the keyword should be recognized
347347
getContext (Position 0 15) ["cabal-version:"] @?= Just (TopLevel, KeyWord "cabal-version:")
348348
, testCase "Cabal version keyword - cursor in keyword" $ do
349-
-- on a file, where the "cabal-version:" keyword is already written
350-
-- but the cursor is in the middle of the keyword, the keyword context
351-
-- is cabal-version since after the keyword, the value needs to be inserted still
352-
getContext (Position 0 5) ["cabal-version:"] @?= Just (TopLevel, KeyWord "cabal-version:")
349+
-- on a file, where the keyword is already written
350+
-- but the cursor is in the middle of the keyword,
351+
-- we are not in a keyword context
352+
getContext (Position 0 5) ["cabal-version:"] @?= Just (TopLevel, None)
353353
, testCase "Cabal version keyword - no value, many spaces" $ do
354354
-- on a file, where the "cabal-version:" keyword is already written
355355
-- the context should still be top level but the keyword should be recognized
356356
getContext (Position 0 45) ["cabal-version:" <> T.replicate 50 " "] @?= Just (TopLevel, KeyWord "cabal-version:")
357-
, testCase "Cabal version keyword - no value, many spaces" $ do
357+
, testCase "Cabal version keyword - keyword partly written" $ do
358358
-- in the first line of the file, if the keyword
359359
-- has not been written completely, the keyword context
360360
-- should still be None
@@ -408,6 +408,8 @@ contextTests =
408408
-- in a stanza context with no value the value may not be written in the next line,
409409
-- when the cursor is not indented and we are in the top level context
410410
getContext (Position 5 0) libraryStanzaData @?= Just (TopLevel, None)
411+
, testCase "Top level - cursor in later line with partially written value" $ do
412+
getContext (Position 5 13) topLevelData @?= Just (TopLevel, KeyWord "name:")
411413
]
412414

413415
-- ------------------------------------------------------------------------
@@ -553,5 +555,5 @@ topLevelData =
553555
, ""
554556
, ""
555557
, ""
556-
, ""
558+
, " eee"
557559
]

0 commit comments

Comments
 (0)