Skip to content

Commit

Permalink
relative path work
Browse files Browse the repository at this point in the history
  • Loading branch information
rbalicki2 committed Jan 6, 2025
1 parent 9897848 commit ed4f7ce
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 47 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/common_lang_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ license = { workspace = true }
intern = { path = "../../relay-crates/intern" }
string_key_newtype = { path = "../string_key_newtype" }
serde = { workspace = true }
pathdiff = { workspace = true }
32 changes: 27 additions & 5 deletions crates/common_lang_types/src/location.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{error::Error, fmt};
use std::{error::Error, fmt, path::PathBuf};

use intern::Lookup;
use intern::string_key::{Intern, Lookup};

use crate::{
text_with_carats::text_with_carats, CurrentWorkingDirectory, RelativePathToSourceFile, Span,
Expand All @@ -22,11 +22,18 @@ pub struct TextSource {
}

impl TextSource {
pub fn read_to_string(&self) -> (&str, String) {
pub fn read_to_string(&self) -> (String, String) {
// TODO maybe intern these or somehow avoid reading a bajillion times.
// This is especially important for when we display many errors.
let file_path = self.relative_path_to_source_file.lookup();
let file_contents = std::fs::read_to_string(file_path).expect("file should exist");
let mut file_path = PathBuf::from(self.current_working_directory.lookup());
let relative_path = self.relative_path_to_source_file.lookup();
file_path.push(relative_path);

let file_contents = std::fs::read_to_string(&file_path).expect("file should exist");
let file_path = file_path
.to_str()
.expect("Expected path to be able to be stringifie.")
.to_string();
if let Some(span) = self.span {
// TODO we're cloning here unnecessarily, I think!
(file_path, file_contents[span.as_usize_range()].to_string())
Expand Down Expand Up @@ -193,3 +200,18 @@ impl<T> From<WithEmbeddedLocation<T>> for WithLocation<T> {
}
}
}

pub fn relative_path_from_absolute_and_working_directory(
current_working_directory: CurrentWorkingDirectory,
absolute_path: &PathBuf,
) -> RelativePathToSourceFile {
pathdiff::diff_paths(
absolute_path,
PathBuf::from(current_working_directory.lookup()),
)
.expect("Expected path to be diffable")
.to_str()
.expect("Expected path to be able to be stringified")
.intern()
.into()
}
18 changes: 9 additions & 9 deletions crates/isograph_compiler/src/isograph_literals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use std::{
};

use common_lang_types::{
FilePath, Location, RelativePathToSourceFile, Span, TextSource, WithLocation,
relative_path_from_absolute_and_working_directory, FilePath, Location,
RelativePathToSourceFile, Span, TextSource, WithLocation,
};
use intern::string_key::Intern;
use isograph_config::CompilerConfig;
Expand Down Expand Up @@ -122,20 +123,19 @@ pub(crate) fn read_and_parse_iso_literals(
// TODO don't intern unless there's a match
let interned_file_path = file_path.to_string_lossy().into_owned().intern().into();

let file_name = canonicalized_root_path
.join(file_path)
.to_str()
.expect("file_path should be a valid string")
.intern()
.into();
let absolute_path = canonicalized_root_path.join(&file_path);
let relative_path_to_source_file = relative_path_from_absolute_and_working_directory(
config.current_working_directory,
&absolute_path,
);

let mut extraction_results = vec![];
let mut isograph_literal_parse_errors = vec![];

for iso_literal_extraction in extract_iso_literals_from_file_content(&file_content) {
match process_iso_literal_extraction(
iso_literal_extraction,
file_name,
relative_path_to_source_file,
interned_file_path,
config,
) {
Expand All @@ -145,7 +145,7 @@ pub(crate) fn read_and_parse_iso_literals(
}

if isograph_literal_parse_errors.is_empty() {
Ok((file_name, extraction_results))
Ok((relative_path_to_source_file, extraction_results))
} else {
Err(isograph_literal_parse_errors)
}
Expand Down
30 changes: 11 additions & 19 deletions crates/isograph_compiler/src/source_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use common_lang_types::{RelativePathToSourceFile, TextSource};
use graphql_lang_types::{GraphQLTypeSystemDocument, GraphQLTypeSystemExtensionDocument};
use graphql_schema_parser::{parse_schema, parse_schema_extensions};
use intern::string_key::Intern;
use isograph_config::CompilerConfig;
use isograph_config::{absolute_and_relative_paths, AbsolutePathAndRelativePath, CompilerConfig};
use isograph_lang_parser::IsoLiteralExtractionResult;
use isograph_schema::UnvalidatedSchema;

Expand Down Expand Up @@ -36,7 +36,7 @@ impl SourceFiles {
let mut schema_extensions = HashMap::new();
for schema_extension_path in config.schema_extensions.iter() {
let (file_path, extensions_document) =
read_and_parse_schema_extensions(&schema_extension_path.absolute_path, config)?;
read_and_parse_schema_extensions(schema_extension_path, config)?;
schema_extensions.insert(file_path, extensions_document);
}

Expand Down Expand Up @@ -217,7 +217,10 @@ impl SourceFiles {
path: &PathBuf,

Check failure on line 217 in crates/isograph_compiler/src/source_files.rs

View workflow job for this annotation

GitHub Actions / Run cargo fmt and clippy

writing `&PathBuf` instead of `&Path` involves a new object where a slice will do
config: &CompilerConfig,
) -> Result<(), BatchCompileError> {
let (file_path, document) = read_and_parse_schema_extensions(path, config)?;
let absolute_and_relative =
absolute_and_relative_paths(config.current_working_directory, path.clone());
let (file_path, document) =
read_and_parse_schema_extensions(&absolute_and_relative, config)?;
self.schema_extensions.insert(file_path, document);
Ok(())
}
Expand Down Expand Up @@ -273,13 +276,7 @@ fn read_and_parse_graphql_schema(
) -> Result<GraphQLTypeSystemDocument, BatchCompileError> {
let content = read_schema_file(&config.schema.absolute_path)?;
let schema_text_source = TextSource {
relative_path_to_source_file: config
.schema
.absolute_path
.to_str()
.expect("Expected schema to be valid string")
.intern()
.into(),
relative_path_to_source_file: config.schema.relative_path,
span: None,
current_working_directory: config.current_working_directory,
};
Expand All @@ -293,25 +290,20 @@ fn intern_file_path(path: &Path) -> RelativePathToSourceFile {
}

pub fn read_and_parse_schema_extensions(
schema_extension_path: &PathBuf,
schema_extension_path: &AbsolutePathAndRelativePath,
config: &CompilerConfig,
) -> Result<(RelativePathToSourceFile, GraphQLTypeSystemExtensionDocument), BatchCompileError> {
let file_path = schema_extension_path
.to_str()
.expect("Expected schema extension to be valid string")
.intern()
.into();
let extension_content = read_schema_file(schema_extension_path)?;
let extension_content = read_schema_file(&schema_extension_path.absolute_path)?;
let extension_text_source = TextSource {
relative_path_to_source_file: file_path,
relative_path_to_source_file: schema_extension_path.relative_path,
span: None,
current_working_directory: config.current_working_directory,
};

let schema_extensions = parse_schema_extensions(&extension_content, extension_text_source)
.map_err(|with_span| with_span.to_with_location(extension_text_source))?;

Ok((file_path, schema_extensions))
Ok((schema_extension_path.relative_path, schema_extensions))
}

fn get_canonicalized_root_path(project_root: &PathBuf) -> Result<PathBuf, BatchCompileError> {
Expand Down
1 change: 0 additions & 1 deletion crates/isograph_config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@ serde = { workspace = true }
serde_json = { workspace = true }
colorize = { workspace = true }
tracing = { workspace = true }
pathdiff = { workspace = true }
18 changes: 9 additions & 9 deletions crates/isograph_config/src/compilation_options.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::path::PathBuf;

use common_lang_types::{CurrentWorkingDirectory, RelativePathToSourceFile};
use intern::string_key::{Intern, Lookup};
use common_lang_types::{
relative_path_from_absolute_and_working_directory, CurrentWorkingDirectory,
RelativePathToSourceFile,
};
use schemars::JsonSchema;
use serde::Deserialize;
use tracing::warn;
Expand Down Expand Up @@ -265,16 +267,14 @@ fn create_generate_file_extensions(
}
}

fn absolute_and_relative_paths(
pub fn absolute_and_relative_paths(
current_working_directory: CurrentWorkingDirectory,
absolute_path: PathBuf,
) -> AbsolutePathAndRelativePath {
let relative_path = pathdiff::diff_paths(&absolute_path, current_working_directory.lookup())
.expect("Expected to be able to create relative path")
.to_str()
.expect("Expected to be able to stringify.")
.intern()
.into();
let relative_path = relative_path_from_absolute_and_working_directory(
current_working_directory,
&absolute_path,
);

AbsolutePathAndRelativePath {
absolute_path,
Expand Down
11 changes: 8 additions & 3 deletions crates/isograph_lsp/src/semantic_tokens/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ mod entrypoint;
mod semantic_token_generator;
pub(crate) mod semantic_token_legend;

use std::path::PathBuf;

use crate::{
lsp_runtime_error::LSPRuntimeResult,
lsp_state::LSPState,
row_col_offset::{diff_to_end_of_slice, get_index_from_diff, RowColDiff},
};
use client_field::client_field_declaration_to_tokens;
use common_lang_types::{Span, TextSource};
use common_lang_types::{relative_path_from_absolute_and_working_directory, Span, TextSource};
use entrypoint::entrypoint_declaration_to_tokens;
use intern::string_key::Intern;
use isograph_compiler::{extract_iso_literals_from_file_content, IsoLiteralExtraction};
Expand Down Expand Up @@ -55,7 +57,10 @@ pub fn on_semantic_token_full_request(
let initial_diff =
diff_to_end_of_slice(&file_text[index_of_last_token..iso_literal_start_index]);

let file_path = text_document.uri.path().intern();
let file_path = relative_path_from_absolute_and_working_directory(
state.config.current_working_directory,
&PathBuf::from(text_document.uri.path()),
);
let text_source = TextSource {
relative_path_to_source_file: file_path.into(),
span: Some(Span::new(
Expand All @@ -66,7 +71,7 @@ pub fn on_semantic_token_full_request(
};
let iso_literal_extraction_result = parse_iso_literal(
iso_literal_text,
file_path.into(),
text_document.uri.path().intern().into(),
const_export_name,
text_source,
);
Expand Down

0 comments on commit ed4f7ce

Please sign in to comment.