public static PsiFile insertDummyIdentifier(final Editor editor, PsiFile file) { boolean selection = editor.getSelectionModel().hasSelection(); final int startOffset = selection ? editor.getSelectionModel().getSelectionStart() : editor.getCaretModel().getOffset(); final int endOffset = selection ? editor.getSelectionModel().getSelectionEnd() : startOffset; return insertDummyIdentifierIfNeeded( file, startOffset, endOffset, CompletionUtil.DUMMY_IDENTIFIER_TRIMMED); }
public static List<TemplateImpl> listApplicableTemplateWithInsertingDummyIdentifier( Editor editor, PsiFile file, boolean selectionOnly) { int startOffset = editor.getSelectionModel().getSelectionStart(); file = insertDummyIdentifier(editor, file); return listApplicableTemplates(file, startOffset, selectionOnly); }
public void startTemplateWithPrefix( final Editor editor, final TemplateImpl template, final int templateStart, @Nullable final PairProcessor<String, String> processor, @Nullable final String argument) { final int caretOffset = editor.getCaretModel().getOffset(); final TemplateState templateState = initTemplateState(editor); CommandProcessor commandProcessor = CommandProcessor.getInstance(); commandProcessor.executeCommand( myProject, () -> { editor.getDocument().deleteString(templateStart, caretOffset); editor.getCaretModel().moveToOffset(templateStart); editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE); editor.getSelectionModel().removeSelection(); Map<String, String> predefinedVarValues = null; if (argument != null) { predefinedVarValues = new HashMap<String, String>(); predefinedVarValues.put(TemplateImpl.ARG, argument); } templateState.start(template, processor, predefinedVarValues); }, CodeInsightBundle.message("insert.code.template.command"), null); }
@Nullable public Runnable prepareTemplate( final Editor editor, char shortcutChar, @Nullable final PairProcessor<String, String> processor) { if (editor.getSelectionModel().hasSelection()) { return null; } PsiFile file = PsiUtilBase.getPsiFileInEditor(editor, myProject); if (file == null) return null; Map<TemplateImpl, String> template2argument = findMatchingTemplates(file, editor, shortcutChar, TemplateSettings.getInstance()); List<CustomLiveTemplate> customCandidates = ContainerUtil.findAll( CustomLiveTemplate.EP_NAME.getExtensions(), customLiveTemplate -> shortcutChar == customLiveTemplate.getShortcut() && (editor.getCaretModel().getCaretCount() <= 1 || supportsMultiCaretMode(customLiveTemplate))); if (!customCandidates.isEmpty()) { int caretOffset = editor.getCaretModel().getOffset(); PsiFile fileCopy = insertDummyIdentifierIfNeeded(file, caretOffset, caretOffset, ""); Document document = editor.getDocument(); for (final CustomLiveTemplate customLiveTemplate : customCandidates) { if (isApplicable(customLiveTemplate, editor, fileCopy)) { final String key = customLiveTemplate.computeTemplateKey(new CustomTemplateCallback(editor, fileCopy)); if (key != null) { int offsetBeforeKey = caretOffset - key.length(); CharSequence text = document.getImmutableCharSequence(); if (template2argument == null || !containsTemplateStartingBefore( template2argument, offsetBeforeKey, caretOffset, text)) { return () -> customLiveTemplate.expand(key, new CustomTemplateCallback(editor, file)); } } } } } return startNonCustomTemplates(template2argument, editor, processor); }
private void startTemplate( final Editor editor, final String selectionString, final Template template, boolean inSeparateCommand, TemplateEditingListener listener, final PairProcessor<String, String> processor, final Map<String, String> predefinedVarValues) { final TemplateState templateState = initTemplateState(editor); //noinspection unchecked templateState.getProperties().put(ExpressionContext.SELECTION, selectionString); if (listener != null) { templateState.addTemplateStateListener(listener); } Runnable r = () -> { if (selectionString != null) { ApplicationManager.getApplication() .runWriteAction(() -> EditorModificationUtil.deleteSelectedText(editor)); } else { editor.getSelectionModel().removeSelection(); } templateState.start((TemplateImpl) template, processor, predefinedVarValues); }; if (inSeparateCommand) { CommandProcessor.getInstance() .executeCommand( myProject, r, CodeInsightBundle.message("insert.code.template.command"), null); } else { r.run(); } if (shouldSkipInTests()) { if (!templateState.isFinished()) templateState.gotoEnd(false); } }