Skip to content

Commit 87ae166

Browse files
VeryMilkyJoeVeryMilkyJoe
VeryMilkyJoe
authored andcommitted
Currently range is not being set correctly for inserts
1 parent 7639c4d commit 87ae166

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

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

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -201,15 +201,12 @@ getFilePathCompletionContext dir prefixInfo =
201201
CabalCompletionContext
202202
{ completionPrefix = filepathPrefix
203203
, completionSuffix = Just suffix
204-
, completionRange = editRange
204+
, completionRange = Range completionStart completionEnd
205205
, completionCabalFilePath = dir
206206
}
207207
where
208-
(Position linePos charPos) = VFS.cursorPos prefixInfo
209-
editRange =
210-
Range
211-
(Position linePos (fromIntegral charPos - fromIntegral (T.length filepathPrefix)))
212-
(Position linePos charPos)
208+
completionEnd = VFS.cursorPos prefixInfo
209+
completionStart = Position (_line completionEnd) (_character completionEnd - (fromIntegral $ T.length filepathPrefix))
213210
filepathPrefix = T.takeWhileEnd (not . (`elem` stopConditionChars)) prevLineText
214211
(prevLineText, endLineText) = T.splitAt cursorColumn $ VFS.fullLine prefixInfo
215212
suffix = if (apostropheOrSpaceSeparator == '\"') && even (T.count "\"" endLineText) then "\"" else ""
@@ -246,15 +243,15 @@ buildCompletion completionItem =
246243
-- Completor API
247244
-- ----------------------------------------------------------------
248245

249-
{- | Returns a CabalCompletionItem with the given range
250-
and text to be inserted with no suffix to be written
246+
{- | Returns a CabalCompletionItem with the given starting position
247+
and text to be inserted where the displayed text is the same as the inserted text
251248
after the inserted text
252249
-}
253250
makeSimpleCabalCompletionItem :: Range -> T.Text -> CabalCompletionItem
254251
makeSimpleCabalCompletionItem r txt = CabalCompletionItem txt Nothing r
255252

256-
{- | Returns a CabalCompletionItem with the given range,
257-
text to be inserted and suffix to be written after the inserted text
253+
{- | Returns a CabalCompletionItem with the given starting position,
254+
text to be inserted and text to be displayed in completionsuggestion
258255
-}
259256
makeCabalCompletionItem :: Range -> T.Text -> T.Text -> CabalCompletionItem
260257
makeCabalCompletionItem r insertTxt displayTxt = CabalCompletionItem insertTxt (Just displayTxt) r
@@ -271,7 +268,9 @@ noopCompleter _ = pure []
271268
constantCompleter :: [T.Text] -> Completer
272269
constantCompleter completions ctxInfo = do
273270
let scored = Fuzzy.simpleFilter 1000 10 (completionPrefix ctxInfo) completions
274-
pure $ map (makeSimpleCabalCompletionItem (completionRange ctxInfo) . Fuzzy.original) scored
271+
let range = completionRange ctxInfo
272+
pure $ map (makeSimpleCabalCompletionItem range . Fuzzy.original) scored
273+
275274

276275
{- | Completer to be used when a file path can be
277276
completed for the field, takes the file path of the directory to start from,
@@ -283,24 +282,29 @@ filePathCompleter ctx = do
283282
complInfo = pathCompletionInfoFromCompletionContext ctx
284283
traceShowM ("---> COMPLETER", complInfo)
285284
filePathCompletions <- listFileCompletions complInfo
286-
traceShowM ("Completions: ", filePathCompletions) -- ("Completions: ",["MyLib.hs"])
287-
traceShowM ("LeftOver", prefixLeftOver complInfo) -- ""
285+
traceShowM ("Completions: ", filePathCompletions)
286+
traceShowM ("LeftOver", prefixLeftOver complInfo)
288287
let scored = Fuzzy.simpleFilter 1000 10 (prefixLeftOver complInfo) (map T.pack filePathCompletions)
289-
--[Scored {score = 0, original = "MyLib.hs"}]
290288
forM
291289
(traceShowId scored)
292290
( \compl' -> do
293291
let compl = Fuzzy.original compl'
294-
withSuffix <- addSuffixIfFilePath suffix compl
295-
pure $ makeCabalCompletionItem (completionRange ctx) (T.pack (prefixPathInfo complInfo) <> "/" <> withSuffix) (T.pack (prefixPathInfo complInfo) <> "/" <> compl)
292+
fullFilePath <- makeFullFilePath suffix compl complInfo
293+
let charCursorPos = _character $ _end (completionRange ctx)
294+
let newEndChar = charCursorPos + ((fromIntegral $ T.length compl) - (fromIntegral (T.length $ prefixLeftOver complInfo)))
295+
let actualRange = Range (_start (completionRange ctx)) (Position (_line $ _end (completionRange ctx)) newEndChar)
296+
pure $ makeCabalCompletionItem actualRange fullFilePath fullFilePath
296297
)
297298

298299
where
299-
addSuffixIfFilePath :: T.Text -> T.Text -> IO T.Text
300-
addSuffixIfFilePath suffix' completion' = do
301-
isFilePath <- doesFileExist $ T.unpack completion'
302-
let completion = if isFilePath then T.append completion' suffix' else completion'
303-
pure completion
300+
-- | Takes a suffix and a text and returns filepath with the suffix
301+
makeFullFilePath :: T.Text -> T.Text -> PathCompletionInfo -> IO T.Text
302+
makeFullFilePath suffix' completion' complInfo = do
303+
let fullPath' = prefixPathInfo complInfo </> T.unpack completion'
304+
traceShowM ("full path" ,fullPath')
305+
isFilePath <- doesFileExist fullPath'
306+
let fullPath = if isFilePath then fullPath' ++ T.unpack suffix' else fullPath'
307+
pure $ T.pack fullPath
304308

305309
listFileCompletions :: PathCompletionInfo -> IO [FilePath]
306310
listFileCompletions complInfo = do

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ pathCompleterTests = testGroup "Path Completion Tests"
200200
CabalCompletionContext
201201
{ completionPrefix = prefix
202202
, completionSuffix = Nothing
203-
, completionRange = Range (Position 0 0) (Position 0 0)
203+
, completionRange = Range (Position 0 0) (Position 0 0)
204204
, completionCabalFilePath = fp </> "test.cabal"
205205
}
206206
getTestDir :: IO FilePath

0 commit comments

Comments
 (0)