Skip to content

Commit 70e7d3d

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

File tree

3 files changed

+47
-24
lines changed

3 files changed

+47
-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: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +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)
30+
import Debug.Trace (traceShowM)
3131

3232
{- | Takes information needed to build possible completion items
3333
and returns the list of possible completion items
@@ -77,7 +77,17 @@ data KeyWordContext
7777
type KeyWordName = T.Text
7878
type StanzaName = T.Text
7979

80-
-- Information about the current completion status
80+
{- | Information about the current completion status
81+
82+
Example: @"dir1/fi@ having been written to the file
83+
would correspond to:
84+
85+
@
86+
completionPrefix = "dir1/fi"
87+
completionSuffix = Just "\\""
88+
...
89+
@
90+
-}
8191
data CabalCompletionContext = CabalCompletionContext
8292
{ completionPrefix :: T.Text
8393
-- ^ text prefix to complete
@@ -166,7 +176,7 @@ getContext pos ls =
166176
-}
167177
getKeyWordContext :: Position -> [T.Text] -> Map KeyWordName a -> Maybe KeyWordContext
168178
getKeyWordContext pos ls keywords = do
169-
case traceShowId $ lastNonEmptyLineM of
179+
case lastNonEmptyLineM of
170180
Nothing -> Just None
171181
Just lastLine' -> do
172182
let (whiteSpaces, lastLine) = T.span (== ' ') lastLine'
@@ -181,14 +191,13 @@ getKeyWordContext pos ls keywords = do
181191
Just kw -> Just $ KeyWord kw
182192
else Just None
183193
where
194+
currentLineM = ls Extra.!? (fromIntegral $ pos ^. JL.line)
195+
lastNonEmptyLineM :: Maybe T.Text
184196
lastNonEmptyLineM = do
185197
cur' <- currentLineM
186-
traceShowM ("cur line", cur')
187198
let cur = stripPartiallyWritten $ T.take (fromIntegral $ pos ^. JL.character) cur'
188-
traceShowM ("cur line before pref", cur)
189199
List.find (not . T.null . T.stripEnd)
190200
$ cur : previousLines pos ls
191-
currentLineM = ls Extra.!? (fromIntegral $ pos ^. JL.line)
192201

193202
{- | Parse the given set of lines (starting before current cursor position
194203
up to the start of the file) to find the nearest stanza declaration,
@@ -237,8 +246,8 @@ stripPartiallyWritten = T.dropWhileEnd (\y -> (y /= ' ') && (y /= ':'))
237246
checks whether a suffix needs to be completed,
238247
and calculates the range in the document in which to complete
239248
-}
240-
getFilePathCompletionContext :: FilePath -> VFS.PosPrefixInfo -> CabalCompletionContext
241-
getFilePathCompletionContext dir prefixInfo =
249+
getCabalCompletionContext :: FilePath -> VFS.PosPrefixInfo -> CabalCompletionContext
250+
getCabalCompletionContext dir prefixInfo =
242251
CabalCompletionContext
243252
{ completionPrefix = filepathPrefix
244253
, completionSuffix = Just suffix
@@ -264,7 +273,7 @@ getFilePathCompletionContext dir prefixInfo =
264273
cursorColumn = fromIntegral $ VFS.cursorPos prefixInfo ^. JL.character
265274
-- if the filepath is inside apostrophes, we parse until the apostrophe,
266275
-- otherwise we parse until a space occurs
267-
stopConditionChars = apostropheOrSpaceSeparator : [',']
276+
stopConditionChars = apostropheOrSpaceSeparator : [',', ':']
268277

269278
buildCompletion :: CabalCompletionItem -> J.CompletionItem
270279
buildCompletion completionItem =
@@ -317,8 +326,12 @@ filePathCompleter :: Completer
317326
filePathCompleter ctx = do
318327
let suffix = fromMaybe "" $ completionSuffix ctx
319328
complInfo = pathCompletionInfoFromCompletionContext ctx
329+
toMatch = fromMaybe (partialFileName complInfo) $ T.stripPrefix "./" $ partialFileName complInfo
320330
filePathCompletions <- listFileCompletions complInfo
321-
let scored = Fuzzy.simpleFilter 1000 10 (partialFileName complInfo) (map T.pack filePathCompletions)
331+
traceShowM ("match string:", toMatch)
332+
traceShowM ("completions:", filePathCompletions)
333+
let scored = Fuzzy.simpleFilter 1000 10 toMatch (map T.pack filePathCompletions)
334+
traceShowM ("scored:", scored)
322335
forM
323336
scored
324337
( \compl' -> do
@@ -327,7 +340,7 @@ filePathCompleter ctx = do
327340
pure $ makeCabalCompletionItem (completionRange ctx) fullFilePath fullFilePath
328341
)
329342
where
330-
-- \| Takes a suffix, a completed path and a pathCompletionInfo and
343+
-- Takes a suffix, a completed path and a pathCompletionInfo and
331344
-- generates the whole filepath including the already written prefix
332345
-- and the suffix in case the completed path is a filepath
333346
makeFullFilePath :: T.Text -> T.Text -> PathCompletionInfo -> IO T.Text
@@ -354,7 +367,7 @@ directoryCompleter ctx = do
354367
pure $ makeCabalCompletionItem (completionRange ctx) fullDirPath fullDirPath
355368
)
356369
where
357-
-- \| Takes a directory and PathCompletionInfo and
370+
-- Takes a directory and PathCompletionInfo and
358371
-- returns the whole path including the prefix that was already written
359372
makeFullDirPath :: T.Text -> PathCompletionInfo -> IO T.Text
360373
makeFullDirPath completion' complInfo = do
@@ -420,6 +433,16 @@ mkDirFromCWD complInfo fp = Posix.addTrailingPathSeparator $ mkCompletionDirecto
420433
421434
Note that partialFileName combined with partialFileDir results in
422435
the original prefix.
436+
437+
Example:
438+
On the written filepath: @dir1/fi@ the
439+
resulting PathCompletionInfo would be:
440+
441+
@
442+
partialFileName = "fi"
443+
partialFileDir = "dir1/dir2/fi"
444+
...
445+
@
423446
-}
424447
data PathCompletionInfo = PathCompletionInfo
425448
{ 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)