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

Add Google and Kotlinlang support to IDEA plugin #183

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion ktfmt_idea_plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ plugins {
java
}

val ktfmtVersion = "0.20"
val ktfmtVersion = "0.21"

group = "com.facebook"
version = "1.1-SNAPSHOT.$ktfmtVersion"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@

package com.facebook.ktfmt.intellij;

import static com.facebook.ktfmt.FormatterKt.DROPBOX_FORMAT;

import com.facebook.ktfmt.FormatterKt;
import com.facebook.ktfmt.FormattingOptions;
import com.facebook.ktfmt.ParseError;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableMap;
import com.google.googlejavaformat.java.FormatterException;
import com.intellij.openapi.util.TextRange;

import java.util.Map;

final class FormatterUtil {
Expand All @@ -36,16 +34,18 @@ private FormatterUtil() {}
*
* @return formatted code
*/
static Map<TextRange, String> getReplacements(boolean isDropboxStyle, String code) {
static Map<TextRange, String> getReplacements(UiFormatterStyle uiFormatterStyle, String code) {
try {
return ImmutableMap.of(TextRange.allOf(code), formatCode(isDropboxStyle, code));
return ImmutableMap.of(TextRange.allOf(code), formatCode(uiFormatterStyle, code));
} catch (FormatterException | ParseError e) {
return ImmutableMap.of();
}
}

@VisibleForTesting
static String formatCode(boolean isDropboxStyle, String code) throws FormatterException {
return FormatterKt.format(isDropboxStyle ? DROPBOX_FORMAT : new FormattingOptions(), code);
static String formatCode(UiFormatterStyle uiFormatterStyle, String code)
throws FormatterException {

return FormatterKt.format(uiFormatterStyle.getFormattingOptions(), code);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.facebook.ktfmt.intellij;

import static java.util.Comparator.comparing;

import com.google.common.collect.ImmutableList;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.WriteCommandAction;
Expand All @@ -29,12 +27,15 @@
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.impl.CheckUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.idea.KotlinFileType;

import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.idea.KotlinFileType;

import static java.util.Comparator.comparing;

/**
* A {@link CodeStyleManager} implementation which formats .kt files with ktfmt. Formatting of all
Expand Down Expand Up @@ -92,7 +93,9 @@ public PsiElement reformatRange(
}
}

/** Return whether or not this formatter can handle formatting the given file. */
/**
* Return whether or not this formatter can handle formatting the given file.
*/
private boolean overrideFormatterForFile(PsiFile file) {
return KotlinFileType.INSTANCE.getName().equals(file.getFileType().getName())
&& KtfmtSettings.getInstance(getProject()).isEnabled();
Expand Down Expand Up @@ -125,10 +128,10 @@ private void formatInternal(PsiFile file, Collection<TextRange> ranges) {
* formatter (usually using {@link #performReplacements(Document, Map)}.
*/
private void format(Document document, Collection<TextRange> ranges) {
boolean isDropboxStyle = KtfmtSettings.getInstance(getProject()).getIsDropboxStyle();
UiFormatterStyle uiFormatterStyle = KtfmtSettings.getInstance(getProject()).getUiFormatterStyle();

performReplacements(
document, FormatterUtil.getReplacements(isDropboxStyle, document.getText()));
document, FormatterUtil.getReplacements(uiFormatterStyle, document.getText()));
}

private void performReplacements(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;
import java.awt.Insets;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.awt.Insets;

public class KtfmtConfigurable extends BaseConfigurable implements SearchableConfigurable {

Expand Down Expand Up @@ -80,7 +81,7 @@ public JComponent createComponent() {
public void apply() throws ConfigurationException {
KtfmtSettings settings = KtfmtSettings.getInstance(project);
settings.setEnabled(enable.isSelected() ? EnabledState.ENABLED : getDisabledState());
settings.setDropboxStyle(((UiFormatterStyle) styleComboBox.getSelectedItem()).convert());
settings.setUiFormatterStyle(((UiFormatterStyle) styleComboBox.getSelectedItem()));
}

private EnabledState getDisabledState() {
Expand All @@ -95,16 +96,14 @@ private EnabledState getDisabledState() {
public void reset() {
KtfmtSettings settings = KtfmtSettings.getInstance(project);
enable.setSelected(settings.isEnabled());
styleComboBox.setSelectedItem(UiFormatterStyle.convert(settings.getIsDropboxStyle()));
styleComboBox.setSelectedItem(settings.getUiFormatterStyle());
}

@Override
public boolean isModified() {
KtfmtSettings settings = KtfmtSettings.getInstance(project);
return enable.isSelected() != settings.isEnabled()
|| !styleComboBox
.getSelectedItem()
.equals(UiFormatterStyle.convert(settings.getIsDropboxStyle()));
|| !styleComboBox.getSelectedItem().equals(settings.getUiFormatterStyle());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ boolean isUninitialized() {
return state.enabled.equals(EnabledState.UNKNOWN);
}

boolean getIsDropboxStyle() {
return state.isDropboxStyle;
UiFormatterStyle getUiFormatterStyle() {
return state.uiFormatterStyle;
}

void setDropboxStyle(boolean isDropboxStyle) {
state.isDropboxStyle = isDropboxStyle;
void setUiFormatterStyle(UiFormatterStyle uiFormatterStyle) {
state.uiFormatterStyle = uiFormatterStyle;
}

enum EnabledState {
Expand All @@ -78,7 +78,7 @@ enum EnabledState {
static class State {

private EnabledState enabled = EnabledState.UNKNOWN;
public boolean isDropboxStyle = false;
public UiFormatterStyle uiFormatterStyle = UiFormatterStyle.DEFAULT;

// enabled used to be a boolean so we use bean property methods for backwards
// compatibility
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,33 @@

package com.facebook.ktfmt.intellij;

import java.util.Arrays;
import java.util.Objects;
import com.facebook.ktfmt.FormattingOptions;

import static com.facebook.ktfmt.FormatterKt.DROPBOX_FORMAT;
import static com.facebook.ktfmt.FormatterKt.GOOGLE_FORMAT;
import static com.facebook.ktfmt.FormatterKt.KOTLINLANG_FORMAT;

/** Configuration options for the formatting style. */
enum UiFormatterStyle {
DEFAULT("Default ktfmt style", false),
DROPBOX("Dropbox style", true);
DEFAULT("Default", new FormattingOptions()),
DROPBOX("Dropbox", DROPBOX_FORMAT),
GOOGLE("Google (internal)", GOOGLE_FORMAT),
KOTLINLANG("Kotlinlang", KOTLINLANG_FORMAT);

private final String description;
private final boolean isDropboxStyle;
private final FormattingOptions formattingOptions;

UiFormatterStyle(String description, boolean isDropboxStyle) {
JavierSegoviaCordoba marked this conversation as resolved.
Show resolved Hide resolved
UiFormatterStyle(String description, FormattingOptions formattingOptions) {
this.description = description;
this.isDropboxStyle = isDropboxStyle;
this.formattingOptions = formattingOptions;
}

FormattingOptions getFormattingOptions() {
return formattingOptions;
}

@Override
public String toString() {
return description;
}

public boolean convert() {
return isDropboxStyle;
}

static UiFormatterStyle convert(boolean isDropboxStyle) {
return Arrays.stream(UiFormatterStyle.values())
.filter(value -> Objects.equals(value.isDropboxStyle, isDropboxStyle))
.findFirst()
.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@

package com.facebook.ktfmt.intellij;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import static org.junit.Assert.assertEquals;

@RunWith(JUnit4.class)
public class FormatterUtilTest {
@Test
public void getReplacements() throws Exception {
String code = "val a = 5";
String expected = "val a = 5\n";
String actual = FormatterUtil.formatCode(false, code);
String actual = FormatterUtil.formatCode(UiFormatterStyle.DEFAULT, code);

assertEquals(expected, actual);
}
Expand Down