Repository: typst/typst
File: crates/typst-syntax/src/lexer.rs (Rust)

Code before:

        // Add lines.
        for (i, line) in lines.enumerate() {
            let dedent = if i == 0 && !skipped { 0 } else { dedent };
            let offset: usize = line.chars().take(dedent).map(char::len_utf8).sum();
            self.s.eat_newline();
            self.s.advance(offset);
            self.push_raw(SyntaxKind::RawTrimmed);
            self.s.advance(line.len() - offset);
            self.push_raw(SyntaxKind::Text);
        }

        // Add final trimmed.
        if self.s.cursor() < end - backticks {
            self.s.jump(end - backticks);
            self.push_raw(SyntaxKind::RawTrimmed);
        }
        self.s.jump(end);
    }

    fn inline_raw(&mut self, start: usize, end: usize, backticks: usize) {
        self.s.jump(start + backticks);

        while self.s.cursor() < end - backticks {
            let c = self.s.peek().unwrap(); // Valid, as we are not at end of file.
            if is_newline(c) {
                self.push_raw(SyntaxKind::Text);
                self.s.eat_newline();
                self.push_raw(SyntaxKind::RawTrimmed);
                continue;
            }
            self.s.eat();
        }
        self.push_raw(SyntaxKind::Text);

        self.s.jump(end);
    }

    /// Push the current cursor that marks the end of a raw segment of
    /// the given `kind`.
    fn push_raw(&mut self, kind: SyntaxKind) {
        let end = self.s.cursor();
        self.raw.push((kind, end));
    }

    fn link(&mut self) -> SyntaxKind {
        let (link, balanced) = link_prefix(self.s.after());
        self.s.advance(link.len());

        if !balanced {
            return self.error(

Review comment (suggestion):
```suggestion
            if self.s.at(is_newline) {
```

Code after:

        // Add lines.
        for (i, line) in lines.enumerate() {
            let dedent = if i == 0 && !skipped { 0 } else { dedent };
            let offset: usize = line.chars().take(dedent).map(char::len_utf8).sum();
            self.s.eat_newline();
            self.s.advance(offset);
            self.push_raw(SyntaxKind::RawTrimmed);
            self.s.advance(line.len() - offset);
            self.push_raw(SyntaxKind::Text);
        }

        // Add final trimmed.
        if self.s.cursor() < end - backticks {
            self.s.jump(end - backticks);
            self.push_raw(SyntaxKind::RawTrimmed);
        }
        self.s.jump(end);
    }

    fn inline_raw(&mut self, start: usize, end: usize, backticks: usize) {
        self.s.jump(start + backticks);

        while self.s.cursor() < end - backticks {
            if self.s.at(is_newline) {
                self.push_raw(SyntaxKind::Text);
                self.s.eat_newline();
                self.push_raw(SyntaxKind::RawTrimmed);
                continue;
            }
            self.s.eat();
        }
        self.push_raw(SyntaxKind::Text);

        self.s.jump(end);
    }

    /// Push the current cursor that marks the end of a raw segment of
    /// the given `kind`.
    fn push_raw(&mut self, kind: SyntaxKind) {
        let end = self.s.cursor();
        self.raw.push((kind, end));
    }

    fn link(&mut self) -> SyntaxKind {
        let (link, balanced) = link_prefix(self.s.after());
        self.s.advance(link.len());

        if !balanced {
            return self.error(
                "automatic links cannot contain unbalanced brackets, \