Repository: helix-editor/helix
File: helix-view/src/document.rs (Rust)

Code before:
            diff_handle: None,
            config,
            version_control_head: None,
            focused_at: std::time::Instant::now(),
            readonly: false,
            jump_labels: HashMap::new(),
        }
    }

    pub fn default(config: Arc<dyn DynAccess<Config>>) -> Self {
        let line_ending: LineEnding = config.load().default_line_ending.into();
        let text = Rope::from(line_ending.as_str());
        Self::from(text, None, config)
    }

    // TODO: async fn?
    /// Create a new document from `path`. Encoding is auto-detected, but it can be manually
    /// overwritten with the `encoding` parameter.
    pub fn open(
        path: &Path,
        encoding: Option<&'static Encoding>,
        config_loader: Option<Arc<ArcSwap<syntax::Loader>>>,
        config: Arc<dyn DynAccess<Config>>,
    ) -> Result<Self, Error> {
        // if the path is not a regular file (e.g.: /dev/random) it should not be opened
        if path.exists() && !path.is_file() && !path.is_symlink() {
            return Err(anyhow::anyhow!(IrregularFileError {
                msg: format!("Path argument must be a regular file, a directory, or a symlink.")
            }));
        }

        // Open the file if it exists, otherwise assume it is a new file (and thus empty).
        let (rope, encoding, has_bom) = if path.exists() {
            let mut file =
                std::fs::File::open(path).context(format!("unable to open {:?}", path))?;
            from_reader(&mut file, encoding)?
        } else {
            let line_ending: LineEnding = config.load().default_line_ending.into();
            let encoding = encoding.unwrap_or(encoding::UTF_8);
            (Rope::from(line_ending.as_str()), encoding, false)
        };

        let mut doc = Self::from(rope, Some((encoding, has_bom)), config);

        // set the path and try detecting the language
        doc.set_path(Some(path));
        if let Some(loader) = config_loader {
            doc.detect_language(loader);
        }

        doc.detect_indent_and_line_ending();

Review comment (performance):
this is quite inefficient since it requires 3 syscalls, you can do it in one call with `path.metadata().map_or(false, |metadata| !metadata.is_file())`

Code after:
            config,
            version_control_head: None,
            focused_at: std::time::Instant::now(),
            readonly: false,
            jump_labels: HashMap::new(),
        }
    }

    pub fn default(config: Arc<dyn DynAccess<Config>>) -> Self {
        let line_ending: LineEnding = config.load().default_line_ending.into();
        let text = Rope::from(line_ending.as_str());
        Self::from(text, None, config)
    }

    // TODO: async fn?
    /// Create a new document from `path`. Encoding is auto-detected, but it can be manually
    /// overwritten with the `encoding` parameter.
    pub fn open(
        path: &Path,
        encoding: Option<&'static Encoding>,
        config_loader: Option<Arc<ArcSwap<syntax::Loader>>>,
        config: Arc<dyn DynAccess<Config>>,
    ) -> Result<Self, DocumentOpenError> {
        // If the path is not a regular file (e.g.: /dev/random) it should not be opened.
        if path
            .metadata()
            .map_or(false, |metadata| !metadata.is_file())
        {
            return Err(DocumentOpenError::IrregularFile);
        }

        // Open the file if it exists, otherwise assume it is a new file (and thus empty).
        let (rope, encoding, has_bom) = if path.exists() {
            let mut file = std::fs::File::open(path)?;
            from_reader(&mut file, encoding)?
        } else {
            let line_ending: LineEnding = config.load().default_line_ending.into();
            let encoding = encoding.unwrap_or(encoding::UTF_8);
            (Rope::from(line_ending.as_str()), encoding, false)
        };

        let mut doc = Self::from(rope, Some((encoding, has_bom)), config);

        // set the path and try detecting the language
        doc.set_path(Some(path));
        if let Some(loader) = config_loader {
            doc.detect_language(loader);
        }

        doc.detect_indent_and_line_ending();