summaryrefslogtreecommitdiff
path: root/extension/src/lang-server/codeActions.ts
blob: 892c69beff0dba01d4a1c6df557279ff6dc82a30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import * as vscode from "vscode";

class ContinueQuickFixProvider implements vscode.CodeActionProvider {
  public static readonly providedCodeActionKinds = [
    vscode.CodeActionKind.QuickFix,
  ];

  provideCodeActions(
    document: vscode.TextDocument,
    range: vscode.Range | vscode.Selection,
    context: vscode.CodeActionContext,
    token: vscode.CancellationToken
  ): vscode.ProviderResult<(vscode.Command | vscode.CodeAction)[]> {
    if (context.diagnostics.length === 0) {
      return [];
    }

    const createQuickFix = (edit: boolean) => {
      const diagnostic = context.diagnostics[0];
      const quickFix = new vscode.CodeAction(
        edit ? "Fix with Continue" : "Ask Continue",
        vscode.CodeActionKind.QuickFix
      );
      quickFix.isPreferred = false;
      const surroundingRange = new vscode.Range(
        Math.max(0, range.start.line - 3),
        0,
        Math.min(document.lineCount, range.end.line + 3),
        0
      );
      quickFix.command = {
        command: "continue.quickFix",
        title: "Continue Quick Fix",
        arguments: [
          diagnostic.message,
          document.getText(surroundingRange),
          edit,
        ],
      };
      return quickFix;
    };
    return [
      // createQuickFix(true),
      createQuickFix(false),
    ];
  }
}

export default function registerQuickFixProvider() {
  // In your extension's activate function:
  vscode.languages.registerCodeActionsProvider(
    { language: "*" },
    new ContinueQuickFixProvider(),
    {
      providedCodeActionKinds: ContinueQuickFixProvider.providedCodeActionKinds,
    }
  );
}