private void doInsert(InsertionContext context, @NotNull LookupElement item, @Nullable GoSignature signature) { int paramsCount = signature != null ? signature.getParameters().getParameterDeclarationList().size() : 0; InsertHandler<LookupElement> handler = paramsCount == 0 ? ParenthesesInsertHandler.NO_PARAMETERS : ParenthesesInsertHandler.WITH_PARAMETERS; handler.handleInsert(context, item); if (signature != null) { AutoPopupController.getInstance(context.getProject()).autoPopupParameterInfo(context.getEditor(), null); } } };
private static void autoImport(@NotNull InsertionContext context, @NotNull GoNamedElement element) { PsiFile file = context.getFile(); boolean vendoringEnabled = GoVendoringUtil.isVendoringEnabled(ModuleUtilCore.findModuleForPsiElement(file)); String importPath = element.getContainingFile().getImportPath(vendoringEnabled); if (StringUtil.isEmpty(importPath)) return; PsiDocumentManager.getInstance(context.getProject()).commitDocument(context.getEditor().getDocument()); PsiReference reference = file.findReferenceAt(context.getStartOffset()); if (reference != null) { PsiElement referenceElement = reference.getElement(); GoImportPackageQuickFix fix = new GoImportPackageQuickFix(referenceElement, importPath); fix.invoke(context.getProject(), file, context.getEditor(), referenceElement, referenceElement); } } }
@Override public void handleInsert(@NotNull InsertionContext context, LookupElement item) { Editor editor = context.getEditor(); int tailOffset = context.getTailOffset(); Document document = editor.getDocument(); context.commitDocument(); boolean staysAtChar = document.getTextLength() > tailOffset && document.getCharsSequence().charAt(tailOffset) == myChar; context.setAddCompletionChar(false); if (!staysAtChar) { document.insertString(tailOffset, String.valueOf(myChar)); } editor.getCaretModel().moveToOffset(tailOffset + 1); AutoPopupController.getInstance(context.getProject()).scheduleAutoPopup(editor); } }
@Override public void handleInsert(@NotNull InsertionContext context, LookupElement item) { Editor editor = context.getEditor(); int tailOffset = context.getTailOffset(); Document document = editor.getDocument(); context.commitDocument(); boolean staysAtChar = (document.getTextLength() > tailOffset) && (document.getCharsSequence().charAt(tailOffset) == myCharacter); context.setAddCompletionChar(false); if (!staysAtChar) { document.insertString(tailOffset, String.valueOf(myCharacter)); } editor.getCaretModel().moveToOffset(tailOffset + 1); AutoPopupController.getInstance(context.getProject()).scheduleAutoPopup(editor); } }
@Nullable public static InsertHandler<LookupElement> createTemplateBasedInsertHandler(@NotNull String templateId) { return (context, item) -> { Template template = TemplateSettings.getInstance().getTemplateById(templateId); Editor editor = context.getEditor(); if (template != null) { editor.getDocument().deleteString(context.getStartOffset(), context.getTailOffset()); TemplateManager.getInstance(context.getProject()).startTemplate(editor, template); } else { int currentOffset = editor.getCaretModel().getOffset(); CharSequence documentText = editor.getDocument().getImmutableCharSequence(); if (documentText.length() <= currentOffset || documentText.charAt(currentOffset) != ' ') { EditorModificationUtil.insertStringAtCaret(editor, " "); } else { EditorModificationUtil.moveCaretRelatively(editor, 1); } } }; } }
@NotNull private static InsertHandler<LookupElement> createTemplateBasedInsertHandler(@NotNull String templateId, @Nullable String traileringString) { return (context, item) -> { Template template = TemplateSettings.getInstance().getTemplateById(templateId); Editor editor = context.getEditor(); if (template != null) { editor.getDocument().deleteString(context.getStartOffset(), context.getTailOffset()); TemplateManager.getInstance(context.getProject()).startTemplate(editor, template); } else { int currentOffset = editor.getCaretModel().getOffset(); CharSequence documentText = editor.getDocument().getImmutableCharSequence(); if (documentText.length() <= currentOffset || documentText.charAt(currentOffset) != ' ') { if (traileringString != null) { EditorModificationUtil.insertStringAtCaret(editor, traileringString); } } else { EditorModificationUtil.moveCaretRelatively(editor, 1); } } }; }
@Override public void handleInsert(@NotNull InsertionContext context, LookupElement item) { Editor editor = context.getEditor(); CharSequence documentText = context.getDocument().getImmutableCharSequence(); int offset = skipWhiteSpaces(editor.getCaretModel().getOffset(), documentText); if (documentText.charAt(offset) != '{') { Project project = context.getProject(); Template template = TemplateManager.getInstance(project).createTemplate("braces", "go", myOneLine ? "{$END$}" : " {\n$END$\n}"); template.setToReformat(true); TemplateManager.getInstance(project).startTemplate(editor, template); } else { editor.getCaretModel().moveToOffset(offset); ApplicationManager.getApplication().runWriteAction(() -> { EditorActionHandler enterAction = EditorActionManager.getInstance().getActionHandler(IdeActions.ACTION_EDITOR_START_NEW_LINE); enterAction.execute(editor, editor.getCaretModel().getCurrentCaret(), ((EditorEx)editor).getDataContext()); }); } }
block = (GoBlock)declaration.add(GoElementFactory.createBlock(context.getProject())); block = (GoBlock)block.replace(GoElementFactory.createBlock(context.getProject())); GoSignature newSignature = GoElementFactory.createFunctionSignatureFromText(context.getProject(), testingType.getSignature(testingQualifier)); GoSignature signature = declaration.getSignature(); PsiDocumentManager.getInstance(context.getProject()).doPostponedOperationsAndUnblockDocument(document); GoStatement firstStatement = ContainerUtil.getFirstItem(block.getStatementList()); if (firstStatement != null) { CodeStyleManager.getInstance(context.getProject()).adjustLineIndent(document, offsetToMove);
public static void insertUse(InsertionContext context, String fqnAnnotation) { PsiElement element = PsiUtilCore.getElementAtOffset(context.getFile(), context.getStartOffset()); PhpPsiElement scopeForUseOperator = PhpCodeInsightUtil.findScopeForUseOperator(element); if(null == scopeForUseOperator) { return; } // PhpCodeInsightUtil.canImport: // copied from PhpReferenceInsertHandler; throws an error on PhpContractUtil because of "fully qualified names only" // but that is catch on phpstorm side already; looks fixed now so use fqn if(!fqnAnnotation.startsWith("\\")) { fqnAnnotation = "\\" + fqnAnnotation; } // this looks suitable! :) if(PhpCodeInsightUtil.alreadyImported(scopeForUseOperator, fqnAnnotation) == null) { PsiDocumentManager.getInstance(context.getProject()).commitDocument(context.getDocument()); PhpAliasImporter.insertUseStatement(fqnAnnotation, scopeForUseOperator); PsiDocumentManager.getInstance(context.getProject()).doPostponedOperationsAndUnblockDocument(context.getDocument()); } } }
for (PsiElement psiElement : PhpElementsUtil.getPsiElementsBySignature(context.getProject(), twigExtension.getSignature())) { if(!(psiElement instanceof Function)) { continue;
@Override public void handleInsert(@NotNull final InsertionContext context, @NotNull LookupElement item) { final Editor editor = context.getEditor(); if ("returns".equals(item.getLookupString())) { EditorModificationUtil.insertStringAtCaret(editor, " "); context.setLaterRunnable(() -> new CodeCompletionHandlerBase(CompletionType.BASIC).invokeCompletion(context.getProject(), editor, 1)); } } }
@Override public void handleInsert(@NotNull final InsertionContext context, @NotNull final LookupElement item) { // fixme this is bad check for auto-inserting, i belive if (context.getCompletionChar() != '\u0000') { context.setLaterRunnable(() -> { Editor editor = context.getEditor(); new CodeCompletionHandlerBase(CompletionType.BASIC).invokeCompletion(context.getProject(), editor, 1); }); } } }
private static LookupElement createKeywordLookupItem(String keyword) { LookupElementBuilder builder = LookupElementBuilder.create(keyword.toLowerCase()).withCaseSensitivity(false).bold(); boolean braces = keyword.endsWith("{") || keyword.endsWith("}"); if (!braces) { return keyword.startsWith("%") ? TailTypeDecorator.withTail(builder, TailType.SPACE) : builder; } else { final String closing = keyword.endsWith("{") ? keyword.substring(0, keyword.length()-1) + "}" : null; return PrioritizedLookupElement.withPriority(builder.withInsertHandler((context, item) -> { int caret = context.getTailOffset(); Document document = context.getDocument(); StringBuilder sb = new StringBuilder("\n"); caret += sb.length(); if (closing != null) { int indentSize = CodeStyleSettingsManager.getInstance(context.getProject()). getCurrentSettings().getIndentSize(JFlexFileType.INSTANCE); sb.append(StringUtil.repeat(" ", indentSize)); caret += indentSize; sb.append("\n").append(closing).append("\n"); } document.insertString(context.getTailOffset(), sb); context.getEditor().getCaretModel().moveToOffset(caret); }), 1.d / keyword.length()); } } }
PsiDocumentManager.getInstance(context.getProject()).commitDocument(context.getDocument()); final PsiElement headOfFunction = PsiTreeUtil.findElementOfClassAtRange(context.getFile(), context.getStartOffset(), context.getTailOffset(), PsiElement.class); final TemplateBuilderFactory factory = TemplateBuilderFactoryImpl.getInstance(); final Project project = context.getProject(); PsiDocumentManager.getInstance(project).commitDocument(document);