Skip to content

Commit

Permalink
Fix keyword context not ignoring filecontent after cursor
Browse files Browse the repository at this point in the history
Fix some tests
  • Loading branch information
VeryMilkyJoe committed Jun 16, 2023
1 parent 6a0834b commit d4cdec4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
19 changes: 15 additions & 4 deletions plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import System.Directory (doesDirectoryExist,
import qualified System.FilePath as FP
import qualified System.FilePath.Posix as Posix
import qualified Text.Fuzzy.Parallel as Fuzzy
import Debug.Trace (traceShowM, traceShowId)

{- | Takes information needed to build possible completion items
and returns the list of possible completion items
Expand Down Expand Up @@ -165,7 +166,7 @@ getContext pos ls =
-}
getKeyWordContext :: Position -> [T.Text] -> Map KeyWordName a -> Maybe KeyWordContext
getKeyWordContext pos ls keywords = do
case lastNonEmptyLineM of
case traceShowId $ lastNonEmptyLineM of
Nothing -> Just None
Just lastLine' -> do
let (whiteSpaces, lastLine) = T.span (== ' ') lastLine'
Expand All @@ -181,8 +182,12 @@ getKeyWordContext pos ls keywords = do
else Just None
where
lastNonEmptyLineM = do
cur <- currentLineM
List.find (not . T.null . T.stripEnd) $ cur : previousLines pos ls
cur' <- currentLineM
traceShowM ("cur line", cur')
let cur = stripPartiallyWritten $ T.take (fromIntegral $ pos ^. JL.character) cur'
traceShowM ("cur line before pref", cur)
List.find (not . T.null . T.stripEnd)
$ cur : previousLines pos ls
currentLineM = ls Extra.!? (fromIntegral $ pos ^. JL.line)

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


{- | Takes a line of text and removes the last partially written word.
-}
stripPartiallyWritten :: T.Text -> T.Text
stripPartiallyWritten = T.dropWhileEnd (\y -> (y /= ' ') && (y /= ':'))

{- | Takes information about the current file's file path,
the current cursor position in the file
and its contents; and builds a CabalCompletionItem
Expand Down Expand Up @@ -556,7 +567,7 @@ stanzaKeywordMap =
, ("hs-source-dirs:", directoryCompleter)
, ("default-extensions:", noopCompleter)
, ("other-extensions:", noopCompleter)
, ("default-language:", noopCompleter)
, ("default-language:", constantCompleter ["GHC2021", "Haskell2010", "Haskell98"])
, ("other-languages:", noopCompleter)
, ("build-tool-depends:", noopCompleter)
, ("buildable:", constantCompleter ["True", "False"])
Expand Down
24 changes: 13 additions & 11 deletions plugins/hls-cabal-plugin/test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -255,17 +255,17 @@ pathCompleterTests =
testDir <- getTestDir
completions <- directoryCompleter $ simpleCabalCompletionContext "" testDir
let insertCompletions = map itemInsert completions
sort insertCompletions @?= ["dir1/", "dir2/"]
sort insertCompletions @?= ["./dir1/", "./dir2/"]
, testCase "Current Directory - alternative writing" $ do
testDir <- getTestDir
completions <- directoryCompleter $ simpleCabalCompletionContext "./" testDir
let insertCompletions = map itemInsert completions
sort insertCompletions @?= ["dir1/", "dir2/"]
sort insertCompletions @?= ["./dir1/", "./dir2/"]
, testCase "Current Directory - incomplete directory path written" $ do
testDir <- getTestDir
completions <- directoryCompleter $ simpleCabalCompletionContext "di" testDir
let insertCompletions = map itemInsert completions
sort insertCompletions @?= ["dir1/", "dir2/"]
sort insertCompletions @?= ["./dir1/", "./dir2/"]
, testCase "Current Directory - incomplete filepath written" $ do
testDir <- getTestDir
completions <- directoryCompleter $ simpleCabalCompletionContext "te" testDir
Expand All @@ -280,7 +280,7 @@ pathCompleterTests =
testDir <- getTestDir
completions <- directoryCompleter $ simpleCabalCompletionContext "dir2/" testDir
let insertCompletions = map itemInsert completions
sort insertCompletions @?= ["dir3/"]
sort insertCompletions @?= ["dir2/dir3/"]
, testCase "Nonexistent directory" $ do
testDir <- getTestDir
completions <- directoryCompleter $ simpleCabalCompletionContext "dir2/dir4/" testDir
Expand Down Expand Up @@ -342,19 +342,19 @@ contextTests =
-- be top level without a specified keyword
getContext (Position 0 0) [""] @?= Just (TopLevel, None)
, testCase "Cabal version keyword - no value" $ do
-- on a file, where the "cabal-version:" keyword is already written
-- on a file, where the keyword is already written
-- the context should still be toplevel but the keyword should be recognized
getContext (Position 0 15) ["cabal-version:"] @?= Just (TopLevel, KeyWord "cabal-version:")
, testCase "Cabal version keyword - cursor in keyword" $ do
-- on a file, where the "cabal-version:" keyword is already written
-- but the cursor is in the middle of the keyword, the keyword context
-- is cabal-version since after the keyword, the value needs to be inserted still
getContext (Position 0 5) ["cabal-version:"] @?= Just (TopLevel, KeyWord "cabal-version:")
-- on a file, where the keyword is already written
-- but the cursor is in the middle of the keyword,
-- we are not in a keyword context
getContext (Position 0 5) ["cabal-version:"] @?= Just (TopLevel, None)
, testCase "Cabal version keyword - no value, many spaces" $ do
-- on a file, where the "cabal-version:" keyword is already written
-- the context should still be top level but the keyword should be recognized
getContext (Position 0 45) ["cabal-version:" <> T.replicate 50 " "] @?= Just (TopLevel, KeyWord "cabal-version:")
, testCase "Cabal version keyword - no value, many spaces" $ do
, testCase "Cabal version keyword - keyword partly written" $ do
-- in the first line of the file, if the keyword
-- has not been written completely, the keyword context
-- should still be None
Expand Down Expand Up @@ -408,6 +408,8 @@ contextTests =
-- in a stanza context with no value the value may not be written in the next line,
-- when the cursor is not indented and we are in the top level context
getContext (Position 5 0) libraryStanzaData @?= Just (TopLevel, None)
, testCase "Top level - cursor in later line with partially written value" $ do
getContext (Position 5 13) topLevelData @?= Just (TopLevel, KeyWord "name:")
]

-- ------------------------------------------------------------------------
Expand Down Expand Up @@ -553,5 +555,5 @@ topLevelData =
, ""
, ""
, ""
, ""
, " eee"
]

0 comments on commit d4cdec4

Please sign in to comment.