From 8ad087272a537a168ae2bb0897b2ff8a790344c2 Mon Sep 17 00:00:00 2001 From: haruka Date: Mon, 18 Sep 2023 13:35:43 -0700 Subject: [PATCH] Plugin doesn't work with if "Only VCS changed text" is selected from code-reformat settings (#386) Summary: Fixes facebook/ktfmt#228 The code reformat with "Only VCS changed text" falls back to the IDE Kotlin settings due to a missing override in KtfmtCodeStyleManager.java. This is also true of the popular "Save Actions" plugin which reformats with the same command. This change adds the missing override exactly as it appears in the reference project google/google-java-format at https://github.com/google/google-java-format/blob/d86e930de93f123994fba151a8d289b8035db87b/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatCodeStyleManager.java#L76 Confirmed to fix the issue on Android Studio 2021.1.1 with the manual code-reformat and in the Save Actions plugin. Pull Request resolved: https://github.com/facebook/ktfmt/pull/386 Reviewed By: strulovich Differential Revision: D49322592 Pulled By: hick209 fbshipit-source-id: 99d56d428603292ffb6efb6551e7fa619367c0bd --- .../ktfmt/intellij/KtfmtCodeStyleManager.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/ktfmt_idea_plugin/src/main/java/com/facebook/ktfmt/intellij/KtfmtCodeStyleManager.java b/ktfmt_idea_plugin/src/main/java/com/facebook/ktfmt/intellij/KtfmtCodeStyleManager.java index d2184ea1..7594050a 100644 --- a/ktfmt_idea_plugin/src/main/java/com/facebook/ktfmt/intellij/KtfmtCodeStyleManager.java +++ b/ktfmt_idea_plugin/src/main/java/com/facebook/ktfmt/intellij/KtfmtCodeStyleManager.java @@ -26,10 +26,13 @@ import com.intellij.psi.PsiDocumentManager; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; +import com.intellij.psi.codeStyle.ChangedRangesInfo; import com.intellij.psi.codeStyle.CodeStyleManager; import com.intellij.psi.impl.CheckUtil; import com.intellij.util.IncorrectOperationException; +import java.util.ArrayList; import java.util.Collection; +import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.TreeMap; @@ -66,6 +69,17 @@ public void reformatText(PsiFile file, Collection ranges) } } + @Override + public void reformatTextWithContext(@NotNull PsiFile file, @NotNull ChangedRangesInfo info) + throws IncorrectOperationException { + List ranges = new ArrayList<>(); + if (info.insertedRanges != null) { + ranges.addAll(info.insertedRanges); + } + ranges.addAll(info.allChangedRanges); + reformatTextWithContext(file, ranges); + } + @Override public void reformatTextWithContext(PsiFile file, Collection ranges) { if (overrideFormatterForFile(file)) {