func keyCandidates()

in internal/langserver/handlers/tfschema/body_candidates.go [169:220]


func keyCandidates(props []schema.Property, r lsp.Range, parentNode *parser.HclNode) []lsp.CompletionItem {
	candidates := make([]lsp.CompletionItem, 0)
	propSet := make(map[string]bool)
	for _, prop := range props {
		if propSet[prop.Name] {
			continue
		}
		propSet[prop.Name] = true
		content := prop.Name
		newText := ""
		sortText := fmt.Sprintf("1%s", content)
		if prop.Modifier == schema.Required {
			sortText = fmt.Sprintf("0%s", content)
		}

		keyPart := fmt.Sprintf(`%s =`, content)
		if parentNode.KeyValueFormat == parser.QuotedKeyEqualValue {
			keyPart = fmt.Sprintf(`"%s" =`, content)
		} else if parentNode.KeyValueFormat == parser.QuotedKeyColonValue {
			keyPart = fmt.Sprintf(`"%s":`, content)
		}

		switch prop.Type {
		case "string":
			newText = fmt.Sprintf(`%s "$0"`, keyPart)
		case "array":
			newText = fmt.Sprintf(`%s [$0]`, keyPart)
		case "object":
			newText = fmt.Sprintf("%s {\n\t$0\n}", keyPart)
		default:
			newText = fmt.Sprintf(`%s $0`, keyPart)
		}
		candidates = append(candidates, lsp.CompletionItem{
			Label:  content,
			Kind:   lsp.PropertyCompletion,
			Detail: fmt.Sprintf("%s (%s)", prop.Name, prop.Modifier),
			Documentation: lsp.MarkupContent{
				Kind:  "markdown",
				Value: fmt.Sprintf("Type: `%s`  \n%s\n", prop.Type, prop.Description),
			},
			SortText:         sortText,
			InsertTextFormat: lsp.SnippetTextFormat,
			InsertTextMode:   lsp.AdjustIndentation,
			TextEdit: &lsp.TextEdit{
				Range:   r,
				NewText: newText,
			},
			Command: constTriggerSuggestCommand(),
		})
	}
	return candidates
}