Skip to content

Commit

Permalink
rename SourceFileName to RelativePathToSourceFile for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
rbalicki2 committed Jan 5, 2025
1 parent 6714b0b commit d8e1c85
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 18 deletions.
6 changes: 3 additions & 3 deletions crates/common_lang_types/src/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{error::Error, fmt};

use intern::Lookup;

use crate::{text_with_carats::text_with_carats, SourceFileName, Span, WithSpan};
use crate::{text_with_carats::text_with_carats, RelativePathToSourceFile, Span, WithSpan};

/// A source, which consists of a filename, and an optional span
/// indicating the subset of the file which corresponds to the
Expand All @@ -13,15 +13,15 @@ use crate::{text_with_carats::text_with_carats, SourceFileName, Span, WithSpan};
/// during watch mode.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct TextSource {
pub path: SourceFileName,
pub relative_path_to_source_file: RelativePathToSourceFile,
pub span: Option<Span>,
}

impl TextSource {
pub fn read_to_string(&self) -> (&str, 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.path.lookup();
let file_path = self.relative_path_to_source_file.lookup();
let file_contents = std::fs::read_to_string(file_path).expect("file should exist");
if let Some(span) = self.span {
// TODO we're cloning here unnecessarily, I think!
Expand Down
2 changes: 1 addition & 1 deletion crates/common_lang_types/src/string_key_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ string_key_newtype!(IsographDirectiveName);

string_key_newtype!(FieldArgumentName);

string_key_newtype!(SourceFileName);
string_key_newtype!(RelativePathToSourceFile);

string_key_newtype!(ArtifactFileType);

Expand Down
10 changes: 6 additions & 4 deletions crates/isograph_compiler/src/isograph_literals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use std::{
path::{Path, PathBuf},
};

use common_lang_types::{FilePath, Location, SourceFileName, Span, TextSource, WithLocation};
use common_lang_types::{
FilePath, Location, RelativePathToSourceFile, Span, TextSource, WithLocation,
};
use intern::string_key::Intern;
use isograph_lang_parser::{
parse_iso_literal, IsoLiteralExtractionResult, IsographLiteralParseError,
Expand Down Expand Up @@ -110,7 +112,7 @@ pub(crate) fn read_and_parse_iso_literals(
canonicalized_root_path: &Path,
) -> Result<
(
SourceFileName,
RelativePathToSourceFile,
Vec<(IsoLiteralExtractionResult, TextSource)>,
),
Vec<WithLocation<IsographLiteralParseError>>,
Expand Down Expand Up @@ -180,7 +182,7 @@ pub(crate) fn process_iso_literals(

pub fn process_iso_literal_extraction(
iso_literal_extraction: IsoLiteralExtraction<'_>,
file_name: SourceFileName,
file_name: RelativePathToSourceFile,
interned_file_path: FilePath,
) -> Result<(IsoLiteralExtractionResult, TextSource), WithLocation<IsographLiteralParseError>> {
let IsoLiteralExtraction {
Expand All @@ -191,7 +193,7 @@ pub fn process_iso_literal_extraction(
iso_function_called_with_paren: has_paren,
} = iso_literal_extraction;
let text_source = TextSource {
path: file_name,
relative_path_to_source_file: file_name,
span: Some(Span::new(
iso_literal_start_index as u32,
(iso_literal_start_index + iso_literal_text.len()) as u32,
Expand Down
18 changes: 10 additions & 8 deletions crates/isograph_compiler/src/source_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
path::{Path, PathBuf},
};

use common_lang_types::{SourceFileName, TextSource};
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;
Expand All @@ -25,7 +25,7 @@ use crate::{
#[derive(Clone, Debug, Eq, PartialEq, Default)]
pub struct SourceFiles {
pub schema: GraphQLTypeSystemDocument,
pub schema_extensions: HashMap<SourceFileName, GraphQLTypeSystemExtensionDocument>,
pub schema_extensions: HashMap<RelativePathToSourceFile, GraphQLTypeSystemExtensionDocument>,
pub contains_iso: ContainsIso,
}

Expand Down Expand Up @@ -263,7 +263,7 @@ fn read_and_parse_graphql_schema(
) -> Result<GraphQLTypeSystemDocument, BatchCompileError> {
let content = read_schema_file(schema_path)?;
let schema_text_source = TextSource {
path: schema_path
relative_path_to_source_file: schema_path
.to_str()
.expect("Expected schema to be valid string")
.intern()
Expand All @@ -275,21 +275,21 @@ fn read_and_parse_graphql_schema(
Ok(schema)
}

fn intern_file_path(path: &Path) -> SourceFileName {
fn intern_file_path(path: &Path) -> RelativePathToSourceFile {
path.to_string_lossy().into_owned().intern().into()
}

pub fn read_and_parse_schema_extensions(
schema_extension_path: &PathBuf,
) -> Result<(SourceFileName, GraphQLTypeSystemExtensionDocument), BatchCompileError> {
) -> 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_text_source = TextSource {
path: file_path,
relative_path_to_source_file: file_path,
span: None,
};

Expand Down Expand Up @@ -322,7 +322,9 @@ fn process_exposed_fields(schema: &mut UnvalidatedSchema) -> Result<(), BatchCom
}

#[derive(Clone, Debug, Eq, PartialEq, Default)]
pub struct ContainsIso(pub HashMap<SourceFileName, Vec<(IsoLiteralExtractionResult, TextSource)>>);
pub struct ContainsIso(
pub HashMap<RelativePathToSourceFile, Vec<(IsoLiteralExtractionResult, TextSource)>>,
);

impl ContainsIso {
pub fn stats(&self) -> ContainsIsoStats {
Expand Down Expand Up @@ -351,7 +353,7 @@ impl ContainsIso {
}

impl Deref for ContainsIso {
type Target = HashMap<SourceFileName, Vec<(IsoLiteralExtractionResult, TextSource)>>;
type Target = HashMap<RelativePathToSourceFile, Vec<(IsoLiteralExtractionResult, TextSource)>>;

fn deref(&self) -> &Self::Target {
&self.0
Expand Down
2 changes: 1 addition & 1 deletion crates/isograph_lsp/src/semantic_tokens/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub fn on_semantic_token_full_request(

let file_path = text_document.uri.path().intern();
let text_source = TextSource {
path: file_path.into(),
relative_path_to_source_file: file_path.into(),
span: Some(Span::new(
iso_literal_start_index as u32,
(iso_literal_start_index + iso_literal_text.len()) as u32,
Expand Down
2 changes: 1 addition & 1 deletion crates/tests/tests/directives_deserialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn unwrap_directive(

fn parse_mutation(source: &str) -> Result<Vec<ExposeFieldDirective>, Box<dyn Error>> {
let text_source = TextSource {
path: "dummy".intern().into(),
relative_path_to_source_file: "dummy".intern().into(),
span: None,
};
let document =
Expand Down

0 comments on commit d8e1c85

Please sign in to comment.