Skip to content

Visual Studio Code, rebuilt in Rust

Rust
110 KB
Raw Blame
//! A text file opened in the editor and the editor group holding it: the lines as the text model
//! holds them (textModel.ts), and what the editor derives from them for drawing (indentation,
//! indent guides, word highlights).

use std::cell::{Cell, Ref, RefCell, RefMut};
use std::collections::{HashMap, HashSet};
use std::ops::Range;
use std::path::{Path, PathBuf};
use std::rc::Rc;
use std::sync::Arc;
use std::time::{Duration, Instant};

use config::configuration::Overrides;
use config::indentation::creation_options;
use config::service::Settings;
use files::encoding;

use crate::diff_editor::DiffInput;
use crate::merge_editor::{Input, MergeInput};
use crate::multi_diff_editor::MultiDiffInput;
use crate::languages::Languages;
use crate::syntax::Tokenization;

mod anchor_select;
pub mod auto_closing;
pub mod auto_indent;
mod bracket_matching;
mod caret_operations;
pub mod clipboard;
pub mod commands;
pub mod cursor_undo;
pub mod decoration_types;
mod comment;
pub(crate) mod cursor;
pub mod drag_and_drop;
mod edit;
#[cfg(test)]
pub(crate) use edit::tests::editor as test_editor;
pub mod edit_stack;
pub mod find;
pub mod folding;
pub mod injected_text;
pub(crate) mod in_place_replace;
mod folding_controller;
mod graphemes;
mod indentation;
mod lines_operations;
pub mod marker_decorations;
pub mod save_participants;
pub mod view_zones;
pub mod view_state;
pub mod minimap;
pub mod model_service;
mod mouse_selection;
mod multicursor;
pub mod section_headers;
pub mod smart_select;
pub mod smooth_scroll;
pub(crate) mod snippet;
pub mod snippet_session;
pub mod sticky_scroll;
pub mod unicode_highlighter;
pub mod unusual_line_terminators;
mod view;
pub mod whitespace;
pub mod word_operations;
pub mod word_wrap;
pub mod zoom;

pub use comment::LineCommentKind;
pub use lines_operations::CaseTransform;
pub use cursor::{Position, Selection, column_from_visible};
use folding::{FoldingMarkers, FoldingModel};
pub use edit::{ContentChange, ContentChanged, CursorEdit, EditReason, History, Pasted, Replace, char_column, end_of, selections_after, text_between, utf16_column};
pub use multicursor::{ANNOUNCES_CURSOR_CHANGE, ColumnSelectData, Reveal};
pub use snippet::{SnippetText, expand as expand_snippet};
pub use view::{HORIZONTAL_SCROLLBAR_HEIGHT, LINE_HEIGHT, Scrollbar, VERTICAL_SCROLLBAR_WIDTH, VerticalReveal, Viewport};

pub struct TextDocument {
    /// The URI scheme: "file", "untitled", or the scheme of an extension's file system provider.
    pub scheme: String,
    /// The file path, or the URI path for other schemes.
    pub path: PathBuf,
    pub lines: Vec<String>,
    pub eol: &'static str,
    /// The `textFileService` encoding id, such as "utf8", "utf8bom" or "windows1252".
    pub encoding: String,
    pub language_id: String,
    pub indentation: Indentation,
    /// `foldingRules.offSide` of the language, used by the indent guides of blank lines.
    pub off_side: bool,
    /// `foldingRules.markers` of the language.
    pub folding_markers: Option<Arc<FoldingMarkers>>,
    /// `wordPattern` of the language (`getWordAtPosition`), `DEFAULT_WORD_REGEXP` without one.
    pub word_pattern: Option<Arc<onig::Regex>>,
    /// `comments.blockComment` start and end tokens of the language.
    pub block_comment: Option<(String, String)>,
    pub tokens: Tokenization,
    /// `TextModel.isTooLargeForTokenization`, decided at creation: no tokens, folding, word highlights or sticky scroll.
    pub too_large_for_tokenization: bool,
    /// Why the file editor shows the editor placeholder in place of this empty model.
    pub open_error: Option<OpenError>,
}

/// The errors textFileEditor.ts `handleSetInputError` turns into an editor placeholder.
#[derive(Clone, Debug, PartialEq)]
pub enum OpenError {
    /// `FILE_IS_BINARY` with the file's size in bytes: binaryEditor.ts.
    Binary(u64),
    /// `FILE_TOO_LARGE` with the file's size in bytes, over `workbench.editorLargeFileConfirmation`.
    TooLarge(u64),
    /// `FILE_NOT_FOUND`.
    NotFound,
    /// Any other read error.
    Failed,
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Indentation {
    pub tab_size: usize,
    /// `indentSize`; `None` while it follows the tab size (`'tabSize'`).
    pub indent_size: Option<usize>,
    pub insert_spaces: bool,
}

impl Indentation {
    pub fn indent_size(&self) -> usize {
        self.indent_size.unwrap_or(self.tab_size)
    }
}

/// `editor.tabSize` and `editor.insertSpaces` defaults, for documents built without settings.
#[cfg(test)]
const DEFAULT_INDENTATION: Indentation = Indentation { tab_size: 4, indent_size: None, insert_spaces: true };

/// textResourceConfiguration.ts `getLanguage` of a resource without a model: `files.associations`, else
/// `guessLanguageIdByFilepathOrFirstLine` by its path alone.
pub fn language_of_path(scheme: &str, path: &Path, languages: &Languages, settings: &Settings) -> Option<String> {
    let associations = settings.get_value(Some("files.associations"), &Overrides::default()).unwrap_or_default();
    let (uri_path, _) = split_query(scheme, path);
    Languages::configured_language(&associations, uri_path).or_else(|| languages.guess_language(uri_path, "").map(str::to_string))
}

impl TextDocument {
    /// What the model keeps of its language's configuration, read again when the language or its configuration
    /// changes (`LanguageConfigurationService.onDidChange`).
    pub fn read_language_configuration(&mut self, languages: &Languages) {
        let language_id = self.language_id.as_str();
        self.off_side = languages.off_side(language_id);
        self.folding_markers = languages.folding_markers(language_id);
        self.word_pattern = languages.rules(language_id).and_then(|rules| rules.word_pattern.clone());
        self.block_comment = languages.block_comment(language_id).map(|(start, end)| (start.to_string(), end.to_string()));
    }

    pub fn read(path: &Path, languages: &Languages, settings: &Settings) -> std::io::Result<Self> {
        files::unc::check_unc_access(path)?;
        let bytes = std::fs::read(path)?;
        Ok(Self::from_bytes("file", path, &bytes, None, languages, settings))
    }

    /// A document of any scheme from its bytes, decoded as `TextFileService.readStream` does: in the `requested`
    /// encoding, else the one its BOM or zero bytes show, else `files.encoding`.
    pub fn from_bytes(scheme: &str, path: &Path, bytes: &[u8], requested: Option<&str>, languages: &Languages, settings: &Settings) -> Self {
        let resource = on_disk(scheme).then_some(path);
        let detected = crate::encoding_guess::detect(bytes, settings, resource, false);
        let language = language_of_path(scheme, path, languages, settings);
        let encoding = encoding::read_encoding(settings, &Overrides { resource, override_identifier: language.as_deref() }, requested, detected.as_deref());
        let text = encoding::decode(bytes, &encoding);
        Self::from_text(scheme, path, &text, encoding, None, languages, settings)
    }

    /// An untitled model (untitledTextEditorModel.ts): `language_id` or the language of its name and first line.
    pub fn untitled(path: &Path, text: &str, language_id: Option<&str>, languages: &Languages, settings: &Settings) -> Self {
        let (encoding, _) = encoding::write_encoding(settings, &Overrides { resource: None, override_identifier: language_id }, None);
        Self::from_text("untitled", path, text, encoding, language_id, languages, settings)
    }

    pub fn from_text(scheme: &str, path: &Path, text: &str, encoding: String, language_id: Option<&str>, languages: &Languages, settings: &Settings) -> Self {
        let (lines, found_eol) = split_lines(text);
        let associations = settings.get_value(Some("files.associations"), &Overrides::default()).unwrap_or_default();
        let (uri_path, _) = split_query(scheme, path);
        let configured = Languages::configured_language(&associations, uri_path);
        let language_id = language_id.or(configured.as_deref()).unwrap_or_else(|| languages.language_of(uri_path, lines[0].as_str())).to_string();
        let off_side = languages.off_side(&language_id);
        let folding_markers = languages.folding_markers(&language_id);
        let word_pattern = languages.rules(&language_id).and_then(|rules| rules.word_pattern.clone());
        let block_comment = languages.block_comment(&language_id).map(|(start, end)| (start.to_string(), end.to_string()));
        let indentation = creation_indentation(&lines, settings, path, &language_id);
        let eol = found_eol.unwrap_or_else(|| default_eol(settings, path, &language_id));
        let too_large_for_tokenization = too_large_for_tokenization(text, lines.len(), settings, path, &language_id);
        let tokens = Tokenization::new(languages.grammars.grammar(&language_id).filter(|_| !too_large_for_tokenization));
        TextDocument { scheme: scheme.to_string(), path: path.to_path_buf(), lines, eol, encoding, language_id, indentation, off_side, folding_markers, word_pattern, block_comment, tokens, too_large_for_tokenization, open_error: None }
    }

    pub fn uri_string(&self) -> String {
        uri_string(&self.scheme, &self.path)
    }

    /// `UriComponents` as the extension host revives them.
    pub fn uri(&self) -> serde_json::Value {
        uri_value(&self.scheme, &self.path)
    }

    pub fn is(&self, scheme: &str, path: &Path) -> bool {
        self.scheme == scheme && self.path == path
    }

    /// uriIdentityService.ts `extUri.isEqual` for the disk, which has no PathCaseSensitive: a path in any case.
    pub fn is_on_disk(&self, scheme: &str, path: &Path) -> bool {
        self.scheme == scheme && on_disk(scheme) && self.path.to_string_lossy().to_lowercase() == path.to_string_lossy().to_lowercase()
    }

    pub fn name(&self) -> String {
        split_query(&self.scheme, &self.path).0.file_name().unwrap_or_default().to_string_lossy().into_owned()
    }
}

/// fileService.ts `hasProvider` for the providers the workbench reads itself: the disk (`file`) and
/// fileUserDataProvider.ts over it (`vscode-userdata`). A model of either reads and writes its path on disk.
pub fn on_disk(scheme: &str) -> bool {
    matches!(scheme, "file" | files::user_data::SCHEME)
}

/// The `UriComponents` of a model's scheme and path, as the extension host revives them.
pub fn uri_value(scheme: &str, path: &Path) -> serde_json::Value {
    // Elsewhere a `//host` start stays in the path (a_non_file_uri_keeps_its_authority).
    if scheme == "file" && cfg!(windows) {
        return crate::uri::Uri::file(&path.to_string_lossy()).to_json();
    }
    let (path, fragment) = split_fragment(scheme, path);
    let (path, query) = split_query(scheme, path);
    let path = path.to_string_lossy();
    // host_requests.rs `uri_parts` keys a non-file URI's authority as the `//authority` start of its path.
    let (authority, path) = match path.strip_prefix("//").filter(|_| scheme != "file") {
        Some(rest) => rest.split_at(rest.find('/').unwrap_or(rest.len())),
        None => ("", &*path),
    };
    serde_json::json!({ "$mid": 1, "scheme": scheme, "authority": authority, "path": path, "query": query, "fragment": fragment })
}

/// A non-file URI's query is kept after a `?` in its model's path (host_requests.rs `uri_parts`), so models of
/// one path with different queries (git's `ref`) stay apart: the path without it and the query.
pub fn split_query<'a>(scheme: &str, path: &'a Path) -> (&'a Path, &'a str) {
    let text = path.to_str().unwrap_or_default();
    match text.split_once('?').filter(|_| scheme != "file") {
        Some((path, query)) => (Path::new(path), query),
        None => (path, ""),
    }
}

/// A notebook cell's or Editor Playground snippet's URI differs from its notebook's or page's only in the fragment,
/// kept after a `#` in its model's path.
pub fn split_fragment<'a>(scheme: &str, path: &'a Path) -> (&'a Path, &'a str) {
    let text = path.to_str().unwrap_or_default();
    match text.rsplit_once('#').filter(|_| has_fragment(scheme)) {
        Some((path, fragment)) => (Path::new(path), fragment),
        None => (path, ""),
    }
}

/// Whether models of `scheme` keep their URI's fragment in their path (`split_fragment`).
pub fn has_fragment(scheme: &str) -> bool {
    matches!(scheme, crate::notebook::CELL_SCHEME | crate::workbench::walkthrough_part::SNIPPET_SCHEME)
}

/// The indentation a model starts with (`TextModel.resolveOptions` from `getCreationOptions`): the language's
/// `editor.tabSize` and `editor.insertSpaces`, guessed from the text when `editor.detectIndentation` is on.
fn creation_indentation(lines: &[String], settings: &Settings, path: &Path, language_id: &str) -> Indentation {
    let options = creation_options(&settings.configuration, path, language_id);
    let configured = Indentation { tab_size: options.tab_size, indent_size: options.indent_size, insert_spaces: options.insert_spaces };
    match options.detect_indentation {
        true => guess_indentation(lines, Indentation { indent_size: None, ..configured }),
        false => configured,
    }
}

/// modelService.ts `_getEOL` and `_readModelOptions`: the language's `files.eol` when it is `\n` or `\r\n`, else LF
/// as `auto` gives on macOS. textResourcePropertiesService.ts reads it with the resource too.
fn default_eol(settings: &Settings, path: &Path, language_id: &str) -> &'static str {
    let overrides = Overrides { resource: Some(path), override_identifier: Some(language_id) };
    match settings.get_value(Some("files.eol"), &overrides) {
        Some(eol) if eol == "\r\n" => "\r\n",
        _ => "\n",
    }
}

/// Lines split at `\r\n`, `\r` or `\n`; the EOL is CRLF when most line breaks hold a CR, else LF, and none for a
/// text without line breaks (PieceTreeTextBufferBuilder `_getEOL`).
pub(crate) fn split_lines(text: &str) -> (Vec<String>, Option<&'static str>) {
    let (mut lines, mut line) = (Vec::new(), String::new());
    let (mut breaks, mut with_cr) = (0, 0);
    let mut chars = text.chars().peekable();
    while let Some(c) = chars.next() {
        match c {
            '\r' => {
                breaks += 1;
                with_cr += 1;
                chars.next_if_eq(&'\n');
                lines.push(std::mem::take(&mut line));
            }
            '\n' => {
                breaks += 1;
                lines.push(std::mem::take(&mut line));
            }
            _ => line.push(c),
        }
    }
    lines.push(line);
    let eol = match breaks {
        0 => None,
        _ if with_cr * 2 > breaks => Some("\r\n"),
        _ => Some("\n"),
    };
    (lines, eol)
}

/// `guessIndentation` (indentationGuesser.ts), over UTF-16 code units as upstream counts them.
/// `URI.toString()`: `file:///a%20b` for a file, `scheme:/path` for a URI without authority.
pub fn uri_string(scheme: &str, path: &Path) -> String {
    if scheme == "file" && cfg!(windows) {
        return crate::uri::Uri::file(&path.to_string_lossy()).to_string(false);
    }
    let mut uri = match scheme {
        "file" => String::from("file://"),
        scheme => format!("{scheme}:"),
    };
    let (path, fragment) = split_fragment(scheme, path);
    let (path, query) = split_query(scheme, path);
    let encode = |uri: &mut String, text: &str, keep_slash: bool| {
        for byte in text.bytes() {
            match byte {
                b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'.' | b'_' | b'~' => uri.push(byte as char),
                b'/' if keep_slash => uri.push('/'),
                _ => uri.push_str(&format!("%{byte:02X}")),
            }
        }
    };
    encode(&mut uri, &path.to_string_lossy(), true);
    if !query.is_empty() {
        uri.push('?');
        encode(&mut uri, query, false);
    }
    if !fragment.is_empty() {
        uri.push('#');
        encode(&mut uri, fragment, false);
    }
    uri
}

pub fn guess_indentation(lines: &[String], default: Indentation) -> Indentation {
    const ALLOWED_TAB_SIZE_GUESSES: [usize; 7] = [2, 4, 6, 8, 3, 5, 7];
    const MAX_ALLOWED_TAB_SIZE_GUESS: usize = 8;
    let (mut lines_with_tabs, mut lines_with_spaces) = (0, 0);
    let mut previous_line: Vec<u16> = Vec::new();
    let mut previous_indentation = 0;
    let mut spaces_diff_count = [0usize; 9];
    for line in lines.iter().take(10000) {
        let line: Vec<u16> = line.encode_utf16().collect();
        let Some(indentation) = line.iter().position(|&c| c != u16::from(b' ') && c != u16::from(b'\t')) else { continue };
        let tabs = line[..indentation].iter().filter(|&&c| c == u16::from(b'\t')).count();
        if tabs > 0 {
            lines_with_tabs += 1;
        } else if indentation > 1 {
            lines_with_spaces += 1;
        }
        let diff = spaces_diff(&previous_line, previous_indentation, &line, indentation);
        if diff.looks_like_alignment && !(default.insert_spaces && default.tab_size == diff.spaces) {
            continue;
        }
        if diff.spaces <= MAX_ALLOWED_TAB_SIZE_GUESS {
            spaces_diff_count[diff.spaces] += 1;
        }
        previous_line = line;
        previous_indentation = indentation;
    }

    let mut insert_spaces = default.insert_spaces;
    if lines_with_tabs != lines_with_spaces {
        insert_spaces = lines_with_tabs < lines_with_spaces;
    }
    let mut tab_size = default.tab_size;
    if insert_spaces {
        let mut best_score = 0;
        for size in ALLOWED_TAB_SIZE_GUESSES {
            if spaces_diff_count[size] > best_score {
                best_score = spaces_diff_count[size];
                tab_size = size;
            }
        }
        // Indented 4 most often but 2 nearly as often: the file is probably indented by 2 with nested blocks.
        if tab_size == 4 && spaces_diff_count[4] > 0 && spaces_diff_count[2] > 0 && spaces_diff_count[2] * 3 >= spaces_diff_count[4] * 2 {
            tab_size = 2;
        }
    }
    Indentation { tab_size, indent_size: None, insert_spaces }
}

struct SpacesDiff {
    spaces: usize,
    looks_like_alignment: bool,
}

/// `spacesDiff`: how much the indentation of `b` differs from `a`, in spaces, where it is unambiguous.
fn spaces_diff(a: &[u16], a_length: usize, b: &[u16], b_length: usize) -> SpacesDiff {
    let none = SpacesDiff { spaces: 0, looks_like_alignment: false };
    let space = u16::from(b' ');
    let common = a[..a_length].iter().zip(&b[..b_length]).take_while(|(x, y)| x == y).count();
    let count = |s: &[u16]| (s.iter().filter(|&&c| c == space).count(), s.iter().filter(|&&c| c != space).count());
    let (a_spaces, a_tabs) = count(&a[common..a_length]);
    let (b_spaces, b_tabs) = count(&b[common..b_length]);
    if (a_spaces > 0 && a_tabs > 0) || (b_spaces > 0 && b_tabs > 0) {
        return none;
    }
    let tabs_diff = a_tabs.abs_diff(b_tabs);
    let spaces = a_spaces.abs_diff(b_spaces);
    if tabs_diff == 0 {
        // `b` lines up with a space in `a` after a line ending in a comma: alignment, not indentation.
        let looks_like_alignment = spaces > 0
            && b_spaces >= 1
            && b_spaces - 1 < a.len()
            && b_spaces < b.len()
            && b[b_spaces] != space
            && a[b_spaces - 1] == space
            && a.last() == Some(&u16::from(b','));
        return SpacesDiff { spaces, looks_like_alignment };
    }
    if spaces % tabs_diff == 0 {
        return SpacesDiff { spaces: spaces / tabs_diff, looks_like_alignment: false };
    }
    none
}

/// `getActiveIndentGuide` (guidesTextModelPart.ts) over the levels of `indent_guides`: the lines and level of the
/// guide around 0-based `line`; a line that opens a block (the next line one level deeper) takes that block's guide.
pub fn active_indent_guide(levels: &[usize], line: usize) -> (Range<usize>, usize) {
    let initial = levels[line];
    // (start, end, indent, whether to grow up, whether to grow down)
    let (mut start, mut end, indent, up, down) = if levels.get(line + 1) == Some(&(initial + 1)) {
        (line + 1, line + 1, initial + 1, false, true)
    } else if line > 0 && levels[line - 1] == initial + 1 {
        (line - 1, line - 1, initial + 1, true, false)
    } else if initial == 0 {
        return (line..line + 1, 0);
    } else {
        (line, line, initial, true, true)
    };
    while up && start > 0 && levels[start - 1] >= indent {
        start -= 1;
    }
    while down && end + 1 < levels.len() && levels[end + 1] >= indent {
        end += 1;
    }
    (start..end + 1, indent)
}

/// The indent level of each line, as `getLinesIndentGuides` (guidesTextModelPart.ts) computes it:
/// a blank line takes its level from the lines with content around it.
pub fn indent_guides(lines: &[String], tab_size: usize, indent_size: usize, off_side: bool) -> Vec<usize> {
    let indents: Vec<Option<usize>> = lines.iter().map(|line| indent_level(line, tab_size)).collect();
    let mut levels = Vec::with_capacity(lines.len());
    for (index, indent) in indents.iter().enumerate() {
        if let Some(indent) = indent {
            levels.push(indent.div_ceil(indent_size));
            continue;
        }
        let above = indents[..index].iter().rev().find_map(|indent| *indent);
        let below = indents[index + 1..].iter().find_map(|indent| *indent);
        let level = match (above, below) {
            (Some(above), Some(below)) if above < below => 1 + above / indent_size,
            (Some(above), Some(below)) if above == below => below.div_ceil(indent_size),
            (Some(_), Some(below)) if off_side => below.div_ceil(indent_size),
            (Some(_), Some(below)) => 1 + below / indent_size,
            // At the top or bottom of the file.
            _ => 0,
        };
        levels.push(level);
    }
    levels
}

/// `computeIndentLevel`: the indent width in columns, or `None` for a line of only whitespace.
fn indent_level(line: &str, tab_size: usize) -> Option<usize> {
    let mut indent = 0;
    for c in line.chars() {
        match c {
            ' ' => indent += 1,
            '\t' => indent = indent - indent % tab_size + tab_size,
            _ => return Some(indent),
        }
    }
    None
}

/// `USUAL_WORD_SEPARATORS` (wordHelper.ts); whitespace separates words too.
const WORD_SEPARATORS: &str = "`~!@#$%^&*()-=+[{]}\\|;:'\",.<>/?";

pub(crate) fn is_word_char(c: char) -> bool {
    !c.is_whitespace() && !WORD_SEPARATORS.contains(c)
}

impl TextDocument {
    /// `TokenizationTextModelPart.getWordAtPosition`: the word touching char `column` of `line`, found with the
    /// `wordPattern` of the language there, inside that language's tokens; at a boundary between two
    /// languages, the word of the language on the left.
    pub fn word_at(&self, languages: &Languages, line: usize, column: usize) -> Option<Range<usize>> {
        let text = &self.lines[line];
        let column = column.min(text.chars().count());
        let mut runs: Vec<(usize, usize, String)> = Vec::new();
        let mut start = 0;
        for (end, _, language) in self.tokens.line_tokens(&self.lines, line) {
            let language = language.map_or_else(|| self.language_id.clone(), |language| language.to_string());
            match runs.last_mut() {
                Some(run) if run.2 == language => run.1 = end,
                _ => runs.push((start, end, language)),
            }
            start = end;
        }
        let offset = text.char_indices().nth(column).map_or(text.len(), |(offset, _)| offset);
        let run = runs.iter().position(|run| offset < run.1).unwrap_or(runs.len() - 1);
        let word_in = |run: usize| {
            let (start, end, language) = &runs[run];
            let first = text[..*start].chars().count();
            let word = smart_select::pattern_word_at(word_definition(languages, language), &text[*start..*end], column - first)?;
            Some(first + word.start..first + word.end)
        };
        word_in(run).or_else(|| if run > 0 && runs[run].0 == offset { word_in(run - 1) } else { None })
    }
}

/// `getLanguageConfiguration(language).getWordDefinition()`: the language's `wordPattern`, else `DEFAULT_WORD_REGEXP`.
pub fn word_definition<'a>(languages: &'a Languages, language: &str) -> &'a onig::Regex {
    languages.rules(language).and_then(|rules| rules.word_pattern.as_deref()).unwrap_or(&in_place_replace::DEFAULT_WORD_REGEXP)
}

/// `_wordenize` (textModelSync.impl.ts): the char ranges of the matches of `pattern` in `line`, up to the first
/// empty one.
pub fn pattern_words(pattern: &onig::Regex, line: &str) -> Vec<Range<usize>> {
    // Not `find_iter`: it skips the empty match right after a word, where `exec` returns it.
    let mut words = Vec::new();
    let mut region = onig::Region::new();
    let mut from = 0;
    while pattern.search_with_options(line, from, line.len(), onig::SearchOptions::SEARCH_OPTION_NONE, Some(&mut region)).is_some() {
        let Some((start, end)) = region.pos(0).filter(|(start, end)| start < end) else { break };
        words.push(line[..start].chars().count()..line[..end].chars().count());
        from = end;
    }
    words
}

/// Whole-word, case-sensitive occurrences of `word` in char ranges, as `TextualDocumentHighlightProvider` finds them.
pub fn word_occurrences(lines: &[String], word: &str) -> Vec<(usize, Range<usize>)> {
    let word: Vec<char> = word.chars().collect();
    let mut found = Vec::new();
    for (line_index, line) in lines.iter().enumerate() {
        let chars: Vec<char> = line.chars().collect();
        let mut start = 0;
        while start + word.len() <= chars.len() {
            let end = start + word.len();
            let bounded = (start == 0 || !is_word_char(chars[start - 1])) && (end == chars.len() || !is_word_char(chars[end]));
            if bounded && chars[start..end] == word[..] {
                found.push((line_index, start..end));
                start = end;
            } else {
                start += 1;
            }
        }
    }
    found
}

/// viewLineRenderer.ts: a line longer than `stopRenderingLineAfter` chars draws that many, then a "Show more"
/// span. The char limit, its byte offset and the span's label.
pub fn line_overflow(text: &str, limit: Option<usize>) -> Option<(usize, usize, String)> {
    let limit = limit.filter(|limit| text.len() > *limit)?;
    let (byte, _) = text.char_indices().nth(limit)?;
    Some((limit, byte, format!("Show more ({})", overflowing_char_count(text[byte..].chars().count()))))
}

/// viewLines.css .mtkoverflow: the label in the editor font, 4px padding and a 1px border on both sides.
pub fn show_more_width(label: &str, char_width: f32) -> f32 {
    label.chars().count() as f32 * char_width + 10.
}

/// viewLineRenderer.ts `renderOverflowingCharCount`.
fn overflowing_char_count(count: usize) -> String {
    match count {
        0..1024 => format!("{count} chars"),
        1024..1_048_576 => format!("{:.1} KB", count as f64 / 1024.),
        _ => format!("{:.1} MB", count as f64 / 1024. / 1024.),
    }
}

/// The visible column of char `column` (`visibleColumn` of `_renderLine`): tabs expanded to the next tab stop.
pub fn visible_column(line: &str, column: usize, tab_size: usize) -> usize {
    line.chars().take(column).fold(0, |visible, c| visible + char_columns(c, visible, tab_size))
}

/// The x of char `column` in typical half-width columns: `visible_column` with full-width chars at the font's
/// full-width advance.
pub fn column_x(line: &str, column: usize, tab_size: usize) -> f32 {
    let (mut visible, mut x) = (0, 0.);
    for c in line.chars().take(column) {
        x += char_advance(c, visible, tab_size);
        visible += char_columns(c, visible, tab_size);
    }
    x
}

/// `CursorColumns.toStatusbarColumn`: tabs expanded, every other char one column, for Col.
pub fn logical_visible_column(line: &str, column: usize, tab_size: usize) -> usize {
    line.chars().take(column).fold(0, |visible, c| if c == '\t' { visible + tab_size - visible % tab_size } else { visible + 1 })
}

/// viewLineRenderer.ts `isControlCharacter` past C0 and DEL: the bidi format characters, drawn as `[U+XXXX]`.
pub fn is_bidi_format_character(c: char) -> bool {
    matches!(c as u32, 0x202A..=0x202E | 0x2066..=0x2069 | 0x200E..=0x200F | 0x061C)
}

/// The visible columns char `c` takes at visible column `visible` (`charWidth` of `_renderLine`): a full-width
/// char two, a char past U+FFFF two (upstream counts its two UTF-16 units).
pub fn char_columns(c: char, visible: usize, tab_size: usize) -> usize {
    match c {
        '\t' => tab_size - visible % tab_size,
        c if is_bidi_format_character(c) => 8,
        c if minimap::is_full_width_character(c as u32) || (c as u32) > 0xFFFF => 2,
        _ => 1,
    }
}

/// The drawn width of char `c` in typical half-width columns: a full-width char is as wide as the font draws
/// it (`typicalFullwidthCharacterWidth`), the rest their `char_columns`.
pub fn char_advance(c: char, visible: usize, tab_size: usize) -> f32 {
    if minimap::is_full_width_character(c as u32) { full_width_columns() } else { char_columns(c, visible, tab_size) as f32 }
}

thread_local! {
    /// `typicalFullwidthCharacterWidth / typicalHalfwidthCharacterWidth` of the editor font, as the UI thread measured
    /// it; two cells before. The editor font is fixed (`EDITOR_FONT`), so one value serves every editor.
    static FULL_WIDTH_COLUMNS: Cell<f32> = const { Cell::new(2.) };
}

pub fn full_width_columns() -> f32 {
    FULL_WIDTH_COLUMNS.get()
}

pub fn set_full_width_columns(columns: f32) {
    FULL_WIDTH_COLUMNS.set(columns);
}

/// The char index nearest to x `target` in columns, for a click on the text.
pub fn column_at_visible(line: &str, target: f32, tab_size: usize) -> usize {
    let (mut visible, mut x) = (0, 0.);
    for (index, c) in line.chars().enumerate() {
        let width = char_advance(c, visible, tab_size);
        if target < x + width / 2. {
            return index;
        }
        x += width;
        visible += char_columns(c, visible, tab_size);
    }
    line.chars().count()
}

/// textModel.ts `LARGE_FILE_SIZE_THRESHOLD` (UTF-16 units) and `LARGE_FILE_LINE_COUNT_THRESHOLD`, with
/// `editor.largeFileOptimizations` on.
fn too_large_for_tokenization(text: &str, line_count: usize, settings: &Settings, path: &Path, language_id: &str) -> bool {
    const LARGE_FILE_SIZE_THRESHOLD: usize = 20 * 1024 * 1024;
    const LARGE_FILE_LINE_COUNT_THRESHOLD: usize = 300 * 1000;
    let overrides = Overrides { resource: Some(path), override_identifier: Some(language_id) };
    if settings.get_value(Some("editor.largeFileOptimizations"), &overrides) == Some(serde_json::Value::Bool(false)) {
        return false;
    }
    // A UTF-16 length is at most the UTF-8 one, so only a long text needs counting.
    line_count > LARGE_FILE_LINE_COUNT_THRESHOLD || (text.len() > LARGE_FILE_SIZE_THRESHOLD && text.encode_utf16().count() > LARGE_FILE_SIZE_THRESHOLD)
}

/// The line as drawn: tabs become spaces up to the next tab stop, control characters a space under their picture;
/// the whitespace marks and control pictures are drawn apart (`editor::whitespace`, `workbench::control_characters`).
pub fn expand_tabs(line: &str, tab_size: usize) -> String {
    let mut out = String::with_capacity(line.len());
    let mut visible = 0;
    for c in line.chars() {
        visible += push_drawn_char(&mut out, c, visible, tab_size);
    }
    out
}

/// Appends char `c` as `expand_tabs` draws it at visible column `visible`; returns the columns it takes.
pub fn push_drawn_char(out: &mut String, c: char, visible: usize, tab_size: usize) -> usize {
    if c == '\t' {
        let width = tab_size - visible % tab_size;
        out.extend(std::iter::repeat_n(' ', width));
        width
    } else if c.is_ascii_control() {
        out.push(' ');
        1
    } else if is_bidi_format_character(c) {
        out.push_str("        ");
        8
    } else {
        out.push(c);
        char_columns(c, visible, tab_size)
    }
}

/// The text model (textModel.ts) with what lives on it rather than on an editor: its version, its undo stack,
/// its indent guides, its marker decorations. Every editor of the resource shares it.
pub struct TextModel {
    pub document: TextDocument,
    /// `getVersionId`: 1 when opened, one more per content change.
    pub version_id: u32,
    pub(crate) history: History,
    /// The workbench's undo/redo service the model pushes its edit stack elements into (`EditStack`).
    pub undo_redo: edit_stack::UndoService,
    /// `_initialUndoRedoSnapshot`: the model's stack before its first edit, restored when its elements are not kept.
    pub initial_undo_redo_snapshot: Option<crate::undo_redo::ResourceEditStackSnapshot>,
    /// The indent guide level of each line.
    pub guides: Vec<usize>,
    /// When the extension host last opened this model (`$tryOpenDocument`): it stays open without an editor
    /// for `BoundModelReferenceCollection`'s 3 minutes after that.
    pub host_reference: Option<Instant>,
    /// The text length when the host last opened it: `BoundModelReferenceCollection` holds 80 MB at most.
    pub host_reference_length: usize,
    /// An untitled model the host opened for a path (`hasAssociatedFilePath`): dirty from the start, saved to that file.
    pub associated_file: bool,
    /// fileEditorInput.ts `preferredName`: the name an opener gave the resource's editor, kept while the model lives.
    pub preferred_name: Option<String>,
    /// The decorations of the model's problem markers.
    pub markers: Vec<marker_decorations::MarkerDecoration>,
    /// More markers than `MAX_MARKER_DECORATIONS` (`_isMarkerLimitExceeded`): only that many are decorated.
    pub markers_truncated: bool,
    /// Each replace as (version it applied to, start, end, new end, start line's length before it), for the model's
    /// other editors to follow.
    pub(crate) edits: Vec<(u32, Position, Position, Position, usize)>,
    /// The placeholder model of a custom document's webview editor: whether the document is dirty.
    pub custom_document_dirty: bool,
    pub semantic_tokens: crate::semantic_tokens::ModelSemanticTokens,
}

/// One model shared by the editors of its resource in every group.
pub type SharedModel = Rc<RefCell<TextModel>>;

/// An editor of a text model: the model handle and the editor's own view state.
pub struct OpenEditor {
    pub model: SharedModel,
    /// A preview editor shows in italics and is replaced by the next preview open (`workbench.editor.enablePreview`).
    pub preview: bool,
    /// A pinned editor (editorGroupModel.ts `isSticky`): the group's sticky editors come first, never preview.
    pub sticky: bool,
    /// The `$model<N>` id the text model gets, so the extension host sees a new editor per opened model.
    pub model_id: u32,
    /// The cursors' selections, the primary one first (`CursorCollection`).
    pub selections: Vec<Selection>,
    pub cursor_changed: bool,
    /// The cursor added last, which wins a merge of its direction (`lastAddedCursorIndex`).
    last_added: usize,
    /// Per cursor, visible columns a vertical move could not place at the anchor and the head, kept while
    /// moving up and down (`selectionStartLeftoverVisibleColumns`, `leftoverVisibleColumns`).
    leftovers: Vec<(isize, isize)>,
    /// The selections of the last cursor move with each head (position, injections at it the head sits after).
    past_injections: Option<(Vec<Selection>, Vec<(Position, usize)>)>,
    /// The last column selection and the selections it made: it goes on while they are unchanged (`_columnSelectData`).
    column_select: Option<(ColumnSelectData, Vec<Selection>)>,
    /// The word or line range a mouse selection started from and the selection it made (`selectionStart`): it goes on
    /// while a cursor holds that selection.
    selection_start: Option<((Position, Position), Selection)>,
    /// The ranges of the last Expand or Shrink Selection and the selections it made: kept while they are unchanged.
    smart_select: Option<(Vec<smart_select::SelectionRanges>, Vec<Selection>)>,
    cursor_states: cursor_undo::CursorStates,
    /// Occurrences of the word at the cursor, by line and char range.
    pub highlights: Vec<(usize, Range<usize>)>,
    /// inPlaceReplace.ts `valueSetReplacement`: the replaced value's line and char range, under the version that made it.
    pub value_set_replacement: Option<(u32, usize, Range<usize>)>,
    /// anchorSelect.ts: the selection anchor, while the group's code editor shows this model.
    pub selection_anchor: Option<Position>,
    /// The folding regions, their collapse state and the lines it hides.
    pub folding: FoldingModel,
    /// Extension `FoldingRangeProvider`s fold the model (`SyntaxRangeProvider`), not its indentation.
    pub syntax_folding: bool,
    /// folding.ts `_currentModelHasFoldedImports`: `editor.foldingImportsByDefault` already collapsed the imports.
    pub folded_imports: bool,
    /// folding.ts `onDidChangeModelContent` from another editor of the model: the regions moved with its edits and
    /// are computed again when the update Delayer runs (`EditorsState::refresh_stale_folding`).
    pub folding_stale: bool,
    /// `editor.foldingMaximumRegions` of the indentation provider, and the limit its last regions were cut to.
    pub folding_limit: usize,
    pub folding_limited: Option<usize>,
    /// The sticky scroll scopes of the indentation and marker regions, without the folding model's manual ones.
    pub sticky_model: sticky_scroll::StickyElement,
    /// The scopes of the document symbols and their provider id, used before `sticky_model` (`StickyModelProvider`
    /// with `editor.stickyScroll.defaultModel` `outlineModel`); `None` without symbols.
    pub sticky_outline: Option<(sticky_scroll::StickyElement, Option<String>)>,
    /// The editor's scroll position in pixels, kept with the editor like its view state.
    pub scroll: Scroll,
    /// The widest line drawn so far, in pixels: the content width grows with it and never shrinks.
    max_line_width: f32,
    pub view_zones: Vec<view_zones::ViewZone>,
    /// The extension decorations `$trySetDecorations` set on the editor showing this model.
    pub text_decorations: Vec<decoration_types::TextDecoration>,
    /// The model version this editor's cursors and folding regions were last placed in (`sync_with_model`).
    seen_version: u32,
    /// The snippet session of snippetController2.ts, its placeholders tracked through edits.
    pub snippet_session: Option<snippet_session::SnippetSession>,
    /// cursor.ts `_autoClosedActions`.
    auto_closed: Vec<auto_closing::AutoClosedAction>,
    pub word_wrap: word_wrap::WordWrap,
    /// The text this editor injects between the model's chars (inlay hints, color boxes), by position.
    pub injected_text: Vec<injected_text::InjectedText>,
    /// The `cursorStyle` and `lineNumbers` an extension set with `TextEditor.options` (`codeEditor.updateOptions`),
    /// in the settings' form; upstream keeps them on the group's code editor, here on the editor of one model.
    pub cursor_style_override: Option<&'static str>,
    pub line_numbers_override: Option<&'static str>,
    /// A peek widget's embedded editor (`PeekEditor`): `scrollBeyondLastLine` false, no minimap.
    /// The number is the `$model` id of its current model, a new one per model it shows (`TextEditorSnapshot.id`).
    pub peek_preview: Option<u32>,
}

#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Scroll {
    pub top: f32,
    pub left: f32,
}

/// `BoundModelReferenceCollection` maxAge.
pub const HOST_REFERENCE_AGE: Duration = Duration::from_secs(3 * 60);
/// `BoundModelReferenceCollection` maxLength.
const HOST_REFERENCE_MAX_LENGTH: usize = 80 * 1024 * 1024;
/// `BoundModelReferenceCollection` maxSize.
const HOST_REFERENCE_MAX_COUNT: usize = 50;

impl TextModel {
    /// `BoundModelReferenceCollection.add` with `getValueLength`.
    pub fn add_host_reference(&mut self, opened: Instant) {
        self.host_reference = Some(opened);
        self.host_reference_length = self.document.lines.iter().map(String::len).sum::<usize>() + self.document.lines.len().saturating_sub(1) * self.document.eol.len();
    }
}

impl OpenEditor {
    fn new(document: TextDocument, preview: bool, model_id: u32, undo_redo: edit_stack::UndoService) -> Self {
        let indentation = document.indentation;
        let guides = indent_guides(&document.lines, indentation.tab_size, indentation.indent_size(), document.off_side);
        let model = TextModel { document, version_id: 1, history: History::default(), undo_redo, initial_undo_redo_snapshot: None, guides, host_reference: None, host_reference_length: 0, associated_file: false, preferred_name: None, markers: Vec::new(), markers_truncated: false, edits: Vec::new(), custom_document_dirty: false, semantic_tokens: Default::default() };
        Self::of_model(Rc::new(RefCell::new(model)), preview, model_id)
    }

    /// An editor of a model that is open already.
    pub fn of_model(model: SharedModel, preview: bool, model_id: u32) -> Self {
        let (seen_version, word_wrap) = {
            let model = model.borrow();
            (model.version_id, word_wrap::WordWrap::new(&model.document.lines, model.document.too_large_for_tokenization))
        };
        let mut editor = OpenEditor { seen_version, model, preview, sticky: false, model_id, selections: vec![Selection::default()], cursor_changed: false, last_added: 0, leftovers: Vec::new(), past_injections: None, column_select: None, selection_start: None, smart_select: None, cursor_states: Default::default(), highlights: Vec::new(), value_set_replacement: None, selection_anchor: None, folding: FoldingModel::default(), syntax_folding: false, folded_imports: false, folding_stale: false, sticky_model: Default::default(), folding_limit: folding::MAX_FOLDING_REGIONS_FOR_INDENT, folding_limited: None, sticky_outline: None, scroll: Scroll::default(), max_line_width: 0., view_zones: Vec::new(), snippet_session: None, auto_closed: Vec::new(), word_wrap, text_decorations: Vec::new(), injected_text: Vec::new(), cursor_style_override: None, line_numbers_override: None, peek_preview: None };
        editor.refresh_highlights();
        editor.refresh_folding_ranges();
        editor
    }

    pub fn document(&self) -> Ref<'_, TextDocument> {
        Ref::map(self.model.borrow(), |model| &model.document)
    }

    pub fn document_mut(&mut self) -> RefMut<'_, TextDocument> {
        RefMut::map(self.model.borrow_mut(), |model| &mut model.document)
    }

    pub fn text_model(&self) -> Ref<'_, TextModel> {
        self.model.borrow()
    }

    pub fn text_model_mut(&mut self) -> RefMut<'_, TextModel> {
        self.model.borrow_mut()
    }

    /// `TextModel.setLanguage` and `ModelService._onDidChangeLanguage`: the language's grammar and rules, and its
    /// indentation options when they differ from the old language's (`_setModelOptionsForModel`).
    pub fn set_language(&mut self, language_id: &str, languages: &Languages, settings: &Settings) {
        let indentation = {
            let mut document = self.document_mut();
            let old = creation_options(&settings.configuration, &document.path, &document.language_id);
            let new = creation_options(&settings.configuration, &document.path, language_id);
            document.language_id = language_id.to_string();
            document.read_language_configuration(languages);
            document.tokens = Tokenization::new(languages.grammars.grammar(language_id).filter(|_| !document.too_large_for_tokenization));
            match (old == new, new.detect_indentation) {
                (true, _) => document.indentation,
                (false, true) => guess_indentation(&document.lines, Indentation { tab_size: new.tab_size, indent_size: None, insert_spaces: new.insert_spaces }),
                (false, false) => Indentation { tab_size: new.tab_size, indent_size: new.indent_size, insert_spaces: new.insert_spaces },
            }
        };
        self.set_indentation(indentation);
        self.refresh_folding_ranges();
    }
}

/// An editor group other than the active one (editorGroupView.ts): its editors, their MRU and the size of its
/// code editor. `EditorGroup::activate_group` swaps it with the active group's fields.
pub struct GroupState {
    pub id: u32,
    pub editors: Vec<OpenEditor>,
    recent: Vec<u32>,
    pub viewport: Viewport,
    unicode_banner_closed: bool,
    diff_part_width: Rc<Cell<f32>>,
}

impl GroupState {
    pub fn active_index(&self) -> Option<usize> {
        let id = *self.recent.first()?;
        self.editors.iter().position(|editor| editor.model_id == id)
    }
}

/// The group of the panel's code editor (the Output view's): kept with the editor groups so it edits through
/// the same code, but never in the grid nor a view column.
pub const PANEL_GROUP: u32 = u32::MAX;
/// The modal editor part's one group (modalEditorPart.ts), outside the grid like the panel's.
pub const MODAL_GROUP: u32 = u32::MAX - 1;
/// The Test Results view's code editors (testResultsOutput.ts content providers), in the panel like the Output view's.
pub const TEST_RESULTS_GROUP: u32 = u32::MAX - 2;

/// Whether `id` is a group of a panel view's code editors.
pub fn is_panel_group(id: u32) -> bool {
    id == PANEL_GROUP || id == TEST_RESULTS_GROUP
}

/// A peek widget's embedded code editor: referencesWidget.ts `_preview`, callHierarchyPeek.ts and typeHierarchyPeek.ts
/// `_editor`, quickDiffWidget.ts `diffEditor`'s modified editor.
pub struct PeekEditor {
    pub owner: PeekOwner,
    pub editor: OpenEditor,
    /// Its size when it last revealed a range, for the visible ranges the host gets with it.
    pub viewport: Viewport,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PeekOwner {
    References,
    Hierarchy,
    QuickDiff,
    /// callStackWidget.ts `FrameCodeRenderer`: the Test Results call stack's frame at this index.
    CallStackFrame(usize),
}

/// `IRecentlyClosedEditor`: a closed file editor by its resource, group and tab index, and a side by side editor's
/// secondary side (sideBySideEditorInput.ts `toUntyped` keeps both sides).
pub struct ClosedEditor {
    pub group: u32,
    pub scheme: String,
    pub path: PathBuf,
    pub index: usize,
    pub secondary: Option<PathBuf>,
}
/// The editor part's groups. The active group's editors and state are the fields below; the other groups wait
/// in `other_groups`. Text models are shared by the editors of their resource in every group, so `models`,
/// `diffs`, the disposed models and the view states are the part's.
#[derive(Default)]
pub struct EditorGroup {
    /// The active group's id (`GroupIdentifier`); the first group is 0.
    pub id: u32,
    pub other_groups: Vec<GroupState>,
    /// The other groups' ids, most recently active first (`mostRecentActiveGroups` without the active one).
    group_mru: Vec<u32>,
    next_group_id: u32,
    /// The groups in grid order (`GroupsOrder.GRID_APPEARANCE`), which the host numbers as view columns; the
    /// workbench sets it, empty while the part has one group.
    pub columns: Vec<u32>,
    /// The panel's code editor shows (the Output view is visible): the host sees it as a visible text editor.
    pub panel_editor_shown: bool,
    /// Closed file editors, for the workbench's Reopen Closed Editor.
    pub closed: Vec<ClosedEditor>,
    /// The active group's last editor closed (`closeEmptyGroups` closes the group then).
    pub closed_last_editor: bool,
    pub editors: Vec<OpenEditor>,
    /// Text models open without an editor: documents the extension host opened, and closed editors it still holds.
    pub models: Vec<OpenEditor>,
    /// The active group's model ids, most recently active first: closing the active editor activates the next one
    /// (`focusRecentEditorAfterClose`).
    recent: Vec<u32>,
    next_model_id: u32,
    /// The size of the group's one code editor, as last laid out; every editor of the group shows in it.
    pub viewport: Viewport,
    /// The width of the group's diff or multi diff editor, as last laid out.
    pub diff_part_width: Rc<Cell<f32>>,
    /// A line to center once the code editor has its first size: a diff opened before any editor showed (`revealFirstDiff`).
    pub reveal_after_layout: Option<Position>,
    /// unicodeHighlighter.ts `_bannerClosed`: the banner was closed for the model the code editor shows.
    pub unicode_banner_closed: bool,
    /// The diff editors among `editors`, by their entry's model id: each shows two models found with `model`.
    pub diffs: HashMap<u32, DiffInput>,
    /// The multi diff editors among `editors`, by their entry's model id.
    pub multi_diffs: HashMap<u32, MultiDiffInput>,
    /// The merge editors among `editors`, by their entry's model id: each edits its result model.
    pub merges: HashMap<u32, crate::merge_editor::MergeInput>,
    /// The side by side editors among `editors` (`SideBySideEditorInput`), by their entry's model id: the entry is
    /// the primary side, the secondary is the one editor of a group of its own, outside the grid and the group list.
    pub side_by_sides: HashMap<u32, u32>,
    /// Side by side editors reopened from their untyped form: a plain `SideBySideEditorInput`, whose editor is the
    /// text editor, where the one preferencesService.ts opens is a `SplitSettingsEditorInput`.
    pub plain_side_by_sides: HashSet<u32>,
    /// For an editor among `editors` that stacks several live code editors (a notebook's cells), by its entry's
    /// model id: the model the group's code editor edits, the focused one (`INotebookEditor.activeCodeEditor`).
    /// The others draw as `embedded_code_editor`s without the focus.
    pub stacked_editors: HashMap<u32, crate::diff_editor::Side>,
    /// A code editor drawn in a view zone of a tab's own with the focus, as (tab, its model id): a comment's reply or
    /// edit editor (simpleCommentEditor.ts) or a peek editor; the code editor while its tab is active.
    pub zone_editor: Option<(u32, u32)>,
    /// The peek widgets' embedded code editors, one per widget kind.
    pub peek_editors: Vec<PeekEditor>,
    /// A code editor of the active tab without the focus, set while it draws or lays out (workbench/code_editors.rs):
    /// `active` answers with it.
    pub drawn: Option<u32>,
    /// `$registerTextEditorDecorationType` render options by decoration type key.
    pub decoration_types: HashMap<String, serde_json::Value>,
    pub disposed: model_service::DisposedModels,
    /// The workbench's `IUndoRedoService`, shared by its text models and its file edits.
    pub undo_redo: edit_stack::UndoService,
    pub view_states: view_state::ViewStates,
    /// The locked groups (editorGroupModel.ts `locked`).
    pub locked: HashSet<u32>,
    /// The group whose first editor just opened, which `workbench.editor.autoLockGroups` may lock.
    pub opened_first_editor: Option<u32>,
    /// `workbench.editor.enablePreview` is off: editorGroupView.ts `openEditor` pins every editor it opens.
    pub previews_disabled: bool,
}

impl EditorGroup {
    /// The model the code editor edits: the active editor's, or the modified side of the active diff editor.
    pub fn active(&self) -> Option<&OpenEditor> {
        if let Some(id) = self.drawn {
            return self.editor_by_id(id);
        }
        if let Some(id) = self.focused_zone_editor() {
            return self.editor_by_id(id);
        }
        self.pane_editor()
    }

    /// The code editor the active tab's pane shows, without a focused comment editor in it.
    pub fn pane_editor(&self) -> Option<&OpenEditor> {
        self.code_editor_of(self.active_tab()?)
    }

    pub fn focused_zone_editor(&self) -> Option<u32> {
        self.zone_editor.filter(|(tab, _)| self.recent.first() == Some(tab)).map(|(_, id)| id)
    }

    /// The code editor a tab shows: a diff's focused side editor, a merge editor's result model, else the tab itself.
    fn code_editor_of<'a>(&'a self, tab: &'a OpenEditor) -> Option<&'a OpenEditor> {
        if let Some(editor) = self.multi_diffs.get(&tab.model_id).and_then(MultiDiffInput::focused_editor) {
            return Some(editor);
        }
        if let Some(diff) = self.diffs.get(&tab.model_id).filter(|diff| diff.modified_editor.is_some()) {
            return if diff.original_focused { diff.original_editor.as_ref() } else { diff.modified_editor.as_ref() };
        }
        if let Some(input) = self.merges.get(&tab.model_id).and_then(MergeInput::focused_input_editor) {
            return Some(input);
        }
        match self.edited_side(tab.model_id) {
            Some(side) => self.model(&side.scheme, &side.path),
            None => Some(tab),
        }
    }

    /// The side a diff editor (its modified one), a merge editor (its result) or a notebook editor (its focused cell)
    /// edits in the code editor.
    fn edited_side(&self, id: u32) -> Option<crate::diff_editor::Side> {
        let stacked = || self.stacked_editors.get(&id).cloned();
        self.diffs.get(&id).map(|diff| diff.modified.clone()).or_else(|| self.merges.get(&id).map(|merge| merge.result.clone())).or_else(stacked)
    }

    /// The tab of group `id` that shows, as `group_active` without resolving the model it edits.
    pub fn group_active_tab(&self, id: u32) -> Option<&OpenEditor> {
        match self.group(id) {
            Some(group) => group.editors.get(group.active_index()?),
            None => self.active_tab(),
        }
    }

    pub fn active_mut(&mut self) -> Option<&mut OpenEditor> {
        if let Some(id) = self.drawn.or(self.focused_zone_editor()) {
            return self.code_editor_mut(id);
        }
        self.pane_editor_mut()
    }

    pub fn pane_editor_mut(&mut self) -> Option<&mut OpenEditor> {
        let id = *self.recent.first()?;
        if self.multi_diffs.get(&id).is_some_and(|input| input.focused_editor().is_some()) {
            return self.multi_diffs.get_mut(&id).and_then(MultiDiffInput::focused_editor_mut);
        }
        if self.diffs.get(&id).is_some_and(|diff| diff.modified_editor.is_some()) {
            let diff = self.diffs.get_mut(&id).expect("checked above");
            return if diff.original_focused { diff.original_editor.as_mut() } else { diff.modified_editor.as_mut() };
        }
        if self.merges.get(&id).is_some_and(|merge| merge.focused_input_editor().is_some()) {
            return self.merges.get_mut(&id).and_then(MergeInput::focused_input_editor_mut);
        }
        match self.edited_side(id) {
            Some(modified) => self.model_mut(&modified.scheme, &modified.path),
            None => self.editors.iter_mut().find(|editor| editor.model_id == id),
        }
    }

    /// The active tab's entry, a diff editor's too.
    pub fn active_tab(&self) -> Option<&OpenEditor> {
        let id = *self.recent.first()?;
        self.editors.iter().find(|editor| editor.model_id == id)
    }

    /// The diff editor of the active tab, if it is one.
    pub fn active_diff(&self) -> Option<&DiffInput> {
        self.diffs.get(self.recent.first()?)
    }

    /// The active diff editor's modified side editor, whichever side has the focus.
    pub fn active_diff_modified_mut(&mut self) -> Option<&mut OpenEditor> {
        let id = *self.recent.first()?;
        let modified = self.diffs.get(&id)?.modified.clone();
        if self.diffs[&id].modified_editor.is_some() {
            return self.diffs.get_mut(&id).and_then(|diff| diff.modified_editor.as_mut());
        }
        self.model_mut(&modified.scheme, &modified.path)
    }

    /// Moves the focus of the active merge editor to the input whose code editor is `model_id`, or to the result when
    /// it is the result's; false when `model_id` is neither.
    pub fn focus_merge_side(&mut self, model_id: u32) -> bool {
        let Some(id) = self.recent.first().copied().filter(|id| self.merges.contains_key(id)) else { return false };
        let result = self.merges[&id].result.clone();
        let is_result = self.model(&result.scheme, &result.path).is_some_and(|editor| editor.model_id == model_id);
        let merge = self.merges.get_mut(&id).expect("checked above");
        let input = [Input::One, Input::Two].into_iter().find(|input| merge.input_editors[*input as usize].as_ref().is_some_and(|editor| editor.model_id == model_id));
        if input.is_none() && !is_result {
            return false;
        }
        merge.focused_input = input;
        true
    }

    /// Moves the focus of the active diff editor to its original (`true`) or modified side editor.
    pub fn focus_diff_side(&mut self, original: bool) {
        let Some(diff) = self.recent.first().and_then(|id| self.diffs.get_mut(id)) else { return };
        diff.original_focused = original && diff.original_editor.is_some();
    }

    /// The merge editor of the active tab, if it is one.
    pub fn active_merge(&self) -> Option<&crate::merge_editor::MergeInput> {
        self.merges.get(self.recent.first()?)
    }

    pub fn active_merge_mut(&mut self) -> Option<&mut crate::merge_editor::MergeInput> {
        self.merges.get_mut(self.recent.first()?)
    }

    /// Opens a merge editor as `open_diff` opens a diff editor.
    pub fn open_merge(&mut self, entry: TextDocument, input: MergeInput, preview: bool) {
        let mut input = input;
        input.input_editors = [self.side_editor(&input.input1.side), self.side_editor(&input.input2.side)];
        self.next_model_id += 1;
        self.merges.insert(self.next_model_id, input);
        let editor = OpenEditor::new(entry, preview, self.next_model_id, self.undo_redo.clone());
        self.insert(editor, preview);
    }

    /// The multi diff editor of the active tab, if it is one.
    pub fn active_multi_diff(&self) -> Option<&MultiDiffInput> {
        self.multi_diffs.get(self.recent.first()?)
    }

    /// The model a tab edits: its own, or a diff editor's modified side.
    pub fn tab_model(&self, index: usize) -> &OpenEditor {
        self.editor_model(&self.editors[index])
    }

    /// The model an editor of any group edits: a diff or merge editor's edited side, else the editor's own.
    pub fn editor_model<'a>(&'a self, tab: &'a OpenEditor) -> &'a OpenEditor {
        match self.edited_side(tab.model_id) {
            Some(side) => self.model(&side.scheme, &side.path).expect("a diff or merge editor's models stay open"),
            None => tab,
        }
    }

    /// Opens a diff editor of two open models as `open` opens an editor; `entry` is its text-less tab document.
    pub fn open_diff(&mut self, entry: TextDocument, input: DiffInput, preview: bool) {
        let mut input = input;
        self.attach_side_editors(&mut input);
        self.next_model_id += 1;
        self.diffs.insert(self.next_model_id, input);
        let editor = OpenEditor::new(entry, preview, self.next_model_id, self.undo_redo.clone());
        self.insert(editor, preview);
    }

    /// The tab, model or diff side editor with id `model_id`.
    pub fn editor_by_id(&self, model_id: u32) -> Option<&OpenEditor> {
        self.code_editors().find(|editor| editor.model_id == model_id)
    }

    /// Gives a diff its own code editor of each side's model, at the start of the model.
    pub fn attach_side_editors(&mut self, input: &mut DiffInput) {
        input.original_editor = self.side_editor(&input.original);
        input.modified_editor = self.side_editor(&input.modified);
    }

    /// referencesWidget.ts `_preview.setModel`: `owner`'s peek editor shows the open model `scheme`/`path`, else
    /// `missing()` (`_previewNotAvailableMessage`), a model of its own; its code editor keeps its id.
    pub fn show_in_peek_editor(&mut self, owner: PeekOwner, scheme: &str, path: &Path, missing: impl FnOnce() -> TextDocument) -> &mut PeekEditor {
        let shown = self.peek_editors.iter().position(|peek| peek.owner == owner);
        let model_id = match shown {
            Some(index) => self.peek_editors[index].editor.model_id,
            None => {
                self.next_model_id += 1;
                self.next_model_id
            }
        };
        let same_model = |open: &OpenEditor| shown.is_some_and(|index| Rc::ptr_eq(&self.peek_editors[index].editor.model, &open.model));
        let mut editor = match self.model(scheme, path) {
            Some(open) if same_model(open) => return &mut self.peek_editors[shown.expect("same model")],
            Some(open) => OpenEditor::of_model(open.model.clone(), false, model_id),
            None => OpenEditor::new(missing(), false, model_id, self.undo_redo.clone()),
        };
        // wordHighlighter.ts runs on an explicit cursor move or the text focus, which a model set in a peek is not.
        editor.highlights.clear();
        self.next_model_id += 1;
        editor.peek_preview = Some(self.next_model_id);
        match shown {
            Some(index) => {
                self.peek_editors[index].editor = editor;
                &mut self.peek_editors[index]
            }
            None => {
                self.peek_editors.push(PeekEditor { owner, editor, viewport: Viewport::default() });
                self.peek_editors.last_mut().expect("just pushed")
            }
        }
    }

    pub fn peek_editor(&self, owner: PeekOwner) -> Option<&OpenEditor> {
        self.peek_editors.iter().find(|peek| peek.owner == owner).map(|peek| &peek.editor)
    }

    pub fn remove_peek_editor(&mut self, owner: PeekOwner) -> Option<OpenEditor> {
        let index = self.peek_editors.iter().position(|peek| peek.owner == owner)?;
        Some(self.peek_editors.remove(index).editor)
    }

    pub fn side_editor(&mut self, side: &crate::diff_editor::Side) -> Option<OpenEditor> {
        let model = self.model(&side.scheme, &side.path)?.model.clone();
        self.next_model_id += 1;
        Some(OpenEditor::of_model(model, false, self.next_model_id))
    }

    /// The modified side's editor of a diff taken out of `diffs` (its `modified_editor` and `modified`): its own,
    /// else the model's.
    pub fn diff_modified_mut<'a>(&'a mut self, own: &'a mut Option<OpenEditor>, modified: &crate::diff_editor::Side) -> Option<&'a mut OpenEditor> {
        match own.as_mut() {
            Some(editor) => Some(editor),
            None => self.model_mut(&modified.scheme, &modified.path),
        }
    }

    /// The group's code editors: its tabs, its models without a tab (which the diff views draw by hand), its diff
    /// editors' side editors and its multi diff editors' file side editors.
    pub fn code_editors(&self) -> impl Iterator<Item = &OpenEditor> {
        let side_editors = self.diffs.values().flat_map(|diff| [&diff.original_editor, &diff.modified_editor]).flatten();
        let file_editors = self.multi_diffs.values().flat_map(MultiDiffInput::editors);
        let input_editors = self.merges.values().flat_map(|merge| &merge.input_editors).flatten();
        self.editors.iter().chain(&self.models).chain(side_editors).chain(file_editors).chain(input_editors).chain(self.peek_editors.iter().map(|peek| &peek.editor))
    }

    pub fn code_editor_mut(&mut self, model_id: u32) -> Option<&mut OpenEditor> {
        let side_editors = self.diffs.values_mut().flat_map(|diff| [diff.original_editor.as_mut(), diff.modified_editor.as_mut()]).flatten();
        let file_editors = self.multi_diffs.values_mut().flat_map(MultiDiffInput::editors_mut);
        let input_editors = self.merges.values_mut().flat_map(|merge| &mut merge.input_editors).flatten();
        self.editors.iter_mut().chain(&mut self.models).chain(side_editors).chain(file_editors).chain(input_editors).chain(self.peek_editors.iter_mut().map(|peek| &mut peek.editor)).find(|editor| editor.model_id == model_id)
    }

    /// The active multi diff editor's file and side whose code editor is `model_id`.
    pub fn multi_diff_side(&self, model_id: u32) -> Option<(usize, crate::diff_editor::ZoneSide)> {
        self.active_multi_diff()?.side_of(model_id)
    }

    /// Makes `model_id`, a file side's code editor of the active multi diff editor, its active one.
    pub fn focus_multi_diff_side(&mut self, model_id: u32) -> bool {
        let Some(input) = self.recent.first().and_then(|id| self.multi_diffs.get_mut(id)) else { return false };
        let Some((file, side)) = input.side_of(model_id) else { return false };
        input.focus(file, side);
        true
    }

    pub fn diff_modified<'a>(&'a self, input: &'a DiffInput) -> Option<&'a OpenEditor> {
        input.modified_editor.as_ref().or_else(|| self.model(&input.modified.scheme, &input.modified.path))
    }

    /// Opens a multi diff editor as `open_diff` opens a diff editor.
    pub fn open_multi_diff(&mut self, entry: TextDocument, input: MultiDiffInput, preview: bool) {
        self.next_model_id += 1;
        self.multi_diffs.insert(self.next_model_id, input);
        let editor = OpenEditor::new(entry, preview, self.next_model_id, self.undo_redo.clone());
        self.insert(editor, preview);
    }

    pub fn active_index(&self) -> Option<usize> {
        let id = *self.recent.first()?;
        self.editors.iter().position(|editor| editor.model_id == id)
    }

    /// The open editor of the resource, if any.
    pub fn find(&self, scheme: &str, path: &Path) -> Option<usize> {
        self.editors.iter().position(|editor| editor.document().is_on_disk(scheme, path))
    }

    /// Opens a document right of the active editor, or in place of the preview editor when this open is a preview too.
    pub fn open(&mut self, document: TextDocument, preview: bool) {
        let editor = self.create_model(document, preview);
        self.insert(editor, preview);
    }

    /// Shows `document` in the editor at `index`, in place of the model it showed, and activates it.
    pub fn replace(&mut self, index: usize, document: TextDocument) {
        let mut editor = self.create_model(document, false);
        editor.sticky = self.editors[index].sticky;
        self.restore_view_state(&mut editor);
        let replaced = std::mem::replace(&mut self.editors[index], editor);
        self.recent.retain(|id| *id != replaced.model_id);
        self.remove_input(&replaced.model_id);
        self.keep_or_dispose(replaced);
        self.activate(index);
    }

    /// Shows `document` in the editor at `index` as a new model, also when its resource is the model it showed (an
    /// editor placeholder resolving its file again), and activates it.
    pub fn reload(&mut self, index: usize, document: TextDocument) {
        self.next_model_id += 1;
        let editor = OpenEditor::new(document, self.editors[index].preview, self.next_model_id, self.undo_redo.clone());
        self.swap_editor(index, editor);
        self.activate(index);
    }

    /// `replaceEditors`: the editor at `index` shows `model`, in the old editor's place in the tabs and the recency order.
    pub fn replace_model(&mut self, index: usize, model: SharedModel) {
        self.next_model_id += 1;
        let editor = OpenEditor::of_model(model, self.editors[index].preview, self.next_model_id);
        let (old, new) = (self.editors[index].model_id, editor.model_id);
        let shown = self.recent.first().copied();
        self.recent.iter_mut().filter(|id| **id == old).for_each(|id| *id = new);
        self.swap_editor(index, editor);
        self.shown_model_changed(shown);
    }

    fn swap_editor(&mut self, index: usize, mut editor: OpenEditor) {
        editor.sticky = self.editors[index].sticky;
        self.restore_view_state(&mut editor);
        let replaced = std::mem::replace(&mut self.editors[index], editor);
        self.recent.retain(|id| *id != replaced.model_id);
        self.remove_input(&replaced.model_id);
        self.keep_or_dispose(replaced);
    }

    /// Creates a text model with no editor, as `$tryOpenDocument` does; returns its index in `models`.
    pub fn open_model(&mut self, document: TextDocument) -> usize {
        let model = self.create_model(document, false);
        self.models.push(model);
        self.models.len() - 1
    }

    /// `ModelService.createModel`, with the undo stack a closed model of the same text left; an editor of the
    /// model another group shows when there is one.
    fn create_model(&mut self, document: TextDocument, preview: bool) -> OpenEditor {
        self.next_model_id += 1;
        if let Some(open) = self.all_editors().find(|editor| editor.document().is(&document.scheme, &document.path)) {
            return OpenEditor::of_model(open.model.clone(), preview, self.next_model_id);
        }
        let mut model = OpenEditor::new(document, preview, self.next_model_id, self.undo_redo.clone());
        self.disposed.restore(&mut model);
        model
    }

    /// A model whose editor went: its file's view state is kept; a model something else references stays, else it
    /// is disposed.
    fn keep_or_dispose(&mut self, model: OpenEditor) {
        self.save_view_state(&model);
        // A diff's side editors hold the model too, but the model stays in `models` for `model` to find.
        let side_editors = self.diffs.values().flat_map(|diff| [&diff.original_editor, &diff.modified_editor]).flatten().chain(self.multi_diffs.values().flat_map(MultiDiffInput::editors)).chain(self.merges.values().flat_map(|merge| &merge.input_editors).flatten()).chain(self.peek_editors.iter().map(|peek| &peek.editor));
        let diff_editors = side_editors.filter(|editor| Rc::ptr_eq(&editor.model, &model.model)).count();
        if Rc::strong_count(&model.model) - diff_editors > 1 {
            return;
        }
        if self.is_referenced(&model, Instant::now()) {
            self.models.push(model);
        } else {
            self.disposed.insert(model);
        }
    }

    /// The model of `scheme`/`path`, in an editor of any group or not; the active group's editor first.
    pub fn model(&self, scheme: &str, path: &Path) -> Option<&OpenEditor> {
        self.all_editors().find(|editor| editor.document().is(scheme, path))
    }

    pub fn model_mut(&mut self, scheme: &str, path: &Path) -> Option<&mut OpenEditor> {
        self.all_editors_mut().find(|editor| editor.document().is(scheme, path))
    }

    /// The tabs of every group, the active group's first; `editors` holds the active group's alone.
    pub fn all_tabs(&self) -> impl Iterator<Item = &OpenEditor> {
        self.editors.iter().chain(self.other_groups.iter().flat_map(|group| &group.editors))
    }

    pub fn all_tabs_mut(&mut self) -> impl Iterator<Item = &mut OpenEditor> {
        self.editors.iter_mut().chain(self.other_groups.iter_mut().flat_map(|group| &mut group.editors))
    }

    /// The tabs of every group and the models without a tab, in the same order as `all_editors_mut`.
    pub fn all_editors(&self) -> impl Iterator<Item = &OpenEditor> {
        self.editors.iter().chain(&self.models).chain(self.other_groups.iter().flat_map(|group| &group.editors))
    }

    pub fn all_editors_mut(&mut self) -> impl Iterator<Item = &mut OpenEditor> {
        self.editors.iter_mut().chain(&mut self.models).chain(self.other_groups.iter_mut().flat_map(|group| &mut group.editors))
    }

    /// The editor groups, most recently active first; the panel's group is not one.
    pub fn group_ids_mru(&self) -> Vec<u32> {
        std::iter::once(self.id).chain(self.group_mru.iter().copied()).filter(|id| !is_panel_group(*id) && !self.is_side_group(*id)).collect()
    }

    /// Whether `id` is the group of a side by side editor's secondary side.
    pub fn is_side_group(&self, id: u32) -> bool {
        self.side_by_sides.values().any(|group| *group == id)
    }

    /// The secondary side's group of the active side by side editor.
    pub fn side_group(&self) -> Option<u32> {
        self.side_by_sides.get(&self.active_tab()?.model_id).copied()
    }

    /// The group whose side by side editor shows the secondary group `side`.
    pub fn side_group_owner(&self, side: u32) -> Option<u32> {
        let (entry, _) = self.side_by_sides.iter().find(|(_, group)| **group == side)?;
        let owns = |editors: &[OpenEditor]| editors.iter().any(|editor| editor.model_id == *entry);
        if owns(&self.editors) {
            return Some(self.id);
        }
        self.other_groups.iter().find(|group| owns(&group.editors)).map(|group| group.id)
    }

    /// A new group outside the grid for the secondary side of the side by side editor `entry` of the active group.
    pub fn add_side_group(&mut self, entry: u32) -> u32 {
        self.next_group_id += 1;
        let id = self.next_group_id;
        self.other_groups.push(GroupState { id, editors: Vec::new(), recent: Vec::new(), viewport: self.viewport, unicode_banner_closed: false, diff_part_width: Rc::default() });
        self.side_by_sides.insert(entry, id);
        id
    }

    pub fn group(&self, id: u32) -> Option<&GroupState> {
        self.other_groups.iter().find(|group| group.id == id)
    }

    /// A group's editors, the most recently active first.
    pub fn group_editors_by_recency(&self, id: u32) -> Vec<&OpenEditor> {
        let (editors, recent) = match self.group(id) {
            Some(group) => (&group.editors, &group.recent),
            None => (&self.editors, &self.recent),
        };
        recent.iter().filter_map(|model| editors.iter().find(|editor| editor.model_id == *model)).collect()
    }

    pub fn column_ids(&self) -> Vec<u32> {
        if self.columns.is_empty() { vec![self.id] } else { self.columns.clone() }
    }

    /// A group's editors and the index of its active one.
    pub fn group_tabs(&self, id: u32) -> (&[OpenEditor], Option<usize>) {
        match self.group(id) {
            Some(group) => (&group.editors, group.active_index()),
            None => (&self.editors, self.active_index()),
        }
    }

    /// The model a group's code editor edits, as `active` answers for the active group, and its viewport.
    pub fn group_active(&self, id: u32) -> Option<(&OpenEditor, Viewport)> {
        let Some(group) = self.group(id) else { return self.pane_editor().map(|editor| (editor, self.viewport)) };
        let tab = &group.editors[group.active_index()?];
        let editor = self.code_editor_of(tab)?;
        Some((editor, group.viewport))
    }

    /// Makes group `id` the active one (`activateGroup`); its editor follows what other groups did to its model.
    pub fn activate_group(&mut self, id: u32) {
        if id == self.id {
            return;
        }
        let index = self.other_groups.iter().position(|group| group.id == id).expect("activating an open group");
        let mut group = self.other_groups.remove(index);
        let previous = self.id;
        std::mem::swap(&mut self.id, &mut group.id);
        std::mem::swap(&mut self.editors, &mut group.editors);
        std::mem::swap(&mut self.recent, &mut group.recent);
        std::mem::swap(&mut self.viewport, &mut group.viewport);
        std::mem::swap(&mut self.unicode_banner_closed, &mut group.unicode_banner_closed);
        std::mem::swap(&mut self.diff_part_width, &mut group.diff_part_width);
        self.other_groups.push(group);
        self.group_mru.retain(|group| *group != id);
        self.group_mru.insert(0, previous);
        if let Some(editor) = self.active_mut() {
            editor.sync_with_model();
        }
    }

    /// Whether the part has more than one editor group.
    pub fn has_several_groups(&self) -> bool {
        std::iter::once(self.id).chain(self.other_groups.iter().map(|group| group.id)).filter(|id| !is_panel_group(*id) && *id != MODAL_GROUP && !self.is_side_group(*id)).count() > 1
    }

    /// The panel's group, made on first use.
    pub fn ensure_panel_group(&mut self) {
        self.ensure_detached_group(PANEL_GROUP);
    }

    /// A group outside the grid (the panel's or the modal part's), made on first use.
    pub fn ensure_detached_group(&mut self, id: u32) {
        if self.id != id && self.group(id).is_none() {
            self.other_groups.push(GroupState { id, editors: Vec::new(), recent: Vec::new(), viewport: self.viewport, unicode_banner_closed: false, diff_part_width: Rc::default() });
        }
    }

    /// `addGroup`; with `copy_active` the new group shows the active editor as `splitEditor` copies it: an editor
    /// of the same model with the same view state, pinned. The new group is not activated; answers its id.
    pub fn add_group(&mut self, copy_active: bool) -> u32 {
        self.next_group_id += 1;
        let id = self.next_group_id;
        let mut group = GroupState { id, editors: Vec::new(), recent: Vec::new(), viewport: self.viewport, unicode_banner_closed: false, diff_part_width: Rc::default() };
        if let Some(copy) = copy_active.then(|| self.copy_active()).flatten() {
            let source = self.active_tab().expect("copied from the active tab").model_id;
            self.copy_side_by_side(source, copy.model_id);
            group.recent.push(copy.model_id);
            group.editors.push(copy);
            self.opened_first_editor = Some(id);
        }
        self.other_groups.push(group);
        id
    }

    /// A copy of the side by side editor `source` is one too (`SideBySideEditorInput` opens as is in another group):
    /// `copy` gets a secondary group of its own with a copy of the secondary side's editor.
    fn copy_side_by_side(&mut self, source: u32, copy: u32) {
        let Some(side) = self.side_by_sides.get(&source).copied() else { return };
        let (editors, active) = self.group_tabs(side);
        let secondary = &editors[active.expect("a secondary side shows its editor")];
        let (model, view_state) = (secondary.model.clone(), secondary.view_state(self.viewport));
        self.next_model_id += 1;
        let mut secondary = OpenEditor::of_model(model, false, self.next_model_id);
        secondary.restore_view_state(&view_state, self.viewport);
        let side = self.add_side_group(copy);
        if self.plain_side_by_sides.contains(&source) {
            self.plain_side_by_sides.insert(copy);
        }
        let group = self.other_groups.iter_mut().find(|group| group.id == side).expect("just added");
        group.recent.push(secondary.model_id);
        group.editors.push(secondary);
    }

    /// A copy of the active editor for another group, as `splitEditor` copies it: an editor of the same model with
    /// the same view state, not a preview.
    pub fn copy_active(&mut self) -> Option<OpenEditor> {
        let tab = self.active_tab()?;
        let (model, view_state, tab_id) = (tab.model.clone(), tab.view_state(self.viewport), tab.model_id);
        self.next_model_id += 1;
        let mut copy = OpenEditor::of_model(model, false, self.next_model_id);
        copy.restore_view_state(&view_state, self.viewport);
        let diff = self.diffs.get(&tab_id).map(|diff| {
            let view_state = |editor: &Option<OpenEditor>| editor.as_ref().map(|editor| editor.view_state(self.viewport));
            (DiffInput { inline_now: diff.inline_now, custom: diff.custom.clone(), ..DiffInput::new(diff.original.clone(), diff.modified.clone(), diff.label.clone()) }, view_state(&diff.original_editor), view_state(&diff.modified_editor))
        });
        if let Some((mut input, original_state, modified_state)) = diff {
            self.attach_side_editors(&mut input);
            for (editor, view_state) in [(input.original_editor.as_mut(), original_state), (input.modified_editor.as_mut(), modified_state)] {
                if let (Some(editor), Some(view_state)) = (editor, view_state) {
                    editor.restore_view_state(&view_state, self.viewport);
                }
            }
            self.diffs.insert(copy.model_id, input);
        }
        Some(copy)
    }

    /// `removeGroup` of a group other than the active one: its editors close.
    pub fn remove_group(&mut self, id: u32) {
        let index = self.other_groups.iter().position(|group| group.id == id).expect("removing an open group");
        let group = self.other_groups.remove(index);
        self.group_mru.retain(|group| *group != id);
        for editor in group.editors {
            self.remove_input(&editor.model_id);
            self.keep_or_dispose(editor);
        }
    }

    /// Takes the active editor at `index` out of the active group to move it to another one (`moveEditor`).
    pub fn take_editor(&mut self, index: usize) -> OpenEditor {
        let editor = self.editors.remove(index);
        let shown = self.recent.first().copied();
        self.recent.retain(|id| *id != editor.model_id);
        self.shown_model_changed(shown);
        editor
    }

    /// Opens a moved editor in the active group after its active editor, or activates the group's editor of the
    /// same resource in its place (`doMoveEditorAcrossGroups`).
    pub fn insert_moved(&mut self, editor: OpenEditor) {
        let existing = self.editors.iter().position(|open| Rc::ptr_eq(&open.model, &editor.model) && !self.diffs.contains_key(&open.model_id));
        match existing.filter(|_| !self.diffs.contains_key(&editor.model_id)) {
            Some(index) => {
                let index = if editor.sticky { self.stick(index) } else { index };
                self.activate(index);
                self.remove_input(&editor.model_id);
                self.keep_or_dispose(editor);
            }
            None => self.insert(editor, false),
        }
    }

    /// Every editor's cursors follow the edits other editors of its model made (render calls it each frame). Returns
    /// the model of an editor whose folding regions now wait for the update Delayer.
    pub fn sync_editors(&mut self) -> Option<(u32, SharedModel)> {
        let mut stale = None;
        for editor in self.synced_editors_mut() {
            let seen = editor.seen_version;
            editor.sync_with_model();
            if editor.folding_stale && editor.seen_version != seen {
                stale = Some((editor.model_id, editor.model.clone()));
            }
        }
        stale
    }

    /// The update Delayer of the editors `sync_editors` left stale: their indentation regions are computed again.
    pub fn refresh_stale_folding(&mut self) {
        for editor in self.synced_editors_mut().filter(|editor| editor.folding_stale) {
            editor.refresh_folding_ranges();
        }
    }

    fn synced_editors_mut(&mut self) -> impl Iterator<Item = &mut OpenEditor> {
        let others = self.other_groups.iter_mut().flat_map(|group| &mut group.editors);
        let diff_editors = self.diffs.values_mut().flat_map(|diff| [diff.original_editor.as_mut(), diff.modified_editor.as_mut()]).flatten();
        let file_editors = self.multi_diffs.values_mut().flat_map(MultiDiffInput::editors_mut);
        self.editors.iter_mut().chain(others).chain(diff_editors).chain(file_editors)
    }

    /// Shows the model at `index` of `models` in a new editor.
    pub fn show_model(&mut self, index: usize, preview: bool) {
        let mut editor = self.models.remove(index);
        editor.preview = preview;
        self.insert(editor, preview);
    }

    /// Drops the models without an editor that nothing references any more.
    #[cfg(test)]
    pub fn release_models(&mut self, now: Instant) {
        self.release_models_except(now, |_| false);
    }

    /// `release_models`, keeping the models the workbench `hold`s too (the SCM input boxes'). Returns when the next
    /// host reference expires.
    pub fn release_models_except(&mut self, now: Instant, hold: impl Fn(&OpenEditor) -> bool) -> Option<Instant> {
        let next_expiry = self.bound_host_references(now);
        let (kept, released) = std::mem::take(&mut self.models).into_iter().partition(|model| hold(model) || self.is_referenced(model, now));
        self.models = kept;
        for model in released.into_iter().filter(|model| Rc::strong_count(&model.model) == 1) {
            self.disposed.insert(model);
        }
        next_expiry
    }

    /// `BoundModelReferenceCollection._cleanup`: from 60 host references the oldest go down to 50, and the oldest go
    /// while the referenced text is over 80 MB. Returns when the next one left expires.
    fn bound_host_references(&mut self, now: Instant) -> Option<Instant> {
        let mut referenced: Vec<Rc<RefCell<TextModel>>> = Vec::new();
        for editor in self.all_editors() {
            let live = editor.text_model().host_reference.is_some_and(|opened| now.duration_since(opened) < HOST_REFERENCE_AGE);
            if live && !referenced.iter().any(|model| Rc::ptr_eq(model, &editor.model)) {
                referenced.push(editor.model.clone());
            }
        }
        referenced.sort_by_key(|model| std::cmp::Reverse(model.borrow().host_reference));
        let keep = if referenced.len() >= HOST_REFERENCE_MAX_COUNT * 6 / 5 { HOST_REFERENCE_MAX_COUNT } else { referenced.len() };
        let mut length = 0;
        let mut next_expiry: Option<Instant> = None;
        for (index, model) in referenced.iter().enumerate() {
            let mut model = model.borrow_mut();
            length += model.host_reference_length;
            if index >= keep || length > HOST_REFERENCE_MAX_LENGTH {
                model.host_reference = None;
                continue;
            }
            let expiry = model.host_reference.expect("live above") + HOST_REFERENCE_AGE;
            next_expiry = Some(next_expiry.map_or(expiry, |next| next.min(expiry)));
        }
        next_expiry
    }

    /// textModelResolverService.ts `destroyReferencedObject`: a diff, multi diff or merge editor's side, a peek widget's
    /// editor or an unexpired host reference holds a model, and a dirty one waits for `canDispose`.
    fn is_referenced(&self, model: &OpenEditor, now: Instant) -> bool {
        let shows = |side: &crate::diff_editor::Side| model.document().is(&side.scheme, &side.path);
        let shown_in_diff = self.diffs.values().any(|diff| shows(&diff.original) || shows(&diff.modified))
            || self.multi_diffs.values().any(|input| input.shows(model))
            || self.merges.values().any(|merge| [&merge.base, &merge.input1.side, &merge.input2.side, &merge.result].into_iter().any(shows))
            || self.peek_editors.iter().any(|peek| Rc::ptr_eq(&peek.editor.model, &model.model));
        model.is_dirty() || shown_in_diff || model.text_model().host_reference.is_some_and(|opened| now.duration_since(opened) < HOST_REFERENCE_AGE)
    }

    /// Forgets the diff, multi diff, merge or side by side editor whose entry was `model_id`.
    fn remove_input(&mut self, model_id: &u32) {
        self.diffs.remove(model_id);
        self.multi_diffs.remove(model_id);
        self.merges.remove(model_id);
        self.remove_side_by_side(*model_id);
        self.stacked_editors.remove(model_id);
    }

    /// Makes the side by side editor `entry` its primary side's editor again: its secondary group closes.
    pub fn remove_side_by_side(&mut self, entry: u32) {
        self.plain_side_by_sides.remove(&entry);
        let Some(side) = self.side_by_sides.remove(&entry) else { return };
        let index = self.other_groups.iter().position(|group| group.id == side).expect("the secondary side is not the active group when its editor closes");
        let group = self.other_groups.remove(index);
        self.group_mru.retain(|id| *id != side);
        for editor in group.editors {
            self.keep_or_dispose(editor);
        }
    }

    fn insert(&mut self, mut editor: OpenEditor, preview: bool) {
        let preview = preview && !self.previews_disabled;
        if self.previews_disabled {
            editor.preview = false;
        }
        self.restore_view_state(&mut editor);
        let preview_index = self.editors.iter().position(|editor| editor.preview).filter(|_| preview);
        let index = match preview_index {
            Some(index) => {
                let replaced_id = self.editors[index].model_id;
                self.recent.retain(|id| *id != replaced_id);
                let replaced = std::mem::replace(&mut self.editors[index], editor);
                self.remove_input(&replaced.model_id);
                self.keep_or_dispose(replaced);
                index
            }
            None => {
                let index = self.open_index(editor.sticky);
                self.editors.insert(index, editor);
                index
            }
        };
        self.activate(index);
        if self.editors.len() == 1 {
            self.opened_first_editor = Some(self.id);
        }
    }

    /// `openEditor`: a new editor goes right of the active one and after the sticky ones, a sticky one last among them.
    fn open_index(&self, sticky: bool) -> usize {
        let sticky_count = self.sticky_count();
        let after_active = self.active_index().map_or(self.editors.len(), |active| active + 1);
        if sticky { after_active.min(sticky_count) } else { after_active.max(sticky_count) }
    }

    /// Opens an editor of the open model `scheme`/`path` pinned with `inactive: true`: right after the most recent
    /// editor in the MRU, active only in an empty group (editorGroupModel.ts `openEditor`).
    pub fn open_inactive(&mut self, scheme: &str, path: &Path) {
        let model = self.model(scheme, path).expect("an open model").model.clone();
        self.next_model_id += 1;
        let mut editor = OpenEditor::of_model(model, false, self.next_model_id);
        self.restore_view_state(&mut editor);
        let index = self.open_index(false);
        let id = editor.model_id;
        self.editors.insert(index, editor);
        if self.recent.is_empty() {
            self.activate(index);
            self.opened_first_editor = Some(self.id);
        } else {
            self.recent.insert(1, id);
        }
    }

    pub fn activate(&mut self, index: usize) {
        let id = self.editors[index].model_id;
        let shown = self.recent.first().copied();
        self.recent.retain(|recent| *recent != id);
        self.recent.insert(0, id);
        self.shown_model_changed(shown);
    }

    /// The group's code editor shows another model (`onDidChangeModel`).
    fn shown_model_changed(&mut self, previous: Option<u32>) {
        if self.recent.first().copied() == previous {
            return;
        }
        self.unicode_banner_closed = false;
        // codeEditorWidget.ts `_detachModel` removes the editor's decorations, the anchor too.
        for editor in &mut self.editors {
            editor.selection_anchor = None;
        }
        if let Some(editor) = self.active_mut() {
            editor.clear_cursor_states();
        }
    }

    /// `updateEditorViewState` of a file editor (`tracksEditorViewState`: `FileEditorInput` only).
    pub fn save_view_state(&mut self, editor: &OpenEditor) {
        if on_disk(&editor.document().scheme) {
            self.view_states.save(&editor.document().uri_string(), editor.view_state(self.viewport));
        }
    }

    /// `loadEditorViewState` for an editor new in the group: not with `workbench.editor.restoreViewState` off.
    fn restore_view_state(&mut self, editor: &mut OpenEditor) {
        if !on_disk(&editor.document().scheme) || !self.disposed.setting_enabled("workbench.editor.restoreViewState", &editor.document().path) {
            return;
        }
        let uri = editor.document().uri_string();
        if let Some(state) = self.view_states.load(&uri) {
            editor.restore_view_state(&state, self.viewport);
        }
    }

    pub fn pin(&mut self, index: usize) {
        self.editors[index].preview = false;
    }

    /// editorGroupView.ts `onDidChangePartOptions`: turning previews off pins every group's preview editor.
    pub fn set_previews_disabled(&mut self, disabled: bool) {
        self.previews_disabled = disabled;
        if disabled {
            for editor in self.all_tabs_mut() {
                editor.preview = false;
            }
        }
    }

    pub fn sticky_count(&self) -> usize {
        self.editors.iter().take_while(|editor| editor.sticky).count()
    }

    /// editorGroupModel.ts `doStick`: the editor is pinned and becomes the last sticky one; answers its index.
    pub fn stick(&mut self, index: usize) -> usize {
        if self.editors[index].sticky {
            return index;
        }
        self.pin(index);
        let to = self.sticky_count();
        let editor = self.editors.remove(index);
        self.editors.insert(to, editor);
        self.editors[to].sticky = true;
        to
    }

    /// editorGroupModel.ts `doUnstick`: the editor becomes the first non-sticky one; answers its index.
    pub fn unstick(&mut self, index: usize) -> usize {
        if !self.editors[index].sticky {
            return index;
        }
        let to = self.sticky_count() - 1;
        let editor = self.editors.remove(index);
        self.editors.insert(to, editor);
        self.editors[to].sticky = false;
        to
    }

    /// editorGroupModel.ts `moveEditor` then `pin`, as a move inside the group does; `to` is clamped to the tabs.
    /// A move into the sticky range makes the editor sticky, a move out of it unsticks it.
    pub fn move_editor(&mut self, from: usize, to: usize) {
        let to = to.min(self.editors.len() - 1);
        if from == to {
            return;
        }
        let sticky = to < self.sticky_count();
        let mut editor = self.editors.remove(from);
        editor.sticky = sticky;
        self.editors.insert(to, editor);
        self.pin(to);
    }

    pub fn close(&mut self, index: usize) {
        let closed = self.editors.remove(index);
        if on_disk(&closed.document().scheme) && !self.diffs.contains_key(&closed.model_id) {
            let side = self.side_by_sides.get(&closed.model_id).map(|side| self.group_tabs(*side));
            let secondary = side.and_then(|(editors, active)| Some(editors[active?].document().path.clone()));
            self.closed.push(ClosedEditor { group: self.id, scheme: closed.document().scheme.clone(), path: closed.document().path.clone(), index, secondary });
        }
        self.remove_input(&closed.model_id);
        let shown = self.recent.first().copied();
        self.recent.retain(|id| *id != closed.model_id);
        self.shown_model_changed(shown);
        self.keep_or_dispose(closed);
        self.closed_last_editor = self.editors.is_empty();
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn lines(text: &str) -> Vec<String> {
        split_lines(text).0
    }

    fn editor_in(text: &str, language_id: &str) -> OpenEditor {
        let mut editor = edit::tests::editor(text, "\n");
        editor.document_mut().language_id = language_id.to_string();
        editor
    }

    #[test]
    fn the_word_at_a_column_takes_numbers_with_their_fraction() {
        // model.test.ts 'Get word at position', columns from 0.
        let languages = Languages::new(&[]);
        let word_at = |text: &str, column: usize| editor_in(text, "plaintext").document().word_at(&languages, 0, column);
        let text = "This text has some  words. ";
        assert_eq!(word_at(text, 0), Some(0..4));
        assert_eq!(word_at(text, 4), Some(0..4));
        assert_eq!(word_at(text, 5), Some(5..9));
        assert_eq!(word_at(text, 18), Some(14..18));
        assert_eq!(word_at(text, 19), None);
        assert_eq!(word_at(text, 25), Some(20..25));
        assert_eq!(word_at(text, 26), None);
        assert_eq!(word_at(text, 27), None);
        // The number branch of DEFAULT_WORD_REGEXP, `-?\d*\.\d\w*`.
        assert_eq!(word_at("x = 3.14;", 5), Some(4..8));
        assert_eq!(word_at("x = -1.5e3", 6), Some(4..10));
        assert_eq!(word_at("a.5", 2), Some(1..3));
        assert_eq!(word_at("ab.5", 2), Some(0..2));
        assert_eq!(word_at("1.x", 1), Some(0..1));
    }

    #[test]
    fn the_word_at_a_column_takes_the_word_pattern_of_the_language() {
        // model.test.ts 'issue #61296: VS code freezes when editing CSS file with emoji', columns in chars from 0;
        // `[\w\-?]` for upstream's `[\w-?]`, which Oniguruma rejects.
        let mut languages = Languages::new(&[]);
        languages.configure("testMode", &serde_json::json!({ "wordPattern": r"(#?-?\d*\.\d\w*%?)|(::?[\w-]*(?=[^,{;]*[,{]))|(([@#.!])?[\w\-?]+%?|[@#!.])" }));
        let editor = editor_in(".🐷-a-b", "testMode");
        let document = editor.document();
        assert_eq!(document.word_at(&languages, 0, 0), Some(0..1));
        assert_eq!(document.word_at(&languages, 0, 1), Some(0..1));
        for column in 2..=6 {
            assert_eq!(document.word_at(&languages, 0, column), Some(2..6), "column {column}");
        }
    }

    #[test]
    fn the_word_at_a_column_stays_inside_its_embedded_language() {
        // model.test.ts 'getWordAtPosition at embedded language boundaries': each `x` is the inner language.
        let folder = std::env::temp_dir().join(format!("code-gpui-word-at-{}", std::process::id()));
        std::fs::create_dir_all(&folder).unwrap();
        std::fs::write(folder.join("grammar.json"), r#"{ "scopeName": "source.outer", "patterns": [{ "match": "x+", "name": "meta.embedded.inner" }] }"#).unwrap();
        let package = serde_json::json!({ "contributes": { "grammars": [{ "language": "outerMode", "scopeName": "source.outer", "path": "grammar.json", "embeddedLanguages": { "meta.embedded.inner": "innerMode" } }] } });
        let registry = textmate::Registry::new([(folder.as_path(), &package)]);
        let mut languages = Languages::new(&[]);
        // Without the language boundaries this pattern would take the whole line.
        languages.configure("outerMode", &serde_json::json!({ "wordPattern": r"\S+" }));
        let mut editor = editor_in("ab<xx>ab<x>", "outerMode");
        editor.document_mut().tokens = Tokenization::new(registry.grammar("outerMode"));
        let document = editor.document();
        let words: Vec<_> = (0..=6).map(|column| document.word_at(&languages, 0, column)).collect();
        assert_eq!(words, [Some(0..3), Some(0..3), Some(0..3), Some(3..5), Some(3..5), Some(5..9), Some(5..9)]);
        std::fs::remove_dir_all(folder).unwrap();
    }

    #[test]
    fn long_lines_stop_rendering_after_the_limit() {
        // viewLineRenderer.test.ts 'overflow': "Hello " then "Show more (6 chars)".
        assert_eq!(line_overflow("Hello world!", Some(6)), Some((6, 6, "Show more (6 chars)".to_string())));
        assert_eq!(line_overflow("\u{e9}\u{e9}\u{e9}", Some(2)).map(|(limit, byte, _)| (limit, byte)), Some((2, 4)));
        assert_eq!(line_overflow("abcd", Some(4)), None);
        assert_eq!(line_overflow("\u{e9}\u{e9}", Some(3)), None);
        assert_eq!(line_overflow("abcdef", None), None);
        assert_eq!(overflowing_char_count(1023), "1023 chars");
        assert_eq!(overflowing_char_count(1024), "1.0 KB");
        assert_eq!(overflowing_char_count(1_572_864), "1.5 MB");
    }

    #[test]
    fn opened_files_take_the_configured_indentation() {
        let dir = std::env::temp_dir().join(format!("code-gpui-indentation-{}", std::process::id()));
        std::fs::create_dir_all(dir.join("User")).unwrap();
        let file = dir.join("a.txt");
        std::fs::write(&file, "a\n    b\n        c\n").unwrap();
        let read = |user_settings: &str| {
            std::fs::write(dir.join("User/settings.json"), user_settings).unwrap();
            let settings = Settings::load(config::registry::Registry::core(), dir.join("User"), None);
            TextDocument::read(&file, &Languages::new(&[]), &settings).unwrap().indentation
        };
        assert_eq!(read("{}"), Indentation { tab_size: 4, indent_size: None, insert_spaces: true });
        assert_eq!(read(r#"{ "editor.tabSize": 2, "editor.detectIndentation": false }"#), Indentation { tab_size: 2, indent_size: None, insert_spaces: true });
        assert_eq!(read(r#"{ "editor.insertSpaces": false, "editor.detectIndentation": false }"#), Indentation { tab_size: 4, indent_size: None, insert_spaces: false });
        std::fs::remove_dir_all(&dir).unwrap();
    }

    #[test]
    fn splits_lines_and_picks_the_eol_of_most_breaks() {
        assert_eq!(split_lines("a\nb\n"), (vec!["a".into(), "b".into(), "".into()], Some("\n")));
        assert_eq!(split_lines("a\r\nb\r\nc\n"), (vec!["a".into(), "b".into(), "c".into(), "".into()], Some("\r\n")));
        assert_eq!(split_lines("a\rb"), (vec!["a".into(), "b".into()], Some("\r\n")));
        assert_eq!(split_lines("one line"), (vec!["one line".into()], None));
    }

    #[test]
    fn a_text_without_line_breaks_takes_the_languages_files_eol() {
        let dir = std::env::temp_dir().join(format!("code-gpui-eol-{}", std::process::id()));
        std::fs::create_dir_all(dir.join("User")).unwrap();
        std::fs::write(dir.join("User/settings.json"), r#"{ "files.eol": "\r\n", "[shellscript]": { "files.eol": "\n" } }"#).unwrap();
        let settings = Settings::load(config::registry::Registry::core(), dir.join("User"), None);
        let eol = |text: &str, language: &str| TextDocument::untitled(Path::new("Untitled-1"), text, Some(language), &Languages::new(&[]), &settings).eol;
        assert_eq!(eol("", "plaintext"), "\r\n");
        assert_eq!(eol("", "shellscript"), "\n");
        assert_eq!(eol("a\nb", "plaintext"), "\n");
        std::fs::remove_dir_all(&dir).unwrap();
    }

    #[test]
    fn a_non_file_uri_keeps_its_authority() {
        let settings = Settings::load(config::registry::Registry::core(), std::env::temp_dir().join("code-gpui-no-user"), None);
        let uri = |scheme: &str, path: &str| {
            let document = TextDocument::from_bytes(scheme, Path::new(path), b"", None, &Languages::new(&[]), &settings);
            (document.uri()["authority"].clone(), document.uri()["path"].clone(), document.uri_string())
        };
        assert_eq!(uri("foo", "//testing/virtual.js"), (serde_json::json!("testing"), serde_json::json!("/virtual.js"), "foo://testing/virtual.js".to_string()));
        assert_eq!(uri("sc", "//auth"), (serde_json::json!("auth"), serde_json::json!(""), "sc://auth".to_string()));
        if cfg!(windows) {
            assert_eq!(uri("file", "//w/a.ts"), (serde_json::json!("w"), serde_json::json!("/a.ts"), "file://w/a.ts".to_string()));
        } else {
            assert_eq!(uri("file", "//w/a.ts"), (serde_json::json!(""), serde_json::json!("//w/a.ts"), "file:////w/a.ts".to_string()));
        }
    }

    #[test]
    fn guesses_indentation_like_upstream() {
        let guess = |text: &str| guess_indentation(&lines(text), DEFAULT_INDENTATION);
        let spaces = |tab_size| Indentation { tab_size, indent_size: None, insert_spaces: true };
        let tabs = Indentation { tab_size: 4, indent_size: None, insert_spaces: false };
        assert_eq!(guess("x\n  x\n    x\n  x\n"), spaces(2));
        assert_eq!(guess("x\n    x\n        x\n"), spaces(4));
        assert_eq!(guess("x\n\tx\n\t\tx\n"), tabs);
        assert_eq!(guess("x\n\tx\n    x\n"), spaces(4));
        assert_eq!(guess("x\n   x\n      x\n"), spaces(3));
        assert_eq!(guess("x\n\nno indentation\n"), spaces(4));
        // A line after a comma that lines up with the text above it is alignment, not a 6-space indent.
        assert_eq!(guess("const a = 1,\n      b = 2;\n"), spaces(4));
    }

    #[test]
    fn blank_lines_take_the_indent_guides_of_their_block() {
        let text = lines("a\n\tb\n\n\tc\nd\n\n");
        assert_eq!(indent_guides(&text, 4, 4, false), [0, 1, 1, 1, 0, 0, 0]);
        let python = lines("def f():\n    x\n\ny\n");
        assert_eq!(indent_guides(&python, 4, 4, false), [0, 1, 1, 0, 0]);
        assert_eq!(indent_guides(&python, 4, 4, true), [0, 1, 0, 0, 0]);
    }

    #[test]
    fn the_active_indent_guide_is_the_block_at_the_cursor_or_the_one_it_opens_or_closes() {
        // a {  b  c {  d  e  }  }  x
        let levels = [0, 1, 1, 2, 2, 1, 0, 0];
        assert_eq!(active_indent_guide(&levels, 0), (1..6, 1));
        assert_eq!(active_indent_guide(&levels, 2), (3..5, 2));
        assert_eq!(active_indent_guide(&levels, 3), (3..5, 2));
        assert_eq!(active_indent_guide(&levels, 1), (1..6, 1));
        assert_eq!(active_indent_guide(&levels, 5), (3..5, 2));
        assert_eq!(active_indent_guide(&levels, 6), (1..6, 1));
        assert_eq!(active_indent_guide(&levels, 7), (7..8, 0));
    }

    #[test]
    fn finds_whole_word_occurrences() {
        let text = lines("Port port Port.\nexport Port");
        assert_eq!(word_occurrences(&text, "Port"), [(0, 0..4), (0, 10..14), (1, 7..11)]);
    }

    #[test]
    fn maps_columns_through_tabs() {
        assert_eq!(expand_tabs("\ta\tb", 4), "    a   b");
        assert_eq!(visible_column("\ta\tb", 3, 4), 8);
        assert_eq!(column_at_visible("\ta", 1.9, 4), 0);
        assert_eq!(column_at_visible("\ta", 2.1, 4), 1);
        assert_eq!(column_at_visible("\ta", 9., 4), 2);
    }

    /// viewLineRenderer.ts `_renderLine`: a full-width char takes two visible columns for the tab stops and is drawn
    /// at the font's full-width advance.
    #[test]
    fn full_width_chars_take_two_columns_and_their_advance() {
        assert_eq!(expand_tabs("猫\ta", 4), "猫  a");
        assert_eq!(visible_column("猫\ta", 2, 4), 4);
        let full_width = 1.5;
        set_full_width_columns(full_width);
        assert_eq!(column_x("猫\ta", 2, 4), full_width + 2.);
        assert_eq!(column_at_visible("猫a", full_width + 0.1, 4), 1);
        assert_eq!(injected_text::LineLayout::new("猫a", 4, []).x(1), full_width);
    }

    /// cursorMoveOperations.ts keeps `CursorColumns` visible columns, where a full-width char counts two.
    #[test]
    fn moving_down_keeps_the_visible_column_over_full_width_chars() {
        let text = lines("abc\n猫a");
        assert_eq!(cursor::vertical(&text, &[], Position::new(0, 2), 0, true, 4).0, Position::new(1, 1));
        assert_eq!(cursor::visible_column_from_column("猫a", 1, 4), 2);
    }

    #[test]
    fn previews_replace_each_other_and_closing_activates_the_recent_editor() {
        let document = |name: &str| TextDocument {
            scheme: "file".into(),
            path: PathBuf::from(name),
            lines: vec![String::new()],
            eol: "\n",
            encoding: "utf8".into(),
            language_id: "plaintext".into(),
            indentation: DEFAULT_INDENTATION,
            off_side: false,
            folding_markers: None,
            word_pattern: None,
            block_comment: None,
            tokens: Tokenization::default(),
            too_large_for_tokenization: false,
            open_error: None,
        };
        let names = |group: &EditorGroup| group.editors.iter().map(|editor| editor.document().name()).collect::<Vec<_>>();
        let mut group = EditorGroup::default();
        group.open(document("a"), false);
        group.open(document("b"), true);
        group.open(document("c"), true);
        assert_eq!(names(&group), ["a", "c"]);
        group.activate(0);
        group.open(document("d"), false);
        assert_eq!(names(&group), ["a", "d", "c"]);
        group.close(1);
        assert_eq!(group.active().unwrap().document().name(), "a");
    }

    #[test]
    fn sticky_editors_come_first_and_move_in_and_out_of_the_sticky_range() {
        // editorGroupModel.test.ts 'Sticky Editors'.
        let document = |name: &str| TextDocument {
            scheme: "file".into(),
            path: PathBuf::from(name),
            lines: vec![String::new()],
            eol: "\n",
            encoding: "utf8".into(),
            language_id: "plaintext".into(),
            indentation: DEFAULT_INDENTATION,
            off_side: false,
            folding_markers: None,
            word_pattern: None,
            block_comment: None,
            tokens: Tokenization::default(),
            too_large_for_tokenization: false,
            open_error: None,
        };
        // A sticky editor's name ends in `*`.
        let order = |group: &EditorGroup| group.editors.iter().map(|editor| format!("{}{}", editor.document().name(), if editor.sticky { "*" } else { "" })).collect::<Vec<_>>();
        let index = |group: &EditorGroup, name: &str| group.editors.iter().position(|editor| editor.document().name() == name).unwrap();
        let mut group = EditorGroup::default();
        group.open(document("1"), false);
        group.open(document("2"), false);
        group.open(document("3"), true);
        assert_eq!(group.sticky_count(), 0);
        group.stick(index(&group, "3"));
        assert_eq!(order(&group), ["3*", "1", "2"]);
        assert!(!group.editors[0].preview, "sticking pins");
        group.stick(0);
        assert_eq!(group.sticky_count(), 1, "sticking a sticky editor is a no-op");
        group.stick(index(&group, "2"));
        assert_eq!(order(&group), ["3*", "2*", "1"]);
        group.stick(index(&group, "1"));
        assert_eq!(group.sticky_count(), 3);
        group.unstick(index(&group, "3"));
        assert_eq!(order(&group), ["2*", "1*", "3"]);
        group.unstick(index(&group, "1"));
        group.unstick(index(&group, "2"));
        assert_eq!(group.sticky_count(), 0);
        group.move_editor(index(&group, "1"), 0);
        group.move_editor(index(&group, "2"), 1);
        group.move_editor(index(&group, "3"), 2);
        group.stick(0);
        group.stick(1);
        group.activate(0);
        group.open(document("4"), false);
        assert_eq!(index(&group, "4"), 2, "a new editor opens after the sticky ones");
        group.close(2);
        group.move_editor(index(&group, "1"), 1);
        assert_eq!(order(&group), ["2*", "1*", "3"], "moved within the sticky range");
        group.move_editor(index(&group, "1"), 2);
        assert_eq!(order(&group), ["2*", "3", "1"], "moved out of the sticky range");
        group.move_editor(index(&group, "1"), 0);
        assert_eq!(order(&group), ["1*", "2*", "3"], "moved into the sticky range");
        group.close(0);
        assert_eq!(group.sticky_count(), 1, "closing a sticky editor");
    }

    #[test]
    fn the_cursor_highlights_its_word_and_clamps_to_the_text() {
        let mut group = EditorGroup::default();
        let document = TextDocument {
            scheme: "file".into(),
            path: PathBuf::from("/w/a.txt"),
            lines: lines("Port notes\nport Port"),
            eol: "\n",
            encoding: "utf8".into(),
            language_id: "plaintext".into(),
            indentation: DEFAULT_INDENTATION,
            off_side: false,
            folding_markers: None,
            word_pattern: None,
            block_comment: None,
            tokens: Tokenization::default(),
            too_large_for_tokenization: false,
            open_error: None,
        };
        group.open(document, false);
        let editor = group.active_mut().unwrap();
        assert_eq!(editor.highlights, [(0, 0..4), (1, 5..9)]);
        editor.move_to(Position::new(0, 4), false);
        assert_eq!(editor.highlights.len(), 2);
        editor.move_to(Position::new(1, 4), false);
        assert_eq!(editor.highlights, [(1, 0..4)]);
        editor.move_to(Position::new(1, 4), true);
        editor.selections[0].anchor = Position::new(0, 0);
        editor.refresh_highlights();
        assert!(editor.highlights.is_empty());
        editor.selections[0] = Selection { anchor: Position::new(1, 0), head: Position::new(1, 4) };
        editor.refresh_highlights();
        assert_eq!(editor.highlights, [(1, 0..4)]);
        editor.selections[0].head = Position::new(1, 5);
        editor.refresh_highlights();
        assert!(editor.highlights.is_empty());
    }

    /// textualHighlightProvider.ts `getWordAtPosition`: the language's `wordPattern`, here one with `$` as a word char.
    #[test]
    fn the_word_highlight_takes_the_language_word_pattern() {
        let mut languages = Languages::new(&[]);
        languages.configure("dollar", &serde_json::json!({ "wordPattern": r#"([^`\~\!\@\#\%\^\&\*\(\)\-\=\+\[\{\]\}\\\|\;\:\'\"\,\.\<\>\/\?\s]+)"# }));
        let settings = crate::default_settings();
        let document = TextDocument::from_text("file", Path::new("/w/a.txt"), "let $a = $a;", "utf8".into(), Some("dollar"), &languages, &settings.lock().unwrap());
        let mut group = EditorGroup::default();
        group.open(document, false);
        let editor = group.active_mut().unwrap();
        editor.move_to(Position::new(0, 4), false);
        assert_eq!(editor.highlights, [(0, 4..6), (0, 9..11)]);
    }
}