Skip to content

Commit

Permalink
feat: make it work on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
pbogut committed Sep 24, 2023
1 parent 1862720 commit 3b42d25
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "magento2-ls"
version = "0.0.1"
version = "0.0.2"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ fn main_loop(
let map: HashMap<String, PHPClass> = HashMap::new();

eprint!("Preparing index...");
let root_path = params.root_uri.as_ref().context("Root uri to path")?.path();
let root_uri = params.root_uri.context("Root uri is required")?;
let root_path = root_uri
.to_file_path()
.expect("Root uri should be valid file path");

let mut indexer = Indexer {
php_classes: map,
magento_modules: HashMap::new(),
Expand Down
25 changes: 21 additions & 4 deletions src/xml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct XmlTag {

impl XmlTag {
fn new() -> Self {
XmlTag {
Self {
name: String::new(),
attributes: HashMap::new(),
text: String::new(),
Expand All @@ -46,7 +46,9 @@ fn get_item_from_pos(content: &str, uri: &Url, pos: Position) -> Option<M2Item>

match tag.hover_on {
XmlPart::Attribute(ref attr_name) => match attr_name.as_str() {
"method" | "instance" | "class" => try_method_item_from_tag(&tag),
"method" | "instance" | "class" => try_method_item_from_tag(&tag).or_else(|| {
try_any_item_from_str(tag.attributes.get(attr_name)?, is_frontend_location(path))
}),
"template" => {
try_phtml_item_from_str(tag.attributes.get(attr_name)?, is_frontend_location(path))
}
Expand Down Expand Up @@ -150,8 +152,10 @@ fn try_any_item_from_str(text: &str, is_frontend: bool) -> Option<M2Item> {
try_phtml_item_from_str(text, is_frontend)
} else if text.contains("::") {
try_const_item_from_str(text)
} else {
} else if text.chars().next()?.is_uppercase() {
Some(get_class_item_from_str(text))
} else {
None
}
}

Expand Down Expand Up @@ -225,6 +229,7 @@ mod test {
use std::path::PathBuf;

fn get_test_item(xml: &str, path: &str) -> Option<M2Item> {
let win_path = format!("c:{}", path.replace('/', "\\"));
let mut character = 0;
let mut line = 0;
for l in xml.lines() {
Expand All @@ -235,7 +240,8 @@ mod test {
line += 1;
}
let pos = Position { line, character };
let uri = Url::from_file_path(PathBuf::from(path)).unwrap();
let uri = Url::from_file_path(PathBuf::from(if cfg!(windows) { &win_path } else { path }))
.unwrap();
get_item_from_pos(&xml.replace('|', ""), &uri, pos)
}

Expand Down Expand Up @@ -385,4 +391,15 @@ mod test {
);
assert_eq!(item, Some(M2Item::Class("Some\\Class\\Name".to_string())))
}

#[test]
fn test_should_get_class_from_class_attribute_of_block_tag() {
let item = get_test_item(
r#"<?xml version=\"1.0\"?>
<block class="A\|B\C" name="some_name" template="Some_Module::temp/file.phtml"/>
"#,
"/a/a/c",
);
assert_eq!(item, Some(M2Item::Class("A\\B\\C".to_string())))
}
}

0 comments on commit 3b42d25

Please sign in to comment.