use of com.intellij.codeInsight.template.impl.TemplateState in project intellij-community by JetBrains.
the class ShowIntentionsPass method doCollectInformation.
@Override
public void doCollectInformation(@NotNull ProgressIndicator progress) {
if (!ApplicationManager.getApplication().isUnitTestMode() && !myEditor.getContentComponent().hasFocus())
return;
TemplateState state = TemplateManagerImpl.getTemplateState(myEditor);
if (state != null && !state.isFinished())
return;
DaemonCodeAnalyzerImpl codeAnalyzer = (DaemonCodeAnalyzerImpl) DaemonCodeAnalyzer.getInstance(myProject);
getIntentionActionsToShow();
updateActions(codeAnalyzer);
}
use of com.intellij.codeInsight.template.impl.TemplateState in project intellij-community by JetBrains.
the class SelectAllHandler method execute.
@Override
public void execute(Editor editor, DataContext dataContext) {
final TemplateState templateState = TemplateManagerImpl.getTemplateState(editor);
if (templateState != null && !templateState.isFinished()) {
final TextRange range = templateState.getCurrentVariableRange();
final int caretOffset = editor.getCaretModel().getOffset();
if (range != null && range.getStartOffset() <= caretOffset && caretOffset <= range.getEndOffset()) {
editor.getSelectionModel().setSelection(range.getStartOffset(), range.getEndOffset());
return;
}
}
myOriginalHandler.execute(editor, dataContext);
}
use of com.intellij.codeInsight.template.impl.TemplateState in project intellij-community by JetBrains.
the class TemplateLineStartEndHandler method isEnabledForCaret.
@Override
protected boolean isEnabledForCaret(@NotNull Editor editor, @NotNull Caret caret, DataContext dataContext) {
TemplateState templateState = TemplateManagerImpl.getTemplateState(editor);
if (templateState != null && !templateState.isFinished()) {
TextRange range = templateState.getCurrentVariableRange();
int caretOffset = editor.getCaretModel().getOffset();
if (range != null && range.containsOffset(caretOffset))
return true;
}
return myOriginalHandler.isEnabled(editor, caret, dataContext);
}
use of com.intellij.codeInsight.template.impl.TemplateState 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.TemplateState in project intellij-community by JetBrains.
the class JavaMethodCallElement method startArgumentLiveTemplate.
public static boolean startArgumentLiveTemplate(InsertionContext context, PsiMethod method) {
if (method.getParameterList().getParametersCount() == 0 || context.getCompletionChar() == Lookup.COMPLETE_STATEMENT_SELECT_CHAR || !Registry.is("java.completion.argument.live.template")) {
return false;
}
Editor editor = context.getEditor();
context.commitDocument();
PsiCall call = PsiTreeUtil.findElementOfClassAtOffset(context.getFile(), context.getStartOffset(), PsiCall.class, false);
PsiExpressionList argList = call == null ? null : call.getArgumentList();
if (argList == null || argList.getExpressions().length > 0) {
return false;
}
TextRange argRange = argList.getTextRange();
int caretOffset = editor.getCaretModel().getOffset();
if (!argRange.contains(caretOffset)) {
return false;
}
Template template = createArgTemplate(method, caretOffset, argList, argRange);
context.getDocument().deleteString(caretOffset, argRange.getEndOffset());
TemplateManager.getInstance(method.getProject()).startTemplate(editor, template);
TemplateState templateState = TemplateManagerImpl.getTemplateState(editor);
if (templateState == null)
return false;
setupNonFilledArgumentRemoving(editor, templateState);
editor.putUserData(ARGUMENT_TEMPLATE_ACTIVE, method);
Disposer.register(templateState, () -> {
if (editor.getUserData(ARGUMENT_TEMPLATE_ACTIVE) == method) {
editor.putUserData(ARGUMENT_TEMPLATE_ACTIVE, null);
}
});
return true;
}
Aggregations