diff --git a/Cargo.lock b/Cargo.lock
index b2b2202..672b55a 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -763,6 +763,7 @@ version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cbc04313cab124e498ab1724e739720807b6dc405b9ed0edc5860164d2e4ff70"
dependencies = [
+ "indexmap",
"xml",
]
diff --git a/backend/Cargo.toml b/backend/Cargo.toml
index 70e272b..b3073fc 100644
--- a/backend/Cargo.toml
+++ b/backend/Cargo.toml
@@ -23,6 +23,6 @@ dunce = "1.0.5"
sha2.workspace = true
java-properties = "2.0.0"
-xmltree = "0.12.0"
+xmltree = { version = "0.12.0", features = ["attribute-order"] }
jsonc-parser = { version = "0.34.0", features = ["cst", "serde"] }
libc = "0.2"
diff --git a/backend/src/lib.rs b/backend/src/lib.rs
index e54912d..a991261 100644
--- a/backend/src/lib.rs
+++ b/backend/src/lib.rs
@@ -58,6 +58,7 @@ pub mod project_model;
pub mod project_settings;
pub mod run;
pub mod settings_session;
+pub mod web_facet;
use crate::launch::LAUNCH_FUNCTION_ID;
use crate::run::{
diff --git a/backend/src/module_structure.rs b/backend/src/module_structure.rs
index 60c128f..24685fd 100644
--- a/backend/src/module_structure.rs
+++ b/backend/src/module_structure.rs
@@ -586,7 +586,7 @@ pub fn edit(
if flag(&entry, EXPORTED)? {
element.attributes.insert(EXPORTED.into(), "true".into());
} else {
- element.attributes.remove(EXPORTED);
+ element.attributes.shift_remove(EXPORTED);
}
set_nested(&mut element, TEST, flag(&entry, TEST)?.then_some("true"));
next.push(element);
diff --git a/backend/src/project_settings.rs b/backend/src/project_settings.rs
index a8bd0dd..25bf76b 100644
--- a/backend/src/project_settings.rs
+++ b/backend/src/project_settings.rs
@@ -368,11 +368,20 @@ impl ProjectSettings {
};
let mut groups = if libraries {
let mut groups = Vec::new();
- if !module_sources.is_empty() {
+ if module_sources
+ .iter()
+ .any(|source| source["group"] == "modules")
+ {
groups.push(
json!({"id":"modules","title":local(zh,"Modules","模块"),"layout":"sidebar"}),
);
}
+ if module_sources
+ .iter()
+ .any(|source| source["group"] == "facets")
+ {
+ groups.push(json!({"id":"facets","title":local(zh,"Web Facets","Web Facet"),"layout":"sidebar"}));
+ }
if module_sources.is_empty()
|| sources.iter().any(|source| source["group"] == "libraries")
{
@@ -468,6 +477,8 @@ impl ProjectSettings {
// (folder, source file, source) in folder order, so that an
// enclosing module comes before the modules inside it.
let mut rows: Vec<(PathBuf, PathBuf, Value)> = Vec::new();
+ let mut facet_sources = Vec::new();
+ let mut unreadable_facets: u64 = 0;
for (project, file, revision, classpath, name) in classpaths {
let rel = project_model::relative_inside(root, &file).ok_or_else(fail)?;
let choices = modules::Choices {
@@ -479,6 +490,32 @@ impl ProjectSettings {
if classpath.editable {
bindings.insert(id.clone(), file.clone());
}
+ // Web Facet is stored in another native XML source. Keep it
+ // separate from .classpath so each save replaces exactly one file.
+ // A facet file this page cannot edit safely costs only its own
+ // source: the module still shows, and the page reports one
+ // degradation for all such files.
+ if facet_sources.len() < 256 {
+ let facet_file = project.join(crate::web_facet::FILE);
+ if fs::symlink_metadata(&facet_file).is_ok_and(|info| info.is_file()) {
+ let facet = read(&facet_file).and_then(|(bytes, revision)| {
+ Ok(crate::web_facet::fields(zh, &bytes)?.map(|fields| (revision, fields)))
+ });
+ match facet {
+ Ok(Some((revision, fields))) => {
+ let rel = project_model::relative_inside(root, &facet_file)
+ .ok_or_else(fail)?;
+ let facet_id = format!("facet{:x}", Sha256::digest(rel.as_bytes()));
+ if paths.insert(facet_file.clone()) {
+ bindings.insert(facet_id.clone(), facet_file);
+ facet_sources.push(json!({"id":facet_id,"group":"facets","title":name.clone(),"path":rel,"revision":revision,"writable":true,"fields":fields}));
+ }
+ }
+ Ok(None) => {}
+ Err(_) => unreadable_facets += 1,
+ }
+ }
+ }
rows.push((project, file, json!({"id":id,"group":"modules","title":name,"path":rel,"revision":revision,"writable":classpath.editable,"fields":fields})));
}
let mut unavailable = 0;
@@ -554,6 +591,16 @@ impl ProjectSettings {
}
sources.push(source);
}
+ sources.extend(facet_sources);
+ if unreadable_facets > 0 {
+ // An error from `degraded` only means the arguments were invalid,
+ // so the page itself stands.
+ let _ = context.diagnostics().degraded(
+ context.function_id(),
+ "webFacetUnreadable",
+ Some(unreadable_facets),
+ );
+ }
Ok(sources)
}
/// Prepares only the confirmed native source; publication belongs to the host.
@@ -591,6 +638,8 @@ impl ProjectSettings {
let values = params["values"].as_object().ok_or_else(fail)?;
let replacement = if path.ends_with(".classpath") {
crate::module_structure::edit(&bytes, parent, values)?
+ } else if path.ends_with(crate::web_facet::FILE) {
+ crate::web_facet::edit(&bytes, values)?
} else if path.ends_with(SETTINGS) {
let workspace = context.workspace_context().ok_or_else(fail)?;
let options = session.model_options(workspace.context_id());
@@ -769,8 +818,11 @@ pub(crate) fn write_classpath(
xmltree::XMLNode::Element(_) => doc
.write_with_config(
&mut out,
+ // Eclipse writes ``; matching it keeps a
+ // save from rewriting every self-closing line.
xmltree::EmitterConfig::new()
.perform_indent(true)
+ .pad_self_closing(false)
.write_document_declaration(false),
)
.map_err(|_| fail())?,
diff --git a/backend/src/web_facet.rs b/backend/src/web_facet.rs
new file mode 100644
index 0000000..30a94a7
--- /dev/null
+++ b/backend/src/web_facet.rs
@@ -0,0 +1,277 @@
+//! Safe editing of the Eclipse WTP Dynamic Web Module facet version.
+//! The source-settings host owns conflict checking and publication. This
+//! module prepares only an XML replacement and preserves unrelated entries.
+use lithe_plugin_sdk::{FunctionError, PluginError, Value};
+use serde_json::{json, Map};
+use xmltree::{Element, XMLNode};
+
+pub const FILE: &str = ".settings/org.eclipse.wst.common.project.facet.core.xml";
+const FIELD: &str = "web-facet-version";
+/// Dynamic Web Module versions WTP offers, with the lowest `jst.java` level
+/// each one accepts (1.x levels are written as x). A version is offered only
+/// when the project's installed `jst.java` facet satisfies it, so a save never
+/// produces a combination Eclipse itself rejects.
+const SUPPORTED: &[(&str, u32)] = &[
+ ("2.2", 3),
+ ("2.3", 3),
+ ("2.4", 3),
+ ("2.5", 5),
+ ("3.0", 6),
+ ("3.1", 7),
+ ("4.0", 8),
+ ("5.0", 8),
+ ("6.0", 11),
+ ("6.1", 17),
+];
+
+fn minimum_java(version: &str) -> Option {
+ SUPPORTED
+ .iter()
+ .find(|(known, _)| *known == version)
+ .map(|(_, java)| *java)
+}
+
+/// The Java level of a `jst.java` facet version: "1.8" is 8, "17" is 17.
+fn java_level(version: &str) -> Option {
+ let level = version.strip_prefix("1.").unwrap_or(version);
+ level.parse().ok()
+}
+
+/// Whether a project whose `jst.java` facet is `java` may use `web`.
+/// A project without a readable `jst.java` facet is not constrained here.
+fn compatible(web: &str, java: Option) -> bool {
+ match (minimum_java(web), java) {
+ (None, _) => false,
+ (Some(_), None) => true,
+ (Some(minimum), Some(level)) => level >= minimum,
+ }
+}
+
+fn failed() -> FunctionError {
+ PluginError::new(
+ "java.webFacet.invalid",
+ "The Web Facet source is invalid or has changed. Reload the Java project structure.",
+ )
+ .into()
+}
+
+fn parsed(bytes: &[u8]) -> Result {
+ if bytes
+ .windows(9)
+ .any(|window| window.eq_ignore_ascii_case(b" Result