View Source cure_lsp_code_actions (cure v0.7.0)

LSP Code Actions & Quick Fixes

This module provides automated fixes and suggestions for constraint violations:

Quick Fix Categories

1. Add Runtime Checks

  • Insert guard clauses: when x > 0
  • Add assertions: assert x > 0
  • Add conditional checks with error handling

2. Relax Constraints

  • Suggest weaker constraints that would pass
  • Example: x > 0x >= 0
  • Offer alternative type signatures

3. Add Type Annotations

  • Suggest refinement types based on usage
  • Auto-infer constraints from guards
  • Propagate constraints from callers

4. Pattern Completion

  • Add missing pattern match cases
  • Generate exhaustive match arms

Example Quick Fixes

Add Guard:

% Before
def divide(a: Int, b: Int): Int = a / b

% After
def divide(a: Int, b: Int) when b /= 0: Int = a / b

Relax Constraint:

% Error: Cannot prove Percentage <: Positive
type Percentage = Int when x >= 0 and x <= 100

% Suggested: Use NonNegative instead
type NonNegative = Int when x >= 0
def use_percentage(p: NonNegative): Int = ...

Add Runtime Check:

def process(n: Int): Result =
    if n > 0 then
        divide(100, n)
    else
        error(\"n must be positive\")
    end

Summary

Functions

apply_code_action/2

-spec apply_code_action(map(), binary()) -> binary().

create_text_edit(Range, NewText, Description)

-spec create_text_edit(term(), binary(), binary()) -> map().

generate_code_actions(Uri, Diagnostic, AST)

-spec generate_code_actions(binary(), map(), term()) -> [map()].

generate_quick_fixes(Diagnostic, AST)

-spec generate_quick_fixes(map(), term()) -> [map()].

suggest_assertion(Range, Constraint)

-spec suggest_assertion(term(), term()) -> map().

suggest_guard_clause(Range, Constraint)

-spec suggest_guard_clause(term(), term()) -> map().

suggest_pattern_completion(Range, MissingPattern)

-spec suggest_pattern_completion(term(), term()) -> map().

suggest_relaxed_constraint(Range, Constraint)

-spec suggest_relaxed_constraint(term(), term()) -> map().

suggest_type_annotation(Range, Type)

-spec suggest_type_annotation(term(), term()) -> map().