package com.eiffel.codeInsight.completion;

import com.eiffel.psi.EiffelClassDeclaration;
import com.eiffel.psi.EiffelClassUtil;
import com.eiffel.psi.EiffelTypes;
import com.intellij.codeInsight.completion.*;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.codeInsight.lookup.LookupElementPresentation;
import com.intellij.codeInsight.lookup.LookupElementRenderer;
import com.intellij.icons.AllIcons;
import com.intellij.psi.PsiElement;
import com.intellij.util.ProcessingContext;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class EiffelClassNameCompletionUtil {
    public static void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet resultSet) {
        if (isClassType(parameters.getPosition())) {
            List<EiffelClassDeclaration> classDeclarations = EiffelClassUtil.findClassDeclarations(parameters.getEditor().getProject());
            for (EiffelClassDeclaration declaration : classDeclarations) {
                resultSet.addElement(
                        LookupElementBuilder
                                .create(declaration.getName())
                                .withRenderer(new LookupElementRenderer<LookupElement>() {
                                    @Override
                                    public void renderElement(LookupElement element, LookupElementPresentation presentation) {
                                        presentation.setIcon(AllIcons.Nodes.Class);
                                        presentation.setItemText(EiffelClassUtil.formalizeName(declaration.getName()));
                                        presentation.setItemTextBold(true);
                                    }
                                })
                );
            }
        }
    }

    private static boolean isClassType(@NotNull PsiElement element) {
        if (!element.getNode().getElementType().equals(EiffelTypes.IDENTIFIER)) {
            return false;
        }
        if (element.getNode().getTreeParent() == null) return false;
        if (element.getNode().getTreeParent().getTreeParent() == null) return false;
        return element.getNode().getTreeParent().getTreeParent().getElementType().equals(EiffelTypes.TYPE);
    }
}

@@ -1084,24 +1084,81 @@
 arations) %7B%0A
+                if (declaration.getName() != null) %7B%0A    
             
@@ -1207,16 +1207,20 @@
         
+   
+ 
 LookupEl
@@ -1232,16 +1232,20 @@
 Builder%0A
+    
         
@@ -1299,16 +1299,20 @@
 Name())%0A
+    
         
@@ -1430,16 +1430,20 @@
         
+    
 @Overrid
@@ -1476,24 +1476,28 @@
             
+  
+  
 public void 
@@ -1607,32 +1607,36 @@
                 
+    
 presentation.set
@@ -1695,32 +1695,36 @@
                 
+  
+  
 presentation.set
@@ -1819,32 +1819,36 @@
                 
+    
 presentation.set
@@ -1895,34 +1895,42 @@
                 
+ 
-%7D%0A
+   %7D%0A    
                 
@@ -1964,18 +1964,40 @@
         
-);
+    );%0A                %7D
 %0A       

fix class completion util failing with incomplete classes