public boolean executeFilter(@NotNull Editor editor, @NotNull TextRange range, @NotNull String command) throws IOException { final CharSequence charsSequence = editor.getDocument().getCharsSequence(); final int startOffset = range.getStartOffset(); final int endOffset = range.getEndOffset(); final String output = executeCommand(command, charsSequence.subSequence(startOffset, endOffset)); editor.getDocument().replaceString(startOffset, endOffset, output); return true; }
/** * Replace text in the editor * * @param editor The editor to replace text in * @param start The start offset to change * @param end The end offset to change * @param str The new text */ private void replaceText(@NotNull Editor editor, int start, int end, @NotNull String str) { editor.getDocument().replaceString(start, end, str); final int newEnd = start + str.length(); VimPlugin.getMark().setChangeMarks(editor, new TextRange(start, newEnd)); VimPlugin.getMark().setMark(editor, MarkGroup.MARK_CHANGE_POS, newEnd); }
private void performReplacements( final Document document, final Map<TextRange, String> replacements) { if (replacements.isEmpty()) { return; } TreeMap<TextRange, String> sorted = new TreeMap<>(comparing(TextRange::getStartOffset)); sorted.putAll(replacements); WriteCommandAction.runWriteCommandAction( getProject(), () -> { for (Entry<TextRange, String> entry : sorted.descendingMap().entrySet()) { document.replaceString( entry.getKey().getStartOffset(), entry.getKey().getEndOffset(), entry.getValue()); } PsiDocumentManager.getInstance(getProject()).commitDocument(document); }); } }
/** * Handles QuickFix action invoked on {@link IgnoreEntry}. * * @param project the {@link Project} containing the working file * @param psiFile the {@link PsiFile} containing handled entry * @param startElement the {@link IgnoreEntry} that will be removed * @param endElement the {@link PsiElement} which is ignored in invoked action */ @Override public void invoke(@NotNull Project project, @NotNull PsiFile psiFile, @NotNull PsiElement startElement, @NotNull PsiElement endElement) { if (startElement instanceof IgnoreEntry) { Document document = PsiDocumentManager.getInstance(project).getDocument(psiFile); if (document != null) { int start = startElement.getStartOffsetInParent(); String text = startElement.getText(); String fixed = getFixedPath(text); document.replaceString(start, start + text.length(), fixed); } } }
/** * Sets new content to the editor component. * * @param content new content */ public void setContent(@NotNull final String content) { ApplicationManager.getApplication().runWriteAction( () -> CommandProcessor.getInstance().runUndoTransparentAction( () -> previewDocument.replaceString(0, previewDocument.getTextLength(), content) ) ); } }
/** * Updates editor's content depending on the selected {@link TreePath}. * * @param path selected tree path */ private void updateDescriptionPanel(@NotNull TreePath path) { final TemplateTreeNode node = (TemplateTreeNode) path.getLastPathComponent(); final Resources.Template template = node.getTemplate(); ApplicationManager.getApplication().runWriteAction( () -> CommandProcessor.getInstance().runUndoTransparentAction(() -> { String content = template != null ? StringUtil.notNullize(template.getContent()).replace('\r', '\0') : ""; previewDocument.replaceString(0, previewDocument.getTextLength(), content); List<Pair<Integer, Integer>> pairs = getFilterRanges(profileFilter.getTextEditor().getText(), content); highlightWords(pairs); }) ); }
editor.getDocument().replaceString(startoff, endoff, match); lastMatch = startoff; newpos = EditorHelper.offsetToCharacterPosition(editor, newend);
editor.getDocument().replaceString(finalStartOffset, finalEndOffset, insertString); editor.getCaretModel().moveToOffset(finalEndOffset); }), "Twig Translation Insert " + keyName, null);
@Override protected void run(final Result result) throws Throwable { doc.replaceString(start, stop+1, text); } };
@Override public void invoke(@NotNull Project project, @NotNull PsiFile file, Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) { TextRange r = startElement.getTextRange(); String replacement = String.valueOf(numericValue); Document document = PsiDocumentManager.getInstance(project).getDocument(file); if (document != null) { document.replaceString(r.getStartOffset(), r.getEndOffset(), replacement); PsiDocumentManager.getInstance(project).commitDocument(document); } } }
@Override public void run() { final String dummyIdentifier = CompletionInitializationContext.DUMMY_IDENTIFIER_TRIMMED; if (StringUtil.isEmpty(dummyIdentifier)) return; final int startOffset = position.getTextOffset(); final int endOffset = startOffset + position.getTextLength(); // int startOffset = hostMap.getOffset(CompletionInitializationContext.START_OFFSET); // int endOffset = hostMap.getOffset(CompletionInitializationContext.SELECTION_END_OFFSET); copyDocument.replaceString(startOffset, endOffset, dummyIdentifier); } });
@Override public void invoke(@NotNull Project project, @NotNull PsiFile file, Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) { Document document = PsiDocumentManager.getInstance(project).getDocument(file); if (document != null) { BashBackquote backquote = (BashBackquote) startElement; int endOffset = startElement.getTextRange().getEndOffset(); String command = backquote.getCommandText(); //fixme replace this with a PSI element created by the Bash psi factory document.replaceString(startElement.getTextOffset(), endOffset, "$(" + command + ")"); PsiDocumentManager.getInstance(project).commitDocument(document); } } }
@Override public void invoke(@NotNull Project project, @NotNull PsiFile file, Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) { Document document = PsiDocumentManager.getInstance(project).getDocument(file); if (document != null) { int endOffset = startElement.getTextOffset() + startElement.getTextLength(); String replacement = wrapText(startElement.getText()); document.replaceString(startElement.getTextOffset(), endOffset, replacement); PsiDocumentManager.getInstance(project).commitDocument(document); } }
@Override public void invoke(@NotNull Project project, @NotNull PsiFile file, Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) { Document document = PsiDocumentManager.getInstance(project).getDocument(file); if (document != null) { BashSubshellCommand subshellCommand = (BashSubshellCommand) startElement; //fixme check if the element is inside of a $() block int startOffset = subshellCommand.getTextOffset(); //to include the $ int endOffset = subshellCommand.getTextRange().getEndOffset(); String command = subshellCommand.getCommandText(); document.replaceString(startOffset, endOffset, "`" + command + "`"); PsiDocumentManager.getInstance(project).commitDocument(document); } } }
@Override public void invoke(@NotNull Project project, @NotNull PsiFile file, Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) { TextRange r = startElement.getTextRange(); Document document = PsiDocumentManager.getInstance(project).getDocument(file); String replacement = ValueExpansionUtil.expand(startElement.getText(), enableBash4); if (replacement != null && document != null) { editor.getDocument().replaceString(r.getStartOffset(), r.getEndOffset(), replacement); PsiDocumentManager.getInstance(project).commitDocument(document); } } }
@Override public void visitElement(PsiElement element) { super.visitElement(element); String newValue = convert(element); if (newValue != null) { int startOffset = element.getTextRange().getStartOffset() + delta; int endOffset = element.getTextRange().getEndOffset() + delta; document.replaceString(startOffset, endOffset, newValue); delta += newValue.length() - element.getTextLength(); } }
@Override protected void run(final Result result) throws Throwable { // do all as one operation. if ( insertionPoint>=doc.getTextLength() ) { doc.insertString(doc.getTextLength(), newRule); } else { doc.insertString(insertionPoint, newRule); } doc.replaceString(start_.getStartIndex(), stop_.getStopIndex()+1, nameChooser.ruleName); } };
@SuppressWarnings("Duplicates") @Override public T handleContentChange(@NotNull T element, @NotNull TextRange range, String newContent) throws IncorrectOperationException { final PsiDocumentManager manager = PsiDocumentManager.getInstance(element.getProject()); final Document document = manager.getDocument(element.getContainingFile()); if (document != null) { TextRange elementRange = element.getTextRange(); manager.doPostponedOperationsAndUnblockDocument(document); document.replaceString(elementRange.getStartOffset() + range.getStartOffset(), elementRange.getStartOffset() + range.getEndOffset(), newContent); manager.commitDocument(document); } return element; } }
public void setPreviewText(@Nonnull String text) { final CommandProcessor processor = CommandProcessor.getInstance(); processor.executeCommand(null, () -> { final Application application = ApplicationManager.getApplication(); application.runWriteAction(() -> { previewDocument.replaceString(INITIAL_OFFSET, previewDocument.getTextLength(), text); final int textLength = previewDocument.getTextLength(); final CaretModel caret = previewEditor.getCaretModel(); if (caret.getOffset() >= textLength) { caret.moveToOffset(textLength); } }); }, null, null); }
public void setJson(@NotNull String json) { final CommandProcessor processor = CommandProcessor.getInstance(); processor.executeCommand(project, () -> { final Application application = ApplicationManager.getApplication(); application.runWriteAction(() -> { jsonDocument.replaceString(INITIAL_OFFSET, jsonDocument.getTextLength(), json); final int textLength = jsonDocument.getTextLength(); final CaretModel caret = jsonEditor.getCaretModel(); if (caret.getOffset() >= textLength) { caret.moveToOffset(textLength); } }); }, null, null); }