Java Code Examples for com.intellij.openapi.editor.EditorModificationUtil#moveCaretRelatively()

The following examples show how to use com.intellij.openapi.editor.EditorModificationUtil#moveCaretRelatively() . 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: XQueryXmlGtTypedHandler.java    From intellij-xquery with Apache License 2.0 6 votes vote down vote up
@Override
public Result beforeCharTyped(final char c, final Project project, final Editor editor, final PsiFile editedFile, final FileType fileType) {
    if ((editedFile.getLanguage() instanceof XQueryLanguage) && c == '>') {
        PsiDocumentManager.getInstance(project).commitAllDocuments();
        PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
        if (file == null) return Result.CONTINUE;
        final int offset = editor.getCaretModel().getOffset();
        FileViewProvider provider = file.getViewProvider();
        PsiElement element = provider.findElementAt(offset, XQueryLanguage.class);

        if (element != null && element.getNode() != null && (
                element.getNode().getElementType() == XQueryTypes.XMLTAGEND ||
                        element.getNode().getElementType() == XQueryTypes.XMLEMPTYELEMENTEND)) {
            EditorModificationUtil.moveCaretRelatively(editor, 1);
            editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
            return Result.STOP;
        }
    }
    return Result.CONTINUE;
}
 
Example 2
Source File: EditorEventManager.java    From lsp4intellij with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("WeakerAccess")
public void prepareAndRunSnippet(String insertText) {

    List<SnippetVariable> variables = new ArrayList<>();
    // Extracts variables using placeholder REGEX pattern.
    Matcher varMatcher = Pattern.compile(SNIPPET_PLACEHOLDER_REGEX).matcher(insertText);
    while (varMatcher.find()) {
        variables.add(new SnippetVariable(varMatcher.group(), varMatcher.start(), varMatcher.end()));
    }

    variables.sort(Comparator.comparingInt(o -> o.startIndex));
    final String[] finalInsertText = {insertText};
    variables.forEach(var -> finalInsertText[0] = finalInsertText[0].replace(var.lspSnippetText, "$"));

    String[] splitInsertText = finalInsertText[0].split("\\$");
    finalInsertText[0] = String.join("", splitInsertText);

    TemplateImpl template = (TemplateImpl) TemplateManager.getInstance(getProject()).createTemplate(finalInsertText[0],
            "lsp4intellij");
    template.parseSegments();

    final int[] varIndex = {0};
    variables.forEach(var -> {
        template.addTextSegment(splitInsertText[varIndex[0]]);
        template.addVariable(varIndex[0] + "_" + var.variableValue, new TextExpression(var.variableValue),
                new TextExpression(var.variableValue), true, false);
        varIndex[0]++;
    });
    // If the snippet text ends with a placeholder, there will be no string segment left to append after the last
    // variable.
    if (splitInsertText.length != variables.size()) {
        template.addTextSegment(splitInsertText[splitInsertText.length - 1]);
    }
    template.setInline(true);
    EditorModificationUtil.moveCaretRelatively(editor, -template.getTemplateText().length());
    TemplateManager.getInstance(getProject()).startTemplate(editor, template);
}
 
Example 3
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 4
Source File: JsonInsertValueHandler.java    From intellij-swagger with MIT License 5 votes vote down vote up
private void handleEndingQuote(final InsertionContext insertionContext) {
  final int caretOffset = insertionContext.getEditor().getCaretModel().getOffset();
  final CharSequence chars = insertionContext.getDocument().getCharsSequence();

  final boolean hasEndingQuote = CharArrayUtil.regionMatches(chars, caretOffset, "\"");
  if (!hasEndingQuote) {
    insertionContext.getDocument().insertString(caretOffset, "\"");
    EditorModificationUtil.moveCaretRelatively(insertionContext.getEditor(), 1);
  }
}
 
Example 5
Source File: JsonInsertFieldHandler.java    From intellij-swagger with MIT License 5 votes vote down vote up
private void handleEndingQuote(final InsertionContext insertionContext) {
  final int caretOffset = insertionContext.getEditor().getCaretModel().getOffset();
  final CharSequence chars = insertionContext.getDocument().getCharsSequence();

  final boolean hasEndingQuote = CharArrayUtil.regionMatches(chars, caretOffset, "\"");
  if (!hasEndingQuote) {
    insertionContext.getDocument().insertString(caretOffset, "\"");
  }
  EditorModificationUtil.moveCaretRelatively(insertionContext.getEditor(), 1);
}