rust-analyzer / rowan

Apache License 2.0
704 stars 58 forks source link

How integrate with codespan or similar? or: How get the original source? #125

Open mamcx opened 2 years ago

mamcx commented 2 years ago

I'm looking if move my parsing to rowan, and one thing looks complicated. I wish to also have nice error reporting, like using codespan, but have trouble making the integration. As I see it today, I need to keep 2 copies of the whole source code to make both:

pub type File = SimpleFile<String, String>;  <-- Copy for reports
pub struct FilesDb {
    files: SlotMap<FileId, File>,
}

//Interface to my source code, this is where I need to mix...
impl<'a> Files<'a> for FilesDb {
    type FileId = FileId;
    type Name = String;
    type Source = &'a str;

    fn name(&self, file_id: FileId) -> Result<Self::Name, Error> {
        Ok(self.get(file_id)?.name().to_string())
    }

    fn source(&'a self, file_id: FileId) -> Result<Self::Source, Error> {
        Ok(self.get(file_id)?.source().as_ref())
    }

    fn line_index(&self, file_id: Self::FileId, byte_index: usize) -> Result<usize, Error> {
        self.get(file_id)?.line_index((), byte_index)
    }

    fn line_range(&self, file_id: Self::FileId, line_index: usize) -> Result<Range<usize>, Error> {
        self.get(file_id)?.line_range((), line_index)
    }
}

Now, I see I can allocate the code from the green_nodes:

parser.green_node.to_string()

But then is again a double-copy. How I can get the nodes that are part of the range of an error? Or index into the line/col, etc?

matklad commented 2 years ago

See SyntaxText: https://github.com/rust-analyzer/rowan/blob/master/src/syntax_text.rs