Visitar URL original
Initialize LSP resources for .graphql files · meta/relay@9c8e9f5 · GitHub
Skip to content

Commit 9c8e9f5

Browse files
Initialize LSP resources for .graphql files
1 parent 51e3bc1 commit 9c8e9f5

2 files changed

Lines changed: 69 additions & 35 deletions

File tree

‎compiler/crates/relay-lsp/src/server/lsp_state.rs‎

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use lsp_types::Url;
3737
use relay_compiler::config::Config;
3838
use relay_compiler::get_parser_features;
3939
use relay_compiler::FileCategorizer;
40+
use relay_compiler::FileGroup;
4041
use relay_compiler::ProjectName;
4142
use relay_docblock::parse_docblock_ast;
4243
use relay_docblock::ParseOptions;
@@ -56,7 +57,8 @@ use crate::lsp_runtime_error::LSPRuntimeResult;
5657
use crate::node_resolution_info::create_node_resolution_info;
5758
use crate::utils::extract_executable_definitions_from_text_document;
5859
use crate::utils::extract_feature_from_text;
59-
use crate::utils::extract_project_name_from_url;
60+
use crate::utils::get_file_group_from_uri;
61+
use crate::utils::get_project_name_from_file_group;
6062
use crate::ContentConsumerType;
6163
use crate::DocblockNode;
6264
use crate::Feature;
@@ -344,13 +346,6 @@ impl<TPerfLogger: PerfLogger + 'static, TSchemaDocumentation: SchemaDocumentatio
344346
uri: &Url,
345347
sources: Vec<JavaScriptSourceFeature>,
346348
) -> LSPRuntimeResult<()> {
347-
let project_name = self.extract_project_name_from_url(uri)?;
348-
349-
if let Entry::Vacant(e) = self.project_status.entry(project_name) {
350-
e.insert(ProjectStatus::Activated);
351-
self.notify_lsp_state_resources.notify_one();
352-
}
353-
354349
self.insert_synced_sources(uri, sources);
355350
self.schedule_task(Task::ValidateSyncedSource(uri.clone()));
356351

@@ -362,6 +357,13 @@ impl<TPerfLogger: PerfLogger + 'static, TSchemaDocumentation: SchemaDocumentatio
362357
self.diagnostic_reporter
363358
.clear_quick_diagnostics_for_url(url);
364359
}
360+
361+
fn initialize_lsp_state_resources(&self, project_name: StringKey) {
362+
if let Entry::Vacant(e) = self.project_status.entry(project_name) {
363+
e.insert(ProjectStatus::Activated);
364+
self.notify_lsp_state_resources.notify_one();
365+
}
366+
}
365367
}
366368

367369
impl<TPerfLogger: PerfLogger + 'static, TSchemaDocumentation: SchemaDocumentation + 'static>
@@ -476,7 +478,10 @@ impl<TPerfLogger: PerfLogger + 'static, TSchemaDocumentation: SchemaDocumentatio
476478
}
477479

478480
fn extract_project_name_from_url(&self, url: &Url) -> LSPRuntimeResult<StringKey> {
479-
extract_project_name_from_url(&self.file_categorizer, url, &self.root_dir)
481+
let (file_group, file_path) =
482+
get_file_group_from_uri(&self.file_categorizer, url, &self.root_dir)?;
483+
484+
get_project_name_from_file_group(&file_group, &file_path)
480485
}
481486

482487
fn get_extra_data_provider(&self) -> &dyn LSPExtraDataProvider {
@@ -521,12 +526,31 @@ impl<TPerfLogger: PerfLogger + 'static, TSchemaDocumentation: SchemaDocumentatio
521526
js_server.process_js_source(uri, text);
522527
}
523528

524-
// First we check to see if this document has any GraphQL documents.
525-
let embedded_sources = extract_graphql::extract(text);
526-
if embedded_sources.is_empty() {
527-
Ok(())
528-
} else {
529-
self.process_synced_sources(uri, embedded_sources)
529+
let (file_group, file_path) =
530+
get_file_group_from_uri(&self.file_categorizer, uri, &self.root_dir)?;
531+
let project_name = get_project_name_from_file_group(&file_group, &file_path)?;
532+
533+
match file_group {
534+
FileGroup::Schema { project_set: _ } => {
535+
self.initialize_lsp_state_resources(project_name);
536+
Ok(())
537+
}
538+
FileGroup::Extension { project_set: _ } => {
539+
self.initialize_lsp_state_resources(project_name);
540+
Ok(())
541+
}
542+
FileGroup::Source { project_set: _ } => {
543+
// First we check to see if this document has any GraphQL documents.
544+
let embedded_sources = extract_graphql::extract(text);
545+
546+
if embedded_sources.is_empty() {
547+
Ok(())
548+
} else {
549+
self.initialize_lsp_state_resources(project_name);
550+
self.process_synced_sources(uri, embedded_sources)
551+
}
552+
}
553+
_ => Err(LSPRuntimeError::ExpectedError),
530554
}
531555
}
532556

‎compiler/crates/relay-lsp/src/utils.rs‎

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -73,41 +73,51 @@ pub fn extract_executable_definitions_from_text_document(
7373
Ok(definitions)
7474
}
7575

76-
pub fn extract_project_name_from_url(
76+
pub fn get_file_group_from_uri(
7777
file_categorizer: &FileCategorizer,
7878
url: &Url,
7979
root_dir: &PathBuf,
80-
) -> LSPRuntimeResult<StringKey> {
80+
) -> LSPRuntimeResult<(FileGroup, PathBuf)> {
8181
let absolute_file_path = url.to_file_path().map_err(|_| {
8282
LSPRuntimeError::UnexpectedError(format!("Unable to convert URL to file path: {:?}", url))
8383
})?;
84-
8584
let file_path = absolute_file_path.strip_prefix(root_dir).map_err(|_e| {
8685
LSPRuntimeError::UnexpectedError(format!(
8786
"Failed to strip prefix {:?} from {:?}",
8887
root_dir, absolute_file_path
8988
))
9089
})?;
9190

92-
let project_name = if let FileGroup::Source { project_set } =
93-
file_categorizer.categorize(file_path).map_err(|_| {
94-
LSPRuntimeError::UnexpectedError(format!(
95-
"Unable to categorize the file correctly: {:?}",
96-
file_path
97-
))
98-
})? {
99-
*project_set.first().ok_or_else(|| {
100-
LSPRuntimeError::UnexpectedError(format!(
101-
"Expected to find at least one project for {:?}",
102-
file_path
103-
))
104-
})?
105-
} else {
106-
return Err(LSPRuntimeError::UnexpectedError(format!(
91+
let file_group = file_categorizer.categorize(file_path).map_err(|_| {
92+
LSPRuntimeError::UnexpectedError(format!(
93+
"Unable to categorize the file correctly: {:?}",
94+
file_path
95+
))
96+
})?;
97+
98+
Ok((file_group, file_path.to_path_buf()))
99+
}
100+
101+
pub fn get_project_name_from_file_group(
102+
file_group: &FileGroup,
103+
file_path: &PathBuf,
104+
) -> LSPRuntimeResult<StringKey> {
105+
let project_set = match file_group {
106+
FileGroup::Source { project_set } => Ok(project_set),
107+
FileGroup::Schema { project_set } => Ok(project_set),
108+
FileGroup::Extension { project_set } => Ok(project_set),
109+
_ => Err(LSPRuntimeError::UnexpectedError(format!(
107110
"File path {:?} is not a source set",
108111
file_path
109-
)));
110-
};
112+
))),
113+
}?;
114+
let project_name = *project_set.first().ok_or_else(|| {
115+
LSPRuntimeError::UnexpectedError(format!(
116+
"Expected to find at least one project for {:?}",
117+
file_path
118+
))
119+
})?;
120+
111121
Ok(project_name.into())
112122
}
113123

0 commit comments

Comments
 (0)