|
| 1 | +package net.seesharpsoft.intellij.plugins.csv.intention; |
| 2 | + |
| 3 | +import com.intellij.openapi.editor.Document; |
| 4 | +import com.intellij.openapi.project.Project; |
| 5 | +import com.intellij.openapi.util.TextRange; |
| 6 | +import com.intellij.psi.PsiDocumentManager; |
| 7 | +import com.intellij.psi.PsiElement; |
| 8 | +import net.seesharpsoft.intellij.plugins.csv.CsvColumnInfo; |
| 9 | +import net.seesharpsoft.intellij.plugins.csv.psi.CsvFile; |
| 10 | +import org.jetbrains.annotations.NotNull; |
| 11 | + |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +public abstract class CsvShiftColumnIntentionAction extends CsvIntentionAction { |
| 15 | + |
| 16 | + protected CsvShiftColumnIntentionAction(String text) { |
| 17 | + super(text); |
| 18 | + } |
| 19 | + |
| 20 | + protected static void changeLeftAndRightColumnOrder(@NotNull Project project, CsvFile psiFile, CsvColumnInfo<PsiElement> leftColumnInfo, CsvColumnInfo<PsiElement> rightColumnInfo) { |
| 21 | + Document document = PsiDocumentManager.getInstance(project).getDocument(psiFile); |
| 22 | + document.setText(changeLeftAndRightColumnOrder(document.getText(), leftColumnInfo, rightColumnInfo)); |
| 23 | + } |
| 24 | + |
| 25 | + @NotNull |
| 26 | + protected static String changeLeftAndRightColumnOrder(String text, CsvColumnInfo<PsiElement> leftColumnInfo, CsvColumnInfo<PsiElement> rightColumnInfo) { |
| 27 | + List<PsiElement> rightElements = rightColumnInfo.getElements(); |
| 28 | + List<PsiElement> leftElements = leftColumnInfo.getElements(); |
| 29 | + int lastIndex = 0, maxRows = leftElements.size(); |
| 30 | + StringBuilder newText = new StringBuilder(); |
| 31 | + |
| 32 | + for (int row = 0; row < maxRows; ++row) { |
| 33 | + PsiElement leftElement = leftElements.get(row); |
| 34 | + if (leftElement == null) { |
| 35 | + continue; |
| 36 | + } |
| 37 | + PsiElement rightElement = rightElements.size() > row ? rightElements.get(row) : null; |
| 38 | + |
| 39 | + TextRange leftSeparator = findPreviousSeparatorOrCRLF(leftElement); |
| 40 | + TextRange middleSeparator = findNextSeparatorOrCRLF(leftElement); |
| 41 | + TextRange rightSeparator = rightElement == null ? |
| 42 | + TextRange.create(middleSeparator.getEndOffset(), middleSeparator.getEndOffset()) : |
| 43 | + findNextSeparatorOrCRLF(rightElement); |
| 44 | + |
| 45 | + newText.append(text.substring(lastIndex, leftSeparator.getEndOffset())) |
| 46 | + .append(text.substring(middleSeparator.getEndOffset(), rightSeparator.getStartOffset())) |
| 47 | + .append(",") |
| 48 | + .append(text.substring(leftSeparator.getEndOffset(), middleSeparator.getStartOffset())); |
| 49 | + |
| 50 | + lastIndex = rightSeparator.getStartOffset(); |
| 51 | + } |
| 52 | + newText.append(text.substring(lastIndex)); |
| 53 | + return newText.toString(); |
| 54 | + } |
| 55 | + |
| 56 | + protected static TextRange findPreviousSeparatorOrCRLF(PsiElement psiElement) { |
| 57 | + TextRange textRange; |
| 58 | + PsiElement separator = CsvIntentionHelper.getPreviousSeparator(psiElement); |
| 59 | + if (separator == null) { |
| 60 | + separator = CsvIntentionHelper.getPreviousCRLF(psiElement.getParent()); |
| 61 | + if (separator == null) { |
| 62 | + separator = psiElement.getParent().getParent().getFirstChild(); |
| 63 | + textRange = TextRange.create(separator.getTextRange().getStartOffset(), separator.getTextRange().getStartOffset()); |
| 64 | + } else { |
| 65 | + textRange = separator.getTextRange(); |
| 66 | + } |
| 67 | + } else { |
| 68 | + textRange = separator.getTextRange(); |
| 69 | + } |
| 70 | + return textRange; |
| 71 | + } |
| 72 | + |
| 73 | + protected static TextRange findNextSeparatorOrCRLF(PsiElement psiElement) { |
| 74 | + TextRange textRange; |
| 75 | + PsiElement separator = CsvIntentionHelper.getNextSeparator(psiElement); |
| 76 | + if (separator == null) { |
| 77 | + separator = CsvIntentionHelper.getNextCRLF(psiElement.getParent()); |
| 78 | + if (separator == null) { |
| 79 | + separator = psiElement.getParent().getParent().getLastChild(); |
| 80 | + textRange = TextRange.create(separator.getTextRange().getEndOffset(), separator.getTextRange().getEndOffset()); |
| 81 | + } else { |
| 82 | + textRange = TextRange.create(separator.getTextRange().getStartOffset(), separator.getTextRange().getStartOffset()); |
| 83 | + } |
| 84 | + } else { |
| 85 | + textRange = separator.getTextRange(); |
| 86 | + } |
| 87 | + return textRange; |
| 88 | + } |
| 89 | +} |
0 commit comments