use of com.intellij.codeInsight.template.impl.TemplateState in project intellij-community by JetBrains.
the class InplaceRefactoring method navigateToStarted.
private static void navigateToStarted(final Document oldDocument, final Project project, @Messages.YesNoResult final int exitCode) {
final PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(oldDocument);
if (file != null) {
final VirtualFile virtualFile = file.getVirtualFile();
if (virtualFile != null) {
final FileEditor[] editors = FileEditorManager.getInstance(project).getEditors(virtualFile);
for (FileEditor editor : editors) {
if (editor instanceof TextEditor) {
final Editor textEditor = ((TextEditor) editor).getEditor();
final TemplateState templateState = TemplateManagerImpl.getTemplateState(textEditor);
if (templateState != null) {
if (exitCode == Messages.YES) {
final TextRange range = templateState.getVariableRange(PRIMARY_VARIABLE_NAME);
if (range != null) {
new OpenFileDescriptor(project, virtualFile, range.getStartOffset()).navigate(true);
return;
}
} else if (exitCode > 0) {
templateState.gotoEnd();
return;
}
}
}
}
}
}
}
use of com.intellij.codeInsight.template.impl.TemplateState in project intellij-community by JetBrains.
the class InplaceRefactoring method revertState.
protected void revertState() {
if (myOldName == null)
return;
CommandProcessor.getInstance().executeCommand(myProject, () -> {
final Editor topLevelEditor = InjectedLanguageUtil.getTopLevelEditor(myEditor);
ApplicationManager.getApplication().runWriteAction(() -> {
final TemplateState state = TemplateManagerImpl.getTemplateState(topLevelEditor);
assert state != null;
final int segmentsCount = state.getSegmentsCount();
final Document document = topLevelEditor.getDocument();
for (int i = 0; i < segmentsCount; i++) {
final TextRange segmentRange = state.getSegmentRange(i);
document.replaceString(segmentRange.getStartOffset(), segmentRange.getEndOffset(), myOldName);
}
});
if (!myProject.isDisposed() && myProject.isOpen()) {
PsiDocumentManager.getInstance(myProject).commitDocument(topLevelEditor.getDocument());
}
}, getCommandName(), null);
}
use of com.intellij.codeInsight.template.impl.TemplateState in project intellij-community by JetBrains.
the class MyLookupExpression method calculateResult.
@Override
public Result calculateResult(ExpressionContext context) {
TemplateState templateState = TemplateManagerImpl.getTemplateState(context.getEditor());
final TextResult insertedValue = templateState != null ? templateState.getVariableValue(InplaceRefactoring.PRIMARY_VARIABLE_NAME) : null;
if (insertedValue != null) {
if (!insertedValue.getText().isEmpty())
return insertedValue;
}
return new TextResult(myName);
}
use of com.intellij.codeInsight.template.impl.TemplateState in project intellij-community by JetBrains.
the class VariableInplaceRenamer method restoreSelection.
@Override
protected void restoreSelection() {
if (mySelectedRange != null) {
Editor editor = InjectedLanguageUtil.getTopLevelEditor(myEditor);
TextRange selectedRange;
if (myEditor instanceof EditorWindow) {
PsiFile injected = ((EditorWindow) myEditor).getInjectedFile();
selectedRange = InjectedLanguageManager.getInstance(editor.getProject()).injectedToHost(injected, mySelectedRange);
} else {
selectedRange = mySelectedRange;
}
TemplateState state = TemplateManagerImpl.getTemplateState(editor);
if (state != null) {
for (int i = 0; i < state.getSegmentsCount(); i++) {
final TextRange segmentRange = state.getSegmentRange(i);
TextRange intersection = segmentRange.intersection(selectedRange);
if (intersection != null) {
editor.getSelectionModel().setSelection(intersection.getStartOffset(), intersection.getEndOffset());
return;
}
}
}
myEditor.getSelectionModel().setSelection(mySelectedRange.getStartOffset(), mySelectedRange.getEndOffset());
} else if (!shouldSelectAll()) {
myEditor.getSelectionModel().removeSelection();
}
}
use of com.intellij.codeInsight.template.impl.TemplateState in project intellij-community by JetBrains.
the class EscapeHandler method execute.
@Override
public void execute(Editor editor, DataContext dataContext) {
TemplateState templateState = TemplateManagerImpl.getTemplateState(editor);
if (templateState != null && !templateState.isFinished()) {
SelectionModel selectionModel = editor.getSelectionModel();
LookupImpl lookup = (LookupImpl) LookupManager.getActiveLookup(editor);
// the idea behind lookup checking is that if there is a preselected value in lookup
// then user might want just to close lookup but not finish a template.
// E.g. user wants to move to the next template segment by Tab without completion invocation.
// If there is no selected value in completion that user definitely wants to finish template
boolean lookupIsEmpty = lookup == null || lookup.getCurrentItem() == null;
if (!selectionModel.hasSelection() && lookupIsEmpty) {
CommandProcessor.getInstance().setCurrentCommandName(CodeInsightBundle.message("finish.template.command"));
templateState.gotoEnd(true);
return;
}
}
if (myOriginalHandler.isEnabled(editor, dataContext)) {
myOriginalHandler.execute(editor, dataContext);
}
}
Aggregations