|
| 1 | +package pl.thedeem.intellij.dpl.style; |
| 2 | + |
| 3 | +import com.intellij.lang.injection.InjectedLanguageManager; |
| 4 | +import com.intellij.openapi.editor.Document; |
| 5 | +import com.intellij.openapi.project.Project; |
| 6 | +import com.intellij.openapi.util.Pair; |
| 7 | +import com.intellij.openapi.util.TextRange; |
| 8 | +import com.intellij.psi.*; |
| 9 | +import com.intellij.psi.codeStyle.CodeStyleManager; |
| 10 | +import com.intellij.psi.codeStyle.CodeStyleSettings; |
| 11 | +import com.intellij.psi.impl.source.codeStyle.PostFormatProcessor; |
| 12 | +import com.intellij.psi.util.PsiTreeUtil; |
| 13 | +import org.jetbrains.annotations.NotNull; |
| 14 | + |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.Collection; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +public class DQLPostFormatProcessor implements PostFormatProcessor { |
| 20 | + |
| 21 | + @Override |
| 22 | + public @NotNull PsiElement processElement(@NotNull PsiElement element, |
| 23 | + @NotNull CodeStyleSettings settings) { |
| 24 | + return element; |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public @NotNull TextRange processText(@NotNull PsiFile hostFile, |
| 29 | + @NotNull TextRange range, |
| 30 | + @NotNull CodeStyleSettings settings) { |
| 31 | + Project project = hostFile.getProject(); |
| 32 | + InjectedLanguageManager injector = InjectedLanguageManager.getInstance(project); |
| 33 | + CodeStyleManager styleManager = CodeStyleManager.getInstance(project); |
| 34 | + |
| 35 | + Collection<PsiFile> injectedFiles = findInjectionHosts(hostFile, injector, range); |
| 36 | + final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project); |
| 37 | + if (!injectedFiles.isEmpty()) { |
| 38 | + for (PsiFile host : injectedFiles) { |
| 39 | + PsiFile updated = PsiFileFactory.getInstance(project).createFileFromText(host.getLanguage(), host.getText()); |
| 40 | + styleManager.reformat(updated); |
| 41 | + String text = updated.getText(); |
| 42 | + |
| 43 | + Document d = documentManager.getDocument(host); |
| 44 | + if (d != null) { |
| 45 | + d.setText(text); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + return range; |
| 50 | + } |
| 51 | + |
| 52 | + private @NotNull Collection<PsiFile> findInjectionHosts(@NotNull PsiFile file, @NotNull InjectedLanguageManager injector, @NotNull TextRange range) { |
| 53 | + PsiLanguageInjectionHost injectionHost = injector.getInjectionHost(file); |
| 54 | + List<PsiFile> result = new ArrayList<>(); |
| 55 | + Collection<PsiLanguageInjectionHost> hosts; |
| 56 | + if (injectionHost == null) { |
| 57 | + hosts = PsiTreeUtil.findChildrenOfType(file, PsiLanguageInjectionHost.class); |
| 58 | + } else { |
| 59 | + hosts = List.of(injectionHost); |
| 60 | + } |
| 61 | + for (PsiLanguageInjectionHost host : hosts) { |
| 62 | + List<Pair<PsiElement, TextRange>> files = injector.getInjectedPsiFiles(host); |
| 63 | + if (files != null) { |
| 64 | + for (Pair<PsiElement, TextRange> iFile : files) { |
| 65 | + if (iFile.getSecond().intersects(range)) { |
| 66 | + result.add(iFile.getFirst().getContainingFile()); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return result; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public boolean isWhitespaceOnly() { |
| 77 | + return false; |
| 78 | + } |
| 79 | +} |
0 commit comments