Skip to content

Commit f933011

Browse files
OliverMadineOliver MadineAilrunberberman
authored
Bracketing for snippet completions (haskell#1709)
* Enhancement: haskell#1700 * changed bracketed typed hole completions to regular bracketed completions * removed redudant comment * removed whitespace * Updated test cases to expect bracketed completions, Type constructors with no arguments no longer bracketed * Updated hls tests for bracketed completions * added missing ghcide/example/HLS file * moved ghcide/examoke.HLS to ghcide/bench/example/HLS * Restored ghcide/bench/example/HLS * restored ghcide/bench/example/HLS * removed unnecessary 'T.pack' (OverloadedStrings) Co-authored-by: Oliver Madine <[email protected]> Co-authored-by: Junyoung/Clare Jang <[email protected]> Co-authored-by: Potato Hatsue <[email protected]>
1 parent d2bb0d9 commit f933011

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import Language.LSP.Types
6060
import Language.LSP.Types.Capabilities
6161
import qualified Language.LSP.VFS as VFS
6262
import Outputable (Outputable)
63+
import TyCoRep
6364

6465
-- From haskell-ide-engine/hie-plugin-api/Haskell/Ide/Engine/Context.hs
6566

@@ -247,9 +248,16 @@ mkNameCompItem doc thingParent origName origMod thingType isInfix docs !imp = CI
247248
where
248249
argTypes = getArgs typ
249250
argText :: T.Text
250-
argText = mconcat $ List.intersperse " " $ zipWithFrom snippet 1 argTypes
251+
argText = mconcat $ List.intersperse " " $ zipWithFrom snippet 1 argTypes
251252
snippet :: Int -> Type -> T.Text
252-
snippet i t = "${" <> T.pack (show i) <> ":" <> showGhc t <> "}"
253+
snippet i t = case t of
254+
(TyVarTy _) -> noParensSnippet
255+
(LitTy _) -> noParensSnippet
256+
(TyConApp _ []) -> noParensSnippet
257+
_ -> snippetText i ("(" <> showGhc t <> ")")
258+
where
259+
noParensSnippet = snippetText i (showGhc t)
260+
snippetText i t = "${" <> T.pack (show i) <> ":" <> t <> "}"
253261
getArgs :: Type -> [Type]
254262
getArgs t
255263
| isPredTy t = []

ghcide/test/exe/Main.hs

+4-4
Original file line numberDiff line numberDiff line change
@@ -3900,7 +3900,7 @@ nonLocalCompletionTests =
39003900
"variable"
39013901
["module A where", "f = hea"]
39023902
(Position 1 7)
3903-
[("head", CiFunction, "head ${1:[a]}", True, True, Nothing)],
3903+
[("head", CiFunction, "head ${1:([a])}", True, True, Nothing)],
39043904
completionTest
39053905
"constructor"
39063906
["module A where", "f = Tru"]
@@ -3912,20 +3912,20 @@ nonLocalCompletionTests =
39123912
"type"
39133913
["{-# OPTIONS_GHC -Wall #-}", "module A () where", "f :: Bo", "f = True"]
39143914
(Position 2 7)
3915-
[ ("Bounded", CiInterface, "Bounded ${1:*}", True, True, Nothing),
3915+
[ ("Bounded", CiInterface, "Bounded ${1:(*)}", True, True, Nothing),
39163916
("Bool", CiStruct, "Bool ", True, True, Nothing)
39173917
],
39183918
completionTest
39193919
"qualified"
39203920
["{-# OPTIONS_GHC -Wunused-binds #-}", "module A () where", "f = Prelude.hea"]
39213921
(Position 2 15)
3922-
[ ("head", CiFunction, "head ${1:[a]}", True, True, Nothing)
3922+
[ ("head", CiFunction, "head ${1:([a])}", True, True, Nothing)
39233923
],
39243924
completionTest
39253925
"duplicate import"
39263926
["module A where", "import Data.List", "import Data.List", "f = perm"]
39273927
(Position 3 8)
3928-
[ ("permutations", CiFunction, "permutations ${1:[a]}", False, False, Nothing)
3928+
[ ("permutations", CiFunction, "permutations ${1:([a])}", False, False, Nothing)
39293929
],
39303930
completionTest
39313931
"dont show hidden items"

test/functional/Completion.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ snippetTests = testGroup "snippets" [
253253
item ^. label @?= "foldl"
254254
item ^. kind @?= Just CiFunction
255255
item ^. insertTextFormat @?= Just Snippet
256-
item ^. insertText @?= Just "foldl ${1:b -> a -> b} ${2:b} ${3:t a}"
256+
item ^. insertText @?= Just "foldl ${1:(b -> a -> b)} ${2:b} ${3:(t a)}"
257257

258258
, testCase "work for complex types" $ runSession hlsCommand fullCaps "test/testdata/completion" $ do
259259
doc <- openDoc "Completion.hs" "haskell"
@@ -267,7 +267,7 @@ snippetTests = testGroup "snippets" [
267267
item ^. label @?= "mapM"
268268
item ^. kind @?= Just CiFunction
269269
item ^. insertTextFormat @?= Just Snippet
270-
item ^. insertText @?= Just "mapM ${1:a -> m b} ${2:t a}"
270+
item ^. insertText @?= Just "mapM ${1:(a -> m b)} ${2:(t a)}"
271271

272272
, testCase "work for infix functions" $ runSession hlsCommand fullCaps "test/testdata/completion" $ do
273273
doc <- openDoc "Completion.hs" "haskell"

0 commit comments

Comments
 (0)