2565: Fixed syntax highlighting not refreshing on windows. r=matklad a=omerbenamram

I was encoutering the same probelm described in #1690.

It seems that events initiated by the frontend with `rust-analyzer/decorationsRequest` would go through.
So whenever a user switches tabs, highlighting will update.

However, when decorations are initiated by a notification with `rust-analyzer/publishDecorations`, it would fail on this check here 6cbd8a4a4b/editors/code/src/notifications/publish_decorations.ts (L15) (`targetEditor` will always be `undefined`).

This is because it's trying to match the uri `rust-analyzer` sends (which uses an uppercase drive letter) to the uri provided at `editor.document.uri.toString()`, which is both escaped (uses `%3a` for `:`), and uses a lowercase letter drive.

Aparrently this was an issue for some other extensions aswell - https://github.com/Microsoft/vscode/issues/68325.

But this is the defined behavior - c110d84460/src/vs/vscode.d.ts (L1304)

This fix is only relevant for windows.
I've opted for a server-side fix, since rust will always return uppercase letters for drives, there seems to be no other easy solution than manipulating the Url string before sending it to the frontend.


Closes #1690.

Co-authored-by: Omer Ben-Amram <omerbenamram@gmail.com>
This commit is contained in:
bors[bot] 2019-12-15 16:35:23 +00:00 committed by GitHub
commit 4e24b25c66
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 74 additions and 5 deletions

View file

@ -17,11 +17,13 @@ use ra_project_model::{get_rustc_cfg_options, ProjectWorkspace};
use ra_vfs::{LineEndings, RootEntry, Vfs, VfsChange, VfsFile, VfsRoot, VfsTask, Watch};
use ra_vfs_glob::{Glob, RustPackageFilterBuilder};
use relative_path::RelativePathBuf;
use std::path::{Component, Prefix};
use crate::{
main_loop::pending_requests::{CompletedRequest, LatestRequests},
LspError, Result,
};
use std::str::FromStr;
#[derive(Debug, Clone)]
pub struct Options {
@ -233,8 +235,8 @@ impl WorldSnapshot {
pub fn file_id_to_uri(&self, id: FileId) -> Result<Url> {
let path = self.vfs.read().file2path(VfsFile(id.0));
let url = Url::from_file_path(&path)
.map_err(|_| format!("can't convert path to url: {}", path.display()))?;
let url = url_from_path_with_drive_lowercasing(path)?;
Ok(url)
}
@ -279,3 +281,65 @@ impl WorldSnapshot {
self.analysis.feature_flags()
}
}
/// Returns a `Url` object from a given path, will lowercase drive letters if present.
/// This will only happen when processing windows paths.
///
/// When processing non-windows path, this is essentially the same as `Url::from_file_path`.
fn url_from_path_with_drive_lowercasing(path: impl AsRef<Path>) -> Result<Url> {
let component_has_windows_drive = path
.as_ref()
.components()
.find(|comp| {
if let Component::Prefix(c) = comp {
match c.kind() {
Prefix::Disk(_) | Prefix::VerbatimDisk(_) => return true,
_ => return false,
}
}
false
})
.is_some();
// VSCode expects drive letters to be lowercased, where rust will uppercase the drive letters.
if component_has_windows_drive {
let url_original = Url::from_file_path(&path)
.map_err(|_| format!("can't convert path to url: {}", path.as_ref().display()))?;
let drive_partition: Vec<&str> = url_original.as_str().rsplitn(2, ':').collect();
// There is a drive partition, but we never found a colon.
// This should not happen, but in this case we just pass it through.
if drive_partition.len() == 1 {
return Ok(url_original);
}
let joined = drive_partition[1].to_ascii_lowercase() + ":" + drive_partition[0];
let url = Url::from_str(&joined).expect("This came from a valid `Url`");
Ok(url)
} else {
Ok(Url::from_file_path(&path)
.map_err(|_| format!("can't convert path to url: {}", path.as_ref().display()))?)
}
}
// `Url` is not able to parse windows paths on unix machines.
#[cfg(target_os = "windows")]
#[cfg(test)]
mod path_conversion_windows_tests {
use super::url_from_path_with_drive_lowercasing;
#[test]
fn test_lowercase_drive_letter_with_drive() {
let url = url_from_path_with_drive_lowercasing("C:\\Test").unwrap();
assert_eq!(url.to_string(), "file:///c:/Test");
}
#[test]
fn test_drive_without_colon_passthrough() {
let url = url_from_path_with_drive_lowercasing(r#"\\localhost\C$\my_dir"#).unwrap();
assert_eq!(url.to_string(), "file://localhost/C$/my_dir");
}
}

View file

@ -9,11 +9,16 @@ export interface PublishDecorationsParams {
}
export function handle(params: PublishDecorationsParams) {
const targetEditor = vscode.window.visibleTextEditors.find(
editor => editor.document.uri.toString() === params.uri,
);
const targetEditor = vscode.window.visibleTextEditors.find(editor => {
const unescapedUri = unescape(editor.document.uri.toString());
// Unescaped URI looks like:
// file:///c:/Workspace/ra-test/src/main.rs
return unescapedUri === params.uri;
});
if (!Server.config.highlightingOn || !targetEditor) {
return;
}
Server.highlighter.setHighlights(targetEditor, params.decorations);
}