Repository: TabbyML/tabby
File: clients/vscode/src/InlineCompletionProvider.ts (TypeScript)

Code before:
        break;
      case "dismiss":
        eventData = { type: "dismiss", elapsed };
        break;
      case "accept_word":
        // select_kind should be "word" but not supported by Tabby Server yet, use "line" instead
        eventData = { type: "select", selectKind: "line", elapsed };
        break;
      case "accept_line":
        eventData = { type: "select", selectKind: "line", elapsed };
        break;
      default:
        // unknown event type, should be unreachable
        return;
    }
    const params: EventParams = {
      ...eventData,
      eventId,
      viewId: id,
    };
    // await not required
    this.client.telemetry.postEvent(params);
  }

  /**
   * Get current display range with the current completion item
   * @return {Range | undefined} - The range with the current completion item
   */
  public getCurrentDisplayRange(): Range | undefined {
    const item = this.displayedCompletion?.completions.items[this.displayedCompletion.index];
    const range = item?.range;
    if (!range) {
      return undefined;
    }
    if (!item) {
      return undefined;
    }
    const length = (item.insertText as string).split("\n").length - 1; //remove current line count;
    const completionEnd = range.end.line + length;
    const completionRange = new Range(
      new Position(range.start.line, range.start.character),
      new Position(completionEnd == 0 ? completionEnd : completionEnd + 1, 0),
    );
    this.logger.info("current range with displayed completion item:", completionRange);

    return completionRange;
  }
}


Review comment (suggestion):
```suggestion
   * Calculate the edited range in the modified document as if the current completion item has been accepted.
```

Code after:
        break;
      case "dismiss":
        eventData = { type: "dismiss", elapsed };
        break;
      case "accept_word":
        // select_kind should be "word" but not supported by Tabby Server yet, use "line" instead
        eventData = { type: "select", selectKind: "line", elapsed };
        break;
      case "accept_line":
        eventData = { type: "select", selectKind: "line", elapsed };
        break;
      default:
        // unknown event type, should be unreachable
        return;
    }
    const params: EventParams = {
      ...eventData,
      eventId,
      viewId: id,
    };
    // await not required
    this.client.telemetry.postEvent(params);
  }

  /**
   * Calculate the edited range in the modified document as if the current completion item has been accepted.
   * @return {Range | undefined} - The range with the current completion item
   */
  public calcEditedRangeAfterAccept(): Range | undefined {
    const item = this.displayedCompletion?.completions.items[this.displayedCompletion.index];
    const range = item?.range;
    if (!range) {
      // FIXME: If the item has a null range, we can use current position and text length to calculate the result range
      return undefined;
    }
    if (!item) {
      return undefined;
    }
    const length = (item.insertText as string).split("\n").length - 1; //remove current line count;
    const completionRange = new Range(
      new Position(range.start.line, range.start.character),
      new Position(range.end.line + length + 1, 0),
    );
    this.logger.debug("Calculate edited range for displayed completion item:", completionRange);
    return completionRange;
  }
}