use of com.intellij.codeInsight.template.Template in project intellij-community by JetBrains.
the class PatternCompiler method doCompile.
private static List<PsiElement> doCompile(Project project, MatchOptions options, CompiledPattern result, PrefixProvider prefixProvider, CompileContext context) {
result.clearHandlers();
context.init(result, options, project, options.getScope() instanceof GlobalSearchScope);
final StringBuilder buf = new StringBuilder();
Template template = TemplateManager.getInstance(project).createTemplate("", "", options.getSearchPattern());
int segmentsCount = template.getSegmentsCount();
String text = template.getTemplateText();
buf.setLength(0);
int prevOffset = 0;
for (int i = 0; i < segmentsCount; ++i) {
final int offset = template.getSegmentOffset(i);
final String name = template.getSegmentName(i);
final String prefix = prefixProvider.getPrefix(i);
if (prefix == null) {
throw new MalformedPatternException();
}
buf.append(text.substring(prevOffset, offset));
buf.append(prefix);
buf.append(name);
MatchVariableConstraint constraint = options.getVariableConstraint(name);
if (constraint == null) {
// we do not edited the constraints
constraint = new MatchVariableConstraint();
constraint.setName(name);
options.addVariableConstraint(constraint);
}
SubstitutionHandler handler = result.createSubstitutionHandler(name, prefix + name, constraint.isPartOfSearchResults(), constraint.getMinCount(), constraint.getMaxCount(), constraint.isGreedy());
if (constraint.isWithinHierarchy()) {
handler.setSubtype(true);
}
if (constraint.isStrictlyWithinHierarchy()) {
handler.setStrictSubtype(true);
}
MatchPredicate predicate;
if (!StringUtil.isEmptyOrSpaces(constraint.getRegExp())) {
predicate = new RegExpPredicate(constraint.getRegExp(), options.isCaseSensitiveMatch(), name, constraint.isWholeWordsOnly(), constraint.isPartOfSearchResults());
if (constraint.isInvertRegExp()) {
predicate = new NotPredicate(predicate);
}
addPredicate(handler, predicate);
}
if (constraint.isReference()) {
predicate = new ReferencePredicate(constraint.getNameOfReferenceVar());
if (constraint.isInvertReference()) {
predicate = new NotPredicate(predicate);
}
addPredicate(handler, predicate);
}
addExtensionPredicates(options, constraint, handler);
addScriptConstraint(project, name, constraint, handler);
if (!StringUtil.isEmptyOrSpaces(constraint.getContainsConstraint())) {
predicate = new ContainsPredicate(name, constraint.getContainsConstraint());
if (constraint.isInvertContainsConstraint()) {
predicate = new NotPredicate(predicate);
}
addPredicate(handler, predicate);
}
if (!StringUtil.isEmptyOrSpaces(constraint.getWithinConstraint())) {
assert false;
}
prevOffset = offset;
}
MatchVariableConstraint constraint = options.getVariableConstraint(Configuration.CONTEXT_VAR_NAME);
if (constraint != null) {
SubstitutionHandler handler = result.createSubstitutionHandler(Configuration.CONTEXT_VAR_NAME, Configuration.CONTEXT_VAR_NAME, constraint.isPartOfSearchResults(), constraint.getMinCount(), constraint.getMaxCount(), constraint.isGreedy());
if (!StringUtil.isEmptyOrSpaces(constraint.getWithinConstraint())) {
MatchPredicate predicate = new WithinPredicate(Configuration.CONTEXT_VAR_NAME, constraint.getWithinConstraint(), options.getFileType(), project);
if (constraint.isInvertWithinConstraint()) {
predicate = new NotPredicate(predicate);
}
addPredicate(handler, predicate);
}
addExtensionPredicates(options, constraint, handler);
addScriptConstraint(project, Configuration.CONTEXT_VAR_NAME, constraint, handler);
}
buf.append(text.substring(prevOffset, text.length()));
PsiElement[] matchStatements;
try {
matchStatements = MatcherImplUtil.createTreeFromText(buf.toString(), PatternTreeContext.Block, options.getFileType(), options.getDialect(), options.getPatternContext(), project, false);
if (matchStatements.length == 0)
throw new MalformedPatternException();
} catch (IncorrectOperationException e) {
throw new MalformedPatternException(e.getMessage());
}
NodeFilter filter = LexicalNodesFilter.getInstance();
GlobalCompilingVisitor compilingVisitor = new GlobalCompilingVisitor();
compilingVisitor.compile(matchStatements, context);
ArrayList<PsiElement> elements = new ArrayList<>();
for (PsiElement matchStatement : matchStatements) {
if (!filter.accepts(matchStatement)) {
elements.add(matchStatement);
}
}
new DeleteNodesAction(compilingVisitor.getLexicalNodes()).run();
return elements;
}
use of com.intellij.codeInsight.template.Template in project intellij-plugins by JetBrains.
the class FlexLiveTemplatesTest method doTest.
protected void doTest(final String templateName, final String extension, final Consumer<Integer> segmentHandler) throws Exception {
final Template template = TemplateSettings.getInstance().getTemplate(templateName, "ActionScript");
doTest(template, segmentHandler, getBasePath() + getTestName(false) + "." + extension);
}
use of com.intellij.codeInsight.template.Template in project intellij-plugins by JetBrains.
the class FlexLiveTemplatesTest method doTest.
protected void doTest(final String templateName, final String extension, final String group) throws Exception {
final Template template = TemplateSettings.getInstance().getTemplate(templateName, group);
//noinspection unchecked
doTest(template, Consumer.EMPTY_CONSUMER, getBasePath() + getTestName(false) + "." + extension);
}
use of com.intellij.codeInsight.template.Template in project intellij-community by JetBrains.
the class StringBasedPostfixTemplate method expandForChooseExpression.
@Override
public final void expandForChooseExpression(@NotNull PsiElement expr, @NotNull Editor editor) {
Project project = expr.getProject();
Document document = editor.getDocument();
PsiElement elementForRemoving = getElementToRemove(expr);
document.deleteString(elementForRemoving.getTextRange().getStartOffset(), elementForRemoving.getTextRange().getEndOffset());
TemplateManager manager = TemplateManager.getInstance(project);
String templateString = getTemplateString(expr);
if (templateString == null) {
PostfixTemplatesUtils.showErrorHint(expr.getProject(), editor);
return;
}
Template template = createTemplate(manager, templateString);
if (shouldAddExpressionToContext()) {
template.addVariable("expr", new TextExpression(expr.getText()), false);
}
setVariables(template, expr);
manager.startTemplate(editor, template);
}
use of com.intellij.codeInsight.template.Template in project intellij-community by JetBrains.
the class LookupItemUtil method objectToLookupItem.
/**
* @deprecated
* @see LookupElementBuilder
*/
@NotNull
public static LookupElement objectToLookupItem(Object object) {
if (object instanceof LookupElement)
return (LookupElement) object;
if (object instanceof PsiClass) {
return JavaClassNameCompletionContributor.createClassLookupItem((PsiClass) object, true);
}
if (object instanceof PsiMethod) {
return new JavaMethodCallElement((PsiMethod) object);
}
if (object instanceof PsiVariable) {
return new VariableLookupItem((PsiVariable) object);
}
if (object instanceof PsiExpression) {
return new ExpressionLookupItem((PsiExpression) object);
}
if (object instanceof PsiType) {
return PsiTypeLookupItem.createLookupItem((PsiType) object, null);
}
if (object instanceof PsiPackage) {
return new PackageLookupItem((PsiPackage) object);
}
String s = null;
LookupItem item = new LookupItem(object, "");
if (object instanceof PsiElement) {
s = PsiUtilCore.getName((PsiElement) object);
}
TailType tailType = TailType.NONE;
if (object instanceof PsiMetaData) {
s = ((PsiMetaData) object).getName();
} else if (object instanceof String) {
s = (String) object;
} else if (object instanceof Template) {
s = ((Template) object).getKey();
} else if (object instanceof PresentableLookupValue) {
s = ((PresentableLookupValue) object).getPresentation();
}
if (object instanceof LookupValueWithUIHint && ((LookupValueWithUIHint) object).isBold()) {
item.setBold();
}
if (s == null) {
LOG.error("Null string for object: " + object + " of class " + (object != null ? object.getClass() : null));
}
item.setLookupString(s);
item.setTailType(tailType);
return item;
}
Aggregations