Skip to content

Commit 191414f

Browse files
alexnaspoleapalexnaspo
authored andcommitted
Move pragmas completion to pragmas plugin
1 parent 6fa3e64 commit 191414f

File tree

2 files changed

+31
-30
lines changed
  • ghcide/src/Development/IDE/Plugin/Completions
  • plugins/hls-pragmas-plugin/src/Ide/Plugin

2 files changed

+31
-30
lines changed

ghcide/src/Development/IDE/Plugin/Completions/Logic.hs

-29
Original file line numberDiff line numberDiff line change
@@ -299,11 +299,6 @@ mkExtCompl label =
299299
Nothing Nothing Nothing Nothing Nothing Nothing Nothing
300300
Nothing Nothing Nothing Nothing Nothing Nothing
301301

302-
mkPragmaCompl :: T.Text -> T.Text -> CompletionItem
303-
mkPragmaCompl label insertText =
304-
CompletionItem label (Just CiKeyword) Nothing Nothing
305-
Nothing Nothing Nothing Nothing Nothing (Just insertText) (Just Snippet)
306-
Nothing Nothing Nothing Nothing Nothing Nothing
307302

308303
fromIdentInfo :: Uri -> IdentInfo -> Maybe T.Text -> CompItem
309304
fromIdentInfo doc IdentInfo{..} q = CI
@@ -600,14 +595,7 @@ getCompletions plId ideOpts CC {allModNamesAsNS, anyQualCompls, unqualCompls, qu
600595
, enteredQual `T.isPrefixOf` label
601596
]
602597

603-
filtListWithSnippet f list suffix =
604-
[ toggleSnippets caps config (f label (snippet <> suffix))
605-
| (snippet, label) <- list
606-
, Fuzzy.test fullPrefix label
607-
]
608-
609598
filtImportCompls = filtListWith (mkImportCompl enteredQual) importableModules
610-
filtPragmaCompls = filtListWithSnippet mkPragmaCompl validPragmas
611599
filtOptsCompls = filtListWith mkExtCompl
612600
filtKeywordCompls
613601
| T.null prefixModule = filtListWith mkExtCompl (optKeywords ideOpts)
@@ -628,8 +616,6 @@ getCompletions plId ideOpts CC {allModNamesAsNS, anyQualCompls, unqualCompls, qu
628616
-> return []
629617
| "{-# options_ghc" `T.isPrefixOf` T.toLower fullLine
630618
-> return $ filtOptsCompls (map (T.pack . stripLeading '-') $ flagsForCompletion False)
631-
| "{-# " `T.isPrefixOf` fullLine
632-
-> return $ filtPragmaCompls (pragmaSuffix fullLine)
633619
| otherwise -> do
634620
-- assumes that nubOrdBy is stable
635621
let uniqueFiltCompls = nubOrdBy uniqueCompl filtCompls
@@ -655,21 +641,6 @@ uniqueCompl x y =
655641
-- helper functions for pragmas
656642
-- ---------------------------------------------------------------------
657643

658-
validPragmas :: [(T.Text, T.Text)]
659-
validPragmas =
660-
[ ("LANGUAGE ${1:extension}" , "LANGUAGE")
661-
, ("OPTIONS_GHC -${1:option}" , "OPTIONS_GHC")
662-
, ("INLINE ${1:function}" , "INLINE")
663-
, ("NOINLINE ${1:function}" , "NOINLINE")
664-
, ("INLINABLE ${1:function}" , "INLINABLE")
665-
, ("WARNING ${1:message}" , "WARNING")
666-
, ("DEPRECATED ${1:message}" , "DEPRECATED")
667-
, ("ANN ${1:annotation}" , "ANN")
668-
, ("RULES" , "RULES")
669-
, ("SPECIALIZE ${1:function}" , "SPECIALIZE")
670-
, ("SPECIALIZE INLINE ${1:function}", "SPECIALIZE INLINE")
671-
]
672-
673644
pragmaSuffix :: T.Text -> T.Text
674645
pragmaSuffix fullLine
675646
| "}" `T.isSuffixOf` fullLine = mempty

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

+31-1
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,16 @@ completion _ide _ complParams = do
163163
| "{-# LANGUAGE" `T.isPrefixOf` VFS.fullLine pfix
164164
= J.List $ map buildCompletion
165165
(Fuzzy.simpleFilter (VFS.prefixText pfix) allPragmas)
166+
-- if there already is a closing bracket - complete without one
167+
| isPragmaPrefix (VFS.fullLine pfix) && "}" `T.isSuffixOf` VFS.fullLine pfix
168+
= J.List $ map (\(a, b, c) -> mkPragmaCompl a b c) (validPragmas Nothing)
169+
-- if there is no closing bracket - complete with one
170+
| isPragmaPrefix (VFS.fullLine pfix)
171+
= J.List $ map (\(a, b, c) -> mkPragmaCompl a b c) (validPragmas (Just "}"))
166172
| otherwise
167173
= J.List []
168174
result Nothing = J.List []
175+
isPragmaPrefix line = "{-#" `T.isPrefixOf` line
169176
buildCompletion p =
170177
J.CompletionItem
171178
{ _label = p,
@@ -187,8 +194,31 @@ completion _ide _ complParams = do
187194
_xdata = Nothing
188195
}
189196
_ -> return $ J.List []
190-
191197
-----------------------------------------------------------------------
198+
validPragmas :: Maybe T.Text -> [(T.Text, T.Text, T.Text)]
199+
validPragmas mSuffix =
200+
[ ("LANGUAGE ${1:extension} #-" <> suffix , "LANGUAGE", "{-# LANGUAGE #-}")
201+
, ("OPTIONS_GHC -${1:option} #-" <> suffix , "OPTIONS_GHC", "{-# OPTIONS_GHC #-}")
202+
, ("INLINE ${1:function} #-" <> suffix , "INLINE", "{-# INLINE #-}")
203+
, ("NOINLINE ${1:function} #-" <> suffix , "NOINLINE", "{-# NOINLINE #-}")
204+
, ("INLINABLE ${1:function} #-"<> suffix , "INLINABLE", "{-# INLINABLE #-}")
205+
, ("WARNING ${1:message} #-" <> suffix , "WARNING", "{-# WARNING #-}")
206+
, ("DEPRECATED ${1:message} -#" <> suffix , "DEPRECATED", "{-# DEPRECATED #-}")
207+
, ("ANN ${1:annotation} #-" <> suffix , "ANN", "{-# ANN #-}")
208+
, ("RULES #-}" <> suffix , "RULES", "{-# RULES #-}")
209+
, ("SPECIALIZE ${1:function} #-" <> suffix , "SPECIALIZE", "{-# SPECIALIZE #-}")
210+
, ("SPECIALIZE INLINE ${1:function} #-"<> suffix , "SPECIALIZE INLINE", "{-# SPECIALIZE INLINE #-}")
211+
]
212+
where suffix = case mSuffix of
213+
(Just s) -> s
214+
Nothing -> ""
215+
216+
217+
mkPragmaCompl :: T.Text -> T.Text -> T.Text -> J.CompletionItem
218+
mkPragmaCompl insertText label detail =
219+
J.CompletionItem label (Just J.CiKeyword) Nothing (Just detail)
220+
Nothing Nothing Nothing Nothing Nothing (Just insertText) (Just J.Snippet)
221+
Nothing Nothing Nothing Nothing Nothing Nothing
192222

193223
-- | Find first line after the last file header pragma
194224
-- Defaults to line 0 if the file contains no shebang(s), OPTIONS_GHC pragma(s), or LANGUAGE pragma(s)

0 commit comments

Comments
 (0)