# About the selection range when double-clicking. (Intl.Segmenter)

**URL:** https://discuss.codemirror.net/t/about-the-selection-range-when-double-clicking-intl-segmenter/4241
**Category:** v6
**Created:** [April 7, 2022, 2:48pm UTC](https://discuss.codemirror.net/t/about-the-selection-range-when-double-clicking-intl-segmenter/4241 "2022-04-07T14:48:12Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![arayutw](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/arayutw/32/1425_2.png) [@arayutw](https://discuss.codemirror.net/u/arayutw)
#### Post date: [April 7, 2022, 2:48pm UTC](https://discuss.codemirror.net/t/about-the-selection-range-when-double-clicking-intl-segmenter/4241/1 "2022-04-07T14:48:12Z")

</div>

About the selection range when double-clicking.  
Not surprisingly, within CJK, the current specifications will give unnatural results.  
Therefore, can use the function of `Intl.Segmenter` to properly extract the selection range.  
Currently, I have changed the code as follows and am satisfied with it.

```auto
function groupAt(state, pos, bias = 1) {
        let line = state.doc.lineAt(pos), linePos = pos - line.from;
        if (line.length == 0)
            return EditorSelection.cursor(pos);

        var from, to;

        if ("Intl" in self && "function" === typeof Intl["Segmenter"]) {
            // "山は綺麗だ。" => "山|は|綺麗|だ|。"
            var segmenter = new Intl["Segmenter"]("ja", { "granularity": "word" });
            var segments = segmenter.segment(line["text"]);
            var matches = segments.containing(linePos);
            from = matches["index"];
            to = from + matches["segment"]["length"];
        } else {
            let categorize = state.charCategorizer(pos);

            if (linePos == 0)
                bias = 1;
            else if (linePos == line.length)
                bias = -1;
                from = to = linePos;
            if (bias < 0)
                from = findClusterBreak(line.text, linePos, false);
            else
                to = findClusterBreak(line.text, linePos);
            let cat = categorize(line.text.slice(from, to));
            while (from > 0) {
                let prev = findClusterBreak(line.text, from, false);
                if (categorize(line.text.slice(prev, from)) != cat)
                    break;
                from = prev;
            }
            while (to < line.length) {
                let next = findClusterBreak(line.text, to);
                if (categorize(line.text.slice(to, next)) != cat)
                    break;
                to = next;
            }
        }

        return EditorSelection.range(from + line.from, to + line.from);
    }

```

Because it is a part that can not be changed using plug-ins etc.

- Whether to use Intl.Segmenter?
- Which language code should you use? (`ja` etc)

I think it would be convenient if there is a way to specify this as an option when creating an instance, so I will propose it.

According to can i use, Intl.Segmenter seems to be supported by major browsers.

> **["Intl.Segmenter" | Can I use... Support tables for HTML5, CSS3, etc](https://caniuse.com/?search=Intl.Segmenter)**
>
> "Can I use" provides up-to-date browser support tables for support of front-end web technologies on desktop and mobile web browsers.

MDN has a playground.

> **[Intl.Segmenter() constructor - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter/Segmenter)**
>
> The Intl.Segmenter() constructor creates Intl.Segmenter objects that enable locale-sensitive text segmentation.

---

<div class="post-metadata">

### Author: ![marijn](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/marijn/32/136_2.png) [@marijn](https://discuss.codemirror.net/u/marijn)
#### Post date: [April 22, 2022, 1:55pm UTC](https://discuss.codemirror.net/t/about-the-selection-range-when-double-clicking-intl-segmenter/4241/2 "2022-04-22T13:55:08Z")

</div>

I see your point, but character categories are integrated with features like language-specific word characters (your patch would break, for example, selecting `$name`-style variables in languages that have them) and by-group cursor motion. Also, as long as segmenting is non-standard and not available on Firefox, I don’t really want to redesign these features around it.

---

<div class="post-metadata">

### Author: ![arayutw](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/arayutw/32/1425_2.png) [@arayutw](https://discuss.codemirror.net/u/arayutw)
#### Post date: [April 22, 2022, 2:09pm UTC](https://discuss.codemirror.net/t/about-the-selection-range-when-double-clicking-intl-segmenter/4241/3 "2022-04-22T14:09:22Z")

</div>

Hello. Thank you for your reply.  
Your claim is reasonable, so I’ll try to change it locally.

---

<div class="post-metadata">

### Author: ![marijn](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/marijn/32/136_2.png) [@marijn](https://discuss.codemirror.net/u/marijn)
#### Post date: [April 22, 2022, 3:54pm UTC](https://discuss.codemirror.net/t/about-the-selection-range-when-double-clicking-intl-segmenter/4241/4 "2022-04-22T15:54:52Z")

</div>

An event handler that implements custom behavior for double clicks shouldn’t be too difficult to put together.
