Skip to content

Commit ddaccc4

Browse files
committed
amend language pragma fix
1 parent 85c560c commit ddaccc4

File tree

1 file changed

+14
-10
lines changed
  • plugins/hls-pragmas-plugin/src/Ide/Plugin

1 file changed

+14
-10
lines changed

plugins/hls-pragmas-plugin/src/Ide/Plugin/Pragmas.hs

+14-10
Original file line numberDiff line numberDiff line change
@@ -182,25 +182,29 @@ completion _ide _ complParams = do
182182
}
183183
_ -> return $ J.List []
184184

185-
-- ---------------------------------------------------------------------
185+
-----------------------------------------------------------------------
186186

187-
-- | Find first line after (last pragma / last shebang / beginning of file).
188-
-- Useful for inserting pragmas.
187+
-- | Find first line after the last LANGUAGE pragma
188+
-- Defaults to line 0 if the file contains no shebang(s), OPTIONS_GHC pragma(s), or other LANGUAGE pragma(s)
189+
-- Otherwise it will be one after the count of line numbers, with order: Shebangs -> OPTIONS_GHC -> LANGUAGE
189190
endOfModuleHeader :: T.Text -> Range
190191
endOfModuleHeader contents = Range loc loc
191192
where
192193
loc = Position line 0
193-
line = maybe 0 succ (lastShebang contents <|> lastOptsGhc contents <|> lastLangPrag contents)
194-
lastShebang = lastLineWithPrefix $ T.isPrefixOf "#!"
195-
lastLangPrag = lastLineWithPrefix $ checkPragma "LANGUAGE" 8
196-
lastOptsGhc = lastLineWithPrefix $ checkPragma "OPTIONS_GHC" 11
194+
line = afterLangPragma contents . afterOptsGhc contents $ afterShebang contents
195+
afterLangPragma contents = afterPragma contents "LANGUAGE"
196+
afterOptsGhc contents = afterPragma contents "OPTIONS_GHC"
197+
afterShebang contents = maybe 0 succ $ lastLineWithPrefix (T.isPrefixOf "#!") contents
197198

198199
lastLineWithPrefix :: (T.Text -> Bool) -> T.Text -> Maybe Int
199200
lastLineWithPrefix p contents = listToMaybe $ reverse $ findIndices p $ T.lines contents
200201

201-
checkPragma :: T.Text -> Int -> T.Text -> Bool
202-
checkPragma name n = check
202+
afterPragma :: T.Text -> T.Text -> Int -> Int
203+
afterPragma contents name lineNum = maybe lineNum succ $ lastLineWithPrefix (checkPragma name) contents
204+
205+
checkPragma :: T.Text -> T.Text -> Bool
206+
checkPragma name = check
203207
where
204208
check l = isPragma l && getName l == name
205-
getName l = T.take n $ T.dropWhile isSpace $ T.drop 3 l
209+
getName l = T.take (T.length name) $ T.dropWhile isSpace $ T.drop 3 l
206210
isPragma = T.isPrefixOf "{-#"

0 commit comments

Comments
 (0)