Skip to content

Commit 5c5a6a3

Browse files
committed
Filepath completion now no longer considers './' when matching
Some refactoring
1 parent d4cdec4 commit 5c5a6a3

File tree

3 files changed

+22
-23
lines changed

3 files changed

+22
-23
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,4 +292,4 @@ completion _ide _ complParams = do
292292
where
293293
(Position linePos charPos) = VFS.cursorPos prefix
294294
context = getContext (Position linePos charPos) (Rope.lines $ cnts ^. VFS.file_text)
295-
completionContext = getFilePathCompletionContext fp prefix
295+
completionContext = getCabalCompletionContext fp prefix

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ 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)
3130

3231
{- | Takes information needed to build possible completion items
3332
and returns the list of possible completion items
@@ -166,7 +165,7 @@ getContext pos ls =
166165
-}
167166
getKeyWordContext :: Position -> [T.Text] -> Map KeyWordName a -> Maybe KeyWordContext
168167
getKeyWordContext pos ls keywords = do
169-
case traceShowId $ lastNonEmptyLineM of
168+
case lastNonEmptyLineM of
170169
Nothing -> Just None
171170
Just lastLine' -> do
172171
let (whiteSpaces, lastLine) = T.span (== ' ') lastLine'
@@ -181,14 +180,13 @@ getKeyWordContext pos ls keywords = do
181180
Just kw -> Just $ KeyWord kw
182181
else Just None
183182
where
183+
currentLineM = ls Extra.!? (fromIntegral $ pos ^. JL.line)
184+
lastNonEmptyLineM :: Maybe T.Text
184185
lastNonEmptyLineM = do
185186
cur' <- currentLineM
186-
traceShowM ("cur line", cur')
187187
let cur = stripPartiallyWritten $ T.take (fromIntegral $ pos ^. JL.character) cur'
188-
traceShowM ("cur line before pref", cur)
189188
List.find (not . T.null . T.stripEnd)
190189
$ cur : previousLines pos ls
191-
currentLineM = ls Extra.!? (fromIntegral $ pos ^. JL.line)
192190

193191
{- | Parse the given set of lines (starting before current cursor position
194192
up to the start of the file) to find the nearest stanza declaration,
@@ -237,8 +235,8 @@ stripPartiallyWritten = T.dropWhileEnd (\y -> (y /= ' ') && (y /= ':'))
237235
checks whether a suffix needs to be completed,
238236
and calculates the range in the document in which to complete
239237
-}
240-
getFilePathCompletionContext :: FilePath -> VFS.PosPrefixInfo -> CabalCompletionContext
241-
getFilePathCompletionContext dir prefixInfo =
238+
getCabalCompletionContext :: FilePath -> VFS.PosPrefixInfo -> CabalCompletionContext
239+
getCabalCompletionContext dir prefixInfo =
242240
CabalCompletionContext
243241
{ completionPrefix = filepathPrefix
244242
, completionSuffix = Just suffix
@@ -264,7 +262,7 @@ getFilePathCompletionContext dir prefixInfo =
264262
cursorColumn = fromIntegral $ VFS.cursorPos prefixInfo ^. JL.character
265263
-- if the filepath is inside apostrophes, we parse until the apostrophe,
266264
-- otherwise we parse until a space occurs
267-
stopConditionChars = apostropheOrSpaceSeparator : [',']
265+
stopConditionChars = apostropheOrSpaceSeparator : [',', ':']
268266

269267
buildCompletion :: CabalCompletionItem -> J.CompletionItem
270268
buildCompletion completionItem =
@@ -317,8 +315,9 @@ filePathCompleter :: Completer
317315
filePathCompleter ctx = do
318316
let suffix = fromMaybe "" $ completionSuffix ctx
319317
complInfo = pathCompletionInfoFromCompletionContext ctx
318+
toMatch = fromMaybe "" $ T.stripPrefix "./" $ partialFileName complInfo
320319
filePathCompletions <- listFileCompletions complInfo
321-
let scored = Fuzzy.simpleFilter 1000 10 (partialFileName complInfo) (map T.pack filePathCompletions)
320+
let scored = Fuzzy.simpleFilter 1000 10 toMatch (map T.pack filePathCompletions)
322321
forM
323322
scored
324323
( \compl' -> do
@@ -327,7 +326,7 @@ filePathCompleter ctx = do
327326
pure $ makeCabalCompletionItem (completionRange ctx) fullFilePath fullFilePath
328327
)
329328
where
330-
-- \| Takes a suffix, a completed path and a pathCompletionInfo and
329+
-- Takes a suffix, a completed path and a pathCompletionInfo and
331330
-- generates the whole filepath including the already written prefix
332331
-- and the suffix in case the completed path is a filepath
333332
makeFullFilePath :: T.Text -> T.Text -> PathCompletionInfo -> IO T.Text
@@ -354,7 +353,7 @@ directoryCompleter ctx = do
354353
pure $ makeCabalCompletionItem (completionRange ctx) fullDirPath fullDirPath
355354
)
356355
where
357-
-- \| Takes a directory and PathCompletionInfo and
356+
-- Takes a directory and PathCompletionInfo and
358357
-- returns the whole path including the prefix that was already written
359358
makeFullDirPath :: T.Text -> PathCompletionInfo -> IO T.Text
360359
makeFullDirPath completion' complInfo = do

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ completionHelperTests =
115115
where
116116
getFilePathCursorPrefix :: T.Text -> UInt -> UInt -> T.Text
117117
getFilePathCursorPrefix lineString linePos charPos =
118-
completionPrefix . getFilePathCompletionContext "" $
118+
completionPrefix . getCabalCompletionContext "" $
119119
VFS.PosPrefixInfo
120120
{ VFS.fullLine = lineString
121121
, VFS.prefixModule = ""
@@ -128,35 +128,35 @@ filePathCompletionContextTests =
128128
testGroup
129129
"File Path Completion Context Tests"
130130
[ testCase "empty line" $ do
131-
let complContext = getFilePathCompletionContext "" (simplePosPrefixInfo " " 0 3)
131+
let complContext = getCabalCompletionContext "" (simplePosPrefixInfo " " 0 3)
132132
completionSuffix complContext @?= Just ""
133133
completionPrefix complContext @?= ""
134134
, testCase "simple filepath" $ do
135-
let complContext = getFilePathCompletionContext "" (simplePosPrefixInfo " src/" 0 7)
135+
let complContext = getCabalCompletionContext "" (simplePosPrefixInfo " src/" 0 7)
136136
completionSuffix complContext @?= Just ""
137137
completionPrefix complContext @?= "src/"
138138
, testCase "simple filepath - starting apostrophe" $ do
139-
let complContext = getFilePathCompletionContext "" (simplePosPrefixInfo " \"src/" 0 8)
139+
let complContext = getCabalCompletionContext "" (simplePosPrefixInfo " \"src/" 0 8)
140140
completionSuffix complContext @?= Just "\""
141141
completionPrefix complContext @?= "src/"
142142
, testCase "simple filepath - starting apostrophe, already closed" $ do
143-
let complContext = getFilePathCompletionContext "" (simplePosPrefixInfo " \"src/\"" 0 8)
143+
let complContext = getCabalCompletionContext "" (simplePosPrefixInfo " \"src/\"" 0 8)
144144
completionSuffix complContext @?= Just ""
145145
completionPrefix complContext @?= "src/"
146146
, testCase "second filepath - starting apostrophe" $ do
147-
let complContext = getFilePathCompletionContext "" (simplePosPrefixInfo "fp.txt \"src/" 0 12)
147+
let complContext = getCabalCompletionContext "" (simplePosPrefixInfo "fp.txt \"src/" 0 12)
148148
completionSuffix complContext @?= Just "\""
149149
completionPrefix complContext @?= "src/"
150150
, testCase "middle filepath - starting apostrophe" $ do
151-
let complContext = getFilePathCompletionContext "" (simplePosPrefixInfo "fp.txt \"src/ fp2.txt" 0 12)
151+
let complContext = getCabalCompletionContext "" (simplePosPrefixInfo "fp.txt \"src/ fp2.txt" 0 12)
152152
completionSuffix complContext @?= Just "\""
153153
completionPrefix complContext @?= "src/"
154154
, testCase "middle filepath - starting apostrophe, already closed" $ do
155-
let complContext = getFilePathCompletionContext "" (simplePosPrefixInfo "fp.t xt \"src\" fp2.txt" 0 12)
155+
let complContext = getCabalCompletionContext "" (simplePosPrefixInfo "fp.t xt \"src\" fp2.txt" 0 12)
156156
completionSuffix complContext @?= Just ""
157157
completionPrefix complContext @?= "src"
158158
, testCase "middle filepath - starting apostrophe, already closed" $ do
159-
let complContext = getFilePathCompletionContext "" (simplePosPrefixInfo "\"fp.txt\" \"src fp2.txt" 0 13)
159+
let complContext = getCabalCompletionContext "" (simplePosPrefixInfo "\"fp.txt\" \"src fp2.txt" 0 13)
160160
completionSuffix complContext @?= Just "\""
161161
completionPrefix complContext @?= "src"
162162
]
@@ -341,10 +341,10 @@ contextTests =
341341
-- for a completely empty file, the context needs to
342342
-- be top level without a specified keyword
343343
getContext (Position 0 0) [""] @?= Just (TopLevel, None)
344-
, testCase "Cabal version keyword - no value" $ do
344+
, testCase "Cabal version keyword - no value, no space after :" $ do
345345
-- on a file, where the keyword is already written
346346
-- the context should still be toplevel but the keyword should be recognized
347-
getContext (Position 0 15) ["cabal-version:"] @?= Just (TopLevel, KeyWord "cabal-version:")
347+
getContext (Position 0 14) ["cabal-version:"] @?= Just (TopLevel, KeyWord "cabal-version:")
348348
, testCase "Cabal version keyword - cursor in keyword" $ do
349349
-- on a file, where the keyword is already written
350350
-- but the cursor is in the middle of the keyword,

0 commit comments

Comments
 (0)