List<PsiElement> (TextRange range, TemplateLanguageFileViewProvider viewProvider, Language language) { List<PsiElement> matchingElements = new ArrayList<>(); PsiElement currElement = viewProvider.findElementAt(range.getStartOffset(), language); while (currElement instanceof OuterLanguageElement) { currElement = currElement.getNextSibling(); } if (currElement != null) { currElement = findTopmostElementInRange(currElement, range); Pair<Integer, PsiElement> result = addElementSequence(currElement, range, matchingElements); int lastOffset = result.first; assert lastOffset >= 0 : "Failed to process elements in range: " + range; if (lastOffset < range.getEndOffset()) { matchingElements.addAll(findAllElementsInside(new TextRange(lastOffset, range.getEndOffset()), viewProvider, language)); } } return matchingElements; }