/*
 * Copyright 2013, Bruce Mitchener, Jr.
 *
 * 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 org.dylanfoundry.deft.filetypes.lid.inspections;

import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import org.dylanfoundry.deft.filetypes.lid.psi.LIDProperty;
import org.dylanfoundry.deft.filetypes.lid.psi.LIDVisitor;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;

class MustContainLibraryInspection extends AbstractLIDInspection {
  @Override
  public String getStaticDescription() {
    return "LID files must have a library keyword.";
  }

  @Override
  @Nls
  @NotNull
  public String getDisplayName() {
    return "Must contain 'Library'";
  }

  public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
    return new LIDVisitor() {
      @Override
      public void visitFile(PsiFile file) {
        boolean hasLibraryProperty = false;
        LIDProperty[] properties = PsiTreeUtil.getChildrenOfType(file, LIDProperty.class);
        if (properties != null) {
          for (LIDProperty property : properties) {
            if (property.getFirstChild().getText().toLowerCase().equals("library")) {
              hasLibraryProperty = true;
              break;
            }
          }
        }
        if (hasLibraryProperty == false) {
          holder.registerProblem(properties[0], "Must contain 'Library' property.", ProblemHighlightType.ERROR);
        }
      }
    };
  }
}

@@ -879,16 +879,72 @@
 eeUtil;%0A
+import org.dylanfoundry.deft.filetypes.lid.psi.LIDFile;%0A
 import o
@@ -1622,44 +1622,41 @@
     
-boolean hasLibraryProperty = false;%0A
+if (file instanceof LIDFile) %7B%0A  
     
@@ -1742,32 +1742,34 @@
 class);%0A        
+  
 if (properties !
@@ -1774,24 +1774,74 @@
  != null) %7B%0A
+            boolean hasLibraryProperty = false;%0A  
           fo
@@ -1884,32 +1884,34 @@
 ) %7B%0A            
+ 
+ 
 if (property.get
@@ -1982,16 +1982,18 @@
         
+  
 hasLibra
@@ -2025,16 +2025,18 @@
         
+  
 break;%0A 
@@ -2038,34 +2038,38 @@
 ak;%0A            
-%7D%0A
+  %7D%0A  
           %7D%0A    
@@ -2064,30 +2064,24 @@
       %7D%0A    
-    %7D%0A
         if (
@@ -2107,24 +2107,28 @@
 == false) %7B%0A
+    
           ho
@@ -2228,16 +2228,42 @@
 ERROR);%0A
+            %7D%0A          %7D%0A
         

Make MustContainLibraryInspection robust.