Skip to content

Commit

Permalink
fix: fix ast and lsp filename in windows os (#1301)
Browse files Browse the repository at this point in the history
fix: fix ast and lsp filename in windows os.

Signed-off-by: he1pa <18012015693@163.com>
  • Loading branch information
He1pa committed May 10, 2024
1 parent 57852c3 commit 46ce7e0
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 43 deletions.
1 change: 1 addition & 0 deletions kclvm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 4 additions & 14 deletions kclvm/ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,20 +197,10 @@ impl<T> Node<T> {
}

pub fn node(node: T, (lo, hi): (Loc, Loc)) -> Self {
let filename = {
#[cfg(target_os = "windows")]
{
kclvm_utils::path::convert_windows_drive_letter(&format!(
"{}",
lo.file.name.prefer_remapped()
))
}

#[cfg(not(target_os = "windows"))]
{
format!("{}", lo.file.name.prefer_remapped())
}
};
let filename = kclvm_utils::path::convert_windows_drive_letter(&format!(
"{}",
lo.file.name.prefer_remapped()
));
Self {
id: AstIndex::default(),
node,
Expand Down
1 change: 1 addition & 0 deletions kclvm/error/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ compiler_base_error = "0.1.6"
compiler_base_macros = "0.1.1"
kclvm-span = {path = "../span"}
kclvm-runtime = {path = "../runtime"}
kclvm-utils = {path = "../utils"}

anyhow = "1.0"
tracing = "0.1"
Expand Down
6 changes: 5 additions & 1 deletion kclvm/error/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,12 @@ impl Position {

impl From<Loc> for Position {
fn from(loc: Loc) -> Self {
let filename = kclvm_utils::path::convert_windows_drive_letter(&format!(
"{}",
loc.file.name.prefer_remapped()
));
Self {
filename: format!("{}", loc.file.name.prefer_remapped()),
filename,
line: loc.line as u64,
column: if loc.col_display > 0 {
// Loc col is the (0-based) column offset.
Expand Down
5 changes: 4 additions & 1 deletion kclvm/parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,10 @@ impl Loader {
) -> Self {
Self {
sess,
paths: paths.iter().map(|s| s.to_string()).collect(),
paths: paths
.iter()
.map(|s| kclvm_utils::path::convert_windows_drive_letter(s))
.collect(),
opts: opts.unwrap_or_default(),
module_cache,
missing_pkgs: Default::default(),
Expand Down
21 changes: 7 additions & 14 deletions kclvm/parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,11 @@ impl<'a> Parser<'a> {
let lo = self.sess.lookup_char_pos(lo);
let hi = self.sess.lookup_char_pos(hi);

let filename = {
#[cfg(target_os = "windows")]
{
kclvm_utils::path::convert_windows_drive_letter(&format!(
"{}",
lo.file.name.prefer_remapped()
))
}
let filename = kclvm_utils::path::convert_windows_drive_letter(&format!(
"{}",
lo.file.name.prefer_remapped()
));

#[cfg(not(target_os = "windows"))]
{
format!("{}", lo.file.name.prefer_remapped())
}
};
(
filename,
lo.line as u64,
Expand Down Expand Up @@ -205,7 +196,9 @@ impl<'a> Parser<'a> {
CommentKind::Line(x) => {
let lo = sess.lookup_char_pos(tok.span.lo());
let hi = sess.lookup_char_pos(tok.span.hi());
let filename: String = format!("{}", lo.file.name.prefer_remapped());
let filename = kclvm_utils::path::convert_windows_drive_letter(
&format!("{}", lo.file.name.prefer_remapped()),
);

let node = kclvm_ast::ast::Node {
id: kclvm_ast::ast::AstIndex::default(),
Expand Down
7 changes: 5 additions & 2 deletions kclvm/tools/src/LSP/src/from_lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub(crate) fn abs_path(uri: &Url) -> anyhow::Result<AbsPathBuf> {
// The position in lsp protocol is different with position in ast node whose line number is 1 based.
pub(crate) fn kcl_pos(file: &str, pos: Position) -> KCLPos {
KCLPos {
filename: file.to_string(),
filename: kclvm_utils::path::convert_windows_drive_letter(file),
line: (pos.line + 1) as u64,
column: Some(pos.character as u64),
}
Expand Down Expand Up @@ -52,6 +52,9 @@ pub(crate) fn text_range(text: &str, range: lsp_types::Range) -> Range<usize> {
pub(crate) fn file_path_from_url(url: &Url) -> anyhow::Result<String> {
url.to_file_path()
.ok()
.and_then(|path| path.to_str().map(|p| p.to_string()))
.and_then(|path| {
path.to_str()
.map(|p| kclvm_utils::path::convert_windows_drive_letter(p))
})
.ok_or_else(|| anyhow::anyhow!("can't convert url to file path: {}", url))
}
19 changes: 12 additions & 7 deletions kclvm/tools/src/util/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ impl DataLoader {
pub fn byte_pos_to_pos_in_sourcemap(&self, lo: BytePos, hi: BytePos) -> PosTuple {
let lo = self.sm.lookup_char_pos(lo);
let hi = self.sm.lookup_char_pos(hi);
let filename: String = format!("{}", lo.file.name.prefer_remapped());
let filename = kclvm_utils::path::convert_windows_drive_letter(&format!(
"{}",
lo.file.name.prefer_remapped()
));
(
filename,
lo.line as u64,
Expand All @@ -77,12 +80,14 @@ impl DataLoader {
}

pub fn file_name(&self) -> String {
self.sm
.lookup_char_pos(new_byte_pos(0))
.file
.name
.prefer_remapped()
.to_string()
kclvm_utils::path::convert_windows_drive_letter(&format!(
"{}",
self.sm
.lookup_char_pos(new_byte_pos(0))
.file
.name
.prefer_remapped()
))
}
}

Expand Down
27 changes: 23 additions & 4 deletions kclvm/utils/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ where
}

/// Conver windows drive letter to upcase
#[cfg(target_os = "windows")]
pub fn convert_windows_drive_letter(path: &str) -> String {
#[cfg(target_os = "windows")]
{
let regex = regex::Regex::new(r"(?i)^\\\\\?\\[a-z]:\\").unwrap();
const VERBATIM_PREFIX: &str = r#"\\?\"#;
Expand All @@ -84,15 +84,34 @@ pub fn convert_windows_drive_letter(path: &str) -> String {
&drive_letter.to_uppercase(),
);
}
let regex = regex::Regex::new(r"[a-z]:\\").unwrap();
if regex.is_match(&p) {
let drive_letter = p[0..1].to_string();
p.replace_range(0..1, &drive_letter.to_uppercase());
}
p
}
#[cfg(not(target_os = "windows"))]
{
path.to_owned()
}
}

#[test]
#[cfg(target_os = "windows")]
fn test_convert_drive_letter() {
let path = r"\\?\d:\xx";
assert_eq!(convert_windows_drive_letter(path), r"\\?\D:\xx".to_string())
#[cfg(target_os = "windows")]
{
let path = r"\\?\d:\xx";
assert_eq!(convert_windows_drive_letter(path), r"\\?\D:\xx".to_string());

let path = r"d:\xx";
assert_eq!(convert_windows_drive_letter(path), r"D:\xx".to_string());
}
#[cfg(not(target_os = "windows"))]
{
let path = r".\xx";
assert_eq!(convert_windows_drive_letter(path), path.to_string());
}
}

#[test]
Expand Down

0 comments on commit 46ce7e0

Please sign in to comment.