use of com.intellij.codeInsight.template.impl.TemplateState in project intellij-plugins by JetBrains.
the class DartLiveTemplatesTest method expandTemplate.
public void expandTemplate(final Editor editor) {
final Project project = editor.getProject();
assertNotNull(project);
new ListTemplatesAction().actionPerformedImpl(project, editor);
final LookupImpl lookup = (LookupImpl) LookupManager.getActiveLookup(editor);
assertNotNull(lookup);
lookup.finishLookup(Lookup.NORMAL_SELECT_CHAR);
TemplateState template = TemplateManagerImpl.getTemplateState(editor);
if (template != null) {
Disposer.dispose(template);
}
}
use of com.intellij.codeInsight.template.impl.TemplateState in project intellij-plugins by JetBrains.
the class DartIntroduceHandler method performAction.
public void performAction(DartIntroduceOperation operation) {
final PsiFile file = operation.getFile();
if (!CommonRefactoringUtil.checkReadOnlyStatus(file)) {
return;
}
final Editor editor = operation.getEditor();
if (editor.getSettings().isVariableInplaceRenameEnabled()) {
final TemplateState templateState = TemplateManagerImpl.getTemplateState(operation.getEditor());
if (templateState != null && !templateState.isFinished()) {
return;
}
}
PsiElement element1 = null;
PsiElement element2 = null;
final SelectionModel selectionModel = editor.getSelectionModel();
if (selectionModel.hasSelection()) {
element1 = file.findElementAt(selectionModel.getSelectionStart());
element2 = file.findElementAt(selectionModel.getSelectionEnd() - 1);
if (element1 instanceof PsiWhiteSpace) {
int startOffset = element1.getTextRange().getEndOffset();
element1 = file.findElementAt(startOffset);
}
if (element2 instanceof PsiWhiteSpace) {
int endOffset = element2.getTextRange().getStartOffset();
element2 = file.findElementAt(endOffset - 1);
}
} else {
if (smartIntroduce(operation)) {
return;
}
final CaretModel caretModel = editor.getCaretModel();
final Document document = editor.getDocument();
int lineNumber = document.getLineNumber(caretModel.getOffset());
if ((lineNumber >= 0) && (lineNumber < document.getLineCount())) {
element1 = file.findElementAt(document.getLineStartOffset(lineNumber));
element2 = file.findElementAt(document.getLineEndOffset(lineNumber) - 1);
}
}
final Project project = operation.getProject();
if (element1 == null || element2 == null) {
showCannotPerformError(project, editor);
return;
}
element1 = DartRefactoringUtil.getSelectedExpression(project, file, element1, element2);
if (element1 == null) {
showCannotPerformError(project, editor);
return;
}
if (!checkIntroduceContext(file, editor, element1)) {
return;
}
operation.setElement(element1);
performActionOnElement(operation);
}
use of com.intellij.codeInsight.template.impl.TemplateState in project intellij-plugins by JetBrains.
the class AbstractStepDefinitionCreator method closeActiveTemplateBuilders.
protected void closeActiveTemplateBuilders(PsiFile file) {
final Project project = file.getProject();
final VirtualFile vFile = ObjectUtils.assertNotNull(file.getVirtualFile());
final OpenFileDescriptor descriptor = new OpenFileDescriptor(project, vFile);
FileEditorManager.getInstance(project).getAllEditors(vFile);
FileEditorManager.getInstance(project).openTextEditor(descriptor, true);
final Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
assert editor != null;
final TemplateManager templateManager = TemplateManager.getInstance(file.getProject());
final TemplateState templateState = TemplateManagerImpl.getTemplateState(editor);
final Template template = templateManager.getActiveTemplate(editor);
if (templateState != null && template != null) {
templateState.gotoEnd(false);
}
}
use of com.intellij.codeInsight.template.impl.TemplateState in project intellij-community by JetBrains.
the class AbstractInplaceIntroduceTest method doTestEscape.
protected void doTestEscape(Pass<AbstractInplaceIntroducer> pass) {
String name = getTestName(true);
configureByFile(getBasePath() + name + getExtension());
final boolean enabled = getEditor().getSettings().isVariableInplaceRenameEnabled();
try {
TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable());
getEditor().getSettings().setVariableInplaceRenameEnabled(true);
final AbstractInplaceIntroducer introducer = invokeRefactoring();
if (pass != null) {
pass.pass(introducer);
}
TemplateState state = TemplateManagerImpl.getTemplateState(getEditor());
assert state != null;
state.gotoEnd(true);
checkResultByFile(getBasePath() + name + "_after" + getExtension());
} finally {
getEditor().getSettings().setVariableInplaceRenameEnabled(enabled);
}
}
use of com.intellij.codeInsight.template.impl.TemplateState in project intellij-community by JetBrains.
the class ChangeClassParametersIntention method invoke.
@Override
public void invoke(@NotNull final Project project, final Editor editor, @NotNull final PsiElement element) throws IncorrectOperationException {
final PsiTypeElement typeElement = PsiTreeUtil.getTopmostParentOfType(element, PsiTypeElement.class);
final PsiReferenceParameterList parameterList = PsiTreeUtil.getParentOfType(typeElement, PsiReferenceParameterList.class);
if (parameterList != null) {
final PsiClass aClass = PsiTreeUtil.getParentOfType(element, PsiClass.class);
if (aClass instanceof PsiAnonymousClass) {
editor.getCaretModel().moveToOffset(aClass.getTextOffset());
final PsiTypeElement[] typeElements = parameterList.getTypeParameterElements();
final int changeIdx = ArrayUtil.find(typeElements, typeElement);
final PsiClassType.ClassResolveResult result = ((PsiAnonymousClass) aClass).getBaseClassType().resolveGenerics();
final PsiClass baseClass = result.getElement();
LOG.assertTrue(baseClass != null);
final PsiTypeParameter typeParameter = baseClass.getTypeParameters()[changeIdx];
final TemplateBuilderImpl templateBuilder = (TemplateBuilderImpl) TemplateBuilderFactory.getInstance().createTemplateBuilder(aClass);
final String oldTypeText = typeElement.getText();
final String varName = "param";
templateBuilder.replaceElement(typeElement, varName, new TypeExpression(project, new PsiType[] { typeElement.getType() }), true);
final Template template = templateBuilder.buildInlineTemplate();
TemplateManager.getInstance(project).startTemplate(editor, template, false, null, new TemplateEditingAdapter() {
private String myNewType;
@Override
public void beforeTemplateFinished(TemplateState state, Template template) {
final TextResult value = state.getVariableValue(varName);
myNewType = value != null ? value.getText() : "";
final int segmentsCount = state.getSegmentsCount();
final Document document = state.getEditor().getDocument();
ApplicationManager.getApplication().runWriteAction(() -> {
for (int i = 0; i < segmentsCount; i++) {
final TextRange segmentRange = state.getSegmentRange(i);
document.replaceString(segmentRange.getStartOffset(), segmentRange.getEndOffset(), oldTypeText);
}
});
}
@Override
public void templateFinished(Template template, boolean brokenOff) {
if (!brokenOff) {
final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project);
try {
final PsiType targetParam = elementFactory.createTypeFromText(myNewType, aClass);
if (!(targetParam instanceof PsiClassType)) {
HintManager.getInstance().showErrorHint(editor, JavaErrorMessages.message("generics.type.argument.cannot.be.of.primitive.type"));
return;
}
final PsiClassType classType = (PsiClassType) targetParam;
final PsiClass target = classType.resolve();
if (target == null) {
HintManager.getInstance().showErrorHint(editor, JavaErrorMessages.message("cannot.resolve.symbol", classType.getPresentableText()));
return;
}
final TypeMigrationRules myRules = new TypeMigrationRules();
final PsiSubstitutor substitutor = result.getSubstitutor().put(typeParameter, targetParam);
final PsiType targetClassType = elementFactory.createType(baseClass, substitutor);
myRules.setBoundScope(new LocalSearchScope(aClass));
TypeMigrationProcessor.runHighlightingTypeMigration(project, editor, myRules, ((PsiAnonymousClass) aClass).getBaseClassReference().getParameterList(), targetClassType);
} catch (IncorrectOperationException e) {
HintManager.getInstance().showErrorHint(editor, "Incorrect type");
}
}
}
});
}
}
}
Aggregations