Visual Studio Code, rebuilt in Rust
Rust
fix(windows): read debug and language paths from file uris
rokartur6e81ee0d6b09
diff --git a/native/crates/app/src/debug.rs b/native/crates/app/src/debug.rsindex 6b00108d537..68a2ce78dd5 100644--- a/native/crates/app/src/debug.rs+++ b/native/crates/app/src/debug.rs@@ -830,7 +830,7 @@ pub fn base_breakpoint(id: String, options: &Value) -> BaseBreakpoint {/// The folder path of a `file` `UriComponents` the host passes as a workspace folder.pub fn configuration_folder(uri: &Value) -> Option<std::path::PathBuf> {- (uri["scheme"] == "file").then(|| uri["path"].as_str().map(std::path::PathBuf::from)).flatten()+ (uri["scheme"] == "file").then(|| std::path::PathBuf::from(crate::uri::json_fs_path(uri)))}/// `URI.parse` of a file URI string; other schemes keep the text after the scheme as their path.@@ -873,7 +873,7 @@ fn uri_to_json(uri: &Value) -> Value {let mut object = Map::new();object.insert("$mid".into(), json!(1));if uri["scheme"] == "file" {- object.insert("fsPath".into(), uri["path"].clone());+ object.insert("fsPath".into(), json!(crate::uri::json_fs_path(uri)));}object.insert("external".into(), json!(uri_string(uri)));for key in ["path", "scheme", "authority", "query", "fragment"] {diff --git a/native/crates/app/src/debug/session.rs b/native/crates/app/src/debug/session.rsindex 1e3400dd1a2..7a6a59174c2 100644--- a/native/crates/app/src/debug/session.rs+++ b/native/crates/app/src/debug/session.rs@@ -50,14 +50,14 @@ impl StackFrame {/// The file the frame is in, when it is on disk.pub fn path(&self) -> Option<&str> {let reference = self.source["sourceReference"].as_i64().unwrap_or(0);- self.source["path"].as_str().filter(|path| reference <= 0 && path.starts_with('/'))+ self.source["path"].as_str().filter(|path| reference <= 0 && std::path::Path::new(path).is_absolute())}/// `Source.name`, else the path's last segment.pub fn source_name(&self) -> String {match self.source["name"].as_str() {Some(name) => name.to_owned(),- None => self.source["path"].as_str().and_then(|path| path.rsplit('/').next()).unwrap_or("Unknown Source").to_owned(),+ None => self.source["path"].as_str().and_then(|path| path.rsplit(std::path::is_separator).next()).unwrap_or("Unknown Source").to_owned(),}}@@ -494,8 +494,8 @@ fn path_to_uri(source: &mut Value) {let rest = rest.strip_prefix("//").unwrap_or(rest);let (authority, path) = rest.split_at(rest.find('/').unwrap_or(rest.len()));json!({ "$mid": 1, "scheme": scheme, "authority": authority, "path": percent_decode(path), "query": "", "fragment": "" })- } else if path.starts_with('/') {- json!({ "$mid": 1, "scheme": "file", "authority": "", "path": path, "query": "", "fragment": "" })+ } else if std::path::Path::new(path).is_absolute() {+ crate::uri::Uri::file(path).to_json()} else {return;};@@ -509,7 +509,7 @@ fn uri_to_path(source: &mut Value) {}let uri = source["path"].clone();source["path"] = match uri["scheme"].as_str() {- Some("file") => uri["path"].clone(),+ Some("file") => json!(crate::uri::json_fs_path(&uri)),_ => json!(crate::debug::uri_string(&uri)),};}diff --git a/native/crates/app/src/language_features.rs b/native/crates/app/src/language_features.rsindex 03cd714240c..93cee63be37 100644--- a/native/crates/app/src/language_features.rs+++ b/native/crates/app/src/language_features.rs@@ -136,7 +136,7 @@ pub fn score(selector: &Value, uri: &Value, language: &str, is_synchronized: booreturn 0;}if let Some(pattern) = filter.get("pattern").filter(|pattern| pattern.is_object() || pattern.as_str().is_some_and(|pattern| !pattern.is_empty())) {- if !matches_pattern(pattern, &fs_path(uri)) {+ if !matches_pattern(pattern, &crate::uri::json_fs_path(uri)) {return 0;}value = 10;@@ -153,18 +153,12 @@ fn matches_pattern(pattern: &Value, path: &str) -> bool {if let Some(pattern) = pattern.as_str() {return pattern == path || files::glob::match_pattern(pattern, path, options);}- let base = pattern["baseUri"].as_object().map(|_| fs_path(&pattern["baseUri"])).or_else(|| pattern["base"].as_str().map(String::from)).unwrap_or_default();+ let base = pattern["baseUri"].as_object().map(|_| crate::uri::json_fs_path(&pattern["baseUri"])).or_else(|| pattern["base"].as_str().map(String::from)).unwrap_or_default();let base = base.trim_end_matches(std::path::is_separator);let Some(relative) = path.strip_prefix(base).and_then(|rest| rest.strip_prefix(std::path::is_separator)) else { return false };files::glob::match_pattern(pattern["pattern"].as_str().unwrap_or_default(), relative, options)}-/// `URI.fsPath`.-fn fs_path(uri: &Value) -> String {- let part = |key: &str| uri[key].as_str().unwrap_or_default();- crate::uri::fs_path(part("scheme"), part("authority"), part("path"))-}-fn is_exclusive(selector: &Value) -> bool {match selector {Value::Array(filters) => filters.iter().all(is_exclusive),diff --git a/native/crates/app/src/uri.rs b/native/crates/app/src/uri.rsindex 9889d07d0a5..b48ad0d2190 100644--- a/native/crates/app/src/uri.rs+++ b/native/crates/app/src/uri.rs@@ -161,6 +161,12 @@ pub fn fs_path(scheme: &str, authority: &str, path: &str) -> String {fs_path_on(scheme, authority, path, cfg!(windows))}+/// `fsPath` of `UriComponents` as JSON.+pub fn json_fs_path(uri: &Value) -> String {+ let part = |key: &str| uri[key].as_str().unwrap_or_default();+ fs_path(part("scheme"), part("authority"), part("path"))+}+pub(crate) fn fs_path_on(scheme: &str, authority: &str, path: &str, windows: bool) -> String {let bytes = path.as_bytes();let value = if !authority.is_empty() && path.len() > 1 && scheme == "file" {diff --git a/native/crates/app/src/workbench/breakpoint_widget.rs b/native/crates/app/src/workbench/breakpoint_widget.rsindex 8f713eb8563..b036a63e86a 100644--- a/native/crates/app/src/workbench/breakpoint_widget.rs+++ b/native/crates/app/src/workbench/breakpoint_widget.rs@@ -657,8 +657,8 @@ impl Workbench {/// `buildBreakpointOptions`: `{relative path}: {line}`.fn trigger_label(&self, breakpoint: &Breakpoint) -> String {- let path = std::path::Path::new(breakpoint.uri["path"].as_str().unwrap_or_default());- format!("{}: {}", crate::quick_input::path_label(path, self.workspace()), breakpoint.line_number())+ let path = crate::uri::json_fs_path(&breakpoint.uri);+ format!("{}: {}", crate::quick_input::path_label(std::path::Path::new(&path), self.workspace()), breakpoint.line_number())}fn show_trigger_breakpoint_options(&mut self, position: gpui::Point<gpui::Pixels>, window: &mut Window, cx: &mut Context<Self>) {diff --git a/native/crates/app/src/workbench/debug_commands.rs b/native/crates/app/src/workbench/debug_commands.rsindex 8c857694d78..aa4b0e78ce2 100644--- a/native/crates/app/src/workbench/debug_commands.rs+++ b/native/crates/app/src/workbench/debug_commands.rs@@ -336,7 +336,7 @@ impl Workbench {/// debugService.ts `runTo`, from Run to Cursor and the gutter's Run to Line.pub(super) fn run_to_line(&mut self, uri: &Value, line: u32, column: Option<u32>, window: &mut Window, cx: &mut Context<Self>) {let uri_string = crate::debug::uri_string(uri);- let path = PathBuf::from(uri["path"].as_str().unwrap_or_default());+ let path = PathBuf::from(crate::uri::json_fs_path(uri));let exists = self.debug.model.breakpoints_in(&uri_string).any(|breakpoint| breakpoint.line_number() == line && (column.is_none() || breakpoint.column() == column));let mut breakpoint = None;let mut sends = Vec::new();diff --git a/native/crates/app/src/workbench/debug_service.rs b/native/crates/app/src/workbench/debug_service.rsindex 75879529a28..576547ce0bc 100644--- a/native/crates/app/src/workbench/debug_service.rs+++ b/native/crates/app/src/workbench/debug_service.rs@@ -848,7 +848,8 @@ impl Workbench {fn breakpoints_at_top_frame(&self, session_id: &str, thread_id: i64) -> Vec<String> {let Some(frame) = self.debug.session(session_id).and_then(|session| session.thread(thread_id)).and_then(|thread| thread.frames.first()) else { return Vec::new() };let Some(path) = frame.path() else { return Vec::new() };- let in_file = self.debug.model.breakpoints.iter().filter(|breakpoint| breakpoint.uri["scheme"] == "file" && breakpoint.uri["path"] == path);+ let frame_uri = crate::editor::uri_string("file", std::path::Path::new(path));+ let in_file = self.debug.model.breakpoints.iter().filter(|breakpoint| breakpoint.uri["scheme"] == "file" && breakpoint.uri_string() == frame_uri);let at_frame = in_file.filter(|breakpoint| (frame.line..=frame.end_line).contains(&breakpoint.line_number()) && breakpoint.column().is_none_or(|column| (frame.column..=frame.end_column).contains(&column)));at_frame.map(|breakpoint| breakpoint.id().to_owned()).collect()}@@ -1520,7 +1521,7 @@ fn launch_vs_code_args(arguments: &Value) -> Vec<String> {/// The DAP `Source` of a breakpoint's resource: a file's path, else the URI string.pub(super) fn source_of_uri(uri: &Value) -> Value {let path = match uri["scheme"].as_str() {- Some("file") => uri["path"].clone(),+ Some("file") => json!(crate::uri::json_fs_path(uri)),_ => json!(crate::debug::uri_string(uri)),};let name = uri["path"].as_str().and_then(|path| path.rsplit('/').next()).unwrap_or_default();diff --git a/native/crates/app/src/workbench/debug_view.rs b/native/crates/app/src/workbench/debug_view.rsindex eb9c9fba79f..59221f9b3bf 100644--- a/native/crates/app/src/workbench/debug_view.rs+++ b/native/crates/app/src/workbench/debug_view.rs@@ -867,9 +867,9 @@ impl Workbench {let id = breakpoint.id().to_owned();let dirty = self.editors.editors.iter().chain(&self.editors.models).any(|editor| editor.document().uri_string() == breakpoint.uri_string() && editor.is_dirty());let (icon, _) = super::breakpoint_widget::breakpoint_icon(breakpoint, model, debug_active, dirty);- let path = breakpoint.uri["path"].as_str().unwrap_or_default();- let name = path.rsplit('/').next().unwrap_or_default().to_owned();- let folder = self.relative_folder_label(path);+ let path = crate::uri::json_fs_path(&breakpoint.uri);+ let name = path.rsplit(std::path::is_separator).next().unwrap_or_default().to_owned();+ let folder = self.relative_folder_label(&path);let line = breakpoint.line_number();let uri = breakpoint.uri.clone();rows.push(@@ -880,7 +880,7 @@ impl Workbench {.child(div().flex_1().min_w_0().ml(px(6.)).overflow_hidden().whitespace_nowrap().text_ellipsis().text_size(px(12.)).text_color(description).child(folder)).child(div().flex_none().mr(px(8.)).px(px(4.)).rounded(px(11.)).text_size(px(11.)).bg(self.color("badge.background")).text_color(self.color("badge.foreground")).child(line.to_string())).on_click(cx.listener(move |workbench, _: &ClickEvent, window, cx| {- let Some(path) = uri["path"].as_str().map(std::path::PathBuf::from).filter(|_| uri["scheme"] == "file") else { return };+ let Some(path) = (uri["scheme"] == "file").then(|| std::path::PathBuf::from(crate::uri::json_fs_path(&uri))) else { return };workbench.open_file(path.clone(), false, window, cx);let line = line as usize;workbench.select_range(&path, crate::anything_quick_access::TextRange { start_line_number: line, start_column: 1, end_line_number: line, end_column: 1 }, cx);