mangrove_script/
source_map.rs

1/*
2 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/swamp/mangrove
3 * Licensed under the MIT License. See LICENSE in the project root for license information.
4 */
5
6use crate::SourceMapResource;
7use swamp::prelude::{App, Plugin};
8use swamp_script::prelude::{ResolvedNode, SourceMap, SourceMapLookup, Span};
9
10pub struct SourceMapPlugin;
11
12#[derive(Debug)]
13pub struct SourceMapWrapper {
14    pub source_map: SourceMap,
15}
16
17impl SourceMapLookup for SourceMapWrapper {
18    fn get_text(&self, resolved_node: &ResolvedNode) -> &str {
19        self.source_map.get_span_source(
20            resolved_node.span.file_id,
21            resolved_node.span.offset as usize,
22            resolved_node.span.length as usize,
23        )
24    }
25
26    fn get_text_span(&self, span: &Span) -> &str {
27        self.source_map
28            .get_span_source(span.file_id, span.offset as usize, span.length as usize)
29    }
30}
31
32impl Plugin for SourceMapPlugin {
33    fn build(&self, app: &mut App) {
34        //app.add_system(UpdatePhase::Update, detect_reload_tick);
35        app.insert_resource(SourceMapResource {
36            wrapper: SourceMapWrapper {
37                source_map: SourceMap::new("scripts/".as_ref()),
38            },
39        });
40    }
41}