Skip to content

Commit

Permalink
#226 Comment formatting - test, refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
krasa committed Jan 22, 2024
1 parent 8596658 commit 201ffb6
Show file tree
Hide file tree
Showing 11 changed files with 645 additions and 274 deletions.
1 change: 1 addition & 0 deletions EclipseAdapter/EclipseAdapter.iml
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@
<jarDirectory url="file://$MODULE_DIR$/lib" recursive="false" />
</library>
</orderEntry>
<orderEntry type="module" module-name="EclipseFormatter.main" />
</component>
</module>
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package krasa.formatter.adapter;

import com.intellij.pom.java.LanguageLevel;
import krasa.formatter.eclipse.EclipseFormatterAdapter;
import krasa.formatter.exception.FileDoesNotExistsException;
import krasa.formatter.exception.FormattingFailedException;
import krasa.formatter.settings.Settings;
import org.eclipse.jdt.core.formatter.CodeFormatter;
import org.eclipse.jdt.internal.formatter.DefaultCodeFormatter;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
Expand All @@ -24,12 +21,9 @@ public EclipseJavaFormatterAdapter(Map options) {
}

@Override
public String format(String text, int startOffset, int endOffset, LanguageLevel level)
public String format(int kind, String text, int startOffset, int length, int indentationLevel, String lineSeparator, String languageLevel)
throws FileDoesNotExistsException {
LOG.debug("#formatInternal");
if (endOffset > text.length()) {
endOffset = text.length();
}

IDocument doc = new Document();
try {
doc.set(text);
Expand Down Expand Up @@ -76,20 +70,15 @@ public String format(String text, int startOffset, int endOffset, LanguageLevel
* if offset is lower than 0, length is lower than 0 or length is greater than source length.
*/

LOG.debug("#starting to format by eclipse formatter");
TextEdit edit = defaultCodeFormatter.format(
CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, text, startOffset,
endOffset - startOffset, 0, Settings.LINE_SEPARATOR);
TextEdit edit = defaultCodeFormatter.format(kind, text, startOffset, length, indentationLevel, lineSeparator);
if (edit != null) {
LOG.debug("reformatting done");
edit.apply(doc);
} else {
throw new FormattingFailedException(getErrorMessage(level));
throw new FormattingFailedException(getErrorMessage(languageLevel));
}
return doc.get();
} catch (IndexOutOfBoundsException e) {
LOG.debug(e);
throw new FormattingFailedException(getErrorMessage(level));
throw new FormattingFailedException(e, getErrorMessage(languageLevel));
} catch (BadLocationException e) {
throw new RuntimeException(e);
}
Expand Down
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ repositories {
// Configure Gradle IntelliJ Plugin - read more: https://github.com/JetBrains/gradle-intellij-plugin
intellij {
// version.set("2023.1")
version.set("2022.3")
version.set("LATEST-EAP-SNAPSHOT")
// version.set("2022.3")
type.set("IC") // Target IDE Platform
plugins.set(listOf("java"))
}
Expand Down
Binary file modified lib/eclipse/adapter.jar
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package krasa.formatter.eclipse;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.pom.java.LanguageLevel;
import krasa.formatter.exception.FileDoesNotExistsException;

public abstract class EclipseFormatterAdapter {
protected final Logger LOG = Logger.getInstance(this.getClass().getName());

public abstract String format(String text, int startOffset, int endOffset, LanguageLevel level)
public abstract String format(int kind, String text, int startOffset, int length, int indentationLevel, String lineSeparator, String languageLevel)
throws FileDoesNotExistsException;

protected String getErrorMessage(LanguageLevel effectiveLanguageLevel) {
protected String getErrorMessage(String effectiveLanguageLevel) {
return "languageLevel=" + effectiveLanguageLevel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import krasa.formatter.settings.Settings;
import krasa.formatter.settings.provider.JavaPropertiesProvider;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jdt.core.formatter.CodeFormatter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -53,7 +54,14 @@ public JavaCodeFormatterFacade(Settings settings, Project project) {
public String format(String text, int startOffset, int endOffset, PsiFile psiFile)
throws FileDoesNotExistsException {
LanguageLevel languageLevel = getLanguageLevel(psiFile);
return getCodeFormatter(languageLevel, psiFile).format(text, startOffset, endOffset, languageLevel);
EclipseFormatterAdapter adapter = getCodeFormatter(languageLevel, psiFile);
int kind = CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS;
String lineSeparator = "\n";

if (endOffset > text.length()) {
endOffset = text.length();
}
return adapter.format(kind, text, startOffset, endOffset - startOffset, 0, lineSeparator, languageLevel.toString());
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package krasa.formatter.exception;

import com.intellij.openapi.diagnostic.Logger;
import krasa.formatter.plugin.InvalidPropertyFile;

/**
* @author Vojtech Krasa
*/
public class FormattingFailedException extends RuntimeException {
protected final Logger LOG = Logger.getInstance(this.getClass().getName());

private boolean userError;

public FormattingFailedException(String s) {
Expand All @@ -21,6 +24,12 @@ public FormattingFailedException(InvalidPropertyFile e) {
super(e);
}

public FormattingFailedException(Exception e, String errorMessage) {
super(errorMessage, e);
//todo hack
LOG.debug(e);
}

public boolean isUserError() {

return userError;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package krasa.formatter.eclipse;

import com.intellij.mock.MockApplication;
import com.intellij.openapi.command.impl.DummyProject;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.util.PsiUtilCore;
import krasa.formatter.settings.GlobalSettings;
import krasa.formatter.settings.Settings;
import org.apache.commons.io.FileUtils;
import org.jetbrains.annotations.NotNull;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.UnsupportedEncodingException;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -149,6 +154,24 @@ public void testFormatByXML() throws Exception {
Assert.assertEquals(FORMATTED2, output);
}

@Test
public void testFormatByXML_Formatter_20160606() throws Exception {
MockApplication app = MockApplication.setUp(Disposer.newDisposable());
app.registerService(GlobalSettings.class, new GlobalSettings());
GlobalSettings instance = GlobalSettings.getInstance();
instance.setPathToEclipse("C:/Users/vojtisek/eclipse/java-2023-03");

Settings settings = new Settings();
setPathToConfigFileJava(settings, "resources/Formatter_20160606.xml");
settings.setSelectedJavaProfile("Formatter_20160606");
settings.setEclipseVersion(Settings.FormatterVersion.CUSTOM);

eclipseCodeFormatterFacade = getEclipseCodeFormatterFacade(settings);
String input = FileUtils.readFileToString(new File("src/test/resources/example/Example.java"), "UTF-8");
String output = format(input);
Assert.assertEquals(input, output);
}

@Test
public void testFormatByEPF() throws Exception {
Settings settings = new Settings();
Expand Down
Loading

0 comments on commit 201ffb6

Please sign in to comment.