Skip to content

Commit

Permalink
Wire up NamedAst parsing and position to span
Browse files Browse the repository at this point in the history
Reviewed By: josephsavona

Differential Revision: D27524041

fbshipit-source-id: 4bd5879eab8252e3d095472641da42876994419d
  • Loading branch information
tyao1 authored and facebook-github-bot committed Apr 2, 2021
1 parent bbde6c3 commit 63c4e99
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 10 deletions.
2 changes: 1 addition & 1 deletion compiler/crates/relay-lsp/src/completion/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ pub(crate) fn on_completion<TPerfLogger: PerfLogger + 'static>(
}
}
Err(graphql_err) => {
if let Ok(response) = state.js_resource.on_complete(&params, state.get_schemas()) {
if let Ok(response) = state.js_resource.on_complete(&params, state) {
Ok(response)
} else {
Err(graphql_err)
Expand Down
6 changes: 3 additions & 3 deletions compiler/crates/relay-lsp/src/js_language_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -19,7 +19,7 @@ pub trait JSLanguageServer<TPerfLogger: PerfLogger + 'static> {
fn on_complete(
&self,
params: &<Completion as Request>::Params,
schemas: Schemas,
state: &LSPState<TPerfLogger>,
) -> LSPRuntimeResult<<Completion as Request>::Result>;
}
#[derive(Default)]
Expand All @@ -32,7 +32,7 @@ impl<TPerfLogger: PerfLogger + 'static> JSLanguageServer<TPerfLogger> for NoopJS
fn on_complete(
&self,
_: &<Completion as Request>::Params,
_: Schemas,
_: &LSPState<TPerfLogger>,
) -> LSPRuntimeResult<<Completion as Request>::Result> {
Err(LSPRuntimeError::ExpectedError)
}
Expand Down
3 changes: 2 additions & 1 deletion compiler/crates/relay-lsp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion compiler/crates/relay-lsp/src/server/lsp_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TPerfLogger: PerfLogger + 'static> {
pub struct LSPState<TPerfLogger: PerfLogger + 'static> {
config: Arc<Config>,
compiler: Option<Compiler<TPerfLogger>>,
root_dir: PathBuf,
Expand Down Expand Up @@ -228,6 +228,10 @@ impl<TPerfLogger: PerfLogger + 'static> LSPState<TPerfLogger> {
Ok(())
}

pub fn extract_project_name_from_url(&self, url: &Url) -> LSPRuntimeResult<StringKey> {
extract_project_name_from_url(&self.file_categorizer, url, &self.root_dir)
}

fn insert_synced_sources(&mut self, url: Url, sources: Vec<GraphQLSource>) {
self.start_compiler_once();
self.synced_graphql_documents.insert(url, sources);
Expand Down
2 changes: 1 addition & 1 deletion compiler/crates/relay-lsp/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 13 additions & 3 deletions compiler/crates/relay-lsp/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,20 @@ pub(crate) fn position_to_span(
source: &GraphQLSource,
index_offset: usize,
) -> Option<Span> {
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<u32> {
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 {
Expand All @@ -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
Expand Down

0 comments on commit 63c4e99

Please sign in to comment.