From a9e33fe4f6686b0b86e83f10bf6aedcbe414f39e Mon Sep 17 00:00:00 2001 From: Peter Oberndorfer Date: Tue, 3 Oct 2017 18:03:34 +0200 Subject: [PATCH] CARGO: Support main.rs as crate root in various subfolder This feature was added after cargo 0.21 (not released with any stable Rust version yet) https://github.com/rust-lang/cargo/pull/4496 --- .../org/rust/cargo/project/model/impl/CargoTomlWatcher.kt | 7 ++++++- .../rust/cargo/project/model/impl/CargoTomlWatcherTest.kt | 8 ++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/org/rust/cargo/project/model/impl/CargoTomlWatcher.kt b/src/main/kotlin/org/rust/cargo/project/model/impl/CargoTomlWatcher.kt index 2dd9a82c4ac..7b7e1ffbc74 100644 --- a/src/main/kotlin/org/rust/cargo/project/model/impl/CargoTomlWatcher.kt +++ b/src/main/kotlin/org/rust/cargo/project/model/impl/CargoTomlWatcher.kt @@ -30,6 +30,8 @@ class CargoTomlWatcher( "/src/bin", "/examples", "/tests", "/benches" ) + private val MAIN = "main.rs" + override fun before(events: List) { } @@ -38,9 +40,12 @@ class CargoTomlWatcher( if (isCargoTomlChange(event)) return true if (event.path.endsWith(RustToolchain.CARGO_LOCK)) return true if (event is VFileContentChangeEvent || PathUtil.getFileExtension(event.path) != "rs") return false + val parent = PathUtil.getParentPath(event.path) + val grandParent = PathUtil.getParentPath(parent) + val name = PathUtil.getFileName(event.path) if (IMPLICIT_TARGET_FILES.any { event.path.endsWith(it) }) return true - return IMPLICIT_TARGET_DIRS.any { PathUtil.getParentPath(event.path).endsWith(it) } + return IMPLICIT_TARGET_DIRS.any { parent.endsWith(it) || (name == MAIN && grandParent.endsWith(it)) } } if (events.any(::isInterestingEvent)) { diff --git a/src/test/kotlin/org/rust/cargo/project/model/impl/CargoTomlWatcherTest.kt b/src/test/kotlin/org/rust/cargo/project/model/impl/CargoTomlWatcherTest.kt index 15113555ab1..3de89ef889d 100644 --- a/src/test/kotlin/org/rust/cargo/project/model/impl/CargoTomlWatcherTest.kt +++ b/src/test/kotlin/org/rust/cargo/project/model/impl/CargoTomlWatcherTest.kt @@ -44,6 +44,9 @@ class CargoTomlWatcherTest : RsTestBase() { watcher.checkTriggered(createEvent) watcher.checkNotTriggered(newChangeEvent(binFile)) + // src/bin/*/main.rs + watcher.checkTriggered(newCreateEvent("src/bin/foo/main.rs").second) + // src/main.rs watcher.checkTriggered(newCreateEvent("src/main.rs").second) watcher.checkNotTriggered(newCreateEvent("prefix_src/main.rs").second) @@ -59,6 +62,11 @@ class CargoTomlWatcherTest : RsTestBase() { watcher.checkTriggered(newCreateEvent("tests/foo.rs").second) watcher.checkNotTriggered(newCreateEvent("prefix_tests/foo.rs").second) + // benches/*/main.rs, examples/*/main.rs, tests/*/main.rs + watcher.checkTriggered(newCreateEvent("benches/foo/main.rs").second) + watcher.checkTriggered(newCreateEvent("examples/foo/main.rs").second) + watcher.checkTriggered(newCreateEvent("tests/foo/main.rs").second) + // build.rs watcher.checkTriggered(newCreateEvent("build.rs").second) watcher.checkNotTriggered(newCreateEvent("prefix_build.rs").second)