@@ -27,7 +27,7 @@ import System.Directory (doesDirectoryExist,
27
27
import qualified System.FilePath as FP
28
28
import qualified System.FilePath.Posix as Posix
29
29
import qualified Text.Fuzzy.Parallel as Fuzzy
30
- import Debug.Trace (traceShowM , traceShowId )
30
+ import Debug.Trace (traceShowM )
31
31
32
32
{- | Takes information needed to build possible completion items
33
33
and returns the list of possible completion items
@@ -77,7 +77,17 @@ data KeyWordContext
77
77
type KeyWordName = T. Text
78
78
type StanzaName = T. Text
79
79
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
+ -}
81
91
data CabalCompletionContext = CabalCompletionContext
82
92
{ completionPrefix :: T. Text
83
93
-- ^ text prefix to complete
@@ -166,7 +176,7 @@ getContext pos ls =
166
176
-}
167
177
getKeyWordContext :: Position -> [T. Text ] -> Map KeyWordName a -> Maybe KeyWordContext
168
178
getKeyWordContext pos ls keywords = do
169
- case traceShowId $ lastNonEmptyLineM of
179
+ case lastNonEmptyLineM of
170
180
Nothing -> Just None
171
181
Just lastLine' -> do
172
182
let (whiteSpaces, lastLine) = T. span (== ' ' ) lastLine'
@@ -181,14 +191,13 @@ getKeyWordContext pos ls keywords = do
181
191
Just kw -> Just $ KeyWord kw
182
192
else Just None
183
193
where
194
+ currentLineM = ls Extra. !? (fromIntegral $ pos ^. JL. line)
195
+ lastNonEmptyLineM :: Maybe T. Text
184
196
lastNonEmptyLineM = do
185
197
cur' <- currentLineM
186
- traceShowM (" cur line" , cur')
187
198
let cur = stripPartiallyWritten $ T. take (fromIntegral $ pos ^. JL. character) cur'
188
- traceShowM (" cur line before pref" , cur)
189
199
List. find (not . T. null . T. stripEnd)
190
200
$ cur : previousLines pos ls
191
- currentLineM = ls Extra. !? (fromIntegral $ pos ^. JL. line)
192
201
193
202
{- | Parse the given set of lines (starting before current cursor position
194
203
up to the start of the file) to find the nearest stanza declaration,
@@ -237,8 +246,8 @@ stripPartiallyWritten = T.dropWhileEnd (\y -> (y /= ' ') && (y /= ':'))
237
246
checks whether a suffix needs to be completed,
238
247
and calculates the range in the document in which to complete
239
248
-}
240
- getFilePathCompletionContext :: FilePath -> VFS. PosPrefixInfo -> CabalCompletionContext
241
- getFilePathCompletionContext dir prefixInfo =
249
+ getCabalCompletionContext :: FilePath -> VFS. PosPrefixInfo -> CabalCompletionContext
250
+ getCabalCompletionContext dir prefixInfo =
242
251
CabalCompletionContext
243
252
{ completionPrefix = filepathPrefix
244
253
, completionSuffix = Just suffix
@@ -264,7 +273,7 @@ getFilePathCompletionContext dir prefixInfo =
264
273
cursorColumn = fromIntegral $ VFS. cursorPos prefixInfo ^. JL. character
265
274
-- if the filepath is inside apostrophes, we parse until the apostrophe,
266
275
-- otherwise we parse until a space occurs
267
- stopConditionChars = apostropheOrSpaceSeparator : [' ,' ]
276
+ stopConditionChars = apostropheOrSpaceSeparator : [' ,' , ' : ' ]
268
277
269
278
buildCompletion :: CabalCompletionItem -> J. CompletionItem
270
279
buildCompletion completionItem =
@@ -317,8 +326,12 @@ filePathCompleter :: Completer
317
326
filePathCompleter ctx = do
318
327
let suffix = fromMaybe " " $ completionSuffix ctx
319
328
complInfo = pathCompletionInfoFromCompletionContext ctx
329
+ toMatch = fromMaybe (partialFileName complInfo) $ T. stripPrefix " ./" $ partialFileName complInfo
320
330
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)
322
335
forM
323
336
scored
324
337
( \ compl' -> do
@@ -327,7 +340,7 @@ filePathCompleter ctx = do
327
340
pure $ makeCabalCompletionItem (completionRange ctx) fullFilePath fullFilePath
328
341
)
329
342
where
330
- -- \| Takes a suffix, a completed path and a pathCompletionInfo and
343
+ -- Takes a suffix, a completed path and a pathCompletionInfo and
331
344
-- generates the whole filepath including the already written prefix
332
345
-- and the suffix in case the completed path is a filepath
333
346
makeFullFilePath :: T. Text -> T. Text -> PathCompletionInfo -> IO T. Text
@@ -354,7 +367,7 @@ directoryCompleter ctx = do
354
367
pure $ makeCabalCompletionItem (completionRange ctx) fullDirPath fullDirPath
355
368
)
356
369
where
357
- -- \| Takes a directory and PathCompletionInfo and
370
+ -- Takes a directory and PathCompletionInfo and
358
371
-- returns the whole path including the prefix that was already written
359
372
makeFullDirPath :: T. Text -> PathCompletionInfo -> IO T. Text
360
373
makeFullDirPath completion' complInfo = do
@@ -420,6 +433,16 @@ mkDirFromCWD complInfo fp = Posix.addTrailingPathSeparator $ mkCompletionDirecto
420
433
421
434
Note that partialFileName combined with partialFileDir results in
422
435
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
+ @
423
446
-}
424
447
data PathCompletionInfo = PathCompletionInfo
425
448
{ partialFileName :: T. Text
0 commit comments