# editor: Fix completion accept for optional chaining in Typescript \(\#31878\) · gitcafe/zed

[View on GitCafe](https://git.cafe/gitcafe/zed/commit/06a199da4d8a147c7867f4c82ffafed4974bea24)

Repository: [gitcafe/zed](https://git.cafe/gitcafe/zed)

Visibility: public

Requested revision: 06a199da4d8a147c7867f4c82ffafed4974bea24

Requested commit: 06a199da4d8a147c7867f4c82ffafed4974bea24

Commit: 06a199da4d8a147c7867f4c82ffafed4974bea24

Tree: 1fd2243f04aa18c085c29509bfb57051f1c3d721

Author: Smit Barmase

Committer: GitHub

## Message

````
editor: Fix completion accept for optional chaining in Typescript (#31878)

Closes #31662

Currently, we assume `insert_range` will always end at the cursor and
`replace_range` will also always end after the cursor for calculating
range to replace. This is a particular case for the rust-analyzer, but
not widely true for other language servers.

This PR fixes this assumption, and now `insert_range` and
`replace_range` both can end before cursor.

In this particular case:
```ts
let x: string | undefined;

x.tostˇ // here insert as well as replace range is just "." while new_text is "?.toString()"
```

This change makes it such that if final range to replace ends before
cursor, we extend it till the cursor.

Bonus:
- Improves suffix and subsequence matching to use `label` over
`new_text` as `new_text` can contain end characters like `()` or `$`
which is not visible while accepting the completion.
- Make suffix and subsequence check case insensitive.
- Fixes broken subsequence matching which was not considering the order
of characters while matching subsequence.

Release Notes:

- Fixed an issue where autocompleting optional chaining methods in
TypeScript, such as `x.tostr`, would result in `x?.toString()tostr`
instead of `x?.toString()`.
````

## Parents

- [ab6125ddde518c0c021ecae54f64d86b8c4f5008](https://git.cafe/gitcafe/zed/commit/ab6125ddde518c0c021ecae54f64d86b8c4f5008?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zed/tree/06a199da4d8a147c7867f4c82ffafed4974bea24?format=markdown)
