001    /*
002     * Copyright 2010-2013 JetBrains s.r.o.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.jetbrains.jet.lang.diagnostics;
018    
019    import com.intellij.lang.ASTNode;
020    import com.intellij.openapi.util.TextRange;
021    import com.intellij.psi.PsiElement;
022    import com.intellij.psi.PsiErrorElement;
023    import org.jetbrains.annotations.NotNull;
024    
025    import java.util.Collections;
026    import java.util.List;
027    
028    public class PositioningStrategy<E extends PsiElement> {
029        @NotNull
030        public List<TextRange> markDiagnostic(@NotNull ParametrizedDiagnostic<? extends E> diagnostic) {
031            return mark(diagnostic.getPsiElement());
032        }
033    
034        @NotNull
035        protected List<TextRange> mark(@NotNull E element) {
036            return markElement(element);
037        }
038    
039        public boolean isValid(@NotNull E element) {
040            return !hasSyntaxErrors(element);
041        }
042    
043        @NotNull
044        protected static List<TextRange> markElement(@NotNull PsiElement element) {
045            return Collections.singletonList(element.getTextRange());
046        }
047    
048        @NotNull
049        protected static List<TextRange> markNode(@NotNull ASTNode node) {
050            return Collections.singletonList(node.getTextRange());
051        }
052    
053        @NotNull
054        protected static List<TextRange> markRange(@NotNull TextRange range) {
055            return Collections.singletonList(range);
056        }
057    
058        @NotNull
059        protected static List<TextRange> markRange(@NotNull PsiElement from, @NotNull PsiElement to) {
060            return markRange(new TextRange(from.getTextRange().getStartOffset(), to.getTextRange().getEndOffset()));
061        }
062    
063        protected static boolean hasSyntaxErrors(@NotNull PsiElement psiElement) {
064            if (psiElement instanceof PsiErrorElement) return true;
065    
066            PsiElement[] children = psiElement.getChildren();
067            if (children.length > 0 && hasSyntaxErrors(children[children.length - 1])) return true;
068    
069            return false;
070        }
071    }