Skip to content

Commit e5e2bfa

Browse files
author
Ishmum Jawad Khan
committed
[bug-fix] file contents provided correctly
1 parent 56b0eee commit e5e2bfa

File tree

2 files changed

+29
-28
lines changed

2 files changed

+29
-28
lines changed

plugins/default/src/Ide/Plugin/Pragmas.hs

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43,36 +43,32 @@ descriptor plId = (defaultPluginDescriptor plId)
4343
-- Pragma is added to the first line of the Uri.
4444
-- It is assumed that the pragma name is a valid pragma,
4545
-- thus, not validated.
46-
mkPragmaEdit :: Uri -> T.Text -> Maybe ParsedModule -> Maybe WorkspaceEdit
47-
mkPragmaEdit uri pragmaName mpm = res mpm where
48-
pos = J.Position 0 0
49-
textEdits pm = J.List
50-
[J.TextEdit (endOfModuleHeader pm Nothing)
51-
("{-# LANGUAGE " <> pragmaName <> " #-}\n")
52-
]
53-
res (Just pm) = Just $ J.WorkspaceEdit
54-
(Just . H.singleton uri $ textEdits pm)
55-
Nothing
56-
res Nothing = Nothing
46+
mkPragmaEdit :: Uri -> T.Text -> Range -> Maybe WorkspaceEdit
47+
mkPragmaEdit uri pragmaName rng = res where
48+
textEdits = J.List [J.TextEdit rng ("{-# LANGUAGE " <> pragmaName <> " #-}\n")]
49+
res = Just $ J.WorkspaceEdit (Just . H.singleton uri $ textEdits) Nothing
5750

5851
-- ---------------------------------------------------------------------
5952
-- | Offer to add a missing Language Pragma to the top of a file.
6053
-- Pragmas are defined by a curated list of known pragmas, see 'possiblePragmas'.
6154
codeActionProvider :: CodeActionProvider IdeState
62-
codeActionProvider _ state _plId docId _ (J.CodeActionContext (J.List diags) _monly) = do
55+
codeActionProvider lsp state _plId docId _ (J.CodeActionContext (J.List diags) _monly) = do
6356
let mFile = docId ^. J.uri & uriToFilePath <&> toNormalizedFilePath'
6457
pm <- fmap join $ runAction "addPragma" state $ getParsedModule `traverse` mFile
58+
contents <- LSP.getVirtualFileFunc lsp . toNormalizedUri $ docId ^. J.uri
6559
let dflags = ms_hspp_opts . pm_mod_summary <$> pm
6660
-- Get all potential Pragmas for all diagnostics.
6761
pragmas = nubOrd $ concatMap (\d -> genPragma dflags (d ^. J.message)) diags
68-
cmds <- mapM (mkCodeAction pm) pragmas
62+
text = fmap VFS.virtualFileText contents
63+
rng = endOfModuleHeader pm text
64+
cmds <- mapM (mkCodeAction rng) pragmas
6965
return $ Right $ List cmds
7066
where
71-
mkCodeAction pm pragmaName = do
67+
mkCodeAction rng pragmaName = do
7268
let
73-
codeAction = J.CACodeAction $ J.CodeAction title (Just J.CodeActionQuickFix) (Just (J.List [])) (Just =<< edit) Nothing
69+
codeAction = J.CACodeAction $ J.CodeAction title (Just J.CodeActionQuickFix) (Just (J.List [])) edit Nothing
7470
title = "Add \"" <> pragmaName <> "\""
75-
edit = mkPragmaEdit (docId ^. J.uri) pragmaName pm
71+
edit = mkPragmaEdit (docId ^. J.uri) pragmaName rng
7672
return codeAction
7773

7874
genPragma mDynflags target =
@@ -171,14 +167,19 @@ completion lspFuncs _ide complParams = do
171167

172168
-- | Find the first non-blank line before the first of (module name / imports / declarations).
173169
-- Useful for inserting pragmas.
174-
endOfModuleHeader :: ParsedModule -> Maybe T.Text -> Range
175-
endOfModuleHeader pm contents =
176-
let mod = unLoc $ pm_parsed_source pm
177-
modNameLoc = getLoc <$> hsmodName mod
178-
firstImportLoc = getLoc <$> listToMaybe (hsmodImports mod)
179-
firstDeclLoc = getLoc <$> listToMaybe (hsmodDecls mod)
180-
line = fromMaybe 0 $ firstNonBlankBefore . _line . _start =<< srcSpanToRange =<<
181-
modNameLoc <|> firstImportLoc <|> firstDeclLoc
182-
firstNonBlankBefore n = (n -) . fromMaybe 0 . findIndex (not . T.null) . reverse . take n . T.lines <$> contents
183-
loc = Position line 0
184-
in Range loc loc
170+
endOfModuleHeader :: Maybe ParsedModule -> Maybe T.Text -> Range
171+
endOfModuleHeader mpm contents =
172+
case mpm of
173+
Just pm ->
174+
let mod = unLoc $ pm_parsed_source pm
175+
modNameLoc = getLoc <$> hsmodName mod
176+
firstImportLoc = getLoc <$> listToMaybe (hsmodImports mod)
177+
firstDeclLoc = getLoc <$> listToMaybe (hsmodDecls mod)
178+
line = fromMaybe 0 $ firstNonBlankBefore . _line . _start =<< srcSpanToRange =<<
179+
modNameLoc <|> firstImportLoc <|> firstDeclLoc
180+
firstNonBlankBefore n = findIndex (not . T.null) . take n . T.lines =<< contents
181+
loc = Position line 0
182+
in Range loc loc
183+
_ ->
184+
let loc = Position 0 0
185+
in Range loc loc

test/functional/FunctionalCodeAction.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,8 +564,8 @@ missingPragmaTests = testGroup "missing pragma warning code actions" [
564564
let expected =
565565
[ "#! /usr/bin/env nix-shell"
566566
, "#! nix-shell --pure -i runghc -p \"haskellPackages.ghcWithPackages (hp: with hp; [ turtle ])\""
567-
, "{-# LANGUAGE TypeApplications #-}"
568567
, "{-# LANGUAGE ScopedTypeVariables #-}"
568+
, "{-# LANGUAGE TypeApplications #-}"
569569
, "module TypeApplications where"
570570
, ""
571571
, "foo :: forall a. a -> a"

0 commit comments

Comments
 (0)