func()

in internal/langserver/handlers/complete_v2.go [13:54]


func (svc *service) HandleComplete(ctx context.Context, params protocol.CompletionParams) ([]protocol.CompletionItem, error) {
	docContent, docFileName, err := parser.GetDocumentContent(ctx, params.TextDocument.URI)
	if err != nil {
		return nil, err
	}

	ctxInfo, diags, err := parser.BuildHCLContext(docContent, docFileName, params.Position)
	if err != nil || (diags != nil && diags.HasErrors()) {
		docContent, fieldName, isNewBlock, err := parser.AttemptReparse(docContent, params.Position.Line)
		if err != nil {
			if isNewBlock {
				return GetTopLevelCompletions(params), nil
			}

			return nil, nil
		}

		ctxInfo, diags, err = parser.BuildHCLContext(docContent, docFileName, params.Position)
		if err != nil || (diags != nil && diags.HasErrors()) {
			if utils.MatchAnyPrefix(fieldName, schema.AzureRMPrefix, schema.ResourcesPrefix) {
				return GetTopLevelCompletions(params), nil
			}

			return nil, nil
		}

		if ctxInfo.Block != nil || ctxInfo.SubBlock != nil || ctxInfo.Attribute != nil {
			return GetAttributeCompletions(ctxInfo.Resource, ctxInfo.ParsedPath+"."+fieldName), nil
		}

		return GetTopLevelCompletions(params), nil
	}

	switch {
	case ctxInfo.Attribute != nil:
		return GetAttributeCompletions(ctxInfo.Resource, ctxInfo.ParsedPath), nil
	case ctxInfo.SubBlock != nil || ctxInfo.Block != nil:
		return GetBlockAttributeCompletions(ctxInfo.Resource, ctxInfo.ParsedPath), nil
	default:
		return GetTopLevelCompletions(params), nil
	}
}