Repository: microsoft/edit
File: src/bin/edit/documents.rs (Rust)

Code before:
            let mut tb = buffer.borrow_mut();
            tb.set_margin_enabled(true);
            tb.set_line_highlight_enabled(true);

            if let Some(file) = &mut file {
                tb.read_file(file, None)?;

                if let Some(goto) = goto
                    && goto != Default::default()
                {
                    tb.cursor_move_to_logical(goto);
                }
            }
        }

        let mut doc = Document {
            buffer,
            path: None,
            dir: None,
            filename: Default::default(),
            file_id,
            new_file_counter: 0,
        };
        doc.set_path(path);

        if self.list.len() == 1
            && let Some(current_doc) = self.list.front()
            && current_doc.path.is_none()
            && current_doc.file_id.is_none()
            && !current_doc.buffer.borrow().is_dirty()
        {
            // If there is only one document, and it has no filename, and it is not dirty,
            // replace it with the new document.
            self.list.clear();
        }

        self.list.push_front(doc);
        Ok(self.list.front_mut().unwrap())
    }

    pub fn open_for_reading(path: &Path) -> apperr::Result<File> {
        File::open(path).map_err(apperr::Error::from)
    }

    pub fn open_for_writing(path: &Path) -> apperr::Result<File> {
        File::create(path).map_err(apperr::Error::from)
    }

    // Parse a filename in the form of "filename:line:char".
    // Returns the position of the first colon and the line/char coordinates.
    fn parse_filename_goto(path: &Path) -> (&Path, Option<Point>) {

Review comment (suggestion):
I think we can skip the `.len()` check and just unconditionally check the most recent document. We could theoretically go even one step further and find any document that fulfills the condition... but maybe that's a step too for a first test.

Code after:
            let mut tb = buffer.borrow_mut();
            tb.set_margin_enabled(true);
            tb.set_line_highlight_enabled(true);

            if let Some(file) = &mut file {
                tb.read_file(file, None)?;

                if let Some(goto) = goto
                    && goto != Default::default()
                {
                    tb.cursor_move_to_logical(goto);
                }
            }
        }

        let mut doc = Document {
            buffer,
            path: None,
            dir: None,
            filename: Default::default(),
            file_id,
            new_file_counter: 0,
        };
        doc.set_path(path);

        if let Some(active) = self.active()
            && active.path.is_none()
            && active.file_id.is_none()
            && !active.buffer.borrow().is_dirty()
        {
            // If the current document is a pristine Untitled document with no
            // name and no ID, replace it with the new document.
            self.remove_active();
        }

        self.list.push_front(doc);
        Ok(self.list.front_mut().unwrap())
    }

    pub fn open_for_reading(path: &Path) -> apperr::Result<File> {
        File::open(path).map_err(apperr::Error::from)
    }

    pub fn open_for_writing(path: &Path) -> apperr::Result<File> {
        File::create(path).map_err(apperr::Error::from)
    }

    // Parse a filename in the form of "filename:line:char".
    // Returns the position of the first colon and the line/char coordinates.
    fn parse_filename_goto(path: &Path) -> (&Path, Option<Point>) {
        fn parse(s: &[u8]) -> Option<CoordType> {