/*
 * 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.PsiFileBase;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.psi.FileViewProvider;
import org.jetbrains.annotations.NotNull;
import uk.co.reecedunn.intellij.plugin.xquery.ast.xquery.XQueryFile;
import uk.co.reecedunn.intellij.plugin.xquery.ast.xquery.XQueryModule;
import uk.co.reecedunn.intellij.plugin.xquery.ast.xquery.XQueryStringLiteral;
import uk.co.reecedunn.intellij.plugin.xquery.ast.xquery.XQueryVersionDecl;
import uk.co.reecedunn.intellij.plugin.xquery.filetypes.XQueryFileType;
import uk.co.reecedunn.intellij.plugin.xquery.lang.XQuery;
import uk.co.reecedunn.intellij.plugin.xquery.lang.XQueryVersion;
import uk.co.reecedunn.intellij.plugin.xquery.psi.PsiNavigation;
import uk.co.reecedunn.intellij.plugin.xquery.settings.XQueryProjectSettings;

public class XQueryFileImpl extends PsiFileBase implements XQueryFile {
    public XQueryFileImpl(@NotNull FileViewProvider provider) {
        super(provider, XQuery.INSTANCE);
    }

    @Override
    @SuppressWarnings("NullableProblems") // jacoco Code Coverage reports an unchecked branch when @NotNull is used.
    public FileType getFileType() {
        return XQueryFileType.INSTANCE;
    }

    public XQueryVersion getXQueryVersion() {
        XQueryModule module = PsiNavigation.findChildByClass(this, XQueryModule.class);
        if (module != null) {
            XQueryVersionDecl versionDecl = PsiNavigation.findDirectDescendantByClass(module, XQueryVersionDecl.class);
            if (versionDecl != null) {
                XQueryStringLiteral version = versionDecl.getVersion();
                if (version != null) {
                    XQueryVersion xqueryVersion = XQueryVersion.parse(version.getAtomicValue());
                    if (xqueryVersion != null) {
                        return xqueryVersion;
                    }
                }
            }
        }

        XQueryProjectSettings settings = XQueryProjectSettings.getInstance(getProject());
        return settings.getXQueryVersion();
    }
}

@@ -1193,24 +1193,89 @@
 ryFileType;%0A
+import uk.co.reecedunn.intellij.plugin.xquery.functional.Option;%0A
 import uk.co
@@ -1391,73 +1391,8 @@
 on;%0A
-import uk.co.reecedunn.intellij.plugin.xquery.psi.PsiNavigation;%0A
 impo
@@ -1466,16 +1466,196 @@
 tings;%0A%0A
+import static uk.co.reecedunn.intellij.plugin.xquery.functional.PsiTreeWalker.children;%0Aimport static uk.co.reecedunn.intellij.plugin.xquery.functional.PsiTreeWalker.descendants;%0A%0A
 public c
@@ -2103,212 +2103,122 @@
     
-XQueryModule module = PsiNavigation.findChildByClass(this, XQueryModule.class);%0A        if (module != null) %7B%0A            XQueryVersionDecl versionDecl = PsiNavigation
+return children(this).findFirst(XQueryModule.class).flatMap((module) -%3E%0A            descendants(module)
 .find
-DirectDescendantByClass(module, 
+First(
 XQue
@@ -2241,25 +2241,17 @@
 ass)
-;%0A            if 
+.flatMap(
 (ver
@@ -2258,25 +2258,20 @@
 sionDecl
- != null)
+) -%3E
  %7B%0A     
@@ -2553,16 +2553,28 @@
  return 
+Option.some(
 xqueryVe
@@ -2578,16 +2578,17 @@
 yVersion
+)
 ;%0A      
@@ -2637,21 +2637,78 @@
     
-%7D%0A        %7D%0A%0A
+    return Option.none();%0A            %7D)%0A        ).orElse(() -%3E %7B%0A    
     
@@ -2797,24 +2797,28 @@
 ));%0A        
+    
 return setti
@@ -2837,20 +2837,38 @@
 yVersion();%0A
+        %7D).get();%0A
     %7D%0A%7D%0A

Use the new PsiTreeWalker API in XQueryFileImpl.