@@ -43,36 +43,32 @@ descriptor plId = (defaultPluginDescriptor plId)
43
43
-- Pragma is added to the first line of the Uri.
44
44
-- It is assumed that the pragma name is a valid pragma,
45
45
-- 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
57
50
58
51
-- ---------------------------------------------------------------------
59
52
-- | Offer to add a missing Language Pragma to the top of a file.
60
53
-- Pragmas are defined by a curated list of known pragmas, see 'possiblePragmas'.
61
54
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
63
56
let mFile = docId ^. J. uri & uriToFilePath <&> toNormalizedFilePath'
64
57
pm <- fmap join $ runAction " addPragma" state $ getParsedModule `traverse` mFile
58
+ contents <- LSP. getVirtualFileFunc lsp . toNormalizedUri $ docId ^. J. uri
65
59
let dflags = ms_hspp_opts . pm_mod_summary <$> pm
66
60
-- Get all potential Pragmas for all diagnostics.
67
61
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
69
65
return $ Right $ List cmds
70
66
where
71
- mkCodeAction pm pragmaName = do
67
+ mkCodeAction rng pragmaName = do
72
68
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
74
70
title = " Add \" " <> pragmaName <> " \" "
75
- edit = mkPragmaEdit (docId ^. J. uri) pragmaName pm
71
+ edit = mkPragmaEdit (docId ^. J. uri) pragmaName rng
76
72
return codeAction
77
73
78
74
genPragma mDynflags target =
@@ -171,14 +167,19 @@ completion lspFuncs _ide complParams = do
171
167
172
168
-- | Find the first non-blank line before the first of (module name / imports / declarations).
173
169
-- 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
0 commit comments