Skip to content

Commit 4dbe03b

Browse files
author
Ishmum Jawad Khan
committed
[refactor] minor fixes for performance
1 parent bb550e5 commit 4dbe03b

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

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

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ 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 -> Range -> Maybe WorkspaceEdit
46+
mkPragmaEdit :: Uri -> T.Text -> Range -> WorkspaceEdit
4747
mkPragmaEdit uri pragmaName rng = res where
4848
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
5050

5151
-- ---------------------------------------------------------------------
5252
-- | Offer to add a missing Language Pragma to the top of a file.
@@ -55,18 +55,24 @@ codeActionProvider :: CodeActionProvider IdeState
5555
codeActionProvider lsp state _plId docId _ (J.CodeActionContext (J.List diags) _monly) = do
5656
let mFile = docId ^. J.uri & uriToFilePath <&> toNormalizedFilePath'
5757
pm <- fmap join $ runAction "addPragma" state $ getParsedModule `traverse` mFile
58-
contents <- LSP.getVirtualFileFunc lsp . toNormalizedUri $ docId ^. J.uri
5958
let dflags = ms_hspp_opts . pm_mod_summary <$> pm
6059
-- Get all potential Pragmas for all diagnostics.
6160
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
6470
cmds <- mapM (mkCodeAction rng) pragmas
6571
return $ Right $ List cmds
6672
where
6773
mkCodeAction rng pragmaName = do
6874
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
7076
title = "Add \"" <> pragmaName <> "\""
7177
edit = mkPragmaEdit (docId ^. J.uri) pragmaName rng
7278
return codeAction
@@ -167,19 +173,14 @@ completion lspFuncs _ide complParams = do
167173

168174
-- | Find the first non-blank line before the first of (module name / imports / declarations).
169175
-- Useful for inserting pragmas.
170-
endOfModuleHeader :: Maybe ParsedModule -> Maybe T.Text -> Range
171-
endOfModuleHeader mpm contents =
176+
startOfModuleHeader :: Maybe ParsedModule -> Int
177+
startOfModuleHeader mpm =
172178
case mpm of
173179
Just pm ->
174180
let mod = unLoc $ pm_parsed_source pm
175181
modNameLoc = getLoc <$> hsmodName mod
176182
firstImportLoc = getLoc <$> listToMaybe (hsmodImports mod)
177183
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

Comments
 (0)