# language: Further optimize \`language_for_file\` \(\#28694\) · gitcafe/zed

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

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

Visibility: public

Requested revision: 12b012eab375f97da9d05625c9a879b6d901ab8c

Requested commit: 12b012eab375f97da9d05625c9a879b6d901ab8c

Commit: 12b012eab375f97da9d05625c9a879b6d901ab8c

Tree: 1c97c3bc6f20e3ac10556b61368487c5ba7e073e

Author: Finn Evers

Committer: GitHub

## Message

```
language: Further optimize `language_for_file` (#28694)

Follow-up to #28671

This primarily follows two ideas:
1. We currently take the element with the highest score which appears
last in the iterator (see
[`last_by_key`](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.max_by_key)),
so we can also just reverse the iterator and take the first highest
match instead.
2. Once we have a match with a given precedence, we are not interested
in any matches with a lower or even the same priority, given what was
established in 1. Thus, we also only have to check whether any language
checked afterwards has a higher priority match.

Furthermore, once we have a match with the highest possible precedence,
there is no need to look for any more possible matches. Thus, this PR
also adds short-circuiting for that scenario.

Lastly, I also cleaned-up the custom suffix match (an empty glob-set
will never match so no need to iterate there) as well reorder the
zip-call in the content matches, as we never need the content if there
is no first line pattern present for the checked languages.

Release Notes:

- N/A
```

## Parents

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

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