public static PsiElement[] parsePattern( Project project, String context, String pattern, FileType fileType, Language language, String extension, boolean physical) { int offset = context.indexOf(PATTERN_PLACEHOLDER); final int patternLength = pattern.length(); final String patternInContext = context.replace(PATTERN_PLACEHOLDER, pattern); final String ext = extension != null ? extension : fileType.getDefaultExtension(); final String name = "__dummy." + ext; final PsiFileFactory factory = PsiFileFactory.getInstance(project); final PsiFile file = language == null ? factory.createFileFromText( name, fileType, patternInContext, LocalTimeCounter.currentTime(), physical, true) : factory.createFileFromText(name, language, patternInContext, physical, true); if (file == null) { return PsiElement.EMPTY_ARRAY; } final List<PsiElement> result = new ArrayList<PsiElement>(); PsiElement element = file.findElementAt(offset); if (element == null) { return PsiElement.EMPTY_ARRAY; } PsiElement topElement = element; element = element.getParent(); while (element != null) { if (element.getTextRange().getStartOffset() == offset && element.getTextLength() <= patternLength) { topElement = element; } element = element.getParent(); } if (topElement instanceof PsiFile) { return topElement.getChildren(); } final int endOffset = offset + patternLength; result.add(topElement); topElement = topElement.getNextSibling(); while (topElement != null && topElement.getTextRange().getEndOffset() <= endOffset) { result.add(topElement); topElement = topElement.getNextSibling(); } return result.toArray(new PsiElement[result.size()]); }
private static boolean checkErrorElements(PsiElement element) { final boolean[] result = {true}; final int endOffset = element.getTextRange().getEndOffset(); element.accept( new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(PsiElement element) { super.visitElement(element); if (element instanceof PsiErrorElement && element.getTextRange().getEndOffset() == endOffset) { result[0] = false; } } }); return result[0]; }