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

@@ -5,6 +5,7 @@ import com.intellij.json.json5.Json5FileType;
import com.intellij.openapi.editor.*;
import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.project.DumbService;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
@@ -22,6 +23,7 @@ import org.slf4j.LoggerFactory;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
public class LineEnd extends EditorLinePainter {
@@ -47,17 +49,17 @@ public class LineEnd extends EditorLinePainter {
}
@NotNull GlobalSettingsState globalSettingsState = GlobalSettingsState.getInstance();
if (globalSettingsState.onlySelectLine) {
Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
@Nullable Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
if (editor != null) {
SelectionModel select = editor.getSelectionModel();
VisualPosition start = select.getSelectionStartPosition();
@NotNull SelectionModel select = editor.getSelectionModel();
@Nullable VisualPosition start = select.getSelectionStartPosition();
int lineNum = lineNumber + 1;
if (start != null) {
if (lineNum < start.getLine()) {
return null;
}
}
VisualPosition end = select.getSelectionEndPosition();
@Nullable VisualPosition end = select.getSelectionEndPosition();
if (end != null && lineNum > end.getLine()) {
return null;
}
@@ -82,8 +84,10 @@ public class LineEnd extends EditorLinePainter {
return Collections.singletonList(info);
}
@NotNull
public static String textWithDoc(@NotNull FileInfo fileInfo, int startLine, int endLine) {
public static void textWithDoc(@NotNull FileInfo fileInfo, int startLine, int endLine,
@NotNull ProgressIndicator indicator, @NotNull Consumer<String> func) {
boolean needFraction = indicator.getFraction() != 0;
int size = endLine - startLine;
@NotNull StringBuilder sb = new StringBuilder();
for (int i = startLine; i <= endLine; i++) {
@Nullable LineInfo lineInfo = LineInfo.of(fileInfo, i);
@@ -97,11 +101,15 @@ public class LineEnd extends EditorLinePainter {
sb.append(lineInfo.appSettings.lineEndPrefix).append(doc);
}
sb.append("\n");
indicator.setText2(i + " / " + size + " line");
if (needFraction) {
indicator.setFraction(1.0 * i / size);
}
}
if (sb.length() > 0) {
sb.delete(sb.length() - 1, sb.length());
}
return sb.toString();
func.accept(sb.toString());
}
private static @Nullable String lineDocSkipHave(@Nullable LineInfo lineInfo) {

View File

@@ -2,8 +2,9 @@ package io.github.linwancen.plugin.show;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.WriteCommandAction;
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 com.intellij.openapi.ui.popup.JBPopupFactory;
@@ -18,6 +19,8 @@ import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
/**
* on ProjectViewPopupMenu
*/
@@ -50,34 +53,52 @@ public class LineEndAdd extends DumbAwareAction {
}
@NotNull ListPopup confirmation = JBPopupFactory.getInstance().createConfirmation(
"Add Line Comment?", "Add and replace files!", "Don't add.",
() -> ApplicationManager.getApplication().runReadAction(() -> addDocAll(project, files)), 2);
() -> addDocAll(project, files), 2);
confirmation.showInFocusCenter();
}
private void addDocAll(@NotNull Project project, @NotNull VirtualFile[] files) {
@NotNull ArrayList<VirtualFile> list = new ArrayList<>();
for (@NotNull VirtualFile file : files) {
VfsUtilCore.visitChildrenRecursively(file, new VirtualFileVisitor<Void>() {
@Override
public boolean visitFile(@NotNull VirtualFile file) {
if (!file.isDirectory()) {
ApplicationManager.getApplication().runReadAction(() -> addDoc(project, file));
list.add(file);
}
return true;
}
});
}
if (list.isEmpty()) {
return;
}
new Task.Backgroundable(project, "Show LineEndAdd " + list.size() + " " + list.get(0)) {
@Override
public void run(@NotNull ProgressIndicator indicator) {
WriteCommandAction.runWriteCommandAction(project, () -> {
for (int i = 0; i < list.size(); i++) {
VirtualFile file = list.get(i);
if (file.exists()) {
indicator.setText(i + " / " + list.size() + " file");
indicator.setFraction(1.0 * i / list.size());
addDoc(project, file, indicator);
}
}
});
}
}.queue();
}
private void addDoc(@NotNull Project project, @NotNull VirtualFile file) {
private void addDoc(@NotNull Project project, @NotNull VirtualFile file, @NotNull ProgressIndicator indicator) {
@Nullable FileInfo fileInfo = FileInfo.of(file, project);
if (fileInfo == null) {
return;
}
int startLine = 0;
int endLine = fileInfo.document.getLineCount() - 1;
@NotNull String textWithDoc = LineEnd.textWithDoc(fileInfo, startLine, endLine);
WriteCommandAction.runWriteCommandAction(project, () ->
fileInfo.document.replaceString(0, fileInfo.document.getTextLength() - 1, textWithDoc)
LineEnd.textWithDoc(fileInfo, startLine, endLine, indicator, s ->
fileInfo.document.replaceString(0, fileInfo.document.getTextLength() - 1, s)
);
}
}

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();
}
}

View File

@@ -49,7 +49,7 @@ public class Tree implements ProjectViewNodeDecorator {
if (DumbService.isDumb(project)) {
return;
}
DumbService.getInstance(project).runReadActionInSmartMode(() ->
DumbService.getInstance(project).smartInvokeLater(() ->
ApplicationManager.getApplication().runReadAction(() -> {
@Nullable String doc = treeDoc(node, project);
if (doc == null) {
@@ -59,7 +59,13 @@ public class Tree implements ProjectViewNodeDecorator {
if (coloredText.isEmpty()) {
data.addText(data.getPresentableText(), SimpleTextAttributes.REGULAR_ATTRIBUTES);
}
data.addText(" " + doc, SimpleTextAttributes.GRAY_ATTRIBUTES);
@NotNull String text = " " + doc;
for (@NotNull ColoredFragment fragment : coloredText) {
if (text.equals(fragment.getText())) {
return;
}
}
data.addText(text, SimpleTextAttributes.GRAY_ATTRIBUTES);
}));
}

View File

@@ -117,23 +117,22 @@ public class ConfCache {
}
static void loadAll(@NotNull Project project) {
DumbService.getInstance(project).runReadActionInSmartMode(() ->
ApplicationManager.getApplication().runReadAction(() -> {
@NotNull Collection<VirtualFile> files = FilenameIndex.getAllFilesByExt(project, TsvLoader.EXT);
@NotNull StringBuilder sb = new StringBuilder();
for (@NotNull VirtualFile file : files) {
load(file);
sb.append(file.getName()).append("\n");
}
if (files.isEmpty()) {
return;
}
LOG.info("Ext doc conf load all complete {} files\n{}", files.size(), sb);
}));
DumbService.getInstance(project).smartInvokeLater(() -> {
@NotNull Collection<VirtualFile> files = FilenameIndex.getAllFilesByExt(project, TsvLoader.EXT);
@NotNull StringBuilder sb = new StringBuilder();
for (@NotNull VirtualFile file : files) {
load(file);
sb.append(file.getName()).append("\n");
}
if (files.isEmpty()) {
return;
}
LOG.info("Ext doc conf load all complete {} files\n{}", files.size(), sb);
});
}
static void loadFile(@NotNull VirtualFile file) {
ApplicationManager.getApplication().runReadAction(() -> ConfCache.load(file));
ApplicationManager.getApplication().invokeLater(() -> ConfCache.load(file));
}
private static void load(@NotNull VirtualFile file) {

View File

@@ -1,9 +1,9 @@
package io.github.linwancen.plugin.show.ext.conf;
import com.intellij.ide.projectView.ProjectView;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import io.github.linwancen.plugin.show.settings.ShowBundle;
import org.jetbrains.annotations.NotNull;
@@ -14,7 +14,7 @@ import org.slf4j.LoggerFactory;
/**
* call ConfCache.loadAll
*/
public class ReloadExtDocAction extends AnAction {
public class ReloadExtDocAction extends DumbAwareAction {
private static final Logger LOG = LoggerFactory.getLogger(ReloadExtDocAction.class);