Repository: lapce/lapce
File: lapce-ui/src/editor.rs (Rust)

Code before:
                            ),
                        );
                        text_height += text.size().height;
                    }
                }
            }
        }
    }

    /// Checks if the cursor is on a bracket and highlights the matching bracket if there is one.
    /// If the cursor is between brackets it highlights the enclosing brackets.
    fn paint_bracket_highlight(
        ctx: &mut PaintCtx,
        data: &LapceEditorBufferData,
        screen_lines: &ScreenLines,
    ) {
        if screen_lines.lines.is_empty() {
            return;
        }

        let cursor_offset = data.editor.cursor.offset();

        if let Some(char_at_cursor) = data.doc.buffer().char_at_offset(cursor_offset)
        {
            if is_bracket(char_at_cursor) {
                if let Some(syntax) = data.doc.syntax() {
                    if let Some(new_offset) =
                        syntax.find_matching_pair(cursor_offset)
                    {
                        Self::highlight_char(ctx, data, screen_lines, cursor_offset);
                        Self::highlight_char(ctx, data, screen_lines, new_offset);
                    }
                }
            } else {
                if let Some(syntax) = data.doc.syntax() {
                    let end_line = *screen_lines.lines.last().unwrap();
                    let end = data.doc.buffer().offset_of_line(end_line + 1);

                    for (indice, c) in
                        data.doc.buffer().char_indices_iter(cursor_offset..end)
                    {
                        if is_bracket(c) && matching_pair_direction(c) == Some(false)
                        {
                            let closing_bracket_offset = indice + cursor_offset;

                            if let Some(opening_bracket_offset) =
                                syntax.find_matching_pair(closing_bracket_offset)
                            {
                                if opening_bracket_offset < cursor_offset
                                    && cursor_offset < closing_bracket_offset
                                {

Review comment (refactor):
I think this would look better if you retrieved the syntax without pushing code to the right. What I have in mind is something like this:

```rust
let syntax = if let Some(syntax) = data.doc.syntax() {
    syntax
} else {
    return;
};

if is_bracket(char_at_cursor) {...
```

This way we could remove a duplicated condition and use the left side of our monitors a bit more :)

Same applies to `char_at_cursor`.

Code after:
                        x1 - x0,
                        scale,
                        color,
                    );
                }
            }
        }

        if let Some(diagnostic) = current {
            let start = diagnostic.diagnostic.range.start;
            if let Some(info) = screen_lines.info.get(&(start.line as usize)) {
                if data.editor.cursor.is_normal() {
                    let text_layout = ctx
                        .text()
                        .new_text_layout(diagnostic.diagnostic.message.clone())
                        .font(
                            data.config.ui.font_family(),
                            data.config.ui.font_size() as f64,
                        )
                        .text_color(
                            data.config
                                .get_color_unchecked(LapceTheme::EDITOR_FOREGROUND)
                                .clone(),
                        )
                        .max_width(data.editor.size.borrow().width - 20.0)
                        .build()
                        .unwrap();
                    let text_size = text_layout.size();
                    let mut text_height = text_size.height;

                    let related = diagnostic
                        .diagnostic
                        .related_information
                        .map(|related| {
                            related
                                .iter()
                                .map(|i| {
                                    let text_layout = ctx
                                        .text()
                                        .new_text_layout(i.message.clone())
                                        .font(
                                            data.config.ui.font_family(),
                                            data.config.ui.font_size() as f64,
                                        )
                                        .text_color(
                                            data.config
                                                .get_color_unchecked(
                                                    LapceTheme::EDITOR_FOREGROUND,
                                                )
                                                .clone(),
                                        )