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-community by JetBrains.
the class HighlightUtils method showRenameTemplate.
public static void showRenameTemplate(PsiElement context, PsiNameIdentifierOwner element, PsiReference... references) {
context = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(context);
final Project project = context.getProject();
final FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
final Editor editor = fileEditorManager.getSelectedTextEditor();
if (editor == null) {
return;
}
final TemplateBuilderImpl builder = new TemplateBuilderImpl(context);
final Expression macroCallNode = new MacroCallNode(new SuggestVariableNameMacro());
final PsiElement identifier = element.getNameIdentifier();
builder.replaceElement(identifier, "PATTERN", macroCallNode, true);
for (PsiReference reference : references) {
builder.replaceElement(reference, "PATTERN", "PATTERN", false);
}
final Template template = builder.buildInlineTemplate();
final TextRange textRange = context.getTextRange();
final int startOffset = textRange.getStartOffset();
editor.getCaretModel().moveToOffset(startOffset);
final TemplateManager templateManager = TemplateManager.getInstance(project);
templateManager.startTemplate(editor, template);
}
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-plugins by JetBrains.
the class OverrideImplementMethodFix method buildFunctionsText.
@Override
protected Template buildFunctionsText(TemplateManager templateManager, DartComponent element) {
final Template template = templateManager.createTemplate(getClass().getName(), DART_TEMPLATE_GROUP);
template.setToReformat(true);
if (CodeStyleSettingsManager.getSettings(element.getProject()).INSERT_OVERRIDE_ANNOTATION) {
template.addTextSegment("@override\n");
}
final boolean isField = element instanceof DartVarAccessDeclaration || element instanceof DartVarDeclarationListPart;
if (isField && element.isFinal()) {
template.addTextSegment("final");
template.addTextSegment(" ");
}
final DartReturnType returnType = PsiTreeUtil.getChildOfType(element, DartReturnType.class);
final DartType dartType = PsiTreeUtil.getChildOfType(element, DartType.class);
if (returnType != null) {
template.addTextSegment(DartPresentableUtil.buildTypeText(element, returnType, specializations));
template.addTextSegment(" ");
} else if (dartType != null) {
template.addTextSegment(DartPresentableUtil.buildTypeText(element, dartType, specializations));
template.addTextSegment(" ");
}
if (isField) {
if (returnType == null && dartType == null) {
template.addTextSegment("var");
template.addTextSegment(" ");
}
//noinspection ConstantConditions
template.addTextSegment(element.getName());
if (element.isFinal()) {
template.addTextSegment(" ");
template.addTextSegment("=");
template.addTextSegment(" ");
template.addTextSegment("null");
}
// trailing space is removed when auto-reformatting, but it helps to enter line break if needed
template.addTextSegment("; ");
return template;
}
if (element.isOperator()) {
template.addTextSegment("operator ");
}
if (element.isGetter() || element.isSetter()) {
template.addTextSegment(element.isGetter() ? "get " : "set ");
}
//noinspection ConstantConditions
template.addTextSegment(element.getName());
if (!element.isGetter()) {
template.addTextSegment("(");
template.addTextSegment(DartPresentableUtil.getPresentableParameterList(element, specializations, false, true, true));
template.addTextSegment(")");
}
template.addTextSegment("{\n");
template.addEndVariable();
// trailing space is removed when auto-reformatting, but it helps to enter line break if needed
template.addTextSegment("\n} ");
return template;
}
Aggregations