apollo-language-server 0.6.0

A GraphQL language server with first-class support for Apollo Federation
Documentation
use crate::{
    diagnostics::map_diagnostics_for_lsp, goto_definition::goto_definition_at_position,
    hover::get_hover_at_position,
};
use apollo_compiler::Schema;
use apollo_parser::{Parser, SyntaxTree};
use ropey::Rope;

use super::SchemaWithMetadata;

#[derive(Debug)]
pub struct Monolith {
    pub(crate) uri: lsp::Url,
    pub version: i32,
    pub source_text: Rope,
    pub cst: SyntaxTree,
    schema_with_metadata: SchemaWithMetadata,
}

impl Monolith {
    pub(crate) fn new(
        uri: lsp::Url,
        source_text: String,
        version: i32,
        schema_with_metadata: SchemaWithMetadata,
    ) -> Monolith {
        Monolith {
            uri,
            version,
            cst: Parser::new(&source_text).parse(),
            source_text: source_text.into(),
            schema_with_metadata,
        }
    }

    pub(crate) fn schema(&self) -> &Schema {
        &self.schema_with_metadata.schema
    }

    pub(crate) fn update(
        &mut self,
        source_text: String,
        version: i32,
        schema_with_metadata: SchemaWithMetadata,
    ) {
        self.version = version;
        self.cst = Parser::new(&source_text).parse();
        self.schema_with_metadata = schema_with_metadata;
        self.source_text = source_text.into();
    }

    pub(crate) fn diagnostics(&self) -> Vec<lsp::Diagnostic> {
        map_diagnostics_for_lsp(
            self.schema_with_metadata.parse_errors.as_ref(),
            self.schema_with_metadata.build_errors.as_ref(),
            self.schema_with_metadata.validation_errors.as_ref(),
            self.source_text.to_string(),
        )
        .into()
    }

    pub(crate) fn on_hover(&self, position: &lsp::Position) -> Option<lsp::Hover> {
        get_hover_at_position(
            &self.cst.document(),
            self.schema(),
            &self.source_text,
            position,
        )
    }

    pub(crate) fn goto_definition(
        &self,
        position: &lsp::Position,
    ) -> Option<Vec<lsp::LocationLink>> {
        goto_definition_at_position(
            &self.uri,
            &self.cst.document(),
            self.schema(),
            &self.source_text,
            position,
            None,
        )
    }
}