use of com.intellij.codeInsight.template.impl.TemplateImpl in project intellij-community by JetBrains.
the class IterateOverIterableIntention method isAvailable.
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
final TemplateImpl template = getTemplate();
if (template != null) {
int offset = editor.getCaretModel().getOffset();
int startOffset = offset;
if (editor.getSelectionModel().hasSelection()) {
final int selStart = editor.getSelectionModel().getSelectionStart();
final int selEnd = editor.getSelectionModel().getSelectionEnd();
startOffset = (offset == selStart) ? selEnd : selStart;
}
PsiElement element = file.findElementAt(startOffset);
while (element instanceof PsiWhiteSpace) {
element = element.getPrevSibling();
}
PsiStatement psiStatement = PsiTreeUtil.getParentOfType(element, PsiStatement.class, false);
if (psiStatement != null) {
startOffset = psiStatement.getTextRange().getStartOffset();
}
if (!template.isDeactivated() && (TemplateManagerImpl.isApplicable(file, offset, template) || (TemplateManagerImpl.isApplicable(file, startOffset, template)))) {
return getIterableExpression(editor, file) != null;
}
}
return false;
}
use of com.intellij.codeInsight.template.impl.TemplateImpl in project intellij-community by JetBrains.
the class IterateOverIterableIntention method invoke.
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
final TemplateImpl template = getTemplate();
SelectionModel selectionModel = editor.getSelectionModel();
if (!selectionModel.hasSelection()) {
final PsiExpression iterableExpression = getIterableExpression(editor, file);
LOG.assertTrue(iterableExpression != null);
TextRange textRange = iterableExpression.getTextRange();
selectionModel.setSelection(textRange.getStartOffset(), textRange.getEndOffset());
}
new InvokeTemplateAction(template, editor, project, new HashSet<>()).perform();
}
use of com.intellij.codeInsight.template.impl.TemplateImpl in project intellij-community by JetBrains.
the class CustomTemplateCallback method getMatchingTemplates.
@NotNull
private static List<TemplateImpl> getMatchingTemplates(@NotNull String templateKey) {
TemplateSettings settings = TemplateSettings.getInstance();
List<TemplateImpl> candidates = new ArrayList<>();
for (TemplateImpl template : settings.getTemplates(templateKey)) {
if (!template.isDeactivated()) {
candidates.add(template);
}
}
return candidates;
}
use of com.intellij.codeInsight.template.impl.TemplateImpl in project intellij-community by JetBrains.
the class SurroundWithHandler method doBuildSurroundActions.
@Nullable
private static List<AnAction> doBuildSurroundActions(Project project, Editor editor, PsiFile file, Map<Surrounder, PsiElement[]> surrounders) {
final List<AnAction> applicable = new ArrayList<>();
boolean hasEnabledSurrounders = false;
Set<Character> usedMnemonicsSet = new HashSet<>();
int index = 0;
for (Map.Entry<Surrounder, PsiElement[]> entry : surrounders.entrySet()) {
Surrounder surrounder = entry.getKey();
PsiElement[] elements = entry.getValue();
if (surrounder.isApplicable(elements)) {
char mnemonic;
if (index < 9) {
mnemonic = (char) ('0' + index + 1);
} else if (index == 9) {
mnemonic = '0';
} else {
mnemonic = (char) ('A' + index - 10);
}
index++;
usedMnemonicsSet.add(Character.toUpperCase(mnemonic));
applicable.add(new InvokeSurrounderAction(surrounder, project, editor, elements, mnemonic));
hasEnabledSurrounders = true;
}
}
List<CustomLiveTemplate> customTemplates = TemplateManagerImpl.listApplicableCustomTemplates(editor, file, true);
List<TemplateImpl> templates = TemplateManagerImpl.listApplicableTemplateWithInsertingDummyIdentifier(editor, file, true);
if (!templates.isEmpty() || !customTemplates.isEmpty()) {
applicable.add(new Separator("Live templates"));
}
for (TemplateImpl template : templates) {
applicable.add(new InvokeTemplateAction(template, editor, project, usedMnemonicsSet));
hasEnabledSurrounders = true;
}
for (CustomLiveTemplate customTemplate : customTemplates) {
applicable.add(new WrapWithCustomTemplateAction(customTemplate, editor, file, usedMnemonicsSet));
hasEnabledSurrounders = true;
}
if (!templates.isEmpty() || !customTemplates.isEmpty()) {
applicable.add(Separator.getInstance());
applicable.add(new ConfigureTemplatesAction());
}
return hasEnabledSurrounders ? applicable : null;
}
use of com.intellij.codeInsight.template.impl.TemplateImpl in project intellij-community by JetBrains.
the class CreateFromTemplateActionBase method startLiveTemplate.
public static void startLiveTemplate(@NotNull PsiFile file, @NotNull Map<String, String> defaultValues) {
Editor editor = EditorHelper.openInEditor(file);
if (editor == null)
return;
TemplateImpl template = new TemplateImpl("", file.getText(), "");
template.setInline(true);
int count = template.getSegmentsCount();
if (count == 0)
return;
Set<String> variables = new HashSet<>();
for (int i = 0; i < count; i++) {
variables.add(template.getSegmentName(i));
}
variables.removeAll(TemplateImpl.INTERNAL_VARS_SET);
for (String variable : variables) {
String defaultValue = defaultValues.getOrDefault(variable, variable);
template.addVariable(variable, null, '"' + defaultValue + '"', true);
}
Project project = file.getProject();
WriteCommandAction.runWriteCommandAction(project, () -> editor.getDocument().setText(template.getTemplateText()));
// ensures caret at the start of the template
editor.getCaretModel().moveToOffset(0);
TemplateManager.getInstance(project).startTemplate(editor, template);
}
Aggregations