summaryrefslogtreecommitdiff
path: root/src/ide/jump.rs
blob: 1a96fbbed668b7a18211075fe9803a03a301a317 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use std::num::NonZeroUsize;

use crate::doc::{Element, Frame, Location};
use crate::geom::Point;
use crate::syntax::{LinkedNode, Source, Span, SyntaxKind};
use crate::World;

/// Find the source file and byte offset for a click position.
pub fn jump_to_source<'a>(
    world: &'a dyn World,
    frame: &Frame,
    click: Point,
) -> Option<(&'a Source, usize)> {
    for (mut pos, element) in frame.elements() {
        if let Element::Text(text) = element {
            for glyph in &text.glyphs {
                if glyph.span.is_detached() {
                    continue;
                }

                let width = glyph.x_advance.at(text.size);
                if pos.x <= click.x
                    && pos.x + width >= click.x
                    && pos.y >= click.y
                    && pos.y - text.size <= click.y
                {
                    let source = world.source(glyph.span.source());
                    let node = source.find(glyph.span);
                    let pos = if node.kind() == SyntaxKind::Text {
                        let range = node.range();
                        (range.start + usize::from(glyph.offset)).min(range.end)
                    } else {
                        node.offset()
                    };
                    return Some((source, pos));
                }

                pos.x += width;
            }
        }

        if let Element::Group(group) = element {
            if let Some(span) = jump_to_source(world, &group.frame, click - pos) {
                return Some(span);
            }
        }
    }

    None
}

/// Find the output location for a cursor position.
pub fn jump_to_preview(
    frames: &[Frame],
    source: &Source,
    cursor: usize,
) -> Option<Location> {
    let node = LinkedNode::new(source.root()).leaf_at(cursor)?;
    if node.kind() != SyntaxKind::Text {
        return None;
    }

    let span = node.span();
    for (i, frame) in frames.iter().enumerate() {
        if let Some(pos) = find_in_frame(frame, span) {
            return Some(Location { page: NonZeroUsize::new(i + 1).unwrap(), pos });
        }
    }

    None
}

/// Find the position of a span in a frame.
fn find_in_frame(frame: &Frame, span: Span) -> Option<Point> {
    for (mut pos, element) in frame.elements() {
        if let Element::Text(text) = element {
            for glyph in &text.glyphs {
                if glyph.span == span {
                    return Some(pos);
                }
                pos.x += glyph.x_advance.at(text.size);
            }
        }

        if let Element::Group(group) = element {
            if let Some(point) = find_in_frame(&group.frame, span) {
                return Some(point + pos);
            }
        }
    }

    None
}