MCPcopy Create free account
hub / github.com/microsoft/typescript-go / ProvideCodeActions

Method ProvideCodeActions

internal/ls/codeactions.go:78–158  ·  view source on GitHub ↗

ProvideCodeActions returns code actions for the given range and context

(ctx context.Context, params *lsproto.CodeActionParams)

Source from the content-addressed store, hash-verified

76
77// ProvideCodeActions returns code actions for the given range and context
78func (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 }

Callers 1

handleCodeActionMethod · 0.80

Calls 15

getProgramAndFileMethod · 0.95
createFixAllActionMethod · 0.95
getFixAllQuickFixesMethod · 0.95
NewTextRangeFunction · 0.92
isFixAllKindFunction · 0.85
wantsQuickFixesFunction · 0.85
containsErrorCodeFunction · 0.85
convertToLSPCodeActionFunction · 0.85
InsertMethod · 0.80

Tested by

no test coverage detected