Line End Add/Copy Task

This commit is contained in:
林万程
2023-11-24 14:51:03 +08:00
parent ce51735271
commit fdb4c7d6b5
6 changed files with 99 additions and 50 deletions

View File

@@ -6,7 +6,10 @@ import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.ide.CopyPasteManager;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import io.github.linwancen.plugin.show.bean.FileInfo;
import io.github.linwancen.plugin.show.settings.ShowBundle;
import org.jetbrains.annotations.NotNull;
@@ -31,34 +34,46 @@ public class LineEndCopy extends DumbAwareAction {
@Override
public void actionPerformed(@NotNull AnActionEvent event) {
try {
ApplicationManager.getApplication().runReadAction(() -> copyWithDoc(event));
copyWithDoc(event);
} catch (Throwable e) {
LOG.info("LineEndCopy catch Throwable but log to record.", e);
}
}
private void copyWithDoc(@NotNull AnActionEvent event) {
@Nullable Project project = event.getProject();
if (project == null) {
return;
}
@Nullable FileInfo fileInfo = FileInfo.of(event);
if (fileInfo == null) {
return;
}
int startLine = 0;
int endLine = fileInfo.document.getLineCount() - 1;
// if select
@Nullable Editor editor = event.getData(CommonDataKeys.EDITOR);
if (editor != null) {
@NotNull Caret primaryCaret = editor.getCaretModel().getPrimaryCaret();
int start = primaryCaret.getSelectionStart();
int end = primaryCaret.getSelectionEnd();
try {
startLine = fileInfo.document.getLineNumber(start);
endLine = fileInfo.document.getLineNumber(end);
} catch (Exception e) {
return;
new Task.Backgroundable(project, "Show LineEndCopy " + fileInfo.file.getName()) {
@Override
public void run(@NotNull ProgressIndicator indicator) {
ApplicationManager.getApplication().runReadAction(() -> {
int startLine = 0;
int endLine = fileInfo.document.getLineCount() - 1;
// if select
@Nullable Editor editor = event.getData(CommonDataKeys.EDITOR);
if (editor != null) {
@NotNull Caret primaryCaret = editor.getCaretModel().getPrimaryCaret();
int start = primaryCaret.getSelectionStart();
int end = primaryCaret.getSelectionEnd();
try {
startLine = fileInfo.document.getLineNumber(start);
endLine = fileInfo.document.getLineNumber(end);
} catch (Exception e) {
return;
}
}
LineEnd.textWithDoc(fileInfo, startLine, endLine, indicator, s -> {
@NotNull StringSelection content = new StringSelection(s);
CopyPasteManager.getInstance().setContents(content);
});
});
}
}
@NotNull String textWithDoc = LineEnd.textWithDoc(fileInfo, startLine, endLine);
@NotNull StringSelection content = new StringSelection(textWithDoc);
CopyPasteManager.getInstance().setContents(content);
}.queue();
}
}