/*
 * 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.psi.impl.xquery;

import com.intellij.extapi.psi.ASTWrapperPsiElement;
import com.intellij.lang.ASTNode;
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.functional.Option;
import uk.co.reecedunn.intellij.plugin.xquery.parser.XQueryElementType;
import uk.co.reecedunn.intellij.plugin.xquery.psi.PsiNavigation;
import uk.co.reecedunn.intellij.plugin.xquery.psi.XQueryNamespace;
import uk.co.reecedunn.intellij.plugin.xquery.psi.XQueryNamespaceResolver;
import uk.co.reecedunn.intellij.plugin.xquery.psi.XQueryPrologResolver;

public class XQueryModuleDeclPsiImpl extends ASTWrapperPsiElement implements XQueryModuleDecl, XQueryNamespaceResolver, XQueryPrologResolver {
    public XQueryModuleDeclPsiImpl(@NotNull ASTNode node) {
        super(node);
    }

    @Override
    public Option<XQueryNamespace> resolveNamespace(CharSequence prefix) {
        XQueryNCName name = findChildByType(XQueryElementType.NCNAME);
        if (name == null) {
            return Option.none();
        }

        if (name.getLocalNameElement().getText().equals(prefix)) {
            PsiElement element = findChildByType(XQueryElementType.URI_LITERAL);
            return Option.some(new XQueryNamespace(name.getLocalNameElement(), element, this));
        }
        return Option.none();
    }

    @Override
    public Option<XQueryProlog> resolveProlog() {
        PsiElement file = PsiNavigation.findParentByClass(this, XQueryFile.class);
        PsiElement module = PsiNavigation.findChildByClass(file, XQueryModule.class);
        return Option.of(PsiNavigation.findChildByClass(module, XQueryProlog.class));
    }
}

@@ -1765,28 +1765,31 @@
  %7D%0A%0A        
-if (
+return 
 name.getLoca
@@ -1793,25 +1793,69 @@
 ocalName
-Element()
+().flatMap((localName) -%3E %7B%0A            if (localName
 .getText
@@ -1871,24 +1871,28 @@
 (prefix)) %7B%0A
+    
             
@@ -1964,32 +1964,36 @@
 L);%0A            
+    
 return Option.so
@@ -2019,34 +2019,17 @@
 ace(
-name.getLocalNameElement()
+localName
 , el
@@ -2055,18 +2055,25 @@
     
-%7D%0A
+    %7D%0A   
         
 retu
@@ -2060,32 +2060,33 @@
    %7D%0A           
+ 
 return Option.no
@@ -2087,24 +2087,36 @@
 ion.none();%0A
+        %7D);%0A
     %7D%0A%0A    @

Use the Option version of getLocalName.