Search in sources :

Example 1 with TemplateManagerImpl

use of com.intellij.codeInsight.template.impl.TemplateManagerImpl in project intellij-community by JetBrains.

the class CodeInsightTestUtil method doInlineRename.

@TestOnly
public static void doInlineRename(VariableInplaceRenameHandler handler, final String newName, @NotNull Editor editor, PsiElement elementAtCaret) {
    Project project = editor.getProject();
    TemplateManagerImpl templateManager = (TemplateManagerImpl) TemplateManager.getInstance(project);
    try {
        templateManager.setTemplateTesting(true);
        handler.doRename(elementAtCaret, editor, DataManager.getInstance().getDataContext(editor.getComponent()));
        if (editor instanceof EditorWindow) {
            editor = ((EditorWindow) editor).getDelegate();
        }
        TemplateState state = TemplateManagerImpl.getTemplateState(editor);
        assert state != null;
        final TextRange range = state.getCurrentVariableRange();
        assert range != null;
        final Editor finalEditor = editor;
        new WriteCommandAction.Simple(project) {

            @Override
            protected void run() throws Throwable {
                finalEditor.getDocument().replaceString(range.getStartOffset(), range.getEndOffset(), newName);
            }
        }.execute().throwException();
        state = TemplateManagerImpl.getTemplateState(editor);
        assert state != null;
        state.gotoEnd(false);
    } finally {
        templateManager.setTemplateTesting(false);
    }
}
Also used : Project(com.intellij.openapi.project.Project) TemplateManagerImpl(com.intellij.codeInsight.template.impl.TemplateManagerImpl) TextRange(com.intellij.openapi.util.TextRange) Editor(com.intellij.openapi.editor.Editor) TemplateState(com.intellij.codeInsight.template.impl.TemplateState) EditorWindow(com.intellij.injected.editor.EditorWindow) TestOnly(org.jetbrains.annotations.TestOnly)

Example 2 with TemplateManagerImpl

use of com.intellij.codeInsight.template.impl.TemplateManagerImpl in project intellij-community by JetBrains.

the class ExpandLiveTemplateCustomAction method createExpandTemplateHandler.

public static EditorWriteActionHandler createExpandTemplateHandler(final char shortcutChar) {
    return new EditorWriteActionHandler(true) {

        @Override
        public void executeWriteAction(Editor editor, @Nullable Caret caret, DataContext dataContext) {
            Project project = editor.getProject();
            assert project != null;
            TemplateManager.getInstance(project).startTemplate(editor, shortcutChar);
        }

        @Override
        protected boolean isEnabledForCaret(@NotNull Editor editor, @NotNull Caret caret, DataContext dataContext) {
            Project project = editor.getProject();
            return project != null && ((TemplateManagerImpl) TemplateManager.getInstance(project)).prepareTemplate(editor, shortcutChar, null) != null;
        }
    };
}
Also used : EditorWriteActionHandler(com.intellij.openapi.editor.actionSystem.EditorWriteActionHandler) Project(com.intellij.openapi.project.Project) DataContext(com.intellij.openapi.actionSystem.DataContext) TemplateManagerImpl(com.intellij.codeInsight.template.impl.TemplateManagerImpl) Editor(com.intellij.openapi.editor.Editor) NotNull(org.jetbrains.annotations.NotNull) Nullable(org.jetbrains.annotations.Nullable) Caret(com.intellij.openapi.editor.Caret)

Aggregations

TemplateManagerImpl (com.intellij.codeInsight.template.impl.TemplateManagerImpl)2 Editor (com.intellij.openapi.editor.Editor)2 Project (com.intellij.openapi.project.Project)2 TemplateState (com.intellij.codeInsight.template.impl.TemplateState)1 EditorWindow (com.intellij.injected.editor.EditorWindow)1 DataContext (com.intellij.openapi.actionSystem.DataContext)1 Caret (com.intellij.openapi.editor.Caret)1 EditorWriteActionHandler (com.intellij.openapi.editor.actionSystem.EditorWriteActionHandler)1 TextRange (com.intellij.openapi.util.TextRange)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1 TestOnly (org.jetbrains.annotations.TestOnly)1