How to highlight nested structure in SourceGraph structured search?

75 views Asked by At

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?

1

There are 1 answers

3
CeramicBezel On

(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