Last active
September 22, 2024 20:44
-
-
Save dbarnett/728434b688673c6877adbafdf1f2ffd8 to your computer and use it in GitHub Desktop.
Using Arc in https://github.com/dbarnett/vim-plugin-metadata/blob/3f422df/lib/src/parser/mod.rs
This file contains 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
diff --git a/lib/src/parser/mod.rs b/lib/src/parser/mod.rs | |
index 032c79870f..7d5670577f 100644 | |
--- a/lib/src/parser/mod.rs | |
+++ b/lib/src/parser/mod.rs | |
@@ -1,8 +1,9 @@ | |
use crate::data::VimModule; | |
use crate::{Error, VimNode, VimPlugin}; | |
use std::ffi::OsStr; | |
-use std::path::{Path, PathBuf}; | |
+use std::path::Path; | |
use std::{fs, str}; | |
+use std::sync::Arc; | |
use tree_sitter::{Parser, Point}; | |
use treenodes::TreeNodeMetadata; | |
use walkdir::WalkDir; | |
@@ -43,14 +44,15 @@ | |
} | |
/// Parses all supported metadata from a single plugin at the given path. | |
- pub fn parse_plugin_dir<P: AsRef<Path> + Copy>(&mut self, path: P) -> crate::Result<VimPlugin> { | |
+ pub fn parse_plugin_dir<P: AsRef<Path>>(&mut self, path: P) -> crate::Result<VimPlugin> { | |
let mut modules: Vec<VimModule> = Vec::new(); | |
- let path_depth = path.as_ref().iter().count(); | |
- let walker = WalkDir::new(path) | |
+ let path = Arc::new(path.as_ref().to_owned()); | |
+ let path2 = path.clone(); | |
+ let walker = WalkDir::new(path.clone().as_ref()) | |
.follow_links(true) | |
.sort_by_key(move |e| { | |
- let relative_path = e.path().iter().skip(path_depth).collect::<PathBuf>(); | |
- let (section_index, mut depth) = match order_in_sections(relative_path.as_path()) { | |
+ let relative_path = e.path().strip_prefix(path.clone().as_ref()).unwrap(); | |
+ let (section_index, mut depth) = match order_in_sections(relative_path) { | |
Some((idx, depth)) => (idx, depth), | |
// Placeholder value for path that will be filtered. | |
None => return (usize::MAX, usize::MAX), | |
@@ -65,7 +67,7 @@ | |
.into_iter(); | |
for entry in walker.filter_entry(|e| { | |
// Filter to only include paths under known section dirs. | |
- let relative_path = e.path().strip_prefix(path).unwrap(); | |
+ let relative_path = e.path().strip_prefix(path2.as_ref()).unwrap(); | |
order_in_sections(relative_path).is_some() | |
}) { | |
let entry = entry?; | |
@@ -74,7 +76,7 @@ | |
{ | |
continue; | |
} | |
- let relative_path = entry.path().strip_prefix(path).unwrap(); | |
+ let relative_path = entry.path().strip_prefix(path2.as_ref()).unwrap(); | |
let module = self.parse_module_file(entry.path())?; | |
// Replace absolute path with one relative to plugin root. | |
let module = VimModule { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment