/**
 * 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.
 */
package org.apache.camel.idea;

import com.intellij.codeInsight.completion.CompletionParameters;
import com.intellij.codeInsight.completion.CompletionResultSet;
import com.intellij.codeInsight.completion.PlainPrefixMatcher;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jps.model.java.JavaModuleSourceRootTypes;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

/**
 * Smart completion for editing a Camel endpoint uri, to show a list of property holders can be added.
 * For example editing <tt>jms:queue?{{_CURSOR_HERE_</tt>. Which presents the user
 * with a list of possible properties.
 */
public class CamelSmartCompletionPropertyPlaceholders {
    //TODO Allow this to be configurable
    private static final List<String> IGNORE_PROPERTIES = Arrays.asList("java.", "Logger.", "logger", "appender.", "rootLogger.","camel.springboot.*");

    public  void propertyPlaceholdersSmartCompletion(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet resultSet) {
        Project project = parameters.getOriginalFile().getManager().getProject();

        List<VirtualFile> resourceRoots = ProjectRootManager.getInstance(project).getModuleSourceRoots(JavaModuleSourceRootTypes.PRODUCTION);
        resourceRoots.addAll(ProjectRootManager.getInstance(project).getModuleSourceRoots(JavaModuleSourceRootTypes.TESTS));

        for (final VirtualFile sourceRoot : resourceRoots) {
            VfsUtil.processFilesRecursively(sourceRoot.getCanonicalFile(), virtualFile -> {
                if (virtualFile.getName().endsWith(".properties")) {
                    File file = new File(virtualFile.getPath());
                    Properties properties = new Properties();
                    try {
                        properties.load(Files.newInputStream(file.toPath()));
                    } catch (IOException e) {
                    }//TODO : log a warning, but for now we ignore it and continue.

                    properties.forEach((key, value) -> {
                                String keyStr = (String) key;
                                boolean noneMatch = IGNORE_PROPERTIES.stream().noneMatch(s -> keyStr.startsWith(s));
                                if (noneMatch) {
                                    LookupElementBuilder builder = LookupElementBuilder.create(keyStr+"}}")
                                            .appendTailText((String) value, true)
                                            .withPresentableText(keyStr + " = ");
                                    resultSet.withPrefixMatcher(new PlainPrefixMatcher("")).addElement(builder);
                                }
                            }
                    );
                }
                return true;
            });
        }


    }
}

@@ -421,16 +421,15 @@
 t%0A *
-%0A *     
+ %3Cp%3E%0A *
  htt
@@ -470,16 +470,20 @@
 E-2.0%0A *
+ %3Cp%3E
 %0A * Unle
@@ -1130,16 +1130,68 @@
 roject;%0A
+import com.intellij.openapi.roots.ProjectFileIndex;%0A
 import c
@@ -2042,16 +2042,17 @@
 ogger.%22,
+ 
 %22camel.s
@@ -2078,17 +2078,16 @@
  public 
- 
 void pro
@@ -2553,16 +2553,115 @@
 ESTS));%0A
+        ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();
 %0A       
@@ -2875,73 +2875,181 @@
 es%22)
-) %7B%0A                    File file = new File(virtualFile.getPath(
+ && !projectFileIndex.isExcluded(sourceRoot)) %7B%0A                    getProperties(virtualFile).forEach((key, value) -%3E buildResultSet(resultSet, (String) key, (String) value
 ));%0A
@@ -3050,32 +3050,40 @@
 ue));%0A          
+      %7D%0A
           Proper
@@ -3080,20 +3080,88 @@
     
-Properties p
+      return true;%0A            %7D);%0A        %7D%0A%0A%0A    %7D%0A%0A    @NotNull%0A    private P
 rope
@@ -3170,14 +3170,11 @@
 ies 
-= new 
+get
 Prop
@@ -3184,48 +3184,147 @@
 ies(
-);%0A                    try %7B%0A   
+VirtualFile virtualFile) %7B%0A        File file = new File(virtualFile.getPath());%0A        Properties properties = new Properties();%0A%0A
         
     
@@ -3311,33 +3311,38 @@
 ies();%0A%0A        
- 
+try %7B%0A
             prop
@@ -3399,28 +3399,16 @@
         
-       
-     
 %7D catch 
@@ -3421,36 +3421,24 @@
 eption e) %7B%0A
-            
         %7D//T
@@ -3510,143 +3510,163 @@
     
- 
+return properties;%0A
     
+%7D%0A%0A
     
-   properties.forEach((key, value) -%3E %7B%0A                                String keyStr = (String) key;%0A                        
+private void buildResultSet(@NotNull CompletionResultSet resultSet, String key, String value) %7B%0A        String keyStr = key;%0A
     
@@ -3766,73 +3766,25 @@
     
-                        if (noneMatch) %7B%0A                        
+if (noneMatch) %7B%0A
     
@@ -3860,39 +3860,17 @@
 yStr
-+
+ + 
 %22%7D%7D%22)%0A
-                        
     
@@ -3901,25 +3901,16 @@
 ailText(
-(String) 
 value, t
@@ -3918,32 +3918,8 @@
 ue)%0A
-                        
     
@@ -3976,32 +3976,8 @@
 %22);%0A
-                        
     
@@ -4073,161 +4073,9 @@
     
-                        %7D%0A                            %7D%0A                    );%0A                %7D%0A                return true;%0A            %7D);%0A        %7D%0A%0A
+%7D
 %0A   

Exclude files/directories that are marked as excluded in module settings