func requiredPropertiesCandidates()

in internal/langserver/handlers/tfschema/body_candidates.go [222:294]


func requiredPropertiesCandidates(propertySets []schema.PropertySet, r lsp.Range, parentNode *parser.HclNode) []lsp.CompletionItem {
	candidates := make([]lsp.CompletionItem, 0)
	for _, ps := range propertySets {
		if len(ps.Properties) == 0 {
			continue
		}
		props := make([]schema.Property, 0)
		for _, prop := range ps.Properties {
			props = append(props, prop)
		}
		for range props {
			for i := 0; i < len(props)-1; i++ {
				if props[i].Name > props[i+1].Name {
					props[i], props[i+1] = props[i+1], props[i]
				}
			}
		}
		newText := ""
		index := 1
		for _, prop := range props {
			keyPart := fmt.Sprintf(`%s =`, prop.Name)
			if parentNode.KeyValueFormat == parser.QuotedKeyEqualValue {
				keyPart = fmt.Sprintf(`"%s" =`, prop.Name)
			} else if parentNode.KeyValueFormat == parser.QuotedKeyColonValue {
				keyPart = fmt.Sprintf(`"%s":`, prop.Name)
			}

			if len(prop.Value) != 0 {
				newText += fmt.Sprintf("%s \"%s\"\n", keyPart, prop.Value)
			} else {
				switch prop.Type {
				case "string":
					newText += fmt.Sprintf(`%s "$%d"`, keyPart, index)
				case "array":
					newText += fmt.Sprintf(`%s [$%d]`, keyPart, index)
				case "object":
					newText += fmt.Sprintf("%s {\n\t$%d\n}", keyPart, index)
				default:
					newText += fmt.Sprintf(`%s $%d`, keyPart, index)
				}
				newText += "\n"
				index++
			}
		}

		label := "required-properties"
		if len(ps.Name) != 0 {
			label = fmt.Sprintf("required-properties-%s", ps.Name)
		}
		detail := "Required properties"
		if len(ps.Name) != 0 {
			detail = fmt.Sprintf("Required properties - %s", ps.Name)
		}
		candidates = append(candidates, lsp.CompletionItem{
			Label:  label,
			Kind:   lsp.SnippetCompletion,
			Detail: detail,
			Documentation: lsp.MarkupContent{
				Kind:  "markdown",
				Value: fmt.Sprintf("Type: `%s`  \n```\n%s\n```\n", ps.Name, newText),
			},
			SortText:         "0",
			InsertTextFormat: lsp.SnippetTextFormat,
			InsertTextMode:   lsp.AdjustIndentation,
			TextEdit: &lsp.TextEdit{
				Range:   r,
				NewText: newText,
			},
			Command: constTriggerSuggestCommand(),
		})
	}
	return candidates
}