async provideInlineCompletionItems()

in src/common/language_server/language_client_middleware.ts [54:109]


  async provideInlineCompletionItems(
    document: vscode.TextDocument,
    position: vscode.Position,
    context: vscode.InlineCompletionContext,
    token: vscode.CancellationToken,
    next: (
      document: vscode.TextDocument,
      position: vscode.Position,
      context: vscode.InlineCompletionContext,
      token: vscode.CancellationToken,
    ) => vscode.ProviderResult<vscode.InlineCompletionItem[] | vscode.InlineCompletionList>,
  ) {
    if (!this.#stateManager.isActive()) {
      return [];
    }

    try {
      this.#stateManager.setLoading(true);

      // Short circuit after both cancellation and time have passed
      const shortCircuit = waitForCancellationToken(token)
        .then(() => waitForMs(CANCELLATION_DELAY))
        .then(() => []);

      const response = await Promise.race([
        shortCircuit,
        next(document, position, serializeInlineCompletionContext(context), token),
      ]);

      let uniqueTrackingId;

      if (isInlineCompletionList(response)) {
        const { command, arguments: args } = response?.items?.[0]?.command ?? {};

        if (command === SUGGESTION_ACCEPTED_COMMAND) {
          const [trackingId] = args ?? [];
          uniqueTrackingId = trackingId;
        }

        if (command === START_STREAMING_COMMAND) {
          const [streamId, trackingId] = args ?? [];
          uniqueTrackingId = trackingId;
          return await this.#listenToIncomingStream(position, token, streamId, uniqueTrackingId);
        }
      }

      this.#trackSuggestionShown(uniqueTrackingId);

      return response;
    } catch (e) {
      log.error(e);
      return [];
    } finally {
      this.#stateManager.setLoading(false);
    }
  }