use of com.intellij.codeInsight.template.impl.TemplateImpl in project intellij-community by JetBrains.
the class EmmetParser method parseTemplate.
@Nullable
protected ZenCodingNode parseTemplate() {
ZenCodingToken token = getToken();
if (!(token instanceof IdentifierToken)) {
return null;
}
String templateKey = ((IdentifierToken) token).getText();
advance();
TemplateImpl template = myCallback.findApplicableTemplate(templateKey);
if (template == null && !ZenCodingUtil.isXML11ValidQName(templateKey)) {
return null;
}
final TemplateToken templateToken = new TemplateToken(templateKey);
if (!setTemplate(templateToken, template)) {
return null;
}
return new TemplateNode(templateToken);
}
use of com.intellij.codeInsight.template.impl.TemplateImpl in project intellij-community by JetBrains.
the class LiveTemplateBuilder method buildTemplate.
@NotNull
public TemplateImpl buildTemplate() {
List<Variable> variables = getListWithLimit(myVariables);
if (!findVarOccurence(TemplateImpl.END)) {
if (myLastEndVarName == null) {
for (Variable variable : variables) {
if (isEndVariable(variable.getName())) {
myLastEndVarName = variable.getName();
break;
}
}
}
if (myLastEndVarName != null) {
int endOffset = -1;
if (myAddEndVariableAtTheEndOfTemplate) {
endOffset = myText.length();
} else {
Iterator<VarOccurence> it = myVariableOccurrences.iterator();
while (it.hasNext()) {
VarOccurence occurence = it.next();
if (occurence.myName.equals(myLastEndVarName)) {
endOffset = occurence.myOffset;
break;
}
}
if (endOffset >= 0) {
for (Iterator<Variable> it1 = variables.iterator(); it1.hasNext(); ) {
Variable variable = it1.next();
if (myLastEndVarName.equals(variable.getName()) && variable.isAlwaysStopAt()) {
it.remove();
it1.remove();
}
}
}
}
if (endOffset >= 0) {
myVariableOccurrences.add(new VarOccurence(TemplateImpl.END, endOffset));
}
}
}
TemplateImpl template = new TemplateImpl("", "");
for (Variable variable : variables) {
template.addVariable(variable.getName(), variable.getExpressionString(), variable.getDefaultValueString(), variable.isAlwaysStopAt());
}
List<VarOccurence> variableOccurrences = getListWithLimit(myVariableOccurrences);
Collections.sort(variableOccurrences, (o1, o2) -> {
if (o1.myOffset < o2.myOffset) {
return -1;
}
if (o1.myOffset > o2.myOffset) {
return 1;
}
return 0;
});
int last = 0;
for (VarOccurence occurence : variableOccurrences) {
template.addTextSegment(myText.substring(last, occurence.myOffset));
template.addVariableSegment(occurence.myName);
last = occurence.myOffset;
}
template.addTextSegment(myText.substring(last));
template.setToReformat(myIsToReformat);
return template;
}
use of com.intellij.codeInsight.template.impl.TemplateImpl in project intellij-community by JetBrains.
the class JavaMethodCallElement method setupNonFilledArgumentRemoving.
private static void setupNonFilledArgumentRemoving(final Editor editor, final TemplateState templateState) {
AtomicInteger maxEditedVariable = new AtomicInteger(-1);
editor.getDocument().addDocumentListener(new DocumentAdapter() {
@Override
public void documentChanged(DocumentEvent e) {
maxEditedVariable.set(Math.max(maxEditedVariable.get(), templateState.getCurrentVariableNumber()));
}
}, templateState);
templateState.addTemplateStateListener(new TemplateEditingAdapter() {
@Override
public void currentVariableChanged(TemplateState templateState, Template template, int oldIndex, int newIndex) {
maxEditedVariable.set(Math.max(maxEditedVariable.get(), oldIndex));
}
@Override
public void beforeTemplateFinished(TemplateState state, Template template, boolean brokenOff) {
if (brokenOff) {
removeUntouchedArguments((TemplateImpl) template);
}
}
private void removeUntouchedArguments(TemplateImpl template) {
int firstUnchangedVar = maxEditedVariable.get() + 1;
if (firstUnchangedVar >= template.getVariableCount())
return;
TextRange startRange = templateState.getVariableRange(template.getVariableNameAt(firstUnchangedVar));
TextRange endRange = templateState.getVariableRange(template.getVariableNameAt(template.getVariableCount() - 1));
if (startRange == null || endRange == null)
return;
WriteCommandAction.runWriteCommandAction(editor.getProject(), () -> editor.getDocument().deleteString(startRange.getStartOffset(), endRange.getEndOffset()));
}
});
}
use of com.intellij.codeInsight.template.impl.TemplateImpl in project intellij-community by JetBrains.
the class BasicExpressionCompletionContributor method fillCompletionVariants.
public static void fillCompletionVariants(JavaSmartCompletionParameters parameters, final Consumer<LookupElement> result, PrefixMatcher matcher) {
final PsiElement element = parameters.getPosition();
if (JavaKeywordCompletion.isAfterTypeDot(element)) {
addKeyword(result, element, PsiKeyword.CLASS);
addKeyword(result, element, PsiKeyword.THIS);
}
if (!JavaKeywordCompletion.AFTER_DOT.accepts(element)) {
if (parameters.getParameters().getInvocationCount() <= 1) {
new CollectionsUtilityMethodsProvider(parameters.getPosition(), parameters.getExpectedType(), parameters.getDefaultType(), result).addCompletions(StringUtil.isNotEmpty(matcher.getPrefix()));
}
ClassLiteralGetter.addCompletions(parameters, result, matcher);
final PsiElement position = parameters.getPosition();
final PsiType expectedType = parameters.getExpectedType();
for (final TemplateImpl template : TemplateSettings.getInstance().getTemplates()) {
if (!template.isDeactivated() && template.getTemplateContext().isEnabled(new SmartCompletionContextType())) {
result.consume(new SmartCompletionTemplateItem(template, position));
}
}
addKeyword(result, position, PsiKeyword.TRUE);
addKeyword(result, position, PsiKeyword.FALSE);
final PsiElement parent = position.getParent();
if (parent != null && !(parent.getParent() instanceof PsiSwitchLabelStatement)) {
for (final PsiExpression expression : ThisGetter.getThisExpressionVariants(position)) {
result.consume(new ExpressionLookupItem(expression));
}
}
processDataflowExpressionTypes(position, expectedType, matcher, result);
}
}
use of com.intellij.codeInsight.template.impl.TemplateImpl in project intellij-community by JetBrains.
the class ListTemplateActionTest method addTemplate.
private void addTemplate(String key, String text, String description, String group) {
TemplateManager manager = TemplateManager.getInstance(getProject());
TemplateImpl template = (TemplateImpl) manager.createTemplate(key, group, text);
template.setDescription(description);
TemplateContextType contextType = ContainerUtil.findInstance(TemplateContextType.EP_NAME.getExtensions(), JavaCodeContextType.class);
template.getTemplateContext().setEnabled(contextType, true);
CodeInsightTestUtil.addTemplate(template, myFixture.getTestRootDisposable());
}
Aggregations