use of com.intellij.structuralsearch.plugin.util.CollectingMatchResultSink in project intellij-community by JetBrains.
the class MatcherImpl method matchByDownUp.
@NotNull
protected List<MatchResult> matchByDownUp(PsiElement element, final MatchOptions options) {
final CollectingMatchResultSink sink = new CollectingMatchResultSink();
final CompiledPattern compiledPattern = prepareMatching(sink, options);
matchContext.setShouldRecursivelyMatch(false);
PsiElement targetNode = compiledPattern.getTargetNode();
PsiElement elementToStartMatching = null;
if (targetNode == null) {
targetNode = compiledPattern.getNodes().current();
if (targetNode != null) {
compiledPattern.getNodes().advance();
assert !compiledPattern.getNodes().hasNext();
compiledPattern.getNodes().rewind();
element = element.getParent();
if (element == null) {
return Collections.emptyList();
}
while (element.getClass() != targetNode.getClass()) {
element = element.getParent();
if (element == null)
return Collections.emptyList();
}
elementToStartMatching = element;
}
} else {
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByPsiElement(element);
if (profile == null)
return Collections.emptyList();
targetNode = profile.extendMatchedByDownUp(targetNode);
MatchingHandler handler = null;
while (element.getClass() == targetNode.getClass() || compiledPattern.isTypedVar(targetNode) && compiledPattern.getHandler(targetNode).canMatch(targetNode, element, matchContext)) {
handler = compiledPattern.getHandler(targetNode);
handler.setPinnedElement(element);
elementToStartMatching = element;
if (handler instanceof TopLevelMatchingHandler)
break;
element = element.getParent();
targetNode = targetNode.getParent();
if (options.isLooseMatching()) {
element = profile.updateCurrentNode(element);
targetNode = profile.updateCurrentNode(targetNode);
}
}
if (!(handler instanceof TopLevelMatchingHandler))
return Collections.emptyList();
}
assert targetNode != null : "Could not match down up when no target node";
final LanguageFileType fileType = (LanguageFileType) matchContext.getOptions().getFileType();
match(elementToStartMatching, fileType.getLanguage());
matchContext.getSink().matchingFinished();
return sink.getMatches();
}
use of com.intellij.structuralsearch.plugin.util.CollectingMatchResultSink in project intellij-community by JetBrains.
the class Replacer method testReplace.
public String testReplace(String in, String what, String by, ReplaceOptions options, boolean filePattern, boolean createPhysicalFile, FileType sourceFileType, Language sourceDialect) {
this.options = options;
final MatchOptions matchOptions = this.options.getMatchOptions();
matchOptions.setSearchPattern(what);
this.options.setReplacement(by);
replacementBuilder = null;
context = null;
replaceHandler = null;
matchOptions.clearVariableConstraints();
MatcherImplUtil.transform(matchOptions);
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByFileType(matchOptions.getFileType());
assert profile != null;
profile.checkSearchPattern(project, matchOptions);
checkSupportedReplacementPattern(project, options);
Matcher matcher = new Matcher(project);
try {
PsiElement firstElement, lastElement, parent;
if (options.getMatchOptions().getScope() == null) {
PsiElement[] elements = MatcherImplUtil.createTreeFromText(in, filePattern ? PatternTreeContext.File : PatternTreeContext.Block, sourceFileType, sourceDialect, null, project, createPhysicalFile);
firstElement = elements[0];
lastElement = elements[elements.length - 1];
parent = firstElement.getParent();
matchOptions.setScope(new LocalSearchScope(parent));
} else {
parent = ((LocalSearchScope) options.getMatchOptions().getScope()).getScope()[0];
firstElement = parent.getFirstChild();
lastElement = parent.getLastChild();
}
matchOptions.setResultIsContextMatch(true);
CollectingMatchResultSink sink = new CollectingMatchResultSink();
matcher.testFindMatches(sink, matchOptions);
final List<ReplacementInfo> resultPtrList = new ArrayList<>();
for (final MatchResult result : sink.getMatches()) {
resultPtrList.add(buildReplacement(result));
}
sink.getMatches().clear();
int startOffset = firstElement.getTextRange().getStartOffset();
int endOffset = filePattern ? 0 : parent.getTextLength() - (lastElement.getTextRange().getEndOffset());
// get nodes from text may contain
PsiElement prevSibling = firstElement.getPrevSibling();
if (prevSibling instanceof PsiWhiteSpace) {
startOffset -= prevSibling.getTextLength() - 1;
}
PsiElement nextSibling = lastElement.getNextSibling();
if (nextSibling instanceof PsiWhiteSpace) {
endOffset -= nextSibling.getTextLength() - 1;
}
replaceAll(resultPtrList);
String result = parent.getText();
result = result.substring(startOffset);
result = result.substring(0, result.length() - endOffset);
return result;
} catch (Exception e) {
throw new IncorrectOperationException(e);
} finally {
options.getMatchOptions().setScope(null);
}
}
use of com.intellij.structuralsearch.plugin.util.CollectingMatchResultSink in project intellij-community by JetBrains.
the class MatcherImpl method testFindMatches.
/**
* Finds the matches of given pattern starting from given tree element.
* @param source string for search
* @param pattern to be searched
* @return list of matches found
* @throws MalformedPatternException
* @throws UnsupportedPatternException
*/
protected List<MatchResult> testFindMatches(String source, String pattern, MatchOptions options, boolean filePattern, FileType sourceFileType, String sourceExtension, boolean physicalSourceFile) throws MalformedPatternException, UnsupportedPatternException {
CollectingMatchResultSink sink = new CollectingMatchResultSink();
try {
PsiElement[] elements = MatcherImplUtil.createSourceTreeFromText(source, filePattern ? PatternTreeContext.File : PatternTreeContext.Block, sourceFileType, sourceExtension, project, physicalSourceFile);
options.setSearchPattern(pattern);
options.setScope(new LocalSearchScope(elements));
testFindMatches(sink, options);
} catch (IncorrectOperationException e) {
MalformedPatternException exception = new MalformedPatternException();
exception.initCause(e);
throw exception;
} finally {
options.setScope(null);
}
return sink.getMatches();
}
Aggregations