Java Code Examples for com.intellij.codeInsight.completion.InsertionContext#getCompletionChar()

The following examples show how to use com.intellij.codeInsight.completion.InsertionContext#getCompletionChar() . 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: AddColonSpaceInsertHandler.java    From js-graphql-intellij-plugin with MIT License 6 votes vote down vote up
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) {
    if (!isCharAtColon(editor)) {
      EditorModificationUtil.insertStringAtCaret(editor, ": ");
      PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
    } else {
      editor.getCaretModel().moveToOffset(editor.getCaretModel().getOffset() + 2);
    }
    if (myTriggerAutoPopup) {
      AutoPopupController.getInstance(project).autoPopupMemberLookup(editor, null);
    }
  }
}
 
Example 2
Source File: MacroCustomFunctionInsertHandler.java    From intellij-latte with MIT License 6 votes vote down vote up
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 3
Source File: JsxNameCompletionProvider.java    From reasonml-idea-plugin with MIT License 5 votes vote down vote up
private static void insertTagNameHandler(@NotNull Project project, @NotNull InsertionContext context, @NotNull String tagName) {
    char completionChar = context.getCompletionChar();
    if (completionChar == ' ') {
        context.setAddCompletionChar(false);
    }

    Editor editor = context.getEditor();
    EditorModificationUtil.insertStringAtCaret(editor, " ></" + tagName + ">");
    editor.getCaretModel().moveToOffset(editor.getCaretModel().getOffset() - 4 - tagName.length());

    AutoPopupController.getInstance(project).autoPopupMemberLookup(editor, null);
}
 
Example 4
Source File: FluidViewHelperReferenceInsertHandler.java    From idea-php-typo3-plugin with MIT License 5 votes vote down vote up
@Override
public void handleInsert(InsertionContext context, LookupElement item) {
    Editor editor = context.getEditor();
    if (context.getCompletionChar() == '(') {
        context.setAddCompletionChar(false);
    }

    ViewHelper viewHelper = null;
    PsiElement completionParent = item.getPsiElement().getParent();
    if (completionParent instanceof FluidViewHelperReference) {
        viewHelper = findViewHelperByReference((FluidViewHelperReference) completionParent, item);
    } else if(completionParent instanceof FluidBoundNamespace) {
        // find viewhelper
    } else if (completionParent instanceof FluidFieldChain) {
        viewHelper = findViewHelperByFieldPosition((FluidFieldChain) completionParent, item);
    }

    if (viewHelper == null) {
        editor.getDocument().insertString(context.getTailOffset(), "()");

        return;
    }

    if (viewHelper.arguments.size() == 0) {
        editor.getDocument().insertString(context.getTailOffset(), "()");

        return;
    }

    Template t = LiveTemplateFactory.createInlineArgumentListTemplate(viewHelper);
    List<ViewHelperArgument> requiredArguments = viewHelper.getRequiredArguments();
    requiredArguments.forEach(a -> {
        int pos = requiredArguments.indexOf(a);
        t.addVariable("VAR" + pos, new TextExpression(""), true);
    });

    TemplateManager.getInstance(context.getProject()).startTemplate(context.getEditor(), t);
}
 
Example 5
Source File: CSharpTailInsertHandlerWithChar.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Override
public void handleInsert(InsertionContext context, LookupElement item)
{
	if(context.getCompletionChar() == myEnterChar)
	{
		context.setAddCompletionChar(false);
		myTailType.processTail(context.getEditor(), context.getTailOffset());
	}
}
 
Example 6
Source File: SpaceInsertHandler.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Override
public void handleInsert(InsertionContext context, LookupElement item)
{
	if(context.getCompletionChar() != ' ')
	{
		int tailOffset = context.getTailOffset();
		TailType.insertChar(context.getEditor(), tailOffset, ' ');
		context.getEditor().getCaretModel().moveToOffset(tailOffset + 1);
	}
}
 
Example 7
Source File: CSharpStatementCompletionContributor.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@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 8
Source File: PhpConstGotoCompletionProvider.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
public void handleInsert(@NotNull InsertionContext context, @NotNull LookupElement lookupElement) {
    super.handleInsert(context, lookupElement);

    if (context.getCompletionChar() == ':') {
        context.setAddCompletionChar(false);
    }

    if (!PhpInsertHandlerUtil.isStringAtCaret(context.getEditor(), SCOPE_OPERATOR)) {
        PhpInsertHandlerUtil.insertStringAtCaret(context.getEditor(), SCOPE_OPERATOR);
    }

    PhpCompletionUtil.showCompletion(context);
}
 
Example 9
Source File: TemplateExpressionLookupElement.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static boolean handleCompletionChar(InsertionContext context) {
  if (context.getCompletionChar() == '.') {
    EditorModificationUtil.insertStringAtCaret(context.getEditor(), ".");
    AutoPopupController.getInstance(context.getProject()).autoPopupMemberLookup(context.getEditor(), null);
    return false;
  }
  return true;
}
 
Example 10
Source File: BracesInsertHandler.java    From consulo-unity3d with Apache License 2.0 4 votes vote down vote up
@Override
public void handleInsert(final InsertionContext context, final T item)
{
	final Editor editor = context.getEditor();
	final Document document = editor.getDocument();
	context.commitDocument();
	PsiElement element = findNextToken(context);

	final char completionChar = context.getCompletionChar();
	final boolean putCaretInside = completionChar == '{' || placeCaretInsideParentheses(context, item);

	if(completionChar == '{')
	{
		context.setAddCompletionChar(false);
	}

	if(isToken(element, "{"))
	{
		int lparenthOffset = element.getTextRange().getStartOffset();
		if(mySpaceBeforeParentheses && lparenthOffset == context.getTailOffset())
		{
			document.insertString(context.getTailOffset(), " ");
			lparenthOffset++;
		}

		if(completionChar == '{' || completionChar == '\t')
		{
			editor.getCaretModel().moveToOffset(lparenthOffset + 1);
		}
		else
		{
			editor.getCaretModel().moveToOffset(context.getTailOffset());
		}

		context.setTailOffset(lparenthOffset + 1);

		PsiElement list = element.getParent();
		PsiElement last = list.getLastChild();
		if(isToken(last, "}"))
		{
			int rparenthOffset = last.getTextRange().getStartOffset();
			context.setTailOffset(rparenthOffset + 1);
			if(!putCaretInside)
			{
				for(int i = lparenthOffset + 1; i < rparenthOffset; i++)
				{
					if(!Character.isWhitespace(document.getCharsSequence().charAt(i)))
					{
						return;
					}
				}
				editor.getCaretModel().moveToOffset(context.getTailOffset());
			}
			else if(mySpaceBetweenParentheses && document.getCharsSequence().charAt(lparenthOffset) == ' ')
			{
				editor.getCaretModel().moveToOffset(lparenthOffset + 2);
			}
			else
			{
				editor.getCaretModel().moveToOffset(lparenthOffset + 1);
			}
			return;
		}
	}
	else
	{
		document.insertString(context.getTailOffset(), getSpace(mySpaceBeforeParentheses) + '{' + getSpace(mySpaceBetweenParentheses));
		editor.getCaretModel().moveToOffset(context.getTailOffset());
	}

	if(!myMayInsertRightParenthesis)
	{
		return;
	}

	if(context.getCompletionChar() == '{')
	{
		int tail = context.getTailOffset();
		if(tail < document.getTextLength() && StringUtil.isJavaIdentifierPart(document.getCharsSequence().charAt(tail)))
		{
			return;
		}
	}

	document.insertString(context.getTailOffset(), getSpace(mySpaceBetweenParentheses) + "}");
	if(!putCaretInside)
	{
		editor.getCaretModel().moveToOffset(context.getTailOffset());
	}
}
 
Example 11
Source File: CSharpParenthesesWithSemicolonInsertHandler.java    From consulo-csharp with Apache License 2.0 4 votes vote down vote up
@Override
@RequiredUIAccess
public void handleInsert(InsertionContext context, LookupElement item)
{
	boolean isMethodLike = myDeclaration instanceof DotNetLikeMethodDeclaration;
	if(isMethodLike)
	{
		new CSharpParenthesesInsertHandler((DotNetLikeMethodDeclaration) myDeclaration).handleInsert(context, item);
	}

	if(context.getCompletionChar() != '\n' || context.getFile() instanceof CSharpCodeFragment)
	{
		return;
	}

	// for void method we always insert semicolon
	if(isMethodLike && !(myDeclaration instanceof CSharpConstructorDeclaration) && DotNetTypeRefUtil.isVmQNameEqual(((DotNetLikeMethodDeclaration) myDeclaration).getReturnTypeRef(),
			myDeclaration, DotNetTypes.System.Void))
	{
		if(TailType.SEMICOLON.isApplicable(context))
		{
			TailType.SEMICOLON.processTail(context.getEditor(), context.getTailOffset());
		}
	}
	/*else
	{
		context.commitDocument();
		PsiElement elementAt = context.getFile().findElementAt(context.getStartOffset());
		PsiElement parent = PsiTreeUtil.getParentOfType(elementAt, CSharpMethodCallExpressionImpl.class);
		if(parent != null && parent.getNextSibling() instanceof PsiErrorElement)
		{
			TailType.SEMICOLON.processTail(context.getEditor(), context.getTailOffset());
		}

		parent = PsiTreeUtil.getParentOfType(elementAt, CSharpLocalVariable.class);
		if(parent != null && parent.getNextSibling() instanceof PsiErrorElement)
		{
			TailType.SEMICOLON.processTail(context.getEditor(), context.getTailOffset());
		}
	}*/
}
 
Example 12
Source File: ExpressionOrStatementInsertHandler.java    From consulo-csharp with Apache License 2.0 4 votes vote down vote up
private void handleInsertImpl(InsertionContext context, T item, Editor editor, Document document)
{
	PsiElement element = findNextToken(context);

	final char completionChar = context.getCompletionChar();

	final boolean putCaretInside = completionChar == myOpenChar || placeCaretInsideParentheses(context, item);

	if(completionChar == myOpenChar)
	{
		context.setAddCompletionChar(false);
	}

	boolean canAddSpaceBeforePair = canAddSpaceBeforePair(context, item);

	if(isToken(element, myOpenChar))
	{
		int lparenthOffset = element.getTextRange().getStartOffset();
		if(canAddSpaceBeforePair && lparenthOffset == context.getTailOffset())
		{
			document.insertString(context.getTailOffset(), " ");
			lparenthOffset++;
		}

		if(completionChar == myOpenChar || completionChar == '\t')
		{
			editor.getCaretModel().moveToOffset(lparenthOffset + 1);
		}
		else
		{
			editor.getCaretModel().moveToOffset(context.getTailOffset());
		}

		context.setTailOffset(lparenthOffset + 1);

		PsiElement list = element.getParent();
		PsiElement last = list.getLastChild();
		if(isToken(last, myCloseChar))
		{
			int rparenthOffset = last.getTextRange().getStartOffset();
			context.setTailOffset(rparenthOffset + 1);
			if(!putCaretInside)
			{
				for(int i = lparenthOffset + 1; i < rparenthOffset; i++)
				{
					if(!Character.isWhitespace(document.getCharsSequence().charAt(i)))
					{
						return;
					}
				}
				editor.getCaretModel().moveToOffset(context.getTailOffset());
			}
			else
			{
				editor.getCaretModel().moveToOffset(lparenthOffset + 1);
			}
			return;
		}
	}
	else
	{
		document.insertString(context.getTailOffset(), getSpace(canAddSpaceBeforePair) + myOpenChar);
		editor.getCaretModel().moveToOffset(context.getTailOffset());
	}

	if(context.getCompletionChar() == myOpenChar)
	{
		int tail = context.getTailOffset();
		if(tail < document.getTextLength() && StringUtil.isJavaIdentifierPart(document.getCharsSequence().charAt(tail)))
		{
			return;
		}
	}

	document.insertString(context.getTailOffset(), String.valueOf(myCloseChar));
	if(!putCaretInside)
	{
		editor.getCaretModel().moveToOffset(context.getTailOffset());
	}
}
 
Example 13
Source File: CSharpParenthesesInsertHandler.java    From consulo-csharp with Apache License 2.0 4 votes vote down vote up
@RequiredUIAccess
@Override
public void handleInsert(final InsertionContext context, final LookupElement item)
{
	final Editor editor = context.getEditor();
	final Document document = editor.getDocument();
	context.commitDocument();
	PsiElement element = findNextToken(context);

	final char completionChar = context.getCompletionChar();
	final boolean putCaretInside = completionChar == '(' || placeCaretInsideParentheses();

	if(completionChar == '(')
	{
		context.setAddCompletionChar(false);
	}

	if(isToken(element, "("))
	{
		int lparenthOffset = element.getTextRange().getStartOffset();

		if(completionChar == '(' || completionChar == '\t')
		{
			editor.getCaretModel().moveToOffset(lparenthOffset + 1);
		}
		else
		{
			editor.getCaretModel().moveToOffset(context.getTailOffset());
		}

		context.setTailOffset(lparenthOffset + 1);

		PsiElement list = element.getParent();
		PsiElement last = list.getLastChild();
		if(isToken(last, ")"))
		{
			int rparenthOffset = last.getTextRange().getStartOffset();
			context.setTailOffset(rparenthOffset + 1);
			if(!putCaretInside)
			{
				for(int i = lparenthOffset + 1; i < rparenthOffset; i++)
				{
					if(!Character.isWhitespace(document.getCharsSequence().charAt(i)))
					{
						return;
					}
				}
				editor.getCaretModel().moveToOffset(context.getTailOffset());
			}
			else
			{
				AutoPopupController.getInstance(context.getProject()).autoPopupParameterInfo(editor, CSharpParameterInfoHandler.item(myLikeMethodDeclaration));

				editor.getCaretModel().moveToOffset(lparenthOffset + 1);
			}
			return;
		}
	}
	else
	{
		document.insertString(context.getTailOffset(), "" + "(" + "");
		editor.getCaretModel().moveToOffset(context.getTailOffset());
	}

	if(context.getCompletionChar() == '(')
	{
		//todo use BraceMatchingUtil.isPairedBracesAllowedBeforeTypeInFileType
		int tail = context.getTailOffset();
		if(tail < document.getTextLength() && StringUtil.isJavaIdentifierPart(document.getCharsSequence().charAt(tail)))
		{
			return;
		}
	}

	document.insertString(context.getTailOffset(), ")");
	if(!putCaretInside)
	{
		editor.getCaretModel().moveToOffset(context.getTailOffset());
	}
	else
	{
		AutoPopupController.getInstance(context.getProject()).autoPopupParameterInfo(editor, CSharpParameterInfoHandler.item(myLikeMethodDeclaration));
	}
}
 
Example 14
Source File: LtGtInsertHandler.java    From consulo-csharp with Apache License 2.0 4 votes vote down vote up
@Override
public void handleInsert(final InsertionContext context, final T item)
{
	final Editor editor = context.getEditor();
	final Document document = editor.getDocument();
	context.commitDocument();
	PsiElement element = findNextToken(context);

	final char completionChar = context.getCompletionChar();
	final boolean putCaretInside = completionChar == '<' || placeCaretInsideParentheses(context, item);

	if(completionChar == '<')
	{
		context.setAddCompletionChar(false);
	}

	if(isToken(element, "<"))
	{
		int lparenthOffset = element.getTextRange().getStartOffset();
		if(mySpaceBeforeParentheses && lparenthOffset == context.getTailOffset())
		{
			document.insertString(context.getTailOffset(), " ");
			lparenthOffset++;
		}

		if(completionChar == '<' || completionChar == '\t')
		{
			editor.getCaretModel().moveToOffset(lparenthOffset + 1);
		}
		else
		{
			editor.getCaretModel().moveToOffset(context.getTailOffset());
		}

		context.setTailOffset(lparenthOffset + 1);

		PsiElement list = element.getParent();
		PsiElement last = list.getLastChild();
		if(isToken(last, ">"))
		{
			int rparenthOffset = last.getTextRange().getStartOffset();
			context.setTailOffset(rparenthOffset + 1);
			if(!putCaretInside)
			{
				for(int i = lparenthOffset + 1; i < rparenthOffset; i++)
				{
					if(!Character.isWhitespace(document.getCharsSequence().charAt(i)))
					{
						return;
					}
				}
				editor.getCaretModel().moveToOffset(context.getTailOffset());
			}
			else if(mySpaceBetweenParentheses && document.getCharsSequence().charAt(lparenthOffset) == ' ')
			{
				editor.getCaretModel().moveToOffset(lparenthOffset + 2);
			}
			else
			{
				editor.getCaretModel().moveToOffset(lparenthOffset + 1);
			}
			return;
		}
	}
	else
	{
		document.insertString(context.getTailOffset(), getSpace(mySpaceBeforeParentheses) + "<" + getSpace(mySpaceBetweenParentheses));
		editor.getCaretModel().moveToOffset(context.getTailOffset());
	}

	if(!myMayInsertRightParenthesis)
	{
		return;
	}

	if(context.getCompletionChar() == '<')
	{
		//todo use BraceMatchingUtil.isPairedBracesAllowedBeforeTypeInFileType
		int tail = context.getTailOffset();
		if(tail < document.getTextLength() && StringUtil.isJavaIdentifierPart(document.getCharsSequence().charAt(tail)))
		{
			return;
		}
	}

	document.insertString(context.getTailOffset(), getSpace(mySpaceBetweenParentheses) + ">");
	if(!putCaretInside)
	{
		editor.getCaretModel().moveToOffset(context.getTailOffset());
	}
}
 
Example 15
Source File: ParenthesesInsertHandler.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void handleInsert(final InsertionContext context, final T item) {
  final Editor editor = context.getEditor();
  final Document document = editor.getDocument();
  context.commitDocument();
  PsiElement lParen = findExistingLeftParenthesis(context);

  final char completionChar = context.getCompletionChar();
  final boolean putCaretInside = completionChar == myLeftParenthesis || placeCaretInsideParentheses(context, item);

  if (completionChar == myLeftParenthesis) {
    context.setAddCompletionChar(false);
  }

  if (lParen != null) {
    int lparenthOffset = lParen.getTextRange().getStartOffset();
    if (mySpaceBeforeParentheses && lparenthOffset == context.getTailOffset()) {
      document.insertString(context.getTailOffset(), " ");
      lparenthOffset++;
    }

    if (completionChar == myLeftParenthesis || completionChar == '\t') {
      editor.getCaretModel().moveToOffset(lparenthOffset + 1);
    }
    else {
      editor.getCaretModel().moveToOffset(context.getTailOffset());
    }

    context.setTailOffset(lparenthOffset + 1);

    PsiElement list = lParen.getParent();
    PsiElement last = list.getLastChild();
    if (isToken(last, String.valueOf(myRightParenthesis))) {
      int rparenthOffset = last.getTextRange().getStartOffset();
      context.setTailOffset(rparenthOffset + 1);
      if (!putCaretInside) {
        for (int i = lparenthOffset + 1; i < rparenthOffset; i++) {
          if (!Character.isWhitespace(document.getCharsSequence().charAt(i))) {
            return;
          }
        }
        editor.getCaretModel().moveToOffset(context.getTailOffset());
      }
      else if (mySpaceBetweenParentheses && document.getCharsSequence().charAt(lparenthOffset) == ' ') {
        editor.getCaretModel().moveToOffset(lparenthOffset + 2);
      }
      else {
        editor.getCaretModel().moveToOffset(lparenthOffset + 1);
      }
      return;
    }
  }
  else {
    document.insertString(context.getTailOffset(), getSpace(mySpaceBeforeParentheses) + myLeftParenthesis + getSpace(mySpaceBetweenParentheses));
    editor.getCaretModel().moveToOffset(context.getTailOffset());
  }

  if (!myMayInsertRightParenthesis) return;

  if (context.getCompletionChar() == myLeftParenthesis) {
    //todo use BraceMatchingUtil.isPairedBracesAllowedBeforeTypeInFileType
    int tail = context.getTailOffset();
    if (tail < document.getTextLength() && StringUtil.isJavaIdentifierPart(document.getCharsSequence().charAt(tail))) {
      return;
    }
  }

  document.insertString(context.getTailOffset(), getSpace(mySpaceBetweenParentheses) + myRightParenthesis);
  if (!putCaretInside) {
    editor.getCaretModel().moveToOffset(context.getTailOffset());
  }
}
 
Example 16
Source File: CharTailType.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isApplicable(@Nonnull InsertionContext context) {
  return !context.shouldAddCompletionChar() || context.getCompletionChar() != myChar;
}