Skip to content

Commit 22e2d18

Browse files
committed
File path completion now creates a correct text edit in cabal files
1 parent 4df79fc commit 22e2d18

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import Data.Hashable
2121
import Data.HashMap.Strict (HashMap)
2222
import qualified Data.HashMap.Strict as HashMap
2323
import qualified Data.List.NonEmpty as NE
24+
import qualified Data.Text as T
2425
import qualified Data.Text.Encoding as Encoding
2526
import qualified Data.Text.Utf16.Rope as Rope
2627
import Data.Typeable
@@ -44,9 +45,6 @@ import qualified Language.LSP.Types as LSP
4445
import qualified Language.LSP.Types.Lens as JL
4546
import Language.LSP.VFS (VirtualFile)
4647
import qualified Language.LSP.VFS as VFS
47-
import Debug.Trace (traceShowId)
48-
49-
5048
data Log
5149
= LogModificationTime NormalizedFilePath FileVersion
5250
| LogShake Shake.Log
@@ -150,7 +148,8 @@ cabalRules recorder = do
150148
(t, mCabalSource) <- use_ GetFileContents file
151149
log' Debug $ LogModificationTime file t
152150
contents <- case mCabalSource of
153-
Just sources -> pure $ Encoding.encodeUtf8 sources
151+
Just sources ->
152+
pure $ Encoding.encodeUtf8 sources
154153
Nothing -> do
155154
liftIO $ BS.readFile $ fromNormalizedFilePath file
156155

@@ -286,11 +285,16 @@ completion _ide _ complParams = do
286285
result (Just pfix) fp cnts
287286
| Just ctx <- context = do
288287
let completer = contextToCompleter "" ctx
289-
completions <- completer $ VFS.prefixText $ (traceShowId pfix)
290-
genPkgDesc <- readGenericPackageDescription silent fp
291-
pure $ J.List $ makeCompletionItems pfix genPkgDesc completions
288+
completions <- completer filePathPfix
289+
-- genPkgDesc <- readGenericPackageDescription silent fp
290+
pure $ J.List $ makeCompletionItems editRange completions
292291
| otherwise = pure $ J.List []
293292
where
294-
pos = VFS.cursorPos pfix
295-
context = getContext pos (Rope.lines $ cnts ^. VFS.file_text)
293+
(Position linePos charPos) = VFS.cursorPos pfix
294+
context = getContext (Position linePos charPos) (Rope.lines $ cnts ^. VFS.file_text)
295+
filePathPfix = getFilePathCursorPrefix pfix
296+
editRange =
297+
Range
298+
(Position linePos (fromIntegral charPos - fromIntegral (T.length filePathPfix)))
299+
(Position linePos charPos)
296300

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ contextToCompleter _dir (Stanza s, KeyWord kw) =
8484
-- | Takes info about the current cursor position, information
8585
-- about the handled cabal file and a set of possible keywords
8686
-- and creates completion suggestions that fit the current input from the given list
87-
makeCompletionItems :: VFS.PosPrefixInfo -> GenericPackageDescription -> [T.Text] -> [CompletionItem]
88-
makeCompletionItems _pfix _pkgDesc l = map buildCompletion l
87+
makeCompletionItems :: Range -> [T.Text] -> [CompletionItem]
88+
makeCompletionItems range l = map (buildCompletion range) l
8989

9090
-- | Takes a position and a list of lines (representing a file)
9191
-- and returns the context of the current position
@@ -168,11 +168,11 @@ getFilePathCursorPrefix pfixInfo =
168168
stopConditionChars = apostropheOrSpaceSeparator : [',']
169169

170170

171-
buildCompletion :: T.Text -> J.CompletionItem
172-
buildCompletion label =
171+
buildCompletion ::Range -> T.Text -> J.CompletionItem
172+
buildCompletion range label =
173173
J.CompletionItem label (Just J.CiKeyword) Nothing Nothing
174174
Nothing Nothing Nothing Nothing Nothing Nothing Nothing
175-
Nothing Nothing Nothing Nothing Nothing Nothing
175+
Nothing (Just $ CompletionEditText (TextEdit range label)) Nothing Nothing Nothing Nothing
176176

177177
-- ----------------------------------------------------------------
178178
-- Completor API

0 commit comments

Comments
 (0)