@Override public boolean doEnter(PsiElement psiElement, PsiFile file, @NotNull Editor editor, boolean modified) { GoBlock block = findBlock(psiElement); if (block != null) { editor.getCaretModel().moveToOffset(block.getLbrace().getTextRange().getEndOffset()); } plainEnter(editor); return true; } }
@Override public void invoke(@NotNull Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) { if (!(file instanceof GoFile) || editor == null || !(startElement instanceof GoBlock)) return; PsiElement brace = ((GoBlock)startElement).getRbrace(); if (brace == null) return; Template template = TemplateSettings.getInstance().getTemplateById("go_lang_add_return"); if (template == null) return; int start = brace.getTextRange().getStartOffset(); editor.getCaretModel().moveToOffset(start); editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE); template.setToReformat(true); TemplateManager.getInstance(project).startTemplate(editor, template, true, Collections.emptyMap(), null); } }
@Override public void execute(@NotNull Editor editor, @NotNull DataContext context) { final TextRange visualRange = VimPlugin.getMark().getVisualSelectionMarks(editor); if (visualRange == null) { return; } // NB: Operator ignores SelectionType anyway if (!new Operator().apply(editor, context, SelectionType.CHARACTER_WISE)) { return; } WriteAction.run(() -> { // Leave visual mode executeNormal(parseKeys("<Esc>"), editor); editor.getCaretModel().moveToOffset(visualRange.getStartOffset()); }); }
@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 invoke(@NotNull Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) { if (editor == null) { LOG.error("Cannot run quick fix without editor: " + getClass().getSimpleName(), AttachmentFactory.createAttachment(file.getVirtualFile())); return; } if (!(startElement instanceof GoType)) return; GoType type = (GoType)startElement; PsiElement anchor = PsiTreeUtil.findPrevParent(file, type); String name = "TypeName"; GoTypeDeclaration decl = (GoTypeDeclaration)file.addBefore(GoElementFactory.createTypeDeclaration(project, name, type), anchor); if (decl == null) return; decl = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(decl); if (decl == null) return; GoTypeSpec spec = ContainerUtil.getFirstItem(decl.getTypeSpecList()); if (spec == null) return; TemplateBuilderImpl builder = new TemplateBuilderImpl(file); builder.replaceElement(type, OTHER_NAME, INPUT_NAME, false); builder.replaceElement(spec.getIdentifier(), INPUT_NAME, new ConstantNode(name), true); editor.getCaretModel().moveToOffset(file.getTextRange().getStartOffset()); Template template = builder.buildInlineTemplate(); editor.getCaretModel().moveToOffset(file.getTextRange().getStartOffset()); TemplateManager.getInstance(project).startTemplate(editor, template); }
@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); } }
@Override public void handleInsert(@NotNull InsertionContext context, @NotNull LookupElement item) { int tailOffset = context.getTailOffset(); Document document = context.getDocument(); int documentTextLength = document.getTextLength(); boolean insertParentheses; if (documentTextLength > tailOffset) { String currentTail = document.getText( new TextRange(tailOffset, tailOffset + 1) ); char firstChar = currentTail.charAt(0); insertParentheses = firstChar != ' ' && firstChar != '(' && firstChar != '['; } else { insertParentheses = true; } if (insertParentheses) { context.getDocument().insertString(tailOffset, "()"); // + 1 to put between the `(` and `)` context.getEditor().getCaretModel().moveToOffset(tailOffset + 1); } } }
@Override public void invoke(@NotNull Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) { if (editor == null) { LOG.error("Cannot run quick fix without editor: " + getClass().getSimpleName(), AttachmentFactory.createAttachment(file.getVirtualFile())); return; } if (!(startElement instanceof GoCallExpr)) return; GoCallExpr call = (GoCallExpr)startElement; List<GoExpression> args = call.getArgumentList().getExpressionList(); GoType resultType = ContainerUtil.getFirstItem(GoTypeUtil.getExpectedTypes(call)); PsiElement anchor = PsiTreeUtil.findPrevParent(file, call); Template template = TemplateManager.getInstance(project).createTemplate("", ""); template.addTextSegment("\nfunc " + myName); setupFunctionParameters(template, args, file); setupFunctionResult(template, resultType); template.addTextSegment(" {\n\t"); template.addEndVariable(); template.addTextSegment("\n}"); int offset = anchor.getTextRange().getEndOffset(); editor.getCaretModel().moveToOffset(offset); startTemplate(editor, template, project); }
tail = element.getTextRange().getStartOffset(); if (completionChar != '[' && completionChar != '\t') { editor.getCaretModel().moveToOffset(context.getTailOffset()); editor.getCaretModel().moveToOffset(tail + 1); editor.getCaretModel().moveToOffset(context.getTailOffset()); editor.getCaretModel().moveToOffset(tail + 1); editor.getCaretModel().moveToOffset(context.getTailOffset()); editor.getCaretModel().moveToOffset(context.getTailOffset());
@Override public void invoke(@NotNull Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) { if (editor == null) { LOG.error("Cannot run quick fix without editor: " + getClass().getSimpleName(), AttachmentFactory.createAttachment(file.getVirtualFile())); return; } PsiElement reference = PsiTreeUtil.getNonStrictParentOfType(startElement, GoReferenceExpressionBase.class); PsiElement anchor = reference != null ? findAnchor(reference) : null; if (anchor == null) { LOG.error("Cannot find anchor for " + myWhat + " (GoUnresolvedFixBase), offset: " + editor.getCaretModel().getOffset(), AttachmentFactory.createAttachment(file.getVirtualFile())); return; } Template template = TemplateSettings.getInstance().getTemplateById(myTemplateId); if (template == null) { LOG.error("Cannot find anchor for " + myWhat + " (GoUnresolvedFixBase), offset: " + editor.getCaretModel().getOffset(), AttachmentFactory.createAttachment(file.getVirtualFile())); return; } int start = anchor.getTextRange().getStartOffset(); editor.getCaretModel().moveToOffset(start); template.setToReformat(true); TemplateManager.getInstance(project).startTemplate(editor, template, true, ContainerUtil.stringMap("NAME", myName), null); }
@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()); }); } }
@Override protected void postProcess(PsiFile createdElement, String templateName, Map<String, String> customProperties) { if (createdElement instanceof GoFile) { GoPackageClause packageClause = ((GoFile)createdElement).getPackage(); if (packageClause == null) { return; } Project project = createdElement.getProject(); Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor(); if (editor == null) { return; } VirtualFile virtualFile = createdElement.getContainingFile().getVirtualFile(); if (virtualFile == null) { return; } if (FileDocumentManager.getInstance().getDocument(virtualFile) == editor.getDocument()) { editor.getCaretModel().moveToOffset(packageClause.getTextRange().getEndOffset()); } } }
public void handleInsert(InsertionContext context, LookupElement item) { Editor editor = context.getEditor(); char completionChar = context.getCompletionChar(); if (completionChar == ' ' || StringUtil.containsChar(myIgnoreOnChars, completionChar)) { return; } Project project = editor.getProject(); if (project != null) { int completionCharOffset = getCompletionCharOffset(editor); if (completionCharOffset == -1) { EditorModificationUtil.insertStringAtCaret(editor, ";", false, 1); PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument()); } else { editor.getCaretModel().moveToOffset(editor.getCaretModel().getOffset() + completionCharOffset + 1); } if (myTriggerAutoPopup) { AutoPopupController.getInstance(project).autoPopupMemberLookup(editor, null); } } }
public void handleInsert(InsertionContext context, LookupElement item) { Editor editor = context.getEditor(); char completionChar = context.getCompletionChar(); if (completionChar == ' ' || StringUtil.containsChar(myIgnoreOnChars, completionChar)) { return; } Project project = editor.getProject(); if (project != null) { int completionCharOffset = getCompletionCharOffset(editor); if (completionCharOffset == -1) { EditorModificationUtil.insertStringAtCaret(editor, " {}", false, 2); PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument()); } else { editor.getCaretModel().moveToOffset(editor.getCaretModel().getOffset() + completionCharOffset + 1); } if (myTriggerAutoPopup) { AutoPopupController.getInstance(project).autoPopupMemberLookup(editor, null); } } }
public void handleInsert(InsertionContext context, LookupElement item) { Editor editor = context.getEditor(); char completionChar = context.getCompletionChar(); if (completionChar == ' ' || StringUtil.containsChar(myIgnoreOnChars, completionChar)) { return; } Project project = editor.getProject(); if (project != null) { int completionCharOffset = getCompletionCharOffset(editor); if (completionCharOffset == -1) { EditorModificationUtil.insertStringAtCaret(editor, "()", false, 1); PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument()); } else { editor.getCaretModel().moveToOffset(editor.getCaretModel().getOffset() + completionCharOffset + 1); } if (myTriggerAutoPopup) { AutoPopupController.getInstance(project).autoPopupMemberLookup(editor, null); } } }
public void handleInsert(InsertionContext context, LookupElement item) { Editor editor = context.getEditor(); char completionChar = context.getCompletionChar(); if (completionChar == ' ' || StringUtil.containsChar(myIgnoreOnChars, completionChar)) { return; } Project project = editor.getProject(); if (project != null) { int completionCharOffset = getCompletionCharOffset(editor); if (completionCharOffset == -1) { EditorModificationUtil.insertStringAtCaret(editor, "();", false, 1); PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument()); } else { editor.getCaretModel().moveToOffset(editor.getCaretModel().getOffset() + completionCharOffset + 1); } if (myTriggerAutoPopup) { AutoPopupController.getInstance(project).autoPopupMemberLookup(editor, null); } } }
public void handleInsert(InsertionContext context, LookupElement item) { Editor editor = context.getEditor(); char completionChar = context.getCompletionChar(); if (completionChar == ' ' || StringUtil.containsChar(myIgnoreOnChars, completionChar)) { return; } Project project = editor.getProject(); if (project != null) { int completionCharOffset = getCompletionCharOffset(editor); if (completionCharOffset == -1) { EditorModificationUtil.insertStringAtCaret(editor, ":", false, 1); if (myWithSpace) { EditorModificationUtil.insertStringAtCaret(editor, " ", false, 1); } PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument()); } else { editor.getCaretModel().moveToOffset(editor.getCaretModel().getOffset() + completionCharOffset + 1); } if (myTriggerAutoPopup) { AutoPopupController.getInstance(project).autoPopupMemberLookup(editor, null); } } }
break; case VISUAL_CHARACTER: caretModel.moveToOffset(CaretData.getVisualEnd(caretModel.getPrimaryCaret())); break; case VISUAL_LINE:
GoStatement firstStatement = ContainerUtil.getFirstItem(block.getStatementList()); if (firstStatement != null) { context.getEditor().getCaretModel().moveToOffset(firstStatement.getTextRange().getStartOffset()); context.getEditor().getCaretModel().moveToOffset(offsetToMove); CodeStyleManager.getInstance(context.getProject()).adjustLineIndent(document, offsetToMove);
operation.getEditor().getCaretModel().moveToOffset(varDefinition.getIdentifier().getTextRange().getStartOffset()); }); operation.setOccurrences(newOccurrences);