apollo-language-server 0.7.0

A GraphQL language server with first-class support for Apollo Federation
Documentation
use apollo_compiler::parser::SourceSpan;
use ropey::Rope;
use tower_lsp::lsp_types as lsp;

pub(crate) fn lsp_range_from_ast_sourcespan(
    span: SourceSpan,
    source_text: &Rope,
) -> Option<lsp::Range> {
    let start_offset = span.offset();
    let end_offset = span.end_offset();

    // ask ropey for the start line, given the start offset
    let start_line = source_text.try_byte_to_line(start_offset).ok()?;
    // ask ropey for the character offset of the beginning of the start line
    let start_character = start_offset - source_text.try_line_to_byte(start_line).ok()?;

    // ask ropey for the end line, given the end offset
    let end_line = source_text.try_byte_to_line(end_offset).ok()?;
    // ask ropey for the character offset of the beginning of the end line
    let end_character = end_offset - source_text.try_line_to_byte(end_line).ok()?;

    Some(lsp::Range {
        start: lsp::Position {
            line: start_line as u32,
            character: start_character as u32,
        },
        end: lsp::Position {
            line: end_line as u32,
            character: end_character as u32,
        },
    })
}