Skip to content

Commit 7a6d82b

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

File tree

3 files changed

+43
-24
lines changed

3 files changed

+43
-24
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: 31 additions & 12 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
@@ -77,7 +76,17 @@ data KeyWordContext
7776
type KeyWordName = T.Text
7877
type StanzaName = T.Text
7978

80-
-- Information about the current completion status
79+
{- | Information about the current completion status
80+
81+
Example: @"dir1/fi@ having been written to the file
82+
would correspond to:
83+
84+
@
85+
completionPrefix = "dir1/fi"
86+
completionSuffix = Just "\\""
87+
...
88+
@
89+
-}
8190
data CabalCompletionContext = CabalCompletionContext
8291
{ completionPrefix :: T.Text
8392
-- ^ text prefix to complete
@@ -166,7 +175,7 @@ getContext pos ls =
166175
-}
167176
getKeyWordContext :: Position -> [T.Text] -> Map KeyWordName a -> Maybe KeyWordContext
168177
getKeyWordContext pos ls keywords = do
169-
case traceShowId $ lastNonEmptyLineM of
178+
case lastNonEmptyLineM of
170179
Nothing -> Just None
171180
Just lastLine' -> do
172181
let (whiteSpaces, lastLine) = T.span (== ' ') lastLine'
@@ -181,14 +190,13 @@ getKeyWordContext pos ls keywords = do
181190
Just kw -> Just $ KeyWord kw
182191
else Just None
183192
where
193+
currentLineM = ls Extra.!? (fromIntegral $ pos ^. JL.line)
194+
lastNonEmptyLineM :: Maybe T.Text
184195
lastNonEmptyLineM = do
185196
cur' <- currentLineM
186-
traceShowM ("cur line", cur')
187197
let cur = stripPartiallyWritten $ T.take (fromIntegral $ pos ^. JL.character) cur'
188-
traceShowM ("cur line before pref", cur)
189198
List.find (not . T.null . T.stripEnd)
190199
$ cur : previousLines pos ls
191-
currentLineM = ls Extra.!? (fromIntegral $ pos ^. JL.line)
192200

193201
{- | Parse the given set of lines (starting before current cursor position
194202
up to the start of the file) to find the nearest stanza declaration,
@@ -237,8 +245,8 @@ stripPartiallyWritten = T.dropWhileEnd (\y -> (y /= ' ') && (y /= ':'))
237245
checks whether a suffix needs to be completed,
238246
and calculates the range in the document in which to complete
239247
-}
240-
getFilePathCompletionContext :: FilePath -> VFS.PosPrefixInfo -> CabalCompletionContext
241-
getFilePathCompletionContext dir prefixInfo =
248+
getCabalCompletionContext :: FilePath -> VFS.PosPrefixInfo -> CabalCompletionContext
249+
getCabalCompletionContext dir prefixInfo =
242250
CabalCompletionContext
243251
{ completionPrefix = filepathPrefix
244252
, completionSuffix = Just suffix
@@ -264,7 +272,7 @@ getFilePathCompletionContext dir prefixInfo =
264272
cursorColumn = fromIntegral $ VFS.cursorPos prefixInfo ^. JL.character
265273
-- if the filepath is inside apostrophes, we parse until the apostrophe,
266274
-- otherwise we parse until a space occurs
267-
stopConditionChars = apostropheOrSpaceSeparator : [',']
275+
stopConditionChars = apostropheOrSpaceSeparator : [',', ':']
268276

269277
buildCompletion :: CabalCompletionItem -> J.CompletionItem
270278
buildCompletion completionItem =
@@ -317,8 +325,9 @@ filePathCompleter :: Completer
317325
filePathCompleter ctx = do
318326
let suffix = fromMaybe "" $ completionSuffix ctx
319327
complInfo = pathCompletionInfoFromCompletionContext ctx
328+
toMatch = fromMaybe "" $ T.stripPrefix "./" $ partialFileName complInfo
320329
filePathCompletions <- listFileCompletions complInfo
321-
let scored = Fuzzy.simpleFilter 1000 10 (partialFileName complInfo) (map T.pack filePathCompletions)
330+
let scored = Fuzzy.simpleFilter 1000 10 toMatch (map T.pack filePathCompletions)
322331
forM
323332
scored
324333
( \compl' -> do
@@ -327,7 +336,7 @@ filePathCompleter ctx = do
327336
pure $ makeCabalCompletionItem (completionRange ctx) fullFilePath fullFilePath
328337
)
329338
where
330-
-- \| Takes a suffix, a completed path and a pathCompletionInfo and
339+
-- Takes a suffix, a completed path and a pathCompletionInfo and
331340
-- generates the whole filepath including the already written prefix
332341
-- and the suffix in case the completed path is a filepath
333342
makeFullFilePath :: T.Text -> T.Text -> PathCompletionInfo -> IO T.Text
@@ -354,7 +363,7 @@ directoryCompleter ctx = do
354363
pure $ makeCabalCompletionItem (completionRange ctx) fullDirPath fullDirPath
355364
)
356365
where
357-
-- \| Takes a directory and PathCompletionInfo and
366+
-- Takes a directory and PathCompletionInfo and
358367
-- returns the whole path including the prefix that was already written
359368
makeFullDirPath :: T.Text -> PathCompletionInfo -> IO T.Text
360369
makeFullDirPath completion' complInfo = do
@@ -420,6 +429,16 @@ mkDirFromCWD complInfo fp = Posix.addTrailingPathSeparator $ mkCompletionDirecto
420429
421430
Note that partialFileName combined with partialFileDir results in
422431
the original prefix.
432+
433+
Example:
434+
On the written filepath: @dir1/fi@ the
435+
resulting PathCompletionInfo would be:
436+
437+
@
438+
partialFileName = "fi"
439+
partialFileDir = "dir1/dir2/fi"
440+
...
441+
@
423442
-}
424443
data PathCompletionInfo = PathCompletionInfo
425444
{ partialFileName :: T.Text

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)