This is an automated email from the ASF dual-hosted git repository. lukaszlenart pushed a commit to branch docs/struts-7-5-configuration-grammar in repository https://gitbox.apache.org/repos/asf/struts-intellij-plugin.git
commit 1804ec269ca55dd99f7b6f25f7809b1799cf9936 Author: Lukasz Lenart <[email protected]> AuthorDate: Wed Nov 11 19:15:15 2026 +0100 feat: complete and check package scoped constants Only the built-in scopable names resolve, and their values reuse the constant value converter. Co-authored-by: Cursor <[email protected]> --- .../constant/ConstantValueConverterImpl.java | 15 +++-- .../dom/struts/strutspackage/ScopedConstant.java | 44 +++++++++++++++ .../strutspackage/ScopedConstantNameConverter.java | 64 ++++++++++++++++++++++ .../dom/struts/strutspackage/StrutsPackage.java | 4 ++ .../struts2/dom/struts/StrutsCompletionTest.java | 10 ++++ .../struts2/dom/struts/StrutsHighlightingTest.java | 4 ++ ...uts-completionvariants-scoped-constant-name.xml | 31 +++++++++++ ...pletionvariants-scoped-constant-theme-value.xml | 31 +++++++++++ .../highlighting/struts-scoped-constant.xml | 32 +++++++++++ 9 files changed, 231 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/intellij/struts2/dom/struts/constant/ConstantValueConverterImpl.java b/src/main/java/com/intellij/struts2/dom/struts/constant/ConstantValueConverterImpl.java index a6a20a1..e2fefbe 100644 --- a/src/main/java/com/intellij/struts2/dom/struts/constant/ConstantValueConverterImpl.java +++ b/src/main/java/com/intellij/struts2/dom/struts/constant/ConstantValueConverterImpl.java @@ -17,9 +17,11 @@ package com.intellij.struts2.dom.struts.constant; import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.xml.XmlTag; +import com.intellij.struts2.dom.struts.strutspackage.ScopedConstant; import com.intellij.struts2.model.constant.StrutsConstantKey; import com.intellij.struts2.model.constant.StrutsConstantManager; import com.intellij.util.xml.Converter; +import com.intellij.util.xml.DomElement; import com.intellij.util.xml.GenericDomValue; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -32,10 +34,15 @@ public class ConstantValueConverterImpl extends ConstantValueConverter { @Override @Nullable public Converter<?> getConverter(@NotNull final GenericDomValue domElement) { - final Constant constant = (Constant) domElement.getParent(); - assert constant != null; - - final String constantName = constant.getName().getStringValue(); + final DomElement parent = domElement.getParent(); + final String constantName; + if (parent instanceof Constant constant) { + constantName = constant.getName().getStringValue(); + } else if (parent instanceof ScopedConstant scopedConstant) { + constantName = scopedConstant.getName().getStringValue(); + } else { + return null; + } if (StringUtil.isEmpty(constantName)) { return null; } diff --git a/src/main/java/com/intellij/struts2/dom/struts/strutspackage/ScopedConstant.java b/src/main/java/com/intellij/struts2/dom/struts/strutspackage/ScopedConstant.java new file mode 100644 index 0000000..bd77825 --- /dev/null +++ b/src/main/java/com/intellij/struts2/dom/struts/strutspackage/ScopedConstant.java @@ -0,0 +1,44 @@ +/* + * Copyright 2026 The authors + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.intellij.struts2.dom.struts.strutspackage; + +import com.intellij.ide.presentation.Presentation; +import com.intellij.struts2.dom.struts.constant.ConstantValueConverter; +import com.intellij.util.xml.Convert; +import com.intellij.util.xml.DomElement; +import com.intellij.util.xml.GenericAttributeValue; +import com.intellij.util.xml.NameValue; +import com.intellij.util.xml.Required; +import com.intellij.util.xml.Stubbed; + +/** + * {@code <scoped-constant>}. + */ +@Stubbed +@Presentation(typeName = "Scoped Constant", icon = "AllIcons.Nodes.Parameter") +public interface ScopedConstant extends DomElement { + + @NameValue + @Required + @Convert(ScopedConstantNameConverter.class) + @Stubbed + GenericAttributeValue<String> getName(); + + @Required(nonEmpty = false) + @Convert(ConstantValueConverter.class) + @Stubbed + GenericAttributeValue<String> getValue(); +} diff --git a/src/main/java/com/intellij/struts2/dom/struts/strutspackage/ScopedConstantNameConverter.java b/src/main/java/com/intellij/struts2/dom/struts/strutspackage/ScopedConstantNameConverter.java new file mode 100644 index 0000000..fb2b8c0 --- /dev/null +++ b/src/main/java/com/intellij/struts2/dom/struts/strutspackage/ScopedConstantNameConverter.java @@ -0,0 +1,64 @@ +/* + * Copyright 2026 The authors + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.intellij.struts2.dom.struts.strutspackage; + +import com.intellij.openapi.module.Module; +import com.intellij.struts2.model.constant.StrutsConstant; +import com.intellij.struts2.model.constant.StrutsConstantManager; +import com.intellij.util.containers.ContainerUtil; +import com.intellij.util.xml.ConvertContext; +import com.intellij.util.xml.ResolvingConverter; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Collection; +import java.util.List; +import java.util.Set; + +/** + * Resolves {@code <scoped-constant>} names to registered scopable constants. + */ +public class ScopedConstantNameConverter extends ResolvingConverter.StringConverter { + + static final List<String> SCOPABLE_NAMES = List.of( + "struts.ui.theme", + "struts.ui.templateDir", + "struts.convention.result.path"); + + @Override + public String fromString(@Nullable String name, ConvertContext context) { + if (name == null) { + return null; + } + return getVariants(context).contains(name) ? name : null; + } + + @Override + public @NotNull Collection<String> getVariants(@NotNull ConvertContext context) { + final Module module = context.getModule(); + if (module == null) { + return List.of(); + } + final StrutsConstantManager constantManager = StrutsConstantManager.getInstance(module.getProject()); + final Set<String> registered = ContainerUtil.map2Set(constantManager.getConstants(module), StrutsConstant::getName); + return ContainerUtil.filter(SCOPABLE_NAMES, registered::contains); + } + + @Override + public String getErrorMessage(@Nullable String name, ConvertContext context) { + return "Constant '" + name + "' is not scopable"; + } +} diff --git a/src/main/java/com/intellij/struts2/dom/struts/strutspackage/StrutsPackage.java b/src/main/java/com/intellij/struts2/dom/struts/strutspackage/StrutsPackage.java index d449c21..bdb6c9e 100644 --- a/src/main/java/com/intellij/struts2/dom/struts/strutspackage/StrutsPackage.java +++ b/src/main/java/com/intellij/struts2/dom/struts/strutspackage/StrutsPackage.java @@ -113,6 +113,10 @@ public interface StrutsPackage extends CommonDomModelElement { @SubTagList(value = "action") List<Action> getActions(); + @SubTagList("scoped-constant") + @NotNull + List<ScopedConstant> getScopedConstants(); + /** * Searches the {@code default-class-ref} element for this package, walking up the hierarchy until one is found. * diff --git a/src/test/java/com/intellij/struts2/dom/struts/StrutsCompletionTest.java b/src/test/java/com/intellij/struts2/dom/struts/StrutsCompletionTest.java index 10fb76e..028d1a7 100644 --- a/src/test/java/com/intellij/struts2/dom/struts/StrutsCompletionTest.java +++ b/src/test/java/com/intellij/struts2/dom/struts/StrutsCompletionTest.java @@ -111,4 +111,14 @@ public class StrutsCompletionTest extends StrutsLightHighlightingTestCase { performCompletionVariantTest("struts-completionvariants-multipart_parser_value.xml", "cos", "jakarta", "jakarta-stream", "pell"); } + + public void testCompletionVariantsScopedConstantName() { + performCompletionVariantTest("struts-completionvariants-scoped-constant-name.xml", + "struts.ui.templateDir", "struts.ui.theme"); + } + + public void testCompletionVariantsScopedConstantThemeValue() { + performCompletionVariantTest("struts-completionvariants-scoped-constant-theme-value.xml", + "ajax", "css_xhtml", "html5", "simple", "xhtml"); + } } \ No newline at end of file diff --git a/src/test/java/com/intellij/struts2/dom/struts/StrutsHighlightingTest.java b/src/test/java/com/intellij/struts2/dom/struts/StrutsHighlightingTest.java index fe268d1..5563dd0 100644 --- a/src/test/java/com/intellij/struts2/dom/struts/StrutsHighlightingTest.java +++ b/src/test/java/com/intellij/struts2/dom/struts/StrutsHighlightingTest.java @@ -90,4 +90,8 @@ public class StrutsHighlightingTest extends StrutsLightHighlightingTestCase { performHighlightingTest("struts-package-final.xml"); } + public void testScopedConstantName() { + performHighlightingTest("struts-scoped-constant.xml"); + } + } diff --git a/src/test/testData/strutsXml/completion/struts-completionvariants-scoped-constant-name.xml b/src/test/testData/strutsXml/completion/struts-completionvariants-scoped-constant-name.xml new file mode 100644 index 0000000..dfb3ee2 --- /dev/null +++ b/src/test/testData/strutsXml/completion/struts-completionvariants-scoped-constant-name.xml @@ -0,0 +1,31 @@ +<?xml version="1.0" encoding="UTF-8" ?> + +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--> + + +<!DOCTYPE struts PUBLIC + "-//Apache Software Foundation//DTD Struts Configuration 7.5//EN" + "https://struts.apache.org/dtds/struts-7.5.dtd"> + +<struts> + <package name="ui"> + <scoped-constant name="<caret>" value=""/> + </package> +</struts> diff --git a/src/test/testData/strutsXml/completion/struts-completionvariants-scoped-constant-theme-value.xml b/src/test/testData/strutsXml/completion/struts-completionvariants-scoped-constant-theme-value.xml new file mode 100644 index 0000000..4b96600 --- /dev/null +++ b/src/test/testData/strutsXml/completion/struts-completionvariants-scoped-constant-theme-value.xml @@ -0,0 +1,31 @@ +<?xml version="1.0" encoding="UTF-8" ?> + +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--> + + +<!DOCTYPE struts PUBLIC + "-//Apache Software Foundation//DTD Struts Configuration 7.5//EN" + "https://struts.apache.org/dtds/struts-7.5.dtd"> + +<struts> + <package name="ui"> + <scoped-constant name="struts.ui.theme" value="<caret>"/> + </package> +</struts> diff --git a/src/test/testData/strutsXml/highlighting/struts-scoped-constant.xml b/src/test/testData/strutsXml/highlighting/struts-scoped-constant.xml new file mode 100644 index 0000000..39ef20e --- /dev/null +++ b/src/test/testData/strutsXml/highlighting/struts-scoped-constant.xml @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="UTF-8" ?> + +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--> + + +<!DOCTYPE struts PUBLIC + "-//Apache Software Foundation//DTD Struts Configuration 7.5//EN" + "https://struts.apache.org/dtds/struts-7.5.dtd"> + +<struts> + <package name="ui"> + <scoped-constant name="struts.ui.theme" value="html5"/> + <scoped-constant name="<error descr="Constant 'struts.devMode' is not scopable">struts.devMode</error>" value="true"/> + </package> +</struts>
