pub struct SourceCache {
pub files: Files,
pub file_paths: HashMap<FileId, SourcePath>,
pub file_ids: HashMap<SourcePath, NameIdEntry>,
pub import_paths: Vec<PathBuf>,
pub packages: HashMap<FileId, PathBuf>,
pub package_map: Option<PackageMap>,
}Expand description
The source cache handles reading textual data from the file system or other souces and storing it in a Files instance.
While not ideal, we have to make most of the fields public to allow the LSP to perform its own import resolution.
Fields§
§files: FilesThe content of the program sources plus imports.
file_paths: HashMap<FileId, SourcePath>Reverse map from file ids to source paths.
file_ids: HashMap<SourcePath, NameIdEntry>The name-id table, holding file ids stored in the database indexed by source names.
import_paths: Vec<PathBuf>Paths where to look for imports, as included by the user through either the CLI argument
--import-path or the environment variable $NICKEL_IMPORT_PATH.
packages: HashMap<FileId, PathBuf>A table mapping FileIds to the package that they belong to.
Path dependencies have already been canonicalized to absolute paths.
package_map: Option<PackageMap>The map used to resolve package imports.
Implementations§
Source§impl SourceCache
impl SourceCache
pub fn new() -> Self
Sourcepub fn add_import_paths<P>(&mut self, paths: impl Iterator<Item = P>)
pub fn add_import_paths<P>(&mut self, paths: impl Iterator<Item = P>)
Add paths to the import path list, where the resolver is looking for imported files.
Sourcepub fn set_package_map(&mut self, map: PackageMap)
pub fn set_package_map(&mut self, map: PackageMap)
Sets the package map to use for package import resolution.
Sourcepub fn add_file(
&mut self,
path: impl Into<OsString>,
format: InputFormat,
) -> Result<FileId>
pub fn add_file( &mut self, path: impl Into<OsString>, format: InputFormat, ) -> Result<FileId>
Loads a file and adds it to the name-id table.
Uses the normalized path and the modified at timestamp as the name-id table entry. Overrides any existing entry with the same name.
Sourcepub fn get_or_add_file(
&mut self,
path: impl Into<OsString>,
format: InputFormat,
) -> Result<CacheOp<FileId>>
pub fn get_or_add_file( &mut self, path: impl Into<OsString>, format: InputFormat, ) -> Result<CacheOp<FileId>>
Try to retrieve the id of a file from the cache.
If it was not in cache, try to read it and add it as a new entry.
§In memory sources
As a temporary fix for #2362, if a file path
starts with IN_MEMORY_SOURCE_PATH_PREFIX, the suffix is looked up un-normalized value
first, which makes it possible to hit in-memory only sources by importing a path
"{SOURCE_PATH_PREFX}{src_name}". If it can’t be found, it is looked up normally, so that
it doesn’t break strange file names that happen to contain the source path prefix.
It is theoretically possible that if both the source “abc” and the file “{IN_MEMORY_SOURCE_PATH_PREFIX}abc” exist, the source is imported instead of the intended file. However, given the prefix, it just can’t be accidental. As we want to give access to in-memory sources in any case, although this can be surprising, I don’t see any obvious attack scenario here. This fix is also intended to be temporary. If you still need to make sure this doesn’t happen, one way would be to add some randomness to the name of the sources, so that they can’t be predicted beforehand.
Sourcepub fn add_source<T>(
&mut self,
source_name: SourcePath,
source: T,
) -> Result<FileId>where
T: Read,
pub fn add_source<T>(
&mut self,
source_name: SourcePath,
source: T,
) -> Result<FileId>where
T: Read,
Load a source and add it to the name-id table.
Do not check if a source with the same name already exists: if it is the case, Self::add_source will happily will override the old entry in the name-id table.
Sourcepub fn source(&self, id: FileId) -> &str
pub fn source(&self, id: FileId) -> &str
Returns the content of a file.
Panics if the file id is invalid.
Sourcepub fn clone_source(&self, id: FileId) -> Arc<str>
pub fn clone_source(&self, id: FileId) -> Arc<str>
Returns a cloned Arc to the content of the file.
The Arc is here for the LSP, where the background evaluation is handled by background
threads and processes.
Panics if the file id is invalid.
Sourcepub fn add_string(&mut self, source_name: SourcePath, s: String) -> FileId
pub fn add_string(&mut self, source_name: SourcePath, s: String) -> FileId
Loads a new source as a string and add it to the name-id table.
Do not check if a source with the same name already exists: if it is the case, this one
will override the old entry in the name-id table but the old FileId will remain valid.
Sourcepub fn replace_string(&mut self, source_name: SourcePath, s: String) -> FileId
pub fn replace_string(&mut self, source_name: SourcePath, s: String) -> FileId
Loads a new source as a string, replacing any existing source with the same name.
As opposed to CacheHub::replace_string, this method doesn’t update the other caches. It just affects the source cache.
Sourcepub fn close_in_memory_file(
&mut self,
path: PathBuf,
format: InputFormat,
) -> Result<FileCloseResult, FileCloseError>
pub fn close_in_memory_file( &mut self, path: PathBuf, format: InputFormat, ) -> Result<FileCloseResult, FileCloseError>
Closes a file that has been opened in memory and reloads it from the filesystem. Returns the file ID of the replacement file loaded from the filesystem.
Sourcepub fn id_of(&self, name: &SourcePath) -> Option<FileId>
pub fn id_of(&self, name: &SourcePath) -> Option<FileId>
Retrieves the id of a source given a name.
Note that files added via Self::add_file are indexed by their full normalized path (cf normalize_path).
Sourcepub fn files(&self) -> &Files
pub fn files(&self) -> &Files
Gets a reference to the underlying files. Required by the WASM REPL error reporting code and LSP functions.
Sourcepub fn parse_nickel<'ast>(
&self,
alloc: &'ast AstAlloc,
file_id: FileId,
) -> Result<Ast<'ast>, ParseErrors>
pub fn parse_nickel<'ast>( &self, alloc: &'ast AstAlloc, file_id: FileId, ) -> Result<Ast<'ast>, ParseErrors>
Parses a Nickel source without querying nor populating other caches.
Sourcepub fn parse_other(
&self,
pos_table: &mut PosTable,
file_id: FileId,
format: InputFormat,
) -> Result<NickelValue, ParseError>
pub fn parse_other( &self, pos_table: &mut PosTable, file_id: FileId, format: InputFormat, ) -> Result<NickelValue, ParseError>
Parses a source that isn’t Nickel code without querying nor populating the other caches. Support multiple formats.
The Nickel/non Nickel distinction is a bit artificial at the moment, due to the fact that parsing Nickel returns the new crate::ast::Ast, while parsing other formats don’t go through the new AST first but directly deserialize to the legacy crate::term::Term for simplicity and performance reasons.
Once RFC007 is fully implemented, we might clean it up.
§Panic
This function panics if format is InputFormat::Nickel.
Sourcepub fn is_stdlib_module(&self, file: FileId) -> bool
pub fn is_stdlib_module(&self, file: FileId) -> bool
Returns true if a particular file id represents a Nickel standard library file, false otherwise.
Sourcepub fn get_submodule_file_id(&self, module: StdlibModule) -> Option<FileId>
pub fn get_submodule_file_id(&self, module: StdlibModule) -> Option<FileId>
Retrieves the file id for a given standard libray module.
Sourcepub fn stdlib_modules(
&self,
) -> impl Iterator<Item = (StdlibModule, FileId)> + use<>
pub fn stdlib_modules( &self, ) -> impl Iterator<Item = (StdlibModule, FileId)> + use<>
Returns the list of file ids corresponding to the standard library modules.
Sourcepub fn input_format(&self, file_id: FileId) -> Option<InputFormat>
pub fn input_format(&self, file_id: FileId) -> Option<InputFormat>
Return the format of a given source. Returns None if there is no entry in the source
cache for file_id, or if there is no well-defined input format (e.g. for REPL inputs,
field assignments, etc.).
Trait Implementations§
Source§impl Clone for SourceCache
impl Clone for SourceCache
Source§fn clone(&self) -> SourceCache
fn clone(&self) -> SourceCache
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for SourceCache
impl RefUnwindSafe for SourceCache
impl !Send for SourceCache
impl !Sync for SourceCache
impl Unpin for SourceCache
impl UnsafeUnpin for SourceCache
impl UnwindSafe for SourceCache
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T, U> OverflowingInto<U> for Twhere
U: OverflowingFrom<T>,
impl<T, U> OverflowingInto<U> for Twhere
U: OverflowingFrom<T>,
fn overflowing_into(self) -> (U, bool)
Source§impl<D> OwoColorize for D
impl<D> OwoColorize for D
Source§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
Source§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
Source§fn black(&self) -> FgColorDisplay<'_, Black, Self>
fn black(&self) -> FgColorDisplay<'_, Black, Self>
Source§fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
Source§fn red(&self) -> FgColorDisplay<'_, Red, Self>
fn red(&self) -> FgColorDisplay<'_, Red, Self>
Source§fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
Source§fn green(&self) -> FgColorDisplay<'_, Green, Self>
fn green(&self) -> FgColorDisplay<'_, Green, Self>
Source§fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
Source§fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
Source§fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
Source§fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
Source§fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
Source§fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
Source§fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
Source§fn white(&self) -> FgColorDisplay<'_, White, Self>
fn white(&self) -> FgColorDisplay<'_, White, Self>
Source§fn on_white(&self) -> BgColorDisplay<'_, White, Self>
fn on_white(&self) -> BgColorDisplay<'_, White, Self>
Source§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
Source§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
Source§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
Source§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
Source§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
Source§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
Source§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
Source§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
Source§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
Source§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
Source§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
Source§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
Source§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
Source§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
Source§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
Source§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
Source§fn bold(&self) -> BoldDisplay<'_, Self>
fn bold(&self) -> BoldDisplay<'_, Self>
Source§fn dimmed(&self) -> DimDisplay<'_, Self>
fn dimmed(&self) -> DimDisplay<'_, Self>
Source§fn italic(&self) -> ItalicDisplay<'_, Self>
fn italic(&self) -> ItalicDisplay<'_, Self>
Source§fn underline(&self) -> UnderlineDisplay<'_, Self>
fn underline(&self) -> UnderlineDisplay<'_, Self>
Source§fn blink(&self) -> BlinkDisplay<'_, Self>
fn blink(&self) -> BlinkDisplay<'_, Self>
Source§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
Source§fn reversed(&self) -> ReversedDisplay<'_, Self>
fn reversed(&self) -> ReversedDisplay<'_, Self>
Source§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
Source§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg or
a color-specific method, such as OwoColorize::green, Read moreSource§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg or
a color-specific method, such as OwoColorize::on_yellow, Read moreSource§fn fg_rgb<const R: u8, const G: u8, const B: u8>(
&self,
) -> FgColorDisplay<'_, CustomColor<R, G, B>, Self>
fn fg_rgb<const R: u8, const G: u8, const B: u8>( &self, ) -> FgColorDisplay<'_, CustomColor<R, G, B>, Self>
Source§fn bg_rgb<const R: u8, const G: u8, const B: u8>(
&self,
) -> BgColorDisplay<'_, CustomColor<R, G, B>, Self>
fn bg_rgb<const R: u8, const G: u8, const B: u8>( &self, ) -> BgColorDisplay<'_, CustomColor<R, G, B>, Self>
Source§fn truecolor(&self, r: u8, g: u8, b: u8) -> FgDynColorDisplay<'_, Rgb, Self>
fn truecolor(&self, r: u8, g: u8, b: u8) -> FgDynColorDisplay<'_, Rgb, Self>
Source§fn on_truecolor(&self, r: u8, g: u8, b: u8) -> BgDynColorDisplay<'_, Rgb, Self>
fn on_truecolor(&self, r: u8, g: u8, b: u8) -> BgDynColorDisplay<'_, Rgb, Self>
Source§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi Quirk value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);