@@ -92,12 +92,20 @@ newtype Snippet = Snippet [SnippetAny]
9292instance IsString Snippet where
9393 fromString = snippetText . T. pack
9494
95+ -- | @SnippetAny@ can be used to construct sanitized snippets. See the LSP
96+ -- spec for more details.
9597data SnippetAny
9698 = SText Text
97- | STabStop Int
98- | SPlaceholder Int SnippetAny
99+ -- ^ Literal text
100+ | STabStop Int (Maybe SnippetAny )
101+ -- ^ Creates a tab stop, i.e. parts of the snippet that are meant to be
102+ -- filled in by the user and that can be jumped between using the tab key.
103+ -- The optional field can be used to provide a placeholder value.
99104 | SChoice Int (NonEmpty Text )
105+ -- ^ Presents a choice between the provided values to the user
100106 | SVariable Text (Maybe SnippetAny )
107+ -- ^ Snippet variable. See the spec for possible values. The optional field
108+ -- can be used to provide a default value for when the variable is not set.
101109 deriving (Eq , Show )
102110
103111snippetText :: Text -> Snippet
@@ -114,10 +122,9 @@ snippetToText (Snippet l) = foldMap (snippetAnyToText False) l
114122 where
115123 snippetAnyToText isNested = \ case
116124 SText t -> sanitizeText isNested t
117- STabStop i -> " ${" <> T. pack (show i) <> " }"
118- SPlaceholder i s -> " ${" <> T. pack (show i) <> " :" <> snippetAnyToText True s <> " }"
125+ STabStop i ph -> " ${" <> T. pack (show i) <> foldMap (\ p -> " :" <> snippetAnyToText True p) ph <> " }"
119126 SChoice i (c :| cs) -> " ${" <> T. pack (show i) <> " |" <> c <> foldMap (" ," <> ) cs <> " }"
120- SVariable n md -> " ${" <> n <> maybe mempty (\ x -> " :" <> snippetAnyToText True x) md <> " }"
127+ SVariable n md -> " ${" <> n <> foldMap (\ x -> " :" <> snippetAnyToText True x) md <> " }"
121128 sanitizeText isNested = T. foldl' (sanitizeChar isNested) mempty
122129 sanitizeChar isNested t = (t <> ) . \ case
123130 ' $' -> " \\ $"
0 commit comments