ProvideCodeActions returns code actions for the given range and context
(ctx context.Context, params *lsproto.CodeActionParams)
| 76 | |
| 77 | // ProvideCodeActions returns code actions for the given range and context |
| 78 | func (l *LanguageService) ProvideCodeActions(ctx context.Context, params *lsproto.CodeActionParams) (lsproto.CodeActionResponse, error) { |
| 79 | program, file := l.getProgramAndFile(params.TextDocument.Uri) |
| 80 | |
| 81 | var actions []lsproto.CommandOrCodeAction |
| 82 | |
| 83 | if params.Context != nil && params.Context.Only != nil { |
| 84 | for _, kind := range *params.Context.Only { |
| 85 | matchingKinds := getOrganizeImportsActionsForKind(kind) |
| 86 | for _, matchingKind := range matchingKinds { |
| 87 | organizeAction := l.createOrganizeImportsAction(ctx, program, file, matchingKind) |
| 88 | actions = append(actions, *organizeAction) |
| 89 | } |
| 90 | |
| 91 | if isFixAllKind(kind) { |
| 92 | fixAllAction, err := l.createFixAllAction(ctx, program, file, params.TextDocument.Uri) |
| 93 | if err != nil { |
| 94 | return lsproto.CodeActionResponse{}, err |
| 95 | } |
| 96 | if fixAllAction != nil { |
| 97 | actions = append(actions, *fixAllAction) |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | if params.Context != nil && params.Context.Diagnostics != nil && wantsQuickFixes(params.Context.Only) { |
| 104 | fixIdSeen := make(map[string]*CodeFixProvider) |
| 105 | |
| 106 | var seen []*CodeAction // sorted for binary search dedup, dedup across all diagnostics and providers so if multiple diags produce the same codefix, only one is returned |
| 107 | |
| 108 | for _, diag := range params.Context.Diagnostics { |
| 109 | if diag.Code == nil || diag.Code.Integer == nil { |
| 110 | continue |
| 111 | } |
| 112 | |
| 113 | errorCode := *diag.Code.Integer |
| 114 | |
| 115 | for _, provider := range codeFixProviders { |
| 116 | if !containsErrorCode(provider.ErrorCodes, errorCode) { |
| 117 | continue |
| 118 | } |
| 119 | |
| 120 | position := l.converters.LineAndCharacterToPosition(file, diag.Range.Start) |
| 121 | endPosition := l.converters.LineAndCharacterToPosition(file, diag.Range.End) |
| 122 | fixContext := &CodeFixContext{ |
| 123 | SourceFile: file, |
| 124 | Span: core.NewTextRange(int(position), int(endPosition)), |
| 125 | ErrorCode: errorCode, |
| 126 | Program: program, |
| 127 | LS: l, |
| 128 | Diagnostic: diag, |
| 129 | Params: params, |
| 130 | } |
| 131 | |
| 132 | providerActions, err := provider.GetCodeActions(ctx, fixContext) |
| 133 | if err != nil { |
| 134 | return lsproto.CodeActionResponse{}, err |
| 135 | } |
no test coverage detected