Skip to content
Open
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
1 change: 1 addition & 0 deletions META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
</description>

<change-notes><![CDATA[
1.7: Add Kotlin support<br>
1.6: Fix NPE on Android Studio 2.3 / Update ButterKnife annotation<br>
1.5: Use JDK 1.6 for build.<br>
1.4: Quick-fix for NullPointerException.<br>
Expand Down
15 changes: 13 additions & 2 deletions src/jp/funnything/offing_harbor/ConvertAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataKeys;
import com.intellij.openapi.vfs.VirtualFile;
import org.apache.commons.io.IOUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -74,10 +74,21 @@ private String extractLayoutFileNameFromJavaFile(VirtualFile file) {
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
IOUtils.closeQuietly(reader);
closeQuietly(reader);
}
}

private static void closeQuietly(Reader input) {
try {
if (input != null) {
input.close();
}
} catch (IOException ioe) {
// ignore
}
}


/**
* 基準ファイルから子を優先して探索する
*/
Expand Down
5 changes: 2 additions & 3 deletions src/jp/funnything/offing_harbor/ConvertConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ public boolean willModify() {
}

public enum ConvertFormat {
PLAIN, ANDROID_ANNOTATIONS, BUTTER_KNIFE;
PLAIN, ANDROID_ANNOTATIONS, BUTTER_KNIFE, KOTLIN;

public boolean requireAssignMethod() {
return this == PLAIN;
return this == PLAIN || this == KOTLIN;
}
}

Expand All @@ -35,4 +35,3 @@ public ConvertConfig() {
useSmartType = false;
}
}

13 changes: 13 additions & 0 deletions src/jp/funnything/offing_harbor/ConvertConfigDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class ConvertConfigDialog extends DialogWrapper {
private JRadioButton mFormatPlainRadioButton;
private JRadioButton mFormatAndroidAnnotationsRadioButton;
private JRadioButton mFormatButterKnifeRadioButton;
private JRadioButton mFormatKotlinRadioButton;
private JRadioButton mVisibilityPrivate;
private JRadioButton mVisibilityPackagePrivate;
private JRadioButton mVisibilityProtected;
Expand Down Expand Up @@ -83,6 +84,7 @@ protected JComponent createCenterPanel() {
mFormatPlainRadioButton = new JRadioButton("for plain Android frameworks");
mFormatAndroidAnnotationsRadioButton = new JRadioButton("for AndroidAnnotations");
mFormatButterKnifeRadioButton = new JRadioButton("for ButterKnife");
mFormatKotlinRadioButton = new JRadioButton("for Kotlin");

ActionListener formatChangeListener = new ActionListener() {
@Override
Expand All @@ -93,22 +95,27 @@ public void actionPerformed(ActionEvent actionEvent) {
applyVisibilityConstraint(ConvertConfig.ConvertFormat.ANDROID_ANNOTATIONS);
} else if (actionEvent.getSource() == mFormatButterKnifeRadioButton) {
applyVisibilityConstraint(ConvertConfig.ConvertFormat.BUTTER_KNIFE);
} else if (actionEvent.getSource() == mFormatKotlinRadioButton) {
applyVisibilityConstraint(ConvertConfig.ConvertFormat.KOTLIN);
}
}
};

mFormatPlainRadioButton.addActionListener(formatChangeListener);
mFormatAndroidAnnotationsRadioButton.addActionListener(formatChangeListener);
mFormatButterKnifeRadioButton.addActionListener(formatChangeListener);
mFormatKotlinRadioButton.addActionListener(formatChangeListener);

ButtonGroup formatButtonGroup = new ButtonGroup();
formatButtonGroup.add(mFormatPlainRadioButton);
formatButtonGroup.add(mFormatAndroidAnnotationsRadioButton);
formatButtonGroup.add(mFormatButterKnifeRadioButton);
formatButtonGroup.add(mFormatKotlinRadioButton);

formatBox.add(mFormatPlainRadioButton);
formatBox.add(mFormatAndroidAnnotationsRadioButton);
formatBox.add(mFormatButterKnifeRadioButton);
formatBox.add(mFormatKotlinRadioButton);

box.add(Box.createHorizontalStrut(5));
box.add(formatBox);
Expand Down Expand Up @@ -204,6 +211,8 @@ private ConvertConfig.ConvertFormat getFormat() {
return ConvertConfig.ConvertFormat.ANDROID_ANNOTATIONS;
} else if (mFormatButterKnifeRadioButton.isSelected()) {
return ConvertConfig.ConvertFormat.BUTTER_KNIFE;
} else if (mFormatKotlinRadioButton.isSelected()) {
return ConvertConfig.ConvertFormat.KOTLIN;
} else {
throw new IllegalStateException("assert");
}
Expand All @@ -220,6 +229,9 @@ private void setFormat(ConvertConfig.ConvertFormat format) {
case BUTTER_KNIFE:
mFormatButterKnifeRadioButton.setSelected(true);
break;
case KOTLIN:
mFormatKotlinRadioButton.setSelected(true);
break;
default:
throw new IllegalStateException("assert");
}
Expand All @@ -228,6 +240,7 @@ private void setFormat(ConvertConfig.ConvertFormat format) {
private void applyVisibilityConstraint(ConvertConfig.ConvertFormat format) {
switch (format) {
case PLAIN:
case KOTLIN:
mVisibilityPrivate.setEnabled(true);
mVisibilityPackagePrivate.setEnabled(true);
mVisibilityProtected.setEnabled(true);
Expand Down
35 changes: 29 additions & 6 deletions src/jp/funnything/offing_harbor/ConvertExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import com.intellij.psi.search.SearchScope;
import com.intellij.psi.search.searches.ClassInheritorsSearch;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.io.IOUtils;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
Expand Down Expand Up @@ -145,7 +144,7 @@ public void execute(Project project, VirtualFile file, ConvertConfig config) {
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
IOUtils.closeQuietly(is);
closeQuietly(is);
}

Tree viewNameTree = config.useSmartType ? prepareViewNames(project) : null;
Expand All @@ -157,6 +156,16 @@ public void execute(Project project, VirtualFile file, ConvertConfig config) {
Notifications.Bus.notify(new Notification("OffingHarbor", "OffingHarbor", "Code is copied to clipboard", NotificationType.INFORMATION), project);
}

private static void closeQuietly(InputStream input) {
try {
if (input != null) {
input.close();
}
} catch (IOException ioe) {
// ignore
}
}

private List<AndroidViewInfo> extractViewInfos(InputStream is) {
try {
return traverseViewInfos(DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is));
Expand Down Expand Up @@ -219,7 +228,11 @@ private String generateJavaCode(List<AndroidViewInfo> infos, Tree viewNameTree,
throw new IllegalStateException("assert");
}

methodJavaCode.append("private void assignViews() {" + NL);
if (config.format == ConvertConfig.ConvertFormat.KOTLIN) {
methodJavaCode.append("private fun assignViews() {" + NL);
} else {
methodJavaCode.append("private void assignViews() {" + NL);
}

for (AndroidViewInfo info : infos) {
String type = config.useSmartType ? info.findOptimalType(viewNameTree) : info.type;
Expand All @@ -234,14 +247,24 @@ private String generateJavaCode(List<AndroidViewInfo> infos, Tree viewNameTree,
} else if (config.format == ConvertConfig.ConvertFormat.BUTTER_KNIFE) {
// Butter Knife always requires resource-id
fieldJavaCode.append(String.format("@BindView(R.id.%s)" + NL + "%s%s %s;" + NL, info.id, visibility, type, symbol));
} else if (config.format == ConvertConfig.ConvertFormat.KOTLIN) {
fieldJavaCode.append(String.format("%slateinit var %s: %s" + NL, visibility, symbol, type));
} else {
fieldJavaCode.append(String.format("%s%s %s;" + NL, visibility, type, symbol));
}

if (type.equals("View")) {
methodJavaCode.append(String.format(" %s = findViewById(R.id.%s);" + NL, symbol, info.id));
if (config.format == ConvertConfig.ConvertFormat.KOTLIN) {
if (type.equals("View")) {
methodJavaCode.append(String.format(" %s = findViewById(R.id.%s)" + NL, symbol, info.id));
} else {
methodJavaCode.append(String.format(" %s = findViewById(R.id.%s) as %s" + NL, symbol, info.id, type));
}
} else {
methodJavaCode.append(String.format(" %s = (%s) findViewById(R.id.%s);" + NL, symbol, type, info.id));
if (type.equals("View")) {
methodJavaCode.append(String.format(" %s = findViewById(R.id.%s);" + NL, symbol, info.id));
} else {
methodJavaCode.append(String.format(" %s = (%s) findViewById(R.id.%s);" + NL, symbol, type, info.id));
}
}
}

Expand Down