package com.intellij.codeInspection;

import com.intellij.analysis.AnalysisScope;
import com.intellij.codeHighlighting.HighlightDisplayLevel;
import com.intellij.codeInspection.reference.RefElement;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiErrorElement;
import com.intellij.psi.PsiRecursiveElementVisitor;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;

/**
 * @author yole
 */
public class SyntaxErrorInspection extends GlobalInspectionTool {
  @Override
  public boolean isGraphNeeded() {
    return false;
  }

  @NotNull
  @Override
  public HighlightDisplayLevel getDefaultLevel() {
    return HighlightDisplayLevel.ERROR;
  }

  @Override
  public void runInspection(AnalysisScope scope,
                            final InspectionManager manager,
                            final GlobalInspectionContext globalContext,
                            final ProblemDescriptionsProcessor problemDescriptionsProcessor) {
    scope.accept(new PsiRecursiveElementVisitor() {
      @Override
      public void visitErrorElement(PsiErrorElement element) {
        super.visitErrorElement(element);
        CommonProblemDescriptor descriptor;
        final TextRange textRange = element.getTextRange();
        if (textRange.getLength() > 0) {
          descriptor = manager.createProblemDescriptor(element, element.getErrorDescription(), ProblemHighlightType.ERROR, null);
        }
        else {
          descriptor = manager.createProblemDescriptor(element.getParent(),
                                                       new TextRange(element.getStartOffsetInParent(), element.getStartOffsetInParent()+1),
                                                       element.getErrorDescription(), ProblemHighlightType.ERROR);
        }

        final RefElement refElement = globalContext.getRefManager().getReference(element.getContainingFile());
        problemDescriptionsProcessor.addProblemElement(refElement, descriptor);
      }
    });
  }

  @Nls
  @NotNull
  @Override
  public String getGroupDisplayName() {
    return "General";
  }

  @Nls
  @NotNull
  @Override
  public String getDisplayName() {
    return "Syntax error";
  }

  @NotNull
  @Override
  public String getShortName() {
    return "SyntaxError";
  }
}

@@ -236,16 +236,52 @@
 tRange;%0A
+import com.intellij.psi.PsiElement;%0A
 import c
@@ -1502,57 +1502,76 @@
     
-descriptor = manager.createProblemDescriptor(elem
+PsiElement parent = element;%0A          do %7B%0A            parent = par
 ent.
@@ -1581,17 +1581,17 @@
 Parent()
-,
+;
 %0A       
@@ -1597,57 +1597,147 @@
     
-                                             new 
+%7D while(parent != null && parent.getTextRange().getLength() == 0);%0A          if (parent == null) return;%0A          int offset = element.get
 Text
@@ -1742,23 +1742,17 @@
 xtRange(
-element
+)
 .getStar
@@ -1762,52 +1762,199 @@
 fset
-InParent(), element.getStartOffsetInParent()
+() - parent.getTextRange().getStartOffset();%0A          descriptor = manager.createProblemDescriptor(parent,%0A                                                       new TextRange(offset, offset
 +1),

fix empty element assertion from syntax error inspection