use of com.intellij.openapi.editor.CaretAction in project intellij-community by JetBrains.
the class ZenCodingTemplate method doWrap.
public static void doWrap(@NotNull final String abbreviation, @NotNull final CustomTemplateCallback callback) {
final ZenCodingGenerator defaultGenerator = findApplicableDefaultGenerator(callback.getContext(), true);
assert defaultGenerator != null;
ApplicationManager.getApplication().runWriteAction(() -> CommandProcessor.getInstance().executeCommand(callback.getProject(), () -> callback.getEditor().getCaretModel().runForEachCaret(new CaretAction() {
@Override
public void perform(Caret caret) {
String selectedText = callback.getEditor().getSelectionModel().getSelectedText();
if (selectedText != null) {
String selection = selectedText.trim();
ZenCodingNode node = parse(abbreviation, callback, defaultGenerator, selection);
assert node != null;
PsiElement context = callback.getContext();
ZenCodingGenerator generator = findApplicableGenerator(node, context, true);
List<ZenCodingFilter> filters = getFilters(node, context);
EditorModificationUtil.deleteSelectedText(callback.getEditor());
PsiDocumentManager.getInstance(callback.getProject()).commitAllDocuments();
try {
expand(node, generator, filters, selection, callback, true, Registry.intValue("emmet.segments.limit"));
} catch (EmmetException e) {
CommonRefactoringUtil.showErrorHint(callback.getProject(), callback.getEditor(), e.getMessage(), "Emmet error", "");
}
}
}
}), CodeInsightBundle.message("insert.code.template.command"), null));
}
use of com.intellij.openapi.editor.CaretAction in project intellij-community by JetBrains.
the class InvokeTemplateAction method perform.
public void perform() {
final Document document = myEditor.getDocument();
final VirtualFile file = FileDocumentManager.getInstance().getFile(document);
if (file != null) {
ReadonlyStatusHandler.getInstance(myProject).ensureFilesWritable(file);
}
CommandProcessor.getInstance().executeCommand(myProject, () -> myEditor.getCaretModel().runForEachCaret(new CaretAction() {
public void perform(Caret caret) {
// at a meaningful position rather than at indent 0)
if (myEditor.getSelectionModel().hasSelection() && myTemplate.isToReformat()) {
int offset = myEditor.getSelectionModel().getSelectionStart();
int selectionEnd = myEditor.getSelectionModel().getSelectionEnd();
int lineEnd = document.getLineEndOffset(document.getLineNumber(offset));
while (offset < lineEnd && offset < selectionEnd && (document.getCharsSequence().charAt(offset) == ' ' || document.getCharsSequence().charAt(offset) == '\t')) {
offset++;
}
// avoid extra line break after $SELECTION$ in case when selection ends with a complete line
if (selectionEnd == document.getLineStartOffset(document.getLineNumber(selectionEnd))) {
selectionEnd--;
}
if (offset < lineEnd && offset < selectionEnd) {
// found non-WS character in first line of selection
myEditor.getSelectionModel().setSelection(offset, selectionEnd);
}
}
String selectionString = myEditor.getSelectionModel().getSelectedText();
TemplateManager.getInstance(myProject).startTemplate(myEditor, selectionString, myTemplate);
}
}), "Wrap with template", "Wrap with template " + myTemplate.getKey());
}
use of com.intellij.openapi.editor.CaretAction in project intellij-community by JetBrains.
the class EditorActionHandler method isEnabled.
/**
* @deprecated Implementations should override
* {@link #isEnabledForCaret(Editor, Caret, DataContext)}
* instead,
* client code should invoke
* {@link #isEnabled(Editor, Caret, DataContext)}
* instead.
*/
public boolean isEnabled(Editor editor, final DataContext dataContext) {
if (inCheck) {
return true;
}
inCheck = true;
try {
if (editor == null) {
return false;
}
Editor hostEditor = dataContext == null ? null : CommonDataKeys.HOST_EDITOR.getData(dataContext);
if (hostEditor == null) {
hostEditor = editor;
}
final boolean[] result = new boolean[1];
final CaretTask check = new CaretTask() {
@Override
public void perform(@NotNull Caret caret, @Nullable DataContext dataContext) {
result[0] = true;
}
};
if (myRunForEachCaret) {
hostEditor.getCaretModel().runForEachCaret(new CaretAction() {
@Override
public void perform(Caret caret) {
doIfEnabled(caret, dataContext, check);
}
});
} else {
doIfEnabled(hostEditor.getCaretModel().getCurrentCaret(), dataContext, check);
}
return result[0];
} finally {
inCheck = false;
}
}
Aggregations