/*
 * Copyright (C) 2016 Reece H. Dunn
 *
 * 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 uk.co.reecedunn.intellij.plugin.xquery.annotation;

import com.intellij.lang.annotation.AnnotationHolder;
import com.intellij.lang.annotation.Annotator;
import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import uk.co.reecedunn.intellij.plugin.xquery.ast.xquery.*;
import uk.co.reecedunn.intellij.plugin.xquery.lexer.IXQueryKeywordOrNCNameType;
import uk.co.reecedunn.intellij.plugin.xquery.lexer.SyntaxHighlighter;

public class QNameAnnotator implements Annotator {
    @Override
    public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
        if (!(element instanceof XQueryEQName)) return;
        if (element.getParent() instanceof XQueryEQName) return;

        XQueryEQName qname = (XQueryEQName)element;

        boolean xmlns = false;
        PsiElement prefix = qname.getPrefix().getOrElse(null);
        if (prefix != null && !(prefix instanceof XQueryBracedURILiteral)) {
            if (prefix.getText().equals("xmlns")) {
                xmlns = true;
            } else {
                holder.createInfoAnnotation(prefix, null).setEnforcedTextAttributes(TextAttributes.ERASE_MARKER);
                if ((element.getParent() instanceof XQueryDirAttributeList) || (element.getParent() instanceof XQueryDirElemConstructor)) {
                    holder.createInfoAnnotation(prefix, null).setTextAttributes(SyntaxHighlighter.XML_TAG);
                }
                holder.createInfoAnnotation(prefix, null).setTextAttributes(SyntaxHighlighter.NS_PREFIX);
            }
        }

        PsiElement localname = qname.getLocalNameElement();
        if (localname != null) {
            if (xmlns) {
                holder.createInfoAnnotation(localname, null).setEnforcedTextAttributes(TextAttributes.ERASE_MARKER);
                if (element.getParent() instanceof XQueryDirAttributeList) {
                    holder.createInfoAnnotation(localname, null).setTextAttributes(SyntaxHighlighter.XML_TAG);
                }
                holder.createInfoAnnotation(localname, null).setTextAttributes(SyntaxHighlighter.NS_PREFIX);
            } else if (qname.getParent() instanceof XQueryAnnotation) {
                holder.createInfoAnnotation(localname, null).setEnforcedTextAttributes(TextAttributes.ERASE_MARKER);
                holder.createInfoAnnotation(localname, null).setTextAttributes(SyntaxHighlighter.ANNOTATION);
            } else if (localname.getNode().getElementType() instanceof IXQueryKeywordOrNCNameType) {
                holder.createInfoAnnotation(localname, null).setEnforcedTextAttributes(TextAttributes.ERASE_MARKER);
                holder.createInfoAnnotation(localname, null).setTextAttributes(SyntaxHighlighter.IDENTIFIER);
            } else if (localname instanceof XQueryNCName) {
                if (((XQueryNCName)localname).getLocalNameElement().getNode().getElementType() instanceof IXQueryKeywordOrNCNameType) {
                    holder.createInfoAnnotation(localname, null).setEnforcedTextAttributes(TextAttributes.ERASE_MARKER);
                    holder.createInfoAnnotation(localname, null).setTextAttributes(SyntaxHighlighter.IDENTIFIER);
                }
            }
        }
    }
}

@@ -947,16 +947,81 @@
 uery.*;%0A
+import uk.co.reecedunn.intellij.plugin.xquery.functional.Option;%0A
 import u
@@ -1502,16 +1502,22 @@
         
+final 
 boolean 
@@ -1525,16 +1525,8 @@
 mlns
- = false
 ;%0A  
@@ -1766,16 +1766,47 @@
  else %7B%0A
+                xmlns = false;%0A
         
@@ -2306,17 +2306,23 @@
        %7D
-%0A
+ else %7B
 %0A       
@@ -2326,90 +2326,85 @@
     
-PsiElement localname = qname.getLocalNameElement();%0A        if 
+    xmlns = false;%0A        %7D%0A%0A        qname.getLocalName().map(
 (local
-name != null)
+Name) -%3E
  %7B%0A 
@@ -2468,33 +2468,33 @@
 Annotation(local
-n
+N
 ame, null).setEn
@@ -2666,33 +2666,33 @@
 Annotation(local
-n
+N
 ame, null).setTe
@@ -2791,33 +2791,33 @@
 Annotation(local
-n
+N
 ame, null).setTe
@@ -2972,33 +2972,33 @@
 Annotation(local
-n
+N
 ame, null).setEn
@@ -3089,33 +3089,33 @@
 Annotation(local
-n
+N
 ame, null).setTe
@@ -3186,17 +3186,17 @@
 f (local
-n
+N
 ame.getN
@@ -3300,33 +3300,33 @@
 Annotation(local
-n
+N
 ame, null).setEn
@@ -3417,33 +3417,33 @@
 Annotation(local
-n
+N
 ame, null).setTe
@@ -3514,17 +3514,17 @@
 f (local
-n
+N
 ame inst
@@ -3586,17 +3586,17 @@
 me)local
-n
+N
 ame).get
@@ -3608,17 +3608,29 @@
 Name
-Element()
+().map((name) -%3E name
 .get
@@ -3644,32 +3644,49 @@
 getElementType()
+).getOrElse(null)
  instanceof IXQu
@@ -3756,33 +3756,33 @@
 Annotation(local
-n
+N
 ame, null).setEn
@@ -3885,17 +3885,17 @@
 on(local
-n
+N
 ame, nul
@@ -3982,22 +3982,58 @@
   %7D%0A        
-%7D
+    return Option.none();%0A        %7D);
 %0A    %7D%0A%7D%0A

Use the Option version of getLocalName.