func CodeSampleCandidates()

in internal/langserver/handlers/snippets/snippets.go [50:120]


func CodeSampleCandidates(block *hclsyntax.Block, editRange lsp.Range) []lsp.CompletionItem {
	if block == nil || block.Type == "data" {
		return nil
	}

	if len(block.Labels) != 2 || block.Labels[0] != "azapi_resource" {
		return nil
	}

	typeValue := ""
	for _, attr := range block.Body.Attributes {
		if attr.Name == "type" {
			v, diags := attr.Expr.Value(nil)
			if diags.HasErrors() {
				log.Printf("[ERROR] %s", diags)
				return nil
			}
			typeValue = v.AsString()
			break
		}
	}

	azureResourceType := parseResourceType(typeValue)
	if snippet, ok := snippetMap[strings.ToLower(azureResourceType)]; ok {
		newText := ""
		for _, field := range snippet.Fields {
			if _, ok := block.Body.Attributes[field.Name]; ok {
				continue
			}
			newText += field.Value + "\n"
		}
		if newText == "" {
			return nil
		}

		event := lsp.TelemetryEvent{
			Version: lsp.TelemetryFormatVersion,
			Name:    "textDocument/completion",
			Properties: map[string]interface{}{
				"kind": "code-sample",
				"type": typeValue,
			},
		}
		data, _ := json.Marshal(event)
		return []lsp.CompletionItem{
			{
				Label:  "code sample",
				Kind:   lsp.SnippetCompletion,
				Detail: "Code Sample",
				Documentation: lsp.MarkupContent{
					Kind:  "markdown",
					Value: fmt.Sprintf("```\n%s\n```\n", newText),
				},
				SortText:         "0",
				InsertTextFormat: lsp.SnippetTextFormat,
				InsertTextMode:   lsp.AdjustIndentation,
				TextEdit: &lsp.TextEdit{
					Range:   editRange,
					NewText: newText,
				},
				Command: &lsp.Command{
					Title:     "",
					Command:   "azapi.telemetry",
					Arguments: []json.RawMessage{data},
				},
			},
		}
	}

	return nil
}