diff --git a/compiler/crates/relay-lsp/src/completion/mod.rs b/compiler/crates/relay-lsp/src/completion/mod.rs index cd17d92cc5822..a51ed61ddddf6 100644 --- a/compiler/crates/relay-lsp/src/completion/mod.rs +++ b/compiler/crates/relay-lsp/src/completion/mod.rs @@ -990,7 +990,7 @@ pub(crate) fn on_completion( } } Err(graphql_err) => { - if let Ok(response) = state.js_resource.on_complete(¶ms, state.get_schemas()) { + if let Ok(response) = state.js_resource.on_complete(¶ms, state) { Ok(response) } else { Err(graphql_err) diff --git a/compiler/crates/relay-lsp/src/js_language_server.rs b/compiler/crates/relay-lsp/src/js_language_server.rs index 91c2cd84d5bd3..9cea592227fc9 100644 --- a/compiler/crates/relay-lsp/src/js_language_server.rs +++ b/compiler/crates/relay-lsp/src/js_language_server.rs @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -use crate::{lsp_runtime_error::LSPRuntimeResult, LSPRuntimeError, Schemas}; +use crate::{lsp_runtime_error::LSPRuntimeResult, server::LSPState, LSPRuntimeError}; use common::PerfLogger; use lsp_types::{ request::{Completion, Request}, @@ -19,7 +19,7 @@ pub trait JSLanguageServer { fn on_complete( &self, params: &::Params, - schemas: Schemas, + state: &LSPState, ) -> LSPRuntimeResult<::Result>; } #[derive(Default)] @@ -32,7 +32,7 @@ impl JSLanguageServer for NoopJS fn on_complete( &self, _: &::Params, - _: Schemas, + _: &LSPState, ) -> LSPRuntimeResult<::Result> { Err(LSPRuntimeError::ExpectedError) } diff --git a/compiler/crates/relay-lsp/src/lib.rs b/compiler/crates/relay-lsp/src/lib.rs index ff7166f7ee9a8..995f80e6bd4e0 100644 --- a/compiler/crates/relay-lsp/src/lib.rs +++ b/compiler/crates/relay-lsp/src/lib.rs @@ -41,8 +41,9 @@ use lsp_process_error::LSPProcessResult; pub use lsp_runtime_error::{LSPRuntimeError, LSPRuntimeResult}; use lsp_server::Connection; use relay_compiler::config::Config; -pub use server::Schemas; +pub use server::{LSPState, Schemas}; use std::sync::Arc; +pub use utils::position_to_offset; #[cfg(test)] #[macro_use] extern crate assert_matches; diff --git a/compiler/crates/relay-lsp/src/server/lsp_state.rs b/compiler/crates/relay-lsp/src/server/lsp_state.rs index 990cc9ee8ab86..3c2e1b2c23ae0 100644 --- a/compiler/crates/relay-lsp/src/server/lsp_state.rs +++ b/compiler/crates/relay-lsp/src/server/lsp_state.rs @@ -47,7 +47,7 @@ pub enum ProjectStatus { /// This structure contains all available resources that we may use in the Relay LSP message/notification /// handlers. Such as schema, programs, extra_data_providers, etc... -pub(crate) struct LSPState { +pub struct LSPState { config: Arc, compiler: Option>, root_dir: PathBuf, @@ -228,6 +228,10 @@ impl LSPState { Ok(()) } + pub fn extract_project_name_from_url(&self, url: &Url) -> LSPRuntimeResult { + extract_project_name_from_url(&self.file_categorizer, url, &self.root_dir) + } + fn insert_synced_sources(&mut self, url: Url, sources: Vec) { self.start_compiler_once(); self.synced_graphql_documents.insert(url, sources); diff --git a/compiler/crates/relay-lsp/src/server/mod.rs b/compiler/crates/relay-lsp/src/server/mod.rs index 5dd9ee36f1ea9..65ba410400816 100644 --- a/compiler/crates/relay-lsp/src/server/mod.rs +++ b/compiler/crates/relay-lsp/src/server/mod.rs @@ -52,7 +52,7 @@ use relay_compiler::{config::Config, NoopArtifactWriter}; use std::sync::Arc; pub use crate::LSPExtraDataProvider; -pub(crate) use lsp_state::LSPState; +pub use lsp_state::LSPState; pub use lsp_state::{Schemas, SourcePrograms}; /// Initializes an LSP connection, handling the `initialize` message and `initialized` notification diff --git a/compiler/crates/relay-lsp/src/utils.rs b/compiler/crates/relay-lsp/src/utils.rs index 34a44ac7b3bd4..cda05088f5595 100644 --- a/compiler/crates/relay-lsp/src/utils.rs +++ b/compiler/crates/relay-lsp/src/utils.rs @@ -158,10 +158,20 @@ pub(crate) fn position_to_span( source: &GraphQLSource, index_offset: usize, ) -> Option { + position_to_offset(position, index_offset, source.line_index, &source.text) + .map(|offset| Span::new(offset, offset)) +} + +pub fn position_to_offset( + position: &Position, + index_offset: usize, + line_index: usize, + text: &str, +) -> Option { let mut index_of_first_character_of_current_line = 0; - let mut line_index = source.line_index as u64; + let mut line_index = line_index as u64; - let mut chars = source.text.chars().enumerate().peekable(); + let mut chars = text.chars().enumerate().peekable(); while let Some((index, chr)) = chars.next() { let is_newline = match chr { @@ -181,7 +191,7 @@ pub(crate) fn position_to_span( if line_index == position.line { let start_offset = (index_of_first_character_of_current_line + position.character) as u32; - return Some(Span::new(start_offset, start_offset)); + return Some(start_offset); } } None