Search in sources :

Example 1 with IndexPattern

use of com.intellij.psi.search.IndexPattern in project intellij-community by JetBrains.

the class BaseFilterLexer method advanceTodoItemsCount.

public static TodoScanningState advanceTodoItemsCount(final CharSequence input, final OccurrenceConsumer consumer, TodoScanningState todoScanningState) {
    if (todoScanningState == null) {
        IndexPattern[] patterns = IndexPatternUtil.getIndexPatterns();
        Matcher[] matchers = new Matcher[patterns.length];
        todoScanningState = new TodoScanningState(patterns, matchers);
        for (int i = 0; i < patterns.length; ++i) {
            Pattern pattern = patterns[i].getOptimizedIndexingPattern();
            if (pattern != null) {
                matchers[i] = pattern.matcher("");
            }
        }
    } else {
        todoScanningState.myOccurences.resetQuick();
    }
    for (int i = todoScanningState.myMatchers.length - 1; i >= 0; --i) {
        Matcher matcher = todoScanningState.myMatchers[i];
        if (matcher == null)
            continue;
        matcher.reset(input);
        while (matcher.find()) {
            int start = matcher.start();
            if (start != matcher.end() && todoScanningState.myOccurences.indexOf(start) == -1) {
                consumer.incTodoOccurrence(todoScanningState.myPatterns[i]);
                todoScanningState.myOccurences.add(start);
            }
        }
    }
    return todoScanningState;
}
Also used : IndexPattern(com.intellij.psi.search.IndexPattern) Pattern(java.util.regex.Pattern) IndexPattern(com.intellij.psi.search.IndexPattern) Matcher(java.util.regex.Matcher)

Example 2 with IndexPattern

use of com.intellij.psi.search.IndexPattern in project intellij-community by JetBrains.

the class PlainTextTodoIndexer method map.

@Override
@NotNull
public Map<TodoIndexEntry, Integer> map(@NotNull final FileContent inputData) {
    // matching strings is faster than HeapCharBuffer
    String chars = inputData.getContentAsText().toString();
    final IndexPattern[] indexPatterns = IndexPatternUtil.getIndexPatterns();
    if (indexPatterns.length <= 0) {
        return Collections.emptyMap();
    }
    OccurrenceConsumer occurrenceConsumer = new OccurrenceConsumer(null, true);
    for (IndexPattern indexPattern : indexPatterns) {
        Pattern pattern = indexPattern.getOptimizedIndexingPattern();
        if (pattern != null) {
            Matcher matcher = pattern.matcher(chars);
            while (matcher.find()) {
                if (matcher.start() != matcher.end()) {
                    occurrenceConsumer.incTodoOccurrence(indexPattern);
                }
            }
        }
    }
    Map<TodoIndexEntry, Integer> map = new HashMap<>();
    for (IndexPattern indexPattern : indexPatterns) {
        final int count = occurrenceConsumer.getOccurrenceCount(indexPattern);
        if (count > 0) {
            map.put(new TodoIndexEntry(indexPattern.getPatternString(), indexPattern.isCaseSensitive()), count);
        }
    }
    return map;
}
Also used : OccurrenceConsumer(com.intellij.psi.impl.cache.impl.OccurrenceConsumer) IndexPattern(com.intellij.psi.search.IndexPattern) Pattern(java.util.regex.Pattern) IndexPattern(com.intellij.psi.search.IndexPattern) Matcher(java.util.regex.Matcher) HashMap(java.util.HashMap) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with IndexPattern

use of com.intellij.psi.search.IndexPattern in project intellij-community by JetBrains.

the class IndexPatternSearcher method collectPatternMatches.

private static boolean collectPatternMatches(IndexPattern indexPattern, CharSequence chars, int commentStart, int commentEnd, PsiFile file, TextRange range, Processor<IndexPatternOccurrence> consumer, TIntArrayList matches) {
    Pattern pattern = indexPattern.getPattern();
    if (pattern != null) {
        ProgressManager.checkCanceled();
        CharSequence input = new CharSequenceSubSequence(chars, commentStart, commentEnd);
        Matcher matcher = pattern.matcher(input);
        while (true) {
            //long time1 = System.currentTimeMillis();
            boolean found = matcher.find();
            if (!found)
                break;
            int start = matcher.start() + commentStart;
            int end = matcher.end() + commentStart;
            if (start != end) {
                if ((range == null || range.getStartOffset() <= start && end <= range.getEndOffset()) && matches.indexOf(start) == -1) {
                    matches.add(start);
                    if (!consumer.process(new IndexPatternOccurrenceImpl(file, start, end, indexPattern))) {
                        return false;
                    }
                }
            }
            ProgressManager.checkCanceled();
        }
    }
    return true;
}
Also used : CharSequenceSubSequence(com.intellij.util.text.CharSequenceSubSequence) IndexPattern(com.intellij.psi.search.IndexPattern) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher)

Example 4 with IndexPattern

use of com.intellij.psi.search.IndexPattern in project intellij-community by JetBrains.

the class IndexTodoCacheManagerImpl method getFilesWithTodoItems.

@Override
@NotNull
public PsiFile[] getFilesWithTodoItems() {
    if (myProject.isDefault()) {
        return PsiFile.EMPTY_ARRAY;
    }
    final FileBasedIndex fileBasedIndex = FileBasedIndex.getInstance();
    final Set<PsiFile> allFiles = new HashSet<>();
    final ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
    for (IndexPattern indexPattern : IndexPatternUtil.getIndexPatterns()) {
        final Collection<VirtualFile> files = fileBasedIndex.getContainingFiles(TodoIndex.NAME, new TodoIndexEntry(indexPattern.getPatternString(), indexPattern.isCaseSensitive()), GlobalSearchScope.allScope(myProject));
        ApplicationManager.getApplication().runReadAction(() -> {
            for (VirtualFile file : files) {
                if (projectFileIndex.isInContent(file)) {
                    final PsiFile psiFile = myPsiManager.findFile(file);
                    if (psiFile != null) {
                        allFiles.add(psiFile);
                    }
                }
            }
        });
    }
    return allFiles.isEmpty() ? PsiFile.EMPTY_ARRAY : PsiUtilCore.toPsiFileArray(allFiles);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) IndexPattern(com.intellij.psi.search.IndexPattern) PsiFile(com.intellij.psi.PsiFile) TodoIndexEntry(com.intellij.psi.impl.cache.impl.todo.TodoIndexEntry) FileBasedIndex(com.intellij.util.indexing.FileBasedIndex) HashSet(java.util.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with IndexPattern

use of com.intellij.psi.search.IndexPattern in project intellij-community by JetBrains.

the class IndexPatternSearcher method executeImpl.

protected static boolean executeImpl(IndexPatternSearch.SearchParameters queryParameters, Processor<IndexPatternOccurrence> consumer) {
    final IndexPatternProvider patternProvider = queryParameters.getPatternProvider();
    final PsiFile file = queryParameters.getFile();
    TIntArrayList commentStarts = new TIntArrayList();
    TIntArrayList commentEnds = new TIntArrayList();
    final CharSequence chars = file.getViewProvider().getContents();
    findCommentTokenRanges(file, chars, queryParameters.getRange(), commentStarts, commentEnds);
    TIntArrayList occurrences = new TIntArrayList(1);
    IndexPattern[] patterns = patternProvider != null ? patternProvider.getIndexPatterns() : null;
    for (int i = 0; i < commentStarts.size(); i++) {
        int commentStart = commentStarts.get(i);
        int commentEnd = commentEnds.get(i);
        occurrences.resetQuick();
        if (patternProvider != null) {
            for (int j = patterns.length - 1; j >= 0; --j) {
                if (!collectPatternMatches(patterns[j], chars, commentStart, commentEnd, file, queryParameters.getRange(), consumer, occurrences)) {
                    return false;
                }
            }
        } else {
            if (!collectPatternMatches(queryParameters.getPattern(), chars, commentStart, commentEnd, file, queryParameters.getRange(), consumer, occurrences)) {
                return false;
            }
        }
    }
    return true;
}
Also used : IndexPatternProvider(com.intellij.psi.search.IndexPatternProvider) IndexPattern(com.intellij.psi.search.IndexPattern) TIntArrayList(gnu.trove.TIntArrayList)

Aggregations

IndexPattern (com.intellij.psi.search.IndexPattern)7 Matcher (java.util.regex.Matcher)3 Pattern (java.util.regex.Pattern)3 NotNull (org.jetbrains.annotations.NotNull)3 TodoIndexEntry (com.intellij.psi.impl.cache.impl.todo.TodoIndexEntry)2 IndexPatternProvider (com.intellij.psi.search.IndexPatternProvider)2 Lexer (com.intellij.lexer.Lexer)1 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiFile (com.intellij.psi.PsiFile)1 OccurrenceConsumer (com.intellij.psi.impl.cache.impl.OccurrenceConsumer)1 LexerBasedIdIndexer (com.intellij.psi.impl.cache.impl.id.LexerBasedIdIndexer)1 FileBasedIndex (com.intellij.util.indexing.FileBasedIndex)1 IdDataConsumer (com.intellij.util.indexing.IdDataConsumer)1 CharSequenceSubSequence (com.intellij.util.text.CharSequenceSubSequence)1 THashMap (gnu.trove.THashMap)1 TIntArrayList (gnu.trove.TIntArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1