Move code into the platform
Co-Authored-By: Joseph T. Lyons <19867440+JosephTLyons@users.noreply.github.com>
This commit is contained in:
parent
87c1b190a8
commit
a128439699
5 changed files with 44 additions and 10 deletions
|
@ -68,6 +68,7 @@ pub trait Platform: Send + Sync {
|
||||||
fn write_to_clipboard(&self, item: ClipboardItem);
|
fn write_to_clipboard(&self, item: ClipboardItem);
|
||||||
fn read_from_clipboard(&self) -> Option<ClipboardItem>;
|
fn read_from_clipboard(&self) -> Option<ClipboardItem>;
|
||||||
fn open_url(&self, url: &str);
|
fn open_url(&self, url: &str);
|
||||||
|
fn convert_to_shortened_path(&self, path: &Path) -> PathBuf;
|
||||||
|
|
||||||
fn write_credentials(&self, url: &str, username: &str, password: &[u8]) -> Result<()>;
|
fn write_credentials(&self, url: &str, username: &str, password: &[u8]) -> Result<()>;
|
||||||
fn read_credentials(&self, url: &str) -> Result<Option<(String, Vec<u8>)>>;
|
fn read_credentials(&self, url: &str) -> Result<Option<(String, Vec<u8>)>>;
|
||||||
|
|
|
@ -674,6 +674,18 @@ impl platform::Platform for MacPlatform {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn convert_to_shortened_path(&self, path: &Path) -> PathBuf {
|
||||||
|
match path.strip_prefix(util::paths::HOME.as_path()) {
|
||||||
|
Ok(relative_path) => {
|
||||||
|
let mut shortened_path = PathBuf::new();
|
||||||
|
shortened_path.push("~");
|
||||||
|
shortened_path.push(relative_path);
|
||||||
|
shortened_path
|
||||||
|
}
|
||||||
|
Err(_) => path.to_path_buf(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn write_credentials(&self, url: &str, username: &str, password: &[u8]) -> Result<()> {
|
fn write_credentials(&self, url: &str, username: &str, password: &[u8]) -> Result<()> {
|
||||||
let url = CFString::from(url);
|
let url = CFString::from(url);
|
||||||
let username = CFString::from(username);
|
let username = CFString::from(username);
|
||||||
|
@ -1113,4 +1125,23 @@ mod tests {
|
||||||
platform.pasteboard = unsafe { NSPasteboard::pasteboardWithUniqueName(nil) };
|
platform.pasteboard = unsafe { NSPasteboard::pasteboardWithUniqueName(nil) };
|
||||||
platform
|
platform
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_convert_to_shortened_path() {
|
||||||
|
let platform = build_platform();
|
||||||
|
|
||||||
|
let full_path: PathBuf = [
|
||||||
|
util::paths::HOME.to_string_lossy().to_string(),
|
||||||
|
"a".to_string(),
|
||||||
|
"b".to_string(),
|
||||||
|
"c".to_string(),
|
||||||
|
]
|
||||||
|
.iter()
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let shortened_path_actual = platform.convert_to_shortened_path(&full_path);
|
||||||
|
let shortened_path_expected = PathBuf::from("~/a/b/c");
|
||||||
|
|
||||||
|
assert_eq!(shortened_path_actual, shortened_path_expected);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -175,6 +175,10 @@ impl super::Platform for Platform {
|
||||||
|
|
||||||
fn open_url(&self, _: &str) {}
|
fn open_url(&self, _: &str) {}
|
||||||
|
|
||||||
|
fn convert_to_shortened_path(&self, _path: &Path) -> PathBuf {
|
||||||
|
PathBuf::new()
|
||||||
|
}
|
||||||
|
|
||||||
fn write_credentials(&self, _: &str, _: &str, _: &[u8]) -> Result<()> {
|
fn write_credentials(&self, _: &str, _: &str, _: &[u8]) -> Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::Path;
|
||||||
|
|
||||||
use fuzzy::StringMatch;
|
use fuzzy::StringMatch;
|
||||||
use gpui::{
|
use gpui::{
|
||||||
|
@ -55,20 +55,17 @@ pub struct HighlightedWorkspaceLocation {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HighlightedWorkspaceLocation {
|
impl HighlightedWorkspaceLocation {
|
||||||
pub fn new(string_match: &StringMatch, location: &WorkspaceLocation) -> Self {
|
pub fn new(
|
||||||
|
string_match: &StringMatch,
|
||||||
|
location: &WorkspaceLocation,
|
||||||
|
cx: &gpui::AppContext,
|
||||||
|
) -> Self {
|
||||||
let mut path_start_offset = 0;
|
let mut path_start_offset = 0;
|
||||||
let (names, paths): (Vec<_>, Vec<_>) = location
|
let (names, paths): (Vec<_>, Vec<_>) = location
|
||||||
.paths()
|
.paths()
|
||||||
.iter()
|
.iter()
|
||||||
.map(|path| {
|
.map(|path| {
|
||||||
let mut full_path = PathBuf::new();
|
let full_path = cx.platform().convert_to_shortened_path(&path);
|
||||||
if path.starts_with(util::paths::HOME.as_path()) {
|
|
||||||
full_path.push("~");
|
|
||||||
full_path.push(path.strip_prefix(util::paths::HOME.as_path()).unwrap());
|
|
||||||
} else {
|
|
||||||
full_path.push(path)
|
|
||||||
}
|
|
||||||
|
|
||||||
let highlighted_text = Self::highlights_for_path(
|
let highlighted_text = Self::highlights_for_path(
|
||||||
full_path.as_ref(),
|
full_path.as_ref(),
|
||||||
&string_match.positions,
|
&string_match.positions,
|
||||||
|
|
|
@ -192,6 +192,7 @@ impl PickerDelegate for RecentProjectsView {
|
||||||
let highlighted_location = HighlightedWorkspaceLocation::new(
|
let highlighted_location = HighlightedWorkspaceLocation::new(
|
||||||
&string_match,
|
&string_match,
|
||||||
&self.workspace_locations[string_match.candidate_id],
|
&self.workspace_locations[string_match.candidate_id],
|
||||||
|
&cx,
|
||||||
);
|
);
|
||||||
|
|
||||||
Flex::column()
|
Flex::column()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue