com.intellij.codeInsight.completion.InsertionContext Java Examples
The following examples show how to use
com.intellij.codeInsight.completion.InsertionContext.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JSGraphQLEndpointAutoImportInsertHandler.java From js-graphql-intellij-plugin with MIT License | 6 votes |
public void handleInsert(InsertionContext context, LookupElement item) { final Editor editor = context.getEditor(); final Project project = editor.getProject(); if (project != null) { final JSGraphQLEndpointImportDeclaration[] imports = PsiTreeUtil.getChildrenOfType(context.getFile(), JSGraphQLEndpointImportDeclaration.class); int insertionOffset = 0; if(imports != null && imports.length > 0) { JSGraphQLEndpointImportDeclaration lastImport = imports[imports.length - 1]; insertionOffset = lastImport.getTextRange().getEndOffset(); } final String name = JSGraphQLEndpointImportUtil.getImportName(project, fileToImport); String importDeclaration = "import \"" + name + "\"\n"; if(insertionOffset > 0) { importDeclaration = "\n" + importDeclaration; } editor.getDocument().insertString(insertionOffset, importDeclaration); } }
Example #2
Source File: YamlInsertValueHandler.java From intellij-swagger with MIT License | 6 votes |
private void handleStartingQuote( final InsertionContext insertionContext, final LookupElement lookupElement, final char quoteType) { final int caretOffset = insertionContext.getEditor().getCaretModel().getOffset(); final int startOfLookupStringOffset = caretOffset - lookupElement.getLookupString().length(); final boolean hasStartingQuote = hasStartingQuote(insertionContext, quoteType, startOfLookupStringOffset); if (!hasStartingQuote) { insertionContext .getDocument() .insertString(startOfLookupStringOffset, String.valueOf(quoteType)); } }
Example #3
Source File: YamlKeyInsertHandler.java From intellij-spring-assistant with MIT License | 6 votes |
private void deleteLookupTextAndRetrieveOldValue(InsertionContext context, @NotNull PsiElement elementAtCaret) { if (elementAtCaret.getNode().getElementType() != YAMLTokenTypes.SCALAR_KEY) { deleteLookupPlain(context); } else { YAMLKeyValue keyValue = PsiTreeUtil.getParentOfType(elementAtCaret, YAMLKeyValue.class); assert keyValue != null; context.commitDocument(); // TODO: Whats going on here? if (keyValue.getValue() != null) { YAMLKeyValue dummyKV = YAMLElementGenerator.getInstance(context.getProject()).createYamlKeyValue("foo", "b"); dummyKV.setValue(keyValue.getValue()); } context.setTailOffset(keyValue.getTextRange().getEndOffset()); runWriteCommandAction(context.getProject(), () -> keyValue.getParentMapping().deleteKeyValue(keyValue)); } }
Example #4
Source File: VariableInsertHandler.java From intellij-xquery with Apache License 2.0 | 6 votes |
private PsiElement findElementBeforeNameFragment(InsertionContext context) { final PsiFile file = context.getFile(); PsiElement element = file.findElementAt(context.getStartOffset() - 1); element = getElementInFrontOfWhitespace(file, element); if (element instanceof LeafPsiElement && ((LeafPsiElement) element).getElementType() == XQueryTypes.COLON) { PsiElement elementBeforeColon = file.findElementAt(context.getStartOffset() - 2); if (elementBeforeColon != null) { element = elementBeforeColon; PsiElement beforeElementBeforeColon = file.findElementAt(elementBeforeColon.getTextRange().getStartOffset() - 1); if (beforeElementBeforeColon != null) { element = getElementInFrontOfWhitespace(file, beforeElementBeforeColon); } } } return element; }
Example #5
Source File: YamlKeyInsertHandler.java From intellij-spring-assistant with MIT License | 6 votes |
@Override public void handleInsert(final InsertionContext context, final LookupElement lookupElement) { if (!nextCharAfterSpacesAndQuotesIsColon(getStringAfterAutoCompletedValue(context))) { String existingIndentation = getExistingIndentation(context, lookupElement); Suggestion suggestion = (Suggestion) lookupElement.getObject(); String indentPerLevel = getCodeStyleIntent(context); Module module = findModule(context); String suggestionWithCaret = getSuggestionReplacementWithCaret(module, suggestion, existingIndentation, indentPerLevel); String suggestionWithoutCaret = suggestionWithCaret.replace(CARET, ""); PsiElement currentElement = context.getFile().findElementAt(context.getStartOffset()); assert currentElement != null : "no element at " + context.getStartOffset(); this.deleteLookupTextAndRetrieveOldValue(context, currentElement); insertStringAtCaret(context.getEditor(), suggestionWithoutCaret, false, true, getCaretIndex(suggestionWithCaret)); } }
Example #6
Source File: CSharpParenthesesInsertHandler.java From consulo-csharp with Apache License 2.0 | 6 votes |
@Nullable @RequiredUIAccess protected PsiElement findNextToken(final InsertionContext context) { final PsiFile file = context.getFile(); PsiElement element = file.findElementAt(context.getTailOffset()); if(element instanceof PsiWhiteSpace) { boolean allowParametersOnNextLine = false; if(!allowParametersOnNextLine && element.getText().contains("\n")) { return null; } element = file.findElementAt(element.getTextRange().getEndOffset()); } return element; }
Example #7
Source File: CommonMacroCompletionContributor.java From intellij with Apache License 2.0 | 6 votes |
/** * Rough insertion of load statement somewhere near top of file, preferentially near other load * statements. * * <p>Doesn't try to combine with existing load statements with the same package (we've already * checked whether the symbol is already available). * * <p>TODO(brendandouglas): use buildozer instead. */ private static void insertLoadStatement( InsertionContext context, BuildFile file, String packageLocation, String symbol) { EventLoggingService.getInstance() .logEvent( CommonMacroCompletionContributor.class, "completed", ImmutableMap.of(symbol, packageLocation)); String text = String.format("load(\"%s\", \"%s\")\n", packageLocation, symbol); Document doc = context.getEditor().getDocument(); PsiElement anchor = findAnchorElement(file); int lineNumber = anchor == null ? 0 : doc.getLineNumber(anchor.getTextRange().getStartOffset()); int offset = doc.getLineStartOffset(lineNumber); doc.insertString(offset, text); }
Example #8
Source File: PhpClassReferenceInsertHandler.java From idea-php-symfony2-plugin with MIT License | 6 votes |
public void handleInsert(@NotNull InsertionContext context, @NotNull LookupElement lookupElement) { Object object = lookupElement.getObject(); if (!(object instanceof PhpClass)) { return; } StringBuilder textToInsertBuilder = new StringBuilder(); PhpClass aClass = (PhpClass)object; String fqn = aClass.getNamespaceName(); if(fqn.startsWith("\\")) { fqn = fqn.substring(1); } textToInsertBuilder.append(fqn); context.getDocument().insertString(context.getStartOffset(), textToInsertBuilder); }
Example #9
Source File: YamlValueInsertHandler.java From intellij-spring-assistant with MIT License | 6 votes |
@Override public void handleInsert(final InsertionContext insertionContext, final LookupElement lookupElement) { if (shouldUseQuotes(lookupElement)) { final boolean hasDoubleQuotes = hasStartingOrEndingQuoteOfType(insertionContext, lookupElement, DOUBLE_QUOTE); if (hasDoubleQuotes) { handleEndingQuote(insertionContext, DOUBLE_QUOTE); handleStartingQuote(insertionContext, lookupElement, DOUBLE_QUOTE); } else { handleEndingQuote(insertionContext, SINGLE_QUOTE); handleStartingQuote(insertionContext, lookupElement, SINGLE_QUOTE); } } }
Example #10
Source File: ReplacingConsumerTest.java From litho with Apache License 2.0 | 6 votes |
@Test public void insertHandler() { testHelper.getPsiClass( classes -> { PsiClass OneCls = classes.get(0); List<String> inserts = new ArrayList<>(1); TestCompletionResultSet mutate = new TestCompletionResultSet(); List<String> namesToReplace = new ArrayList<>(); namesToReplace.add("One"); ReplacingConsumer replacingConsumer = new ReplacingConsumer( namesToReplace, mutate, (context, item) -> inserts.add(item.getLookupString())); replacingConsumer.consume(createCompletionResultFor(OneCls)); mutate.elements.get(0).handleInsert(Mockito.mock(InsertionContext.class)); assertThat(inserts).hasSize(1); assertThat(inserts.get(0)).isEqualTo("One"); return true; }, "One.java"); }
Example #11
Source File: MacroCustomFunctionInsertHandler.java From intellij-latte with MIT License | 6 votes |
public void handleInsert(@NotNull InsertionContext context, @NotNull LookupElement lookupElement) { Editor editor = context.getEditor(); if (context.getCompletionChar() == '(') { context.setAddCompletionChar(false); } PsiElement element = context.getFile().findElementAt(context.getStartOffset()); if (element != null && element.getNode().getElementType() == LatteTypes.T_PHP_IDENTIFIER) { boolean notInUse = PhpUseImpl.getUseList(element) == null; if (notInUse) { if (!LatteUtil.isStringAtCaret(editor, "(")) { this.insertParenthesesCodeStyleAware(editor); } else if (LatteUtil.isStringAtCaret(editor, "()")) { editor.getCaretModel().moveCaretRelatively(2, 0, false, false, true); } else { editor.getCaretModel().moveCaretRelatively(1, 0, false, false, true); showParameterInfo(editor); } } } }
Example #12
Source File: PhpClassInsertHandler.java From intellij-latte with MIT License | 6 votes |
public void handleInsert(@NotNull InsertionContext context, @NotNull LookupElement lookupElement) { // for removing first `\` (because class completion is triggered if prev element is `\` and PHP completion adding `\` before) super.handleInsert(context, lookupElement); PsiElement prev = context.getFile().findElementAt(context.getStartOffset() - 1); PsiElement element = context.getFile().findElementAt(context.getStartOffset()); String prevText = prev != null ? prev.getText() : null; String text = element != null ? element.getText() : null; if (prevText == null || text == null || (prevText.startsWith("\\") && !text.startsWith("\\"))) { return; } LattePhpClassUsage classUsage = element.getParent() instanceof LattePhpClassUsage ? (LattePhpClassUsage) element.getParent() : null; String[] className = (classUsage != null ? classUsage.getClassName() : "").split("\\\\"); if ((prevText.length() > 0 || className.length > 1) && element.getNode().getElementType() == LatteTypes.T_PHP_NAMESPACE_RESOLUTION) { Editor editor = context.getEditor(); CaretModel caretModel = editor.getCaretModel(); int offset = caretModel.getOffset(); caretModel.moveToOffset(element.getTextOffset()); editor.getSelectionModel().setSelection(element.getTextOffset(), element.getTextOffset() + 1); EditorModificationUtil.deleteSelectedText(editor); caretModel.moveToOffset(offset - 1); PsiDocumentManager.getInstance(context.getProject()).commitDocument(editor.getDocument()); } }
Example #13
Source File: AttrMacroInsertHandler.java From intellij-latte with MIT License | 6 votes |
public void handleInsert(@NotNull InsertionContext context, @NotNull LookupElement lookupElement) { PsiElement element = context.getFile().findElementAt(context.getStartOffset()); if (element != null && element.getLanguage() == LatteLanguage.INSTANCE && element.getNode().getElementType() == LatteTypes.T_HTML_TAG_NATTR_NAME) { Editor editor = context.getEditor(); CaretModel caretModel = editor.getCaretModel(); int offset = caretModel.getOffset(); if (LatteUtil.isStringAtCaret(editor, "=")) { caretModel.moveToOffset(offset + 2); return; } String attrName = LatteUtil.normalizeNAttrNameModifier(element.getText()); LatteTagSettings macro = LatteConfiguration.getInstance(element.getProject()).getTag(attrName); if (macro != null && !macro.hasParameters()) { return; } editor.getDocument().insertString(offset, "=\"\""); caretModel.moveToOffset(offset + 2); PsiDocumentManager.getInstance(context.getProject()).commitDocument(editor.getDocument()); } }
Example #14
Source File: HaxeLookupElement.java From intellij-haxe with Apache License 2.0 | 6 votes |
@Override public void handleInsert(InsertionContext context) { HaxeBaseMemberModel memberModel = HaxeBaseMemberModel.fromPsi(myComponentName); boolean hasParams = false; boolean isMethod = false; if (memberModel != null) { if (memberModel instanceof HaxeMethodModel) { isMethod = true; HaxeMethodModel methodModel = (HaxeMethodModel)memberModel; hasParams = !methodModel.getParametersWithContext(this.context).isEmpty(); } } if (isMethod) { final LookupElement[] allItems = context.getElements(); final boolean overloadsMatter = allItems.length == 1 && getUserData(FORCE_SHOW_SIGNATURE_ATTR) == null; JavaCompletionUtil.insertParentheses(context, this, overloadsMatter, hasParams); } }
Example #15
Source File: PostfixInsertHandler.java From bamboo-soy with Apache License 2.0 | 5 votes |
public void handleInsert(InsertionContext context, LookupElement item) { Editor editor = context.getEditor(); Project project = editor.getProject(); if (project != null) { EditorModificationUtil.insertStringAtCaret( editor, closingTagBeforeCaret + closingTagAfterCaret); PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument()); EditorModificationUtil.moveCaretRelatively(editor, -closingTagAfterCaret.length()); } }
Example #16
Source File: GenericUtil.java From intellij-spring-assistant with MIT License | 5 votes |
@NotNull public static String getCodeStyleIntent(InsertionContext insertionContext) { final CodeStyleSettings currentSettings = CodeStyleSettingsManager.getSettings(insertionContext.getProject()); final CommonCodeStyleSettings.IndentOptions indentOptions = currentSettings.getIndentOptions(insertionContext.getFile().getFileType()); return indentOptions.USE_TAB_CHARACTER ? "\t" : StringUtil.repeatSymbol(' ', indentOptions.INDENT_SIZE); }
Example #17
Source File: MethodChainLookupElement.java From litho with Apache License 2.0 | 5 votes |
@Override public void handleInsert(InsertionContext context) { context.setAddCompletionChar(false); handleInsertInternal( context.getEditor(), context.getDocument(), context.getStartOffset(), context.getTailOffset(), context.getProject()); final Map<String, String> data = new HashMap<>(); data.put(EventLogger.KEY_TYPE, "required_builder"); data.put(EventLogger.KEY_TARGET, EventLogger.VALUE_COMPLETION_TARGET_CALL); LithoLoggerProvider.getEventLogger().log(EventLogger.EVENT_COMPLETION, data); }
Example #18
Source File: JsonInsertValueHandler.java From intellij-swagger with MIT License | 5 votes |
private void handleStartingQuote( final InsertionContext insertionContext, final LookupElement lookupElement) { final int caretOffset = insertionContext.getEditor().getCaretModel().getOffset(); final int startOfLookupStringOffset = caretOffset - lookupElement.getLookupString().length(); final boolean hasStartingQuote = insertionContext.getDocument().getText().charAt(startOfLookupStringOffset - 1) == '\"'; if (!hasStartingQuote) { insertionContext.getDocument().insertString(startOfLookupStringOffset, "\""); } }
Example #19
Source File: InsertHandlerUtils.java From intellij-xquery with Apache License 2.0 | 5 votes |
private static PsiElement findElementBeforeColon(InsertionContext context) { final PsiFile file = context.getFile(); PsiElement element = file.findElementAt(context.getStartOffset() - 1); if (element instanceof LeafPsiElement && ((LeafPsiElement) element).getElementType() == XQueryTypes.COLON) { return file.findElementAt(context.getStartOffset() - 2); } return null; }
Example #20
Source File: AnnotationMethodInsertHandler.java From idea-php-symfony2-plugin with MIT License | 5 votes |
public void handleInsert(@NotNull InsertionContext context, @NotNull LookupElement lookupElement) { if(lookupElement.getObject() instanceof AnnotationValue) { String addText = "=\"\""; if(((AnnotationValue) lookupElement.getObject()).getType() == AnnotationValue.Type.Array) { addText = "={}"; } PhpInsertHandlerUtil.insertStringAtCaret(context.getEditor(), addText); } else { PhpInsertHandlerUtil.insertStringAtCaret(context.getEditor(), "=\"\""); } context.getEditor().getCaretModel().moveCaretRelatively(-1, 0, false, false, true); }
Example #21
Source File: CSharpStatementCompletionContributor.java From consulo-csharp with Apache License 2.0 | 5 votes |
@Override @RequiredReadAction public void handleInsert(InsertionContext insertionContext, LookupElement item) { Editor editor = insertionContext.getEditor(); int offset = editor.getCaretModel().getOffset(); boolean isVoidReturnType = DotNetTypeRefUtil.isVmQNameEqual(myPseudoMethod.getReturnTypeRef(), myPseudoMethod, DotNetTypes.System.Void); if(isVoidReturnType) { if(insertionContext.getCompletionChar() == '\n') { TailType.insertChar(editor, offset, ';'); insertionContext.getEditor().getCaretModel().moveToOffset(offset + 1); } } else { if(insertionContext.getCompletionChar() == '\n') { TailType.insertChar(editor, offset, ' '); insertionContext.getEditor().getCaretModel().moveToOffset(offset + 1); } AutoPopupController.getInstance(editor.getProject()).autoPopupMemberLookup(editor, null); } }
Example #22
Source File: MyLookupExpression.java From consulo with Apache License 2.0 | 5 votes |
private static LookupElement[] initLookupItems(LinkedHashSet<String> names, PsiNamedElement elementToRename, PsiElement nameSuggestionContext, final boolean shouldSelectAll) { if (names == null) { names = new LinkedHashSet<String>(); for (NameSuggestionProvider provider : Extensions.getExtensions(NameSuggestionProvider.EP_NAME)) { final SuggestedNameInfo suggestedNameInfo = provider.getSuggestedNames(elementToRename, nameSuggestionContext, names); if (suggestedNameInfo != null && provider instanceof PreferrableNameSuggestionProvider && !((PreferrableNameSuggestionProvider)provider).shouldCheckOthers()) { break; } } } final LookupElement[] lookupElements = new LookupElement[names.size()]; final Iterator<String> iterator = names.iterator(); for (int i = 0; i < lookupElements.length; i++) { final String suggestion = iterator.next(); lookupElements[i] = LookupElementBuilder.create(suggestion).withInsertHandler(new InsertHandler<LookupElement>() { @Override public void handleInsert(InsertionContext context, LookupElement item) { if (shouldSelectAll) return; final Editor topLevelEditor = InjectedLanguageUtil.getTopLevelEditor(context.getEditor()); final TemplateState templateState = TemplateManagerImpl.getTemplateState(topLevelEditor); if (templateState != null) { final TextRange range = templateState.getCurrentVariableRange(); if (range != null) { topLevelEditor.getDocument().replaceString(range.getStartOffset(), range.getEndOffset(), suggestion); } } } }); } return lookupElements; }
Example #23
Source File: YamlInsertFieldHandler.java From intellij-swagger with MIT License | 5 votes |
@Override public void handleInsert(final InsertionContext context, final LookupElement item) { if (!StringUtils.nextCharAfterSpacesAndQuotesIsColon( getStringAfterAutoCompletedValue(context))) { final String suffixWithCaret = field.getYamlPlaceholderSuffix(getIndentation(context, item)); final String suffixWithoutCaret = suffixWithCaret.replace(CARET, ""); EditorModificationUtil.insertStringAtCaret( context.getEditor(), suffixWithoutCaret, false, true, getCaretIndex(suffixWithCaret)); } }
Example #24
Source File: RouteLookupElement.java From idea-php-symfony2-plugin with MIT License | 5 votes |
@Override public void handleInsert(InsertionContext context) { if(insertHandler != null) { insertHandler.handleInsert(context, this); } super.handleInsert(context); }
Example #25
Source File: LookupItem.java From consulo with Apache License 2.0 | 5 votes |
@Override public void handleInsert(final InsertionContext context) { final InsertHandler<? extends LookupElement> handler = getInsertHandler(); if (handler != null) { //noinspection unchecked ((InsertHandler)handler).handleInsert(context, this); } if (getTailType() != TailType.UNKNOWN && myInsertHandler == null) { context.setAddCompletionChar(false); final TailType type = handleCompletionChar(context.getEditor(), this, context.getCompletionChar()); type.processTail(context.getEditor(), context.getTailOffset()); } }
Example #26
Source File: TailType.java From consulo with Apache License 2.0 | 5 votes |
@Override public boolean isApplicable(@Nonnull InsertionContext context) { CharSequence text = context.getDocument().getCharsSequence(); int tail = context.getTailOffset(); if (text.length() > tail + 1 && text.charAt(tail) == ' ' && Character.isLetter(text.charAt(tail + 1))) { return false; } return super.isApplicable(context); }
Example #27
Source File: ProjectViewKeywordCompletionContributor.java From intellij with Apache License 2.0 | 5 votes |
@Nullable private static String findNextTokenText(final InsertionContext context) { final PsiFile file = context.getFile(); PsiElement element = file.findElementAt(context.getTailOffset()); while (element != null && element.getTextLength() == 0) { ASTNode next = element.getNode().getTreeNext(); element = next != null ? next.getPsi() : null; } return element != null ? element.getText() : null; }
Example #28
Source File: RequirejsInsertHandler.java From WebStormRequireJsPlugin with MIT License | 5 votes |
@Override public void handleInsert(InsertionContext insertionContext, LookupElement lookupElement) { insertionContext.getDocument().replaceString( lookupElement.getPsiElement().getTextOffset() + 1, insertionContext.getTailOffset(), lookupElement.getLookupString() ); }
Example #29
Source File: BracesInsertHandler.java From consulo-unity3d with Apache License 2.0 | 5 votes |
@Nullable protected PsiElement findNextToken(final InsertionContext context) { final PsiFile file = context.getFile(); PsiElement element = file.findElementAt(context.getTailOffset()); if(element instanceof PsiWhiteSpace) { if(!myAllowParametersOnNextLine && element.getText().contains("\n")) { return null; } element = file.findElementAt(element.getTextRange().getEndOffset()); } return element; }
Example #30
Source File: ExpressionOrStatementInsertHandler.java From consulo-csharp with Apache License 2.0 | 5 votes |
@Override @RequiredWriteAction public void handleInsert(final InsertionContext context, final T item) { final Editor editor = context.getEditor(); final Document document = editor.getDocument(); context.commitDocument(); handleInsertImpl(context, item, editor, document); if(myOpenChar == '{') { document.insertString(editor.getCaretModel().getOffset(), "\n"); } context.commitDocument(); PsiElement elementAt = context.getFile().findElementAt(context.getStartOffset()); PsiElement parent = elementAt == null ? null : elementAt.getParent(); if(parent != null) { CodeStyleManager.getInstance(parent.getProject()).reformat(parent); if(myOpenChar == '{') { EditorWriteActionHandler actionHandler = (EditorWriteActionHandler) EditorActionManager.getInstance().getActionHandler(IdeActions.ACTION_EDITOR_ENTER); actionHandler.executeWriteAction(editor, DataManager.getInstance().getDataContext(editor.getContentComponent())); } } }