Skip to content

Commit a7bc559

Browse files
lithe-support[bot]1lckclaude
authored
feat(java): 在项目结构中安全编辑 Eclipse WTP Web Facet 版本 (#31)
* feat(java): edit existing Eclipse WTP Web Facet version safely * test(java): use exact Web Facet field key in JSON test fixtures * style(java): apply rustfmt layout to Web Facet settings * fix(java): 不合规 Facet 文件不再拖垮项目结构页,版本按 jst.java 限定 - 读取 Facet 失败只跳过该来源,上报一次 webFacetUnreadable 降级 - 补齐 2.2/2.3/2.4/6.1,并按 jst.java 过滤可选版本,保存时同样拒绝 - xmltree 开启 attribute-order,写回不再打乱属性顺序,自闭合标签沿用 Eclipse 的 /> - test-module-structure 用真实 JDT LS 覆盖 Facet 读取、不合规文件、版本限制和保存 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(java): Web Facet 字段 ID 改为 Core 允许的写法,补原生截图 Core 只接受小写字母、数字和 - 组成的字段 ID,webFacetVersion 会让 宿主拒绝整个项目结构页(plugin.settings.invalidSourceEditor)。 - 字段 ID 改为 web-facet-version - test-module-structure 检查所有分组、来源和字段 ID 符合 Core 规则 - visual.mjs 增加选中 Web Facet 来源的截图,存入 docs/assets/module-structure Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: lithe-support[bot] <356900028+lithe-support[bot]@users.noreply.github.com> Co-authored-by: lick <2188718831@qq.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 02f8007 commit a7bc559

12 files changed

Lines changed: 402 additions & 3 deletions

File tree

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎backend/Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ dunce = "1.0.5"
2323

2424
sha2.workspace = true
2525
java-properties = "2.0.0"
26-
xmltree = "0.12.0"
26+
xmltree = { version = "0.12.0", features = ["attribute-order"] }
2727
jsonc-parser = { version = "0.34.0", features = ["cst", "serde"] }
2828
libc = "0.2"

‎backend/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub mod project_model;
5858
pub mod project_settings;
5959
pub mod run;
6060
pub mod settings_session;
61+
pub mod web_facet;
6162

6263
use crate::launch::LAUNCH_FUNCTION_ID;
6364
use crate::run::{

‎backend/src/module_structure.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ pub fn edit(
586586
if flag(&entry, EXPORTED)? {
587587
element.attributes.insert(EXPORTED.into(), "true".into());
588588
} else {
589-
element.attributes.remove(EXPORTED);
589+
element.attributes.shift_remove(EXPORTED);
590590
}
591591
set_nested(&mut element, TEST, flag(&entry, TEST)?.then_some("true"));
592592
next.push(element);

‎backend/src/project_settings.rs‎

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,11 +368,20 @@ impl ProjectSettings {
368368
};
369369
let mut groups = if libraries {
370370
let mut groups = Vec::new();
371-
if !module_sources.is_empty() {
371+
if module_sources
372+
.iter()
373+
.any(|source| source["group"] == "modules")
374+
{
372375
groups.push(
373376
json!({"id":"modules","title":local(zh,"Modules","模块"),"layout":"sidebar"}),
374377
);
375378
}
379+
if module_sources
380+
.iter()
381+
.any(|source| source["group"] == "facets")
382+
{
383+
groups.push(json!({"id":"facets","title":local(zh,"Web Facets","Web Facet"),"layout":"sidebar"}));
384+
}
376385
if module_sources.is_empty()
377386
|| sources.iter().any(|source| source["group"] == "libraries")
378387
{
@@ -468,6 +477,8 @@ impl ProjectSettings {
468477
// (folder, source file, source) in folder order, so that an
469478
// enclosing module comes before the modules inside it.
470479
let mut rows: Vec<(PathBuf, PathBuf, Value)> = Vec::new();
480+
let mut facet_sources = Vec::new();
481+
let mut unreadable_facets: u64 = 0;
471482
for (project, file, revision, classpath, name) in classpaths {
472483
let rel = project_model::relative_inside(root, &file).ok_or_else(fail)?;
473484
let choices = modules::Choices {
@@ -479,6 +490,32 @@ impl ProjectSettings {
479490
if classpath.editable {
480491
bindings.insert(id.clone(), file.clone());
481492
}
493+
// Web Facet is stored in another native XML source. Keep it
494+
// separate from .classpath so each save replaces exactly one file.
495+
// A facet file this page cannot edit safely costs only its own
496+
// source: the module still shows, and the page reports one
497+
// degradation for all such files.
498+
if facet_sources.len() < 256 {
499+
let facet_file = project.join(crate::web_facet::FILE);
500+
if fs::symlink_metadata(&facet_file).is_ok_and(|info| info.is_file()) {
501+
let facet = read(&facet_file).and_then(|(bytes, revision)| {
502+
Ok(crate::web_facet::fields(zh, &bytes)?.map(|fields| (revision, fields)))
503+
});
504+
match facet {
505+
Ok(Some((revision, fields))) => {
506+
let rel = project_model::relative_inside(root, &facet_file)
507+
.ok_or_else(fail)?;
508+
let facet_id = format!("facet{:x}", Sha256::digest(rel.as_bytes()));
509+
if paths.insert(facet_file.clone()) {
510+
bindings.insert(facet_id.clone(), facet_file);
511+
facet_sources.push(json!({"id":facet_id,"group":"facets","title":name.clone(),"path":rel,"revision":revision,"writable":true,"fields":fields}));
512+
}
513+
}
514+
Ok(None) => {}
515+
Err(_) => unreadable_facets += 1,
516+
}
517+
}
518+
}
482519
rows.push((project, file, json!({"id":id,"group":"modules","title":name,"path":rel,"revision":revision,"writable":classpath.editable,"fields":fields})));
483520
}
484521
let mut unavailable = 0;
@@ -554,6 +591,16 @@ impl ProjectSettings {
554591
}
555592
sources.push(source);
556593
}
594+
sources.extend(facet_sources);
595+
if unreadable_facets > 0 {
596+
// An error from `degraded` only means the arguments were invalid,
597+
// so the page itself stands.
598+
let _ = context.diagnostics().degraded(
599+
context.function_id(),
600+
"webFacetUnreadable",
601+
Some(unreadable_facets),
602+
);
603+
}
557604
Ok(sources)
558605
}
559606
/// Prepares only the confirmed native source; publication belongs to the host.
@@ -591,6 +638,8 @@ impl ProjectSettings {
591638
let values = params["values"].as_object().ok_or_else(fail)?;
592639
let replacement = if path.ends_with(".classpath") {
593640
crate::module_structure::edit(&bytes, parent, values)?
641+
} else if path.ends_with(crate::web_facet::FILE) {
642+
crate::web_facet::edit(&bytes, values)?
594643
} else if path.ends_with(SETTINGS) {
595644
let workspace = context.workspace_context().ok_or_else(fail)?;
596645
let options = session.model_options(workspace.context_id());
@@ -769,8 +818,11 @@ pub(crate) fn write_classpath(
769818
xmltree::XMLNode::Element(_) => doc
770819
.write_with_config(
771820
&mut out,
821+
// Eclipse writes `<entry .../>`; matching it keeps a
822+
// save from rewriting every self-closing line.
772823
xmltree::EmitterConfig::new()
773824
.perform_indent(true)
825+
.pad_self_closing(false)
774826
.write_document_declaration(false),
775827
)
776828
.map_err(|_| fail())?,

0 commit comments

Comments
 (0)