-
-
Notifications
You must be signed in to change notification settings - Fork 37
Absolute path support #299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ffrostfall
wants to merge
7
commits into
seaofvoices:main
Choose a base branch
from
ffrostfall:absolute-path-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9ebc0c1
Add absolute path support to roblox string requires
ffrostfall 8864076
Absolute path support pt. 2
ffrostfall 1dc3d00
Merge branch 'absolute-path-support' of https://github.com/ffrostfall…
ffrostfall cfa39a4
Use darklua normalize_path
ffrostfall 47bfeed
Merge branch 'main' into absolute-path-support
ffrostfall 9175ae0
remove redundant pathbuf, switch to has_root
ffrostfall c2a2b8c
Improve code
ffrostfall File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| use std::path::{Path, PathBuf}; | ||
| use std::path::{self, Component, Path, PathBuf}; | ||
|
|
||
| use serde::{Deserialize, Serialize}; | ||
|
|
||
|
|
@@ -23,6 +23,40 @@ struct RojoSourcemapNode { | |
| parent_id: NodeId, | ||
| } | ||
|
|
||
| pub fn normalize_path(path: &Path) -> PathBuf { | ||
| let mut components = path.components().peekable(); | ||
| let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() { | ||
| components.next(); | ||
| PathBuf::from(c.as_os_str()) | ||
| } else { | ||
| PathBuf::new() | ||
| }; | ||
|
|
||
| for component in components { | ||
| match component { | ||
| Component::Prefix(..) => unreachable!(), | ||
| Component::RootDir => { | ||
| ret.push(Component::RootDir); | ||
| } | ||
| Component::CurDir => {} | ||
| Component::ParentDir => { | ||
| if ret.ends_with(Component::ParentDir) { | ||
| ret.push(Component::ParentDir); | ||
| } else { | ||
| let popped = ret.pop(); | ||
| if !popped && !ret.has_root() { | ||
| ret.push(Component::ParentDir); | ||
| } | ||
| } | ||
| } | ||
| Component::Normal(c) => { | ||
| ret.push(c); | ||
| } | ||
| } | ||
| } | ||
| ret | ||
| } | ||
|
|
||
| impl RojoSourcemapNode { | ||
| fn initialize(mut self, relative_to: &Path) -> Self { | ||
| let mut queue = vec![&mut self]; | ||
|
|
@@ -31,7 +65,9 @@ impl RojoSourcemapNode { | |
| while let Some(node) = queue.pop() { | ||
| node.id = index; | ||
| for file_path in &mut node.file_paths { | ||
| *file_path = utils::normalize_path(relative_to.join(&file_path)); | ||
| if file_path.is_relative() { | ||
| *file_path = utils::normalize_path(relative_to.join(&file_path)); | ||
| } | ||
| } | ||
| for child in &mut node.children { | ||
| child.parent_id = index; | ||
|
|
@@ -128,8 +164,25 @@ impl RojoSourcemap { | |
| let from_file = from_file.as_ref(); | ||
| let target_file = target_file.as_ref(); | ||
|
|
||
| let binding = from_file.to_path_buf().join(target_file); | ||
ffrostfall marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| let normalized = normalize_path(binding.as_path()); | ||
|
|
||
| let from_node = self.find_node(from_file)?; | ||
| let target_node = self.find_node(target_file)?; | ||
| let target_node = self.find_node(if from_file.is_absolute() { | ||
| if from_file.is_absolute() { | ||
ffrostfall marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| log::trace!( | ||
| "in absolute rqeuire mode, normalized: {} -> {}", | ||
| from_file.display(), | ||
| normalized.display() | ||
| ); | ||
| normalized.as_path() | ||
| } else { | ||
| target_file | ||
| } | ||
| } else { | ||
| target_file | ||
| })?; | ||
|
|
||
| let from_ancestors = self.hierarchy(from_node); | ||
| let target_ancestors = self.hierarchy(target_node); | ||
|
|
@@ -219,10 +272,17 @@ impl RojoSourcemap { | |
| ids | ||
| } | ||
|
|
||
| fn find_node(&self, path: &Path) -> Option<&RojoSourcemapNode> { | ||
| self.root_node | ||
| .iter() | ||
| .find(|node| node.file_paths.iter().any(|file_path| file_path == path)) | ||
| fn find_node(&self, target_path: &Path) -> Option<&RojoSourcemapNode> { | ||
| self.root_node.iter().find(|node| { | ||
| node.file_paths.iter().any(|file_path| { | ||
| if file_path.is_absolute() { | ||
| file_path.to_path_buf() | ||
| == path::absolute(target_path).expect("failed to convert") | ||
|
||
| } else { | ||
| file_path == target_path | ||
| } | ||
| }) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -277,6 +337,8 @@ mod test { | |
| ); | ||
| } | ||
|
|
||
| // Relative | ||
|
|
||
| #[test] | ||
| fn from_sibling_to_sibling_module() { | ||
| let sourcemap = new_sourcemap( | ||
|
|
@@ -395,5 +457,200 @@ mod test { | |
| script_path(&["parent"]) | ||
| ); | ||
| } | ||
|
|
||
| // Absolute | ||
|
|
||
| #[test] | ||
| fn abs_from_sibling_to_sibling_module() { | ||
| let sourcemap = new_sourcemap( | ||
| r#"{ | ||
| "name": "Project", | ||
| "className": "ModuleScript", | ||
| "filePaths": ["C:/projects/thing/src/init.lua", "C:/projects/thing/default.project.json"], | ||
| "children": [ | ||
| { | ||
| "name": "main", | ||
| "className": "ModuleScript", | ||
| "filePaths": ["C:/projects/thing/src/main.lua"] | ||
| }, | ||
| { | ||
| "name": "value", | ||
| "className": "ModuleScript", | ||
| "filePaths": ["C:/projects/thing/src/value.lua"] | ||
| } | ||
| ] | ||
| }"#, | ||
| ); | ||
| pretty_assertions::assert_eq!( | ||
| sourcemap | ||
| .get_instance_path( | ||
| "C:/projects/thing/src/main.lua", | ||
| "C:/projects/thing/src/value.lua" | ||
| ) | ||
| .unwrap(), | ||
| script_path(&["parent", "value"]) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn abs_from_sibling_to_nested_sibling_module() { | ||
| let sourcemap = new_sourcemap( | ||
| r#"{ | ||
| "name": "Project", | ||
| "className": "ModuleScript", | ||
| "filePaths": ["C:/projects/thing/src/init.lua", "C:/projects/thing/default.project.json"], | ||
| "children": [ | ||
| { | ||
| "name": "main", | ||
| "className": "ModuleScript", | ||
| "filePaths": ["C:/projects/thing/src/main.lua"] | ||
| }, | ||
| { | ||
| "name": "Lib", | ||
| "className": "Folder", | ||
| "children": [ | ||
| { | ||
| "name": "format", | ||
| "className": "ModuleScript", | ||
| "filePaths": ["C:/projects/thing/src/Lib/format.lua"] | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| }"#, | ||
| ); | ||
| pretty_assertions::assert_eq!( | ||
| sourcemap | ||
| .get_instance_path( | ||
| "C:/projects/thing/src/main.lua", | ||
| "C:/projects/thing/src/Lib/format.lua" | ||
| ) | ||
| .unwrap(), | ||
| script_path(&["parent", "Lib", "format"]) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn abs_from_child_require_parent() { | ||
| let sourcemap = new_sourcemap( | ||
| r#"{ | ||
| "name": "Project", | ||
| "className": "ModuleScript", | ||
| "filePaths": ["C:/projects/thing/src/init.lua", "C:/projects/thing/default.project.json"], | ||
| "children": [ | ||
| { | ||
| "name": "main", | ||
| "className": "ModuleScript", | ||
| "filePaths": ["C:/projects/thing/src/main.lua"] | ||
| } | ||
| ] | ||
| }"#, | ||
| ); | ||
| pretty_assertions::assert_eq!( | ||
| sourcemap | ||
| .get_instance_path( | ||
| "C:/projects/thing/src/main.lua", | ||
| "C:/projects/thing/src/init.lua" | ||
| ) | ||
| .unwrap(), | ||
| script_path(&["parent"]) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn abs_from_child_require_parent_nested() { | ||
| let sourcemap = new_sourcemap( | ||
| r#"{ | ||
| "name": "Project", | ||
| "className": "ModuleScript", | ||
| "filePaths": ["C:/projects/thing/src/init.lua", "C:/projects/thing/default.project.json"], | ||
| "children": [ | ||
| { | ||
| "name": "Sub", | ||
| "className": "ModuleScript", | ||
| "filePaths": ["C:/projects/thing/src/Sub/init.lua"], | ||
| "children": [ | ||
| { | ||
| "name": "test", | ||
| "className": "ModuleScript", | ||
| "filePaths": ["C:/projects/thing/src/Sub/test.lua"] | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| }"#, | ||
| ); | ||
| pretty_assertions::assert_eq!( | ||
| sourcemap | ||
| .get_instance_path( | ||
| "C:/projects/thing/src/Sub/test.lua", | ||
| "C:/projects/thing/src/Sub/init.lua" | ||
| ) | ||
| .unwrap(), | ||
| script_path(&["parent"]) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn rel_from_absolute() { | ||
| let sourcemap = new_sourcemap( | ||
| r#"{ | ||
| "name": "Project", | ||
| "className": "ModuleScript", | ||
| "filePaths": ["C:/projects/thing/src/init.luau", "C:/projects/thing/default.project.json"], | ||
| "children": [ | ||
| { | ||
| "name": "Sub", | ||
| "className": "ModuleScript", | ||
| "filePaths": ["C:/projects/thing/src/Sub/init.luau"], | ||
| "children": [ | ||
| { | ||
| "name": "test", | ||
| "className": "ModuleScript", | ||
| "filePaths": ["C:/projects/thing/src/Sub/test.luau"] | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| }"#, | ||
| ); | ||
| pretty_assertions::assert_eq!( | ||
| sourcemap | ||
| .get_instance_path("C:/projects/thing/src/Sub/test.luau", "../init.luau") | ||
| .unwrap(), | ||
| script_path(&["parent"]) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn nested_rel_from_absolute() { | ||
| let sourcemap = new_sourcemap( | ||
| r#"{ | ||
| "name": "Project", | ||
| "className": "ModuleScript", | ||
| "filePaths": ["C:/projects/thing/src/init.luau", "C:/projects/thing/default.project.json"], | ||
| "children": [ | ||
| { | ||
| "name": "Sub", | ||
| "className": "ModuleScript", | ||
| "filePaths": ["C:/projects/thing/src/Sub/init.luau"], | ||
| "children": [ | ||
| { | ||
| "name": "test", | ||
| "className": "ModuleScript", | ||
| "filePaths": ["C:/projects/thing/src/Sub/test.luau"] | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| }"#, | ||
| ); | ||
| pretty_assertions::assert_eq!( | ||
| sourcemap | ||
| .get_instance_path("C:/projects/thing/src/Sub/test.luau", "../init.luau") | ||
| .unwrap(), | ||
| script_path(&["parent"]) | ||
| ); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.