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

The following examples show how to use com.intellij.codeInsight.completion.InsertionContext#getEditor() . 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: JSGraphQLEndpointAutoImportInsertHandler.java    From js-graphql-intellij-plugin with MIT License 6 votes vote down vote up
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 3
Source File: AttrMacroInsertHandler.java    From intellij-latte with MIT License 6 votes vote down vote up
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 4
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 5
Source File: FreeExpressionCompletionProvider.java    From reasonml-idea-plugin with MIT License 5 votes vote down vote up
private static void insertExpression(@NotNull InsertionContext insertionContext, @NotNull LookupElement element) {
    PsiElement psiElement = element.getPsiElement();
    if (psiElement instanceof PsiLet) {
        PsiLet let = (PsiLet) psiElement;
        if (let.isFunction()) {
            insertionContext.setAddCompletionChar(false);
            Editor editor = insertionContext.getEditor();
            EditorModificationUtil.insertStringAtCaret(editor, "()");
            editor.getCaretModel().moveToOffset(editor.getCaretModel().getOffset() - 1);
        }
    }
}
 
Example 6
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 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: 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 9
Source File: FilterInsertHandler.java    From intellij-latte with MIT License 5 votes vote down vote up
public void handleInsert(@NotNull InsertionContext context, @NotNull LookupElement lookupElement) {
	PsiElement element = context.getFile().findElementAt(context.getStartOffset());
	if (element != null && element.getLanguage() == LatteLanguage.INSTANCE) {
		PsiElement parent = element.getParent();

		LatteFilterSettings filter = null;
		if (parent instanceof LatteMacroModifier) {
			String modifierName = ((LatteMacroModifier) parent).getModifierName();
			filter = LatteConfiguration.getInstance(element.getProject()).getFilter(modifierName);
		}

		Editor editor = context.getEditor();
		if (filter != null && filter.getModifierInsert() != null && filter.getModifierInsert().length() > 0) {
			CaretModel caretModel = editor.getCaretModel();
			String text = editor.getDocument().getText();

			int offset = caretModel.getOffset();

			int lastBraceOffset = text.indexOf(":", offset);
			int endOfLineOffset = text.indexOf("\n", offset);

			if (endOfLineOffset == -1) {
				endOfLineOffset = text.length();
			}
			if (lastBraceOffset == -1 || lastBraceOffset > endOfLineOffset) {
				EditorModificationUtil.insertStringAtCaret(editor, filter.getModifierInsert());
				caretModel.moveToOffset(offset + 1);
			}
		}
		PsiDocumentManager.getInstance(context.getProject()).commitDocument(editor.getDocument());
	}
}
 
Example 10
Source File: JSGraphQLEndpointImportInsertHandler.java    From js-graphql-intellij-plugin with MIT License 5 votes vote down vote up
public void handleInsert(InsertionContext context, LookupElement item) {
  final Editor editor = context.getEditor();
  final Project project = editor.getProject();
  if (project != null) {
    EditorModificationUtil.insertStringAtCaret(editor, " \"\"");
    PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
    editor.getCaretModel().moveToOffset(editor.getCaretModel().getOffset() - 1);
    if (myTriggerAutoPopup) {
      AutoPopupController.getInstance(project).autoPopupMemberLookup(editor, null);
    }
  }
}
 
Example 11
Source File: PostfixInsertHandler.java    From bamboo-soy with Apache License 2.0 5 votes vote down vote up
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 12
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 13
Source File: JsxAttributeCompletionProvider.java    From reasonml-idea-plugin with MIT License 5 votes vote down vote up
private static void insertTagAttributeHandler(@NotNull InsertionContext context) {
    context.setAddCompletionChar(false);

    Editor editor = context.getEditor();
    EditorModificationUtil.insertStringAtCaret(editor, "=()");
    editor.getCaretModel().moveToOffset(editor.getCaretModel().getOffset() - 1);
}
 
Example 14
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 15
Source File: MacroInsertHandler.java    From intellij-latte with MIT License 4 votes vote down vote up
public void handleInsert(@NotNull InsertionContext context, @NotNull LookupElement lookupElement) {
	PsiElement element = context.getFile().findElementAt(context.getStartOffset());
	if (element != null && element.getLanguage() == LatteLanguage.INSTANCE) {
		PsiElement parent = element.getParent();

		String spacesBefore = "";
		boolean resolvePairMacro = false;
		boolean lastError = parent.getLastChild().getNode().getElementType() == TokenType.ERROR_ELEMENT;
		String macroName = null;
		LatteTagSettings macro = null;
		if (lastError && element.getNode().getElementType() == LatteTypes.T_MACRO_NAME) {
			macroName = element.getText();
			macro = LatteConfiguration.getInstance(element.getProject()).getTag(macroName);

		} else if (parent instanceof LatteMacroTag) {
			macroName = ((LatteMacroTag) parent).getMacroName();
			macro = LatteConfiguration.getInstance(element.getProject()).getTag(macroName);
		}

		boolean isCloseTag = parent instanceof LatteMacroCloseTag;
		if (!isCloseTag && macro != null && macro.getType() == LatteTagSettings.Type.PAIR) {
			resolvePairMacro = true;
		}

		if (macroName != null) {
			if (resolvePairMacro && macro.isMultiLine()) {
				spacesBefore += LatteUtil.getSpacesBeforeCaret(context.getEditor());
			}

			Editor editor = context.getEditor();
			CaretModel caretModel = editor.getCaretModel();
			String text = editor.getDocument().getText();

			int spaceInserted = 0;
			int offset = caretModel.getOffset();

			if (macro != null && !isCloseTag && macro.hasParameters() && !LatteUtil.isStringAtCaret(editor, " ")) {
				EditorModificationUtil.insertStringAtCaret(editor, " ");
				spaceInserted = 1;
			}

			int lastBraceOffset = text.indexOf("}", offset);
			int endOfLineOffset = text.indexOf("\n", offset);

			if (endOfLineOffset == -1) {
				endOfLineOffset = text.length();
			}
			if (lastBraceOffset == -1 || lastBraceOffset > endOfLineOffset) {
				caretModel.moveToOffset(endOfLineOffset + spaceInserted);
				EditorModificationUtil.insertStringAtCaret(editor, "}");
				lastBraceOffset = endOfLineOffset;
				endOfLineOffset++;
			}

			if (resolvePairMacro) {
				String endTag = "";
				if (macro.isMultiLine()) {
					endTag += "\n\n" + spacesBefore;
				}
				endTag += "{/" + macroName + "}";

				int endTagOffset = text.indexOf(endTag, offset);
				if (endTagOffset == -1 || endTagOffset > endOfLineOffset) {
					caretModel.moveToOffset(lastBraceOffset + spaceInserted + 1);
					EditorModificationUtil.insertStringAtCaret(editor, endTag);
				}
			}

			caretModel.moveToOffset(offset + 1);
			PsiDocumentManager.getInstance(context.getProject()).commitDocument(editor.getDocument());
		}
	}
}
 
Example 16
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 17
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 18
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 19
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 20
Source File: InsertHandlerUtils.java    From intellij-xquery with Apache License 2.0 4 votes vote down vote up
public static void removePreviousNamespaceAndColonIfPresent(InsertionContext context) {
    final Editor editor = context.getEditor();
    final Document document = editor.getDocument();
    PsiElement elementBeforeColon = findElementBeforeColon(context);
    removeNamespaceAndColon(context, document, elementBeforeColon);
}