1

I have the following SourceGraph structured search: repo:… file:… "tls_certs" {...default = {...}...} which correctly matches:

variable "tls_certs" {
  description = "…"
  type        = map(string)
  default = {
    …
  }
}

It's currently highlighting the entire "tls_certs" block. I would like it to highlight only the default = block. Assuming that's possible, how would that be done?

Noel Yap
  • 18,822
  • 21
  • 92
  • 144

1 Answers1

1

(I'm assuming you want to scope your search to Terraform files based on the example match provided)

Try this and see if it works for you: :[~[\s\n]]default = {...} lang:Terraform

It'll match a block of the form default = {...} that's preceded by whitespace or a newline. It's not strictly guaranteed to only match nested structures, but it seems to work well with the lang:Terraform filter.

It uses both the ... and the :[~regexp] syntax of structural search. (Syntax reference docs: https://docs.sourcegraph.com/code_search/reference/structural#syntax-reference)

Example: https://sourcegraph.com/search?q=context:global+:%5B~%5B%5Cs%5Cn%5D%5Ddefault+%3D+%7B...%7D+lang:Terraform+-repo:%5Egithub%5C.com/Wilfred/difftastic$&patternType=structural

  • The question I provided already matches fine. Currently, SourceGraph is highlighting the entire `tls_certs` block. I want it to highlight only the `default` block without matching `default` blocks not in a `tls_certs` block. – Noel Yap Oct 22 '22 at 17:09
  • If I replace the `...default` in my query with `:[~[\s\n]]default`, SourceGraph responds with `No results matched your query`. – Noel Yap Oct 22 '22 at 18:40
  • 2
    I see -- unfortunately it's not possible to highlight only a submatch of an overall pattern in the way you're describing (i.e. only matching `tls_certs` blocks, _and then_ only highlighting the `default` block within those blocks) – CeramicBezel Oct 24 '22 at 20:21