@@ -43,10 +43,10 @@ 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 -> Range -> Maybe WorkspaceEdit
46
+ mkPragmaEdit :: Uri -> T. Text -> Range -> WorkspaceEdit
47
47
mkPragmaEdit uri pragmaName rng = res where
48
48
textEdits = J. List [J. TextEdit rng (" {-# LANGUAGE " <> pragmaName <> " #-}\n " )]
49
- res = Just $ J. WorkspaceEdit (Just . H. singleton uri $ textEdits) Nothing
49
+ res = J. WorkspaceEdit (Just . H. singleton uri $ textEdits) Nothing
50
50
51
51
-- ---------------------------------------------------------------------
52
52
-- | Offer to add a missing Language Pragma to the top of a file.
@@ -55,18 +55,24 @@ codeActionProvider :: CodeActionProvider IdeState
55
55
codeActionProvider lsp state _plId docId _ (J. CodeActionContext (J. List diags) _monly) = do
56
56
let mFile = docId ^. J. uri & uriToFilePath <&> toNormalizedFilePath'
57
57
pm <- fmap join $ runAction " addPragma" state $ getParsedModule `traverse` mFile
58
- contents <- LSP. getVirtualFileFunc lsp . toNormalizedUri $ docId ^. J. uri
59
58
let dflags = ms_hspp_opts . pm_mod_summary <$> pm
60
59
-- Get all potential Pragmas for all diagnostics.
61
60
pragmas = nubOrd $ concatMap (\ d -> genPragma dflags (d ^. J. message)) diags
62
- text = fmap VFS. virtualFileText contents
63
- rng = endOfModuleHeader pm text
61
+ somh = startOfModuleHeader pm
62
+ loc <- if somh > 0
63
+ then do
64
+ contents <- LSP. getVirtualFileFunc lsp . toNormalizedUri $ docId ^. J. uri
65
+ text <- pure $ fmap VFS. virtualFileText contents
66
+ line <- pure . fromMaybe 0 $ findIndex (not . T. null ) . take somh . T. lines =<< text
67
+ return $ Position line 0
68
+ else return $ Position 0 0
69
+ rng <- pure $ Range loc loc
64
70
cmds <- mapM (mkCodeAction rng) pragmas
65
71
return $ Right $ List cmds
66
72
where
67
73
mkCodeAction rng pragmaName = do
68
74
let
69
- codeAction = J. CACodeAction $ J. CodeAction title (Just J. CodeActionQuickFix ) (Just (J. List [] )) edit Nothing
75
+ codeAction = J. CACodeAction $ J. CodeAction title (Just J. CodeActionQuickFix ) (Just (J. List [] )) ( Just edit) Nothing
70
76
title = " Add \" " <> pragmaName <> " \" "
71
77
edit = mkPragmaEdit (docId ^. J. uri) pragmaName rng
72
78
return codeAction
@@ -167,19 +173,14 @@ completion lspFuncs _ide complParams = do
167
173
168
174
-- | Find the first non-blank line before the first of (module name / imports / declarations).
169
175
-- Useful for inserting pragmas.
170
- endOfModuleHeader :: Maybe ParsedModule -> Maybe T. Text -> Range
171
- endOfModuleHeader mpm contents =
176
+ startOfModuleHeader :: Maybe ParsedModule -> Int
177
+ startOfModuleHeader mpm =
172
178
case mpm of
173
179
Just pm ->
174
180
let mod = unLoc $ pm_parsed_source pm
175
181
modNameLoc = getLoc <$> hsmodName mod
176
182
firstImportLoc = getLoc <$> listToMaybe (hsmodImports mod )
177
183
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
184
+ in fromMaybe 0 $ Just . _line . _start =<< srcSpanToRange =<<
185
+ modNameLoc <|> firstImportLoc <|> firstDeclLoc
186
+ _ -> 0
0 commit comments