func()

in src/ulsp/controller/quick-actions/quick_actions.go [300:351]


func (c *controller) refreshAvailableCodeActions(ctx context.Context, documentIdentifier protocol.TextDocumentIdentifier) error {
	s, err := c.sessions.GetFromContext(ctx)
	if err != nil {
		return fmt.Errorf("getting session for code action: %w", err)
	}

	c.actionsMu.Lock()
	defer c.actionsMu.Unlock()

	doc, err := c.documents.GetTextDocument(ctx, documentIdentifier)
	if err != nil {
		var notFoundErr *ulsperrors.DocumentNotFoundError
		if errors.As(err, &notFoundErr) {
			return nil
		}
		return err
	}

	// If the last processed version is current, no need to refresh.
	if !c.currentActionRanges.SetVersion(s.UUID, documentIdentifier, doc.Version) {
		return nil
	}

	// If the document version is new, delete the old actions and regenerate them.
	c.currentActionRanges.DeleteExistingDocumentRanges(s.UUID, documentIdentifier)
	for _, currentAction := range c.enabledActions[s.UUID] {
		isRelevantDoc := currentAction.IsRelevantDocument(s, doc)
		if !isRelevantDoc {
			continue
		}

		actionMatches, err := currentAction.ProcessDocument(ctx, doc)
		if err != nil {
			return fmt.Errorf("processing document for code action: %w", err)
		}

		for _, result := range actionMatches {
			if currentCodeAction, ok := result.(mapper.CodeActionWithRange); ok {
				if _, ok := action.SupportedCodeActionKinds[currentCodeAction.CodeAction.Kind]; !ok {
					return fmt.Errorf("action returned unsupported kind: %s", currentCodeAction.CodeAction.Kind)
				}
				c.currentActionRanges.AddCodeAction(s.UUID, documentIdentifier, currentCodeAction.Range, currentCodeAction.CodeAction)
			} else if currentCodeLens, ok := result.(protocol.CodeLens); ok {
				c.currentActionRanges.AddCodeLens(s.UUID, documentIdentifier, currentCodeLens)
			} else {
				return fmt.Errorf("action returned invalid type: %T", result)
			}
		}
	}

	return nil
}