All files / src/commands suggestion-commands.ts

100% Statements 85/85
92.85% Branches 13/14
100% Functions 1/1
100% Lines 85/85

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 861x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 1x 1x 3x 4x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 4x 1x 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x  
import * as vscode from "vscode";
import { logSuggestionEvent } from "../services/log-service";
import {
  SuggestionChoice,
  currentChoices,
  suggestionContext,
  isSuspended,
  resetSuggestionContext,
  handleBuggedSuggestionReview,
  handleIncorrectSuggestionSelection,
} from "../services/suggestion-service";
 
/**
 * Registers suggestion-related commands and returns their disposables.
 *
 * Commits the inline suggestion into the editor, logs the acceptance event,
 * and handles review if the suggestion contains a known bug.
 */
export function registerSuggestionCommands(): vscode.Disposable[] {
  const accept = vscode.commands.registerCommand(
    "clover.acceptInlineSuggestion",
    async () => {
      const editor = vscode.window.activeTextEditor;
      if (!editor) {
        return;
      }
 
      if (isSuspended) {
        vscode.window.showInformationMessage(
          "Please hover over the suggestions and select the correct one manually",
          { modal: false }
        );
        return;
      }
 
      await vscode.commands.executeCommand(
        "editor.action.inlineSuggest.commit"
      );
      logSuggestionEvent(true, suggestionContext);
 
      if (suggestionContext.hasBug) {
        await handleBuggedSuggestionReview(
          suggestionContext.prompt,
          suggestionContext.suggestions,
          suggestionContext
        );
      }
 
      resetSuggestionContext();
    }
  );
 
  const reject = vscode.commands.registerCommand(
    "clover.rejectInlineSuggestion",
    async () => {
      const editor = vscode.window.activeTextEditor;
      if (!editor) {
        return;
      }
 
      await vscode.commands.executeCommand("editor.action.inlineSuggest.hide");
      await vscode.commands.executeCommand("hideSuggestWidget");
 
      logSuggestionEvent(false, suggestionContext);
      resetSuggestionContext();
    }
  );
 
  const track = vscode.commands.registerCommand(
    "clover.suggestionSelected",
    async (choice: SuggestionChoice) => {
      logSuggestionEvent(choice.isCorrect, suggestionContext);
 
      if (!choice.isCorrect) {
        const rightCode = currentChoices.find((c) => c.isCorrect)?.text || "";
        await handleIncorrectSuggestionSelection(
          suggestionContext.prompt,
          choice.text,
          rightCode
        );
      }
    }
  );
  return [accept, reject, track];
}