package org.asciidoc.intellij.grazie;

import com.intellij.grazie.text.ProblemFilter;
import com.intellij.grazie.text.TextProblem;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiWhiteSpace;
import org.jetbrains.annotations.NotNull;

public class AsciiDocGrazieProblemFilter extends ProblemFilter {

  private final AsciiDocLanguageSupport languageSupport = new AsciiDocLanguageSupport();

  @Override
  public boolean shouldIgnore(@NotNull TextProblem problem) {
    if (problem.getHighlightRanges().size() == 1) {
      // start with the character just before the problem
      int start = problem.getHighlightRanges().get(0).getStartOffset() - 1;
      while (start > 0 && problem.getText().charAt(start) == ' ') {
        // If we're at a whitespace, and the previous element is UNKNOWN, ignore this problem message.
        // This handles problems when parsing a text like "Foo bar. <<id>> should be modelled."
        // would ask for "should" to be capitalized as it is at the start of a sentence.
        PsiElement element = problem.getText().findPsiElementAt(start);
        if (element instanceof PsiWhiteSpace && element.getPrevSibling() != null) {
          if (languageSupport.getElementBehavior(element.getParent(), element) == AsciiDocLanguageSupport.Behavior.UNKNOWN) {
            return true;
          }
        }
        // repeat while we're at a whitespace
        start--;
      }
    }
    return false;
  }
}

@@ -476,62 +476,8 @@
 ) %7B%0A
-    if (problem.getHighlightRanges().size() == 1) %7B%0A  
     
@@ -532,18 +532,16 @@
 lem%0A    
-  
 int star
@@ -573,19 +573,11 @@
 ange
-s
 ().
-get(0).
 getS
@@ -594,18 +594,16 @@
 () - 1;%0A
-  
     whil
@@ -666,18 +666,16 @@
 %7B%0A      
-  
 // If we
@@ -767,18 +767,16 @@
 .%0A      
-  
 // This 
@@ -855,18 +855,16 @@
 elled.%22%0A
-  
       //
@@ -948,18 +948,16 @@
 .%0A      
-  
 PsiEleme
@@ -1012,18 +1012,16 @@
 start);%0A
-  
       if
@@ -1090,26 +1090,24 @@
  != null) %7B%0A
-  
         if (
@@ -1228,18 +1228,16 @@
         
-  
 return t
@@ -1245,30 +1245,26 @@
 ue;%0A        
-  %7D%0A  
+%7D%0A
       %7D%0A    
@@ -1265,18 +1265,16 @@
 %7D%0A      
-  
 // repea
@@ -1309,18 +1309,16 @@
 e%0A      
-  
 start--;
@@ -1318,24 +1318,16 @@
 tart--;%0A
-      %7D%0A
     %7D%0A  

make compatible with 2021.3.1 (#980)