|
| 1 | +/* |
| 2 | + * Copyright 2013-2026 consulo.io |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package consulo.spring.impl.boot.properties; |
| 18 | + |
| 19 | +import com.intellij.lang.properties.IProperty; |
| 20 | +import com.intellij.lang.properties.psi.PropertiesFile; |
| 21 | +import consulo.annotation.component.ComponentScope; |
| 22 | +import consulo.annotation.component.ServiceAPI; |
| 23 | +import consulo.annotation.component.ServiceImpl; |
| 24 | +import consulo.language.psi.PsiElement; |
| 25 | +import consulo.language.psi.PsiFile; |
| 26 | +import consulo.language.psi.PsiManager; |
| 27 | +import consulo.module.Module; |
| 28 | +import consulo.module.content.ModuleRootManager; |
| 29 | +import consulo.project.Project; |
| 30 | +import consulo.virtualFileSystem.VirtualFile; |
| 31 | +import jakarta.inject.Inject; |
| 32 | +import jakarta.inject.Singleton; |
| 33 | +import org.jspecify.annotations.Nullable; |
| 34 | + |
| 35 | +import java.util.ArrayList; |
| 36 | +import java.util.List; |
| 37 | + |
| 38 | +/** |
| 39 | + * Project-scoped service for resolving Spring Boot configuration properties. |
| 40 | + * Searches application.properties / application.yml in resource roots, |
| 41 | + * and spring-configuration-metadata.json in library JARs. |
| 42 | + */ |
| 43 | +@ServiceAPI(ComponentScope.PROJECT) |
| 44 | +@ServiceImpl |
| 45 | +@Singleton |
| 46 | +public class SpringConfigurationPropertySearch { |
| 47 | + private static final String[] CONFIG_FILENAMES = { |
| 48 | + "application.properties", |
| 49 | + "application.yml", |
| 50 | + "application.yaml" |
| 51 | + }; |
| 52 | + |
| 53 | + private static final String[] CONFIG_DIRS = {"", "config/"}; |
| 54 | + |
| 55 | + private final Project myProject; |
| 56 | + |
| 57 | + @Inject |
| 58 | + public SpringConfigurationPropertySearch(Project project) { |
| 59 | + myProject = project; |
| 60 | + } |
| 61 | + |
| 62 | + public static SpringConfigurationPropertySearch getInstance(Project project) { |
| 63 | + return project.getInstance(SpringConfigurationPropertySearch.class); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Resolve a property key to its definition(s) in config files. |
| 68 | + * Returns IProperty elements from .properties files and YAMLKeyValue from .yml files. |
| 69 | + */ |
| 70 | + public List<PsiElement> resolvePropertyKey(String key, @Nullable Module module) { |
| 71 | + if (module == null) { |
| 72 | + return List.of(); |
| 73 | + } |
| 74 | + |
| 75 | + List<PsiElement> results = new ArrayList<>(); |
| 76 | + PsiManager psiManager = PsiManager.getInstance(myProject); |
| 77 | + |
| 78 | + for (VirtualFile configFile : findConfigFiles(module)) { |
| 79 | + PsiFile psiFile = psiManager.findFile(configFile); |
| 80 | + if (psiFile instanceof PropertiesFile propertiesFile) { |
| 81 | + List<? extends IProperty> properties = propertiesFile.findPropertiesByKey(key); |
| 82 | + for (IProperty property : properties) { |
| 83 | + results.add(property.getPsiElement()); |
| 84 | + } |
| 85 | + } |
| 86 | + else { |
| 87 | + // try YAML |
| 88 | + resolveYamlKey(psiFile, key, results); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return results; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Get all property keys from config files + metadata for completion. |
| 97 | + */ |
| 98 | + public List<String> getAllPropertyKeys(@Nullable Module module) { |
| 99 | + if (module == null) { |
| 100 | + return List.of(); |
| 101 | + } |
| 102 | + |
| 103 | + List<String> keys = new ArrayList<>(); |
| 104 | + PsiManager psiManager = PsiManager.getInstance(myProject); |
| 105 | + |
| 106 | + for (VirtualFile configFile : findConfigFiles(module)) { |
| 107 | + PsiFile psiFile = psiManager.findFile(configFile); |
| 108 | + if (psiFile instanceof PropertiesFile propertiesFile) { |
| 109 | + for (IProperty property : propertiesFile.getProperties()) { |
| 110 | + String propKey = property.getKey(); |
| 111 | + if (propKey != null && !propKey.isEmpty()) { |
| 112 | + keys.add(propKey); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + else { |
| 117 | + collectYamlKeys(psiFile, keys); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + // also add keys from spring-configuration-metadata.json in library JARs |
| 122 | + for (SpringMetadataPropertyLoader.MetadataProperty metaProp : SpringMetadataPropertyLoader.loadFromModule(module)) { |
| 123 | + keys.add(metaProp.name()); |
| 124 | + } |
| 125 | + |
| 126 | + return keys; |
| 127 | + } |
| 128 | + |
| 129 | + private List<VirtualFile> findConfigFiles(Module module) { |
| 130 | + List<VirtualFile> files = new ArrayList<>(); |
| 131 | + VirtualFile[] sourceRoots = ModuleRootManager.getInstance(module).getSourceRoots(); |
| 132 | + |
| 133 | + for (VirtualFile root : sourceRoots) { |
| 134 | + for (String dir : CONFIG_DIRS) { |
| 135 | + VirtualFile configDir = dir.isEmpty() ? root : root.findChild("config"); |
| 136 | + if (configDir == null) { |
| 137 | + continue; |
| 138 | + } |
| 139 | + |
| 140 | + for (String filename : CONFIG_FILENAMES) { |
| 141 | + VirtualFile file = configDir.findChild(filename); |
| 142 | + if (file != null) { |
| 143 | + files.add(file); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + // profile-specific files |
| 148 | + for (VirtualFile child : configDir.getChildren()) { |
| 149 | + String name = child.getName(); |
| 150 | + if (name.startsWith("application-") && |
| 151 | + (name.endsWith(".properties") || name.endsWith(".yml") || name.endsWith(".yaml"))) { |
| 152 | + files.add(child); |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + return files; |
| 159 | + } |
| 160 | + |
| 161 | + private void resolveYamlKey(PsiFile psiFile, String key, List<PsiElement> results) { |
| 162 | + try { |
| 163 | + Class<?> yamlFileClass = Class.forName("org.jetbrains.yaml.psi.YAMLFile"); |
| 164 | + Class<?> yamlUtilClass = Class.forName("org.jetbrains.yaml.YAMLUtil"); |
| 165 | + |
| 166 | + if (!yamlFileClass.isInstance(psiFile)) { |
| 167 | + return; |
| 168 | + } |
| 169 | + |
| 170 | + // YAMLUtil.getQualifiedKeyInFile(yamlFile, key.split("\\.")) |
| 171 | + java.lang.reflect.Method method = yamlUtilClass.getMethod("getQualifiedKeyInFile", |
| 172 | + yamlFileClass, String[].class); |
| 173 | + Object result = method.invoke(null, psiFile, key.split("\\.")); |
| 174 | + if (result instanceof PsiElement element) { |
| 175 | + results.add(element); |
| 176 | + } |
| 177 | + } |
| 178 | + catch (Exception ignored) { |
| 179 | + // YAML plugin not available |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + private void collectYamlKeys(PsiFile psiFile, List<String> keys) { |
| 184 | + try { |
| 185 | + Class<?> yamlFileClass = Class.forName("org.jetbrains.yaml.psi.YAMLFile"); |
| 186 | + if (!yamlFileClass.isInstance(psiFile)) { |
| 187 | + return; |
| 188 | + } |
| 189 | + |
| 190 | + collectYamlKeysRecursive(psiFile, keys); |
| 191 | + } |
| 192 | + catch (Exception ignored) { |
| 193 | + // YAML plugin not available |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + private void collectYamlKeysRecursive(PsiElement element, List<String> keys) { |
| 198 | + try { |
| 199 | + Class<?> yamlKeyValueClass = Class.forName("org.jetbrains.yaml.psi.YAMLKeyValue"); |
| 200 | + Class<?> yamlUtilClass = Class.forName("org.jetbrains.yaml.YAMLUtil"); |
| 201 | + Class<?> yamlPsiElementClass = Class.forName("org.jetbrains.yaml.psi.YAMLPsiElement"); |
| 202 | + |
| 203 | + if (yamlKeyValueClass.isInstance(element)) { |
| 204 | + // check if this is a leaf (scalar value, not a mapping) |
| 205 | + java.lang.reflect.Method getValueMethod = yamlKeyValueClass.getMethod("getValue"); |
| 206 | + Object value = getValueMethod.invoke(element); |
| 207 | + Class<?> yamlScalarClass = Class.forName("org.jetbrains.yaml.psi.YAMLScalar"); |
| 208 | + if (value != null && yamlScalarClass.isInstance(value)) { |
| 209 | + java.lang.reflect.Method getConfigFullName = yamlUtilClass.getMethod("getConfigFullName", yamlPsiElementClass); |
| 210 | + Object fullName = getConfigFullName.invoke(null, element); |
| 211 | + if (fullName instanceof String key && !key.isEmpty()) { |
| 212 | + keys.add(key); |
| 213 | + } |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + for (PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()) { |
| 218 | + collectYamlKeysRecursive(child, keys); |
| 219 | + } |
| 220 | + } |
| 221 | + catch (Exception ignored) { |
| 222 | + // YAML plugin not available |
| 223 | + } |
| 224 | + } |
| 225 | +} |
0 commit comments