MCPcopy
hub / github.com/hashicorp/hcl / spaceAfterToken

Function spaceAfterToken

hclwrite/format.go:226–344  ·  view source on GitHub ↗

spaceAfterToken decides whether a particular subject token should have a space after it when surrounded by the given before and after tokens. "before" can be TokenNil, if the subject token is at the start of a sequence.

(subject, before, after *Token)

Source from the content-addressed store, hash-verified

224// space after it when surrounded by the given before and after tokens.
225// "before" can be TokenNil, if the subject token is at the start of a sequence.
226func spaceAfterToken(subject, before, after *Token) bool {
227 switch {
228
229 case after.Type == hclsyntax.TokenNewline || after.Type == hclsyntax.TokenNil:
230 // Never add spaces before a newline
231 return false
232
233 case subject.Type == hclsyntax.TokenIdent && after.Type == hclsyntax.TokenOParen:
234 // Don't split a function name from open paren in a call
235 return false
236
237 case (subject.Type == hclsyntax.TokenIdent && after.Type == hclsyntax.TokenDoubleColon) ||
238 (subject.Type == hclsyntax.TokenDoubleColon && after.Type == hclsyntax.TokenIdent):
239 // Don't split namespace segments in a function call
240 return false
241
242 case subject.Type == hclsyntax.TokenDot || after.Type == hclsyntax.TokenDot:
243 // Don't use spaces around attribute access dots
244 return false
245
246 case after.Type == hclsyntax.TokenComma || after.Type == hclsyntax.TokenEllipsis:
247 // No space right before a comma or ... in an argument list
248 return false
249
250 case subject.Type == hclsyntax.TokenComma:
251 // Always a space after a comma
252 return true
253
254 case subject.Type == hclsyntax.TokenQuotedLit || subject.Type == hclsyntax.TokenStringLit || subject.Type == hclsyntax.TokenOQuote || subject.Type == hclsyntax.TokenOHeredoc || after.Type == hclsyntax.TokenQuotedLit || after.Type == hclsyntax.TokenStringLit || after.Type == hclsyntax.TokenCQuote || after.Type == hclsyntax.TokenCHeredoc:
255 // No extra spaces within templates
256 return false
257
258 case hclsyntax.Keyword([]byte{'i', 'n'}).TokenMatches(subject.asHCLSyntax()) && before.Type == hclsyntax.TokenIdent:
259 // This is a special case for inside for expressions where a user
260 // might want to use a literal tuple constructor:
261 // [for x in [foo]: x]
262 // ... in that case, we would normally produce in[foo] thinking that
263 // in is a reference, but we'll recognize it as a keyword here instead
264 // to make the result less confusing.
265 return true
266
267 case after.Type == hclsyntax.TokenOBrack && (subject.Type == hclsyntax.TokenIdent || subject.Type == hclsyntax.TokenNumberLit || tokenBracketChange(subject) < 0):
268 return false
269
270 case subject.Type == hclsyntax.TokenBang:
271 // No space after a bang
272 return false
273
274 case subject.Type == hclsyntax.TokenMinus:
275 // Since a minus can either be subtraction or negation, and the latter
276 // should _not_ have a space after it, we need to use some heuristics
277 // to decide which case this is.
278 // We guess that we have a negation if the token before doesn't look
279 // like it could be the end of an expression.
280
281 switch before.Type {
282
283 case hclsyntax.TokenNil:

Callers 1

formatSpacesFunction · 0.85

Calls 4

KeywordTypeAlias · 0.92
tokenBracketChangeFunction · 0.85
TokenMatchesMethod · 0.80
asHCLSyntaxMethod · 0.80

Tested by

no test coverage detected