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

The following examples show how to use com.intellij.codeInsight.completion.InsertionContext#commitDocument() . 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: BuildLookupElement.java    From intellij with Apache License 2.0 6 votes vote down vote up
/**
 * If we're wrapping with quotes, handle the (very common) case where we have a closing quote
 * after the caret -- we want to remove this quote.
 */
@Override
public void handleInsert(InsertionContext context) {
  if (!wrapWithQuotes || !insertClosingQuotes()) {
    super.handleInsert(context);
    return;
  }
  Document document = context.getDocument();
  context.commitDocument();
  PsiElement suffix = context.getFile().findElementAt(context.getTailOffset());
  if (suffix != null && suffix.getText().startsWith(quoteWrapping.quoteString)) {
    int offset = suffix.getTextOffset();
    document.deleteString(offset, offset + 1);
    context.commitDocument();
  }
  if (caretInsideQuotes()) {
    context.getEditor().getCaretModel().moveCaretRelatively(-1, 0, false, false, true);
  }
}
 
Example 2
Source File: MethodCompletionContributor.java    From litho with Apache License 2.0 5 votes vote down vote up
private static void handleInsert(PsiMethod method, InsertionContext insertionContext) {
  // Remove lookup string. As in the JavaGenerateMemberCompletionContributor
  insertionContext
      .getDocument()
      .deleteString(insertionContext.getStartOffset() - 1, insertionContext.getTailOffset());
  insertionContext.commitDocument();

  // Insert generation infos
  new MethodGenerateHandler(method)
      .invoke(
          insertionContext.getProject(),
          insertionContext.getEditor(),
          insertionContext.getFile());
}
 
Example 3
Source File: ExpressionOrStatementInsertHandler.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@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()));
		}
	}
}
 
Example 4
Source File: VariableInsertHandler.java    From intellij-xquery with Apache License 2.0 5 votes vote down vote up
@Override
public void handleInsert(InsertionContext context, LookupElement item) {
    final Editor editor = context.getEditor();
    final Document document = editor.getDocument();
    context.commitDocument();
    PsiElement element = findElementBeforeNameFragment(context);
    if (element instanceof LeafPsiElement
            && ((LeafPsiElement) element).getElementType() != XQueryTypes.DOLLAR_SIGN) {
        document.insertString(context.getStartOffset(), "$");
    }
    InsertHandlerUtils.removePreviousNamespaceAndColonIfPresent(context);
}
 
Example 5
Source File: YamlKeyInsertHandler.java    From intellij-spring-assistant with MIT License 4 votes vote down vote up
private void deleteLookupPlain(InsertionContext context) {
  Document document = context.getDocument();
  document.deleteString(context.getStartOffset(), context.getTailOffset());
  context.commitDocument();
}
 
Example 6
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 7
Source File: CSharpTypeLikeLookupElement.java    From consulo-csharp with Apache License 2.0 4 votes vote down vote up
@Override
@RequiredWriteAction
public void handleInsert(InsertionContext context)
{
	super.handleInsert(context);

	if(myAfterNew)
	{
		context.commitDocument();

		PsiDocumentManager.getInstance(context.getProject()).doPostponedOperationsAndUnblockDocument(context.getDocument());

		boolean hasParameters = false;
		if(myExtractor != DotNetGenericExtractor.EMPTY)
		{
			PsiElement psiElement = getPsiElement();
			if(psiElement instanceof CSharpTypeDeclaration)
			{
				DotNetNamedElement[] members = ((CSharpTypeDeclaration) psiElement).getMembers();
				for(DotNetNamedElement member : members)
				{
					if(member instanceof CSharpConstructorDeclaration)
					{
						int length = ((CSharpConstructorDeclaration) member).getParameters().length;
						if(length > 0)
						{
							hasParameters = true;
							break;
						}
					}
				}
			}
			else if(CSharpMethodUtil.isDelegate(psiElement))
			{
				hasParameters = true;
			}
		}

		CaretModel caretModel = context.getEditor().getCaretModel();
		int oldCaretOffset = caretModel.getOffset();

		ParenthesesInsertHandler.getInstance(true).handleInsert(context, this);

		if(!hasParameters)
		{
			caretModel.moveToOffset(oldCaretOffset + 2);
		}
	}
}
 
Example 8
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 9
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 10
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());
  }
}