Skip to content

Commit 7ceb328

Browse files
Introducing reformatting inside the injected DPL & JSON hosts
1 parent cbe9d66 commit 7ceb328

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
- The support is also automatically added to the DQL `parse` command
1010
- Customizable syntax highlighting
1111
- Local code inspections with quick fixes if possible
12-
- Customizable code style support — enforcing indents, line breaking and spaces around elements (only in separate
13-
files)
12+
- Customizable code style support — enforcing indents, line breaking and spaces around elements
1413
- Advanced code completion
1514
- Hover documentation
1615
- Useful intentions for managing DPL expressions
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
}

src/main/resources/META-INF/plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@
367367
implementation="pl.thedeem.intellij.dpl.style.DPLCodeStyleSettingsProvider"/>
368368
<langCodeStyleSettingsProvider
369369
implementation="pl.thedeem.intellij.dpl.style.DPLLanguageCodeStyleSettingsProvider"/>
370+
<postFormatProcessor implementation="pl.thedeem.intellij.dpl.style.DQLPostFormatProcessor"/>
370371

371372
<!-- References -->
372373
<psi.referenceContributor

0 commit comments

Comments
 (0)