Skip to content

Bracketing for snippet completions #1709

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions ghcide/src/Development/IDE/Plugin/Completions/Logic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import Language.LSP.Types
import Language.LSP.Types.Capabilities
import qualified Language.LSP.VFS as VFS
import Outputable (Outputable)
import TyCoRep

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

Expand Down Expand Up @@ -247,9 +248,16 @@ mkNameCompItem doc thingParent origName origMod thingType isInfix docs !imp = CI
where
argTypes = getArgs typ
argText :: T.Text
argText = mconcat $ List.intersperse " " $ zipWithFrom snippet 1 argTypes
argText = mconcat $ List.intersperse " " $ zipWithFrom snippet 1 argTypes
snippet :: Int -> Type -> T.Text
snippet i t = "${" <> T.pack (show i) <> ":" <> showGhc t <> "}"
snippet i t = case t of
(TyVarTy _) -> noParensSnippet
(LitTy _) -> noParensSnippet
(TyConApp _ []) -> noParensSnippet
_ -> snippetText i ("(" <> showGhc t <> ")")
where
noParensSnippet = snippetText i (showGhc t)
snippetText i t = "${" <> T.pack (show i) <> ":" <> t <> "}"
getArgs :: Type -> [Type]
getArgs t
| isPredTy t = []
Expand Down
8 changes: 4 additions & 4 deletions ghcide/test/exe/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3900,7 +3900,7 @@ nonLocalCompletionTests =
"variable"
["module A where", "f = hea"]
(Position 1 7)
[("head", CiFunction, "head ${1:[a]}", True, True, Nothing)],
[("head", CiFunction, "head ${1:([a])}", True, True, Nothing)],
completionTest
"constructor"
["module A where", "f = Tru"]
Expand All @@ -3912,20 +3912,20 @@ nonLocalCompletionTests =
"type"
["{-# OPTIONS_GHC -Wall #-}", "module A () where", "f :: Bo", "f = True"]
(Position 2 7)
[ ("Bounded", CiInterface, "Bounded ${1:*}", True, True, Nothing),
[ ("Bounded", CiInterface, "Bounded ${1:(*)}", True, True, Nothing),
("Bool", CiStruct, "Bool ", True, True, Nothing)
],
completionTest
"qualified"
["{-# OPTIONS_GHC -Wunused-binds #-}", "module A () where", "f = Prelude.hea"]
(Position 2 15)
[ ("head", CiFunction, "head ${1:[a]}", True, True, Nothing)
[ ("head", CiFunction, "head ${1:([a])}", True, True, Nothing)
],
completionTest
"duplicate import"
["module A where", "import Data.List", "import Data.List", "f = perm"]
(Position 3 8)
[ ("permutations", CiFunction, "permutations ${1:[a]}", False, False, Nothing)
[ ("permutations", CiFunction, "permutations ${1:([a])}", False, False, Nothing)
],
completionTest
"dont show hidden items"
Expand Down
4 changes: 2 additions & 2 deletions test/functional/Completion.hs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ snippetTests = testGroup "snippets" [
item ^. label @?= "foldl"
item ^. kind @?= Just CiFunction
item ^. insertTextFormat @?= Just Snippet
item ^. insertText @?= Just "foldl ${1:b -> a -> b} ${2:b} ${3:t a}"
item ^. insertText @?= Just "foldl ${1:(b -> a -> b)} ${2:b} ${3:(t a)}"

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

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