Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(codeStyle): inhibit .cdsprettier.json creation #114

Merged
merged 3 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import com.intellij.application.options.CodeStyle;
import com.intellij.openapi.components.Service;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.codeStyle.CodeStyleSettingsChangeEvent;
import com.intellij.psi.codeStyle.CodeStyleSettingsListener;
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
import com.sap.cap.cds.intellij.CdsFileType;
import org.jetbrains.annotations.NotNull;
import org.json.JSONException;

Expand All @@ -20,6 +22,7 @@
import static com.sap.cap.cds.intellij.util.JsonUtil.isJsonEqual;
import static com.sap.cap.cds.intellij.util.Logger.logger;
import static java.nio.file.Files.readString;
import static java.util.Arrays.stream;

@Service(Service.Level.PROJECT)
public final class CdsCodeStyleSettingsService {
Expand All @@ -36,7 +39,9 @@ public CdsCodeStyleSettingsService(Project project) {
CodeStyleSettingsManager.getInstance(project).subscribe(new CodeStyleSettingsListener() {
@Override
public void codeStyleSettingsChanged(@NotNull CodeStyleSettingsChangeEvent event) {
updateSettingsFile();
if (shouldBeSavedImmediately()) {
updateSettingsFile();
}
}
});
}
Expand All @@ -45,6 +50,16 @@ public void codeStyleSettingsChanged(@NotNull CodeStyleSettingsChangeEvent event
return CodeStyle.getDefaultSettings().getCustomSettings(CdsCodeStyleSettings.class);
}

private boolean shouldBeSavedImmediately() {
if (isSettingsFilePresent()) {
return true;
}

FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
return stream(fileEditorManager.getOpenFiles())
.anyMatch(file -> CdsFileType.EXTENSION.equalsIgnoreCase(file.getExtension()));
}

public boolean isSettingsFilePresent() {
return prettierJsonManager.isJsonFilePresent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.intellij.platform.lsp.api.lsWidget.LspServerWidgetItem;
import com.sap.cap.cds.intellij.CdsFileType;
import com.sap.cap.cds.intellij.CdsIcons;
import com.sap.cap.cds.intellij.codestyle.CdsCodeStyleSettingsService;
import com.sap.cap.cds.intellij.lang.CdsLanguage;
import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.NotNull;
Expand All @@ -18,6 +19,7 @@ public void fileOpened(@NotNull Project project, @NotNull VirtualFile virtualFil
if (!CdsFileType.EXTENSION.equals(virtualFile.getExtension())) {
return;
}
project.getService(CdsCodeStyleSettingsService.class).updateSettingsFile();
lspServerStarter.ensureServerStarted(new CdsLspServerDescriptor(project, CdsLanguage.LABEL));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ class ProjectLifecycleListener : ProjectActivity {
val service = project.getService(CdsCodeStyleSettingsService::class.java)
if (service.isSettingsFilePresent) {
service.updateProjectSettingsFromFile()
} else {
service.updateSettingsFile()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,37 @@ public void testInvalidPrettierJson() {

// Direction settings → .cdsprettier.json

// TODO test project open with non-project settings → no .cdsprettier.json

public void testProjectOpenedWithDefaultSettings_noCdsPrettierJsonCreation() throws IOException {
openProject();
assertFalse(prettierJson.exists());
}

public void testSettingChanged() throws IOException {
public void testSettingChanged_createsPrettierJsonOnlyIfCdsEditorOpen() throws IOException {
CodeStyleSettingsManager codeStyleSettingsManager = CodeStyleSettingsManager.getInstance(project);

openProject();
getCdsCodeStyleSettings().tabSize = 42;
CodeStyleSettingsManager.getInstance(project).notifyCodeStyleSettingsChanged();
codeStyleSettingsManager.notifyCodeStyleSettingsChanged();
assertFalse(prettierJson.exists());

createCdsFile();
openCdsFile();
codeStyleSettingsManager.notifyCodeStyleSettingsChanged();
assertTrue(prettierJson.exists());
assertEquals(42, new JSONObject(readPrettierJson()).get("tabSize"));
}

public void testSettingChangedWithExistingCdsPrettierJson_updatesFile() throws IOException {
openProject();
createPrettierJson();
writePrettierJson("{}");
getCdsCodeStyleSettings().tabSize = 42;
CodeStyleSettingsManager.getInstance(project).notifyCodeStyleSettingsChanged();
assertEquals(42, new JSONObject(readPrettierJson()).get("tabSize"));
}

public void testWriteOnlyLoadedOrNonDefaultOptions() throws Exception {
createPrettierJson();
writePrettierJson("{ \"tabSize\": %d, \"alignAs\": %b }".formatted(defaults.tabSize, !defaults.alignAs));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.intellij.application.options.CodeStyle;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
Expand All @@ -23,6 +24,9 @@

@SuppressWarnings("NewClassNamingConvention")
public class CdsCodeStyleSettingsServiceTestBase extends HeavyPlatformTestCase {

private static final String SOME_CDS = "a.cds";

protected CdsCodeStyleSettings defaults;
protected Project project;
protected File prettierJson;
Expand Down Expand Up @@ -69,6 +73,14 @@ protected void createPrettierJson() {
}
}

protected void createCdsFile() {
try {
WriteAction.computeAndWait(() -> projectDirVFile.findOrCreateChildData(this, SOME_CDS));
} catch (IOException e) {
throw new RuntimeException(e);
}
}

protected void deletePrettierJson() throws IOException {
refreshVfsForFile(prettierJson, false);
WriteAction.runAndWait(() -> getVFile(prettierJson).delete(this));
Expand Down Expand Up @@ -112,6 +124,11 @@ protected void openProject() {
});
}

protected void openCdsFile() {
FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
fileEditorManager.openFile(getVFile(projectDir.resolve(SOME_CDS).toFile()), false);
}

@NotNull
protected CdsCodeStyleSettings getCdsCodeStyleSettings() {
return CodeStyle.getSettings(project).getCustomSettings(CdsCodeStyleSettings.class);
Expand Down
Loading