@@ -150,6 +150,9 @@ allPragmas =
150150
151151-- ---------------------------------------------------------------------
152152
153+ flags :: [T. Text ]
154+ flags = map (T. pack . stripLeading ' -' ) $ flagsForCompletion False
155+
153156completion :: PluginMethodHandler IdeState 'J.TextDocumentCompletion
154157completion _ide _ complParams = do
155158 let (J. TextDocumentIdentifier uri) = complParams ^. J. textDocument
@@ -163,9 +166,19 @@ completion _ide _ complParams = do
163166 | " {-# LANGUAGE" `T.isPrefixOf` VFS. fullLine pfix
164167 = J. List $ map buildCompletion
165168 (Fuzzy. simpleFilter (VFS. prefixText pfix) allPragmas)
169+ | " {-# options_ghc" `T.isPrefixOf` T. toLower (VFS. fullLine pfix)
170+ = J. List $ map mkExtCompl
171+ (Fuzzy. simpleFilter (VFS. prefixText pfix) flags)
172+ -- if there already is a closing bracket - complete without one
173+ | isPragmaPrefix (VFS. fullLine pfix) && " }" `T.isSuffixOf` VFS. fullLine pfix
174+ = J. List $ map (\ (a, b, c) -> mkPragmaCompl a b c) (validPragmas Nothing )
175+ -- if there is no closing bracket - complete with one
176+ | isPragmaPrefix (VFS. fullLine pfix)
177+ = J. List $ map (\ (a, b, c) -> mkPragmaCompl a b c) (validPragmas (Just " }" ))
166178 | otherwise
167179 = J. List []
168180 result Nothing = J. List []
181+ isPragmaPrefix line = " {-#" `T.isPrefixOf` line
169182 buildCompletion p =
170183 J. CompletionItem
171184 { _label = p,
@@ -187,8 +200,31 @@ completion _ide _ complParams = do
187200 _xdata = Nothing
188201 }
189202 _ -> return $ J. List []
190-
191203-----------------------------------------------------------------------
204+ validPragmas :: Maybe T. Text -> [(T. Text , T. Text , T. Text )]
205+ validPragmas mSuffix =
206+ [ (" LANGUAGE ${1:extension} #-" <> suffix , " LANGUAGE" , " {-# LANGUAGE #-}" )
207+ , (" OPTIONS_GHC -${1:option} #-" <> suffix , " OPTIONS_GHC" , " {-# OPTIONS_GHC #-}" )
208+ , (" INLINE ${1:function} #-" <> suffix , " INLINE" , " {-# INLINE #-}" )
209+ , (" NOINLINE ${1:function} #-" <> suffix , " NOINLINE" , " {-# NOINLINE #-}" )
210+ , (" INLINABLE ${1:function} #-" <> suffix , " INLINABLE" , " {-# INLINABLE #-}" )
211+ , (" WARNING ${1:message} #-" <> suffix , " WARNING" , " {-# WARNING #-}" )
212+ , (" DEPRECATED ${1:message} #-" <> suffix , " DEPRECATED" , " {-# DEPRECATED #-}" )
213+ , (" ANN ${1:annotation} #-" <> suffix , " ANN" , " {-# ANN #-}" )
214+ , (" RULES #-" <> suffix , " RULES" , " {-# RULES #-}" )
215+ , (" SPECIALIZE ${1:function} #-" <> suffix , " SPECIALIZE" , " {-# SPECIALIZE #-}" )
216+ , (" SPECIALIZE INLINE ${1:function} #-" <> suffix , " SPECIALIZE INLINE" , " {-# SPECIALIZE INLINE #-}" )
217+ ]
218+ where suffix = case mSuffix of
219+ (Just s) -> s
220+ Nothing -> " "
221+
222+
223+ mkPragmaCompl :: T. Text -> T. Text -> T. Text -> J. CompletionItem
224+ mkPragmaCompl insertText label detail =
225+ J. CompletionItem label (Just J. CiKeyword ) Nothing (Just detail)
226+ Nothing Nothing Nothing Nothing Nothing (Just insertText) (Just J. Snippet )
227+ Nothing Nothing Nothing Nothing Nothing Nothing
192228
193229-- | Find first line after the last file header pragma
194230-- Defaults to line 0 if the file contains no shebang(s), OPTIONS_GHC pragma(s), or LANGUAGE pragma(s)
@@ -218,3 +254,17 @@ checkPragma name = check
218254 check l = isPragma l && getName l == name
219255 getName l = T. take (T. length name) $ T. dropWhile isSpace $ T. drop 3 l
220256 isPragma = T. isPrefixOf " {-#"
257+
258+
259+ stripLeading :: Char -> String -> String
260+ stripLeading _ [] = []
261+ stripLeading c (s: ss)
262+ | s == c = ss
263+ | otherwise = s: ss
264+
265+
266+ mkExtCompl :: T. Text -> J. CompletionItem
267+ mkExtCompl label =
268+ J. CompletionItem label (Just J. CiKeyword ) Nothing Nothing
269+ Nothing Nothing Nothing Nothing Nothing Nothing Nothing
270+ Nothing Nothing Nothing Nothing Nothing Nothing
0 commit comments