├── .idea ├── .name ├── copyright │ └── profiles_settings.xml ├── vcs.xml ├── modules.xml ├── compiler.xml ├── misc.xml └── modules │ └── android-postfix-plugin_main.iml ├── settings.gradle ├── src └── main │ ├── resources │ ├── postfixTemplates │ │ ├── LogTemplate │ │ │ ├── description.html │ │ │ ├── before.java.template │ │ │ └── after.java.template │ │ ├── SnackbarTemplate │ │ │ ├── description.html │ │ │ ├── before.java.template │ │ │ └── after.java.template │ │ ├── GoneTemplate │ │ │ ├── description.html │ │ │ ├── before.java.template │ │ │ └── after.java.template │ │ ├── ToastTemplate │ │ │ ├── description.html │ │ │ ├── before.java.template │ │ │ └── after.java.template │ │ ├── InvisibleTemplate │ │ │ ├── description.html │ │ │ ├── before.java.template │ │ │ └── after.java.template │ │ ├── VisibleTemplate │ │ │ ├── description.html │ │ │ ├── before.java.template │ │ │ └── after.java.template │ │ ├── LogDTemplate │ │ │ ├── before.java.template │ │ │ ├── description.html │ │ │ └── after.java.template │ │ ├── FindViewByIdTemplate │ │ │ ├── description.html │ │ │ ├── before.java.template │ │ │ └── after.java.template │ │ ├── FindViewByIdFieldTemplate │ │ │ ├── description.html │ │ │ ├── before.java.template │ │ │ └── after.java.template │ │ ├── VisibleGoneTemplate │ │ │ ├── before.java.template │ │ │ ├── description.html │ │ │ └── after.java.template │ │ ├── FindViewByIdVariableTemplate │ │ │ ├── description.html │ │ │ ├── before.java.template │ │ │ └── after.java.template │ │ └── TextUtilsIsEmptyTemplate │ │ │ ├── before.java.template │ │ │ ├── description.html │ │ │ └── after.java.template │ └── META-INF │ │ └── plugin.xml │ └── java │ └── com │ └── kogitune │ └── intellij │ └── codeinsight │ └── postfix │ ├── templates │ └── surround │ │ ├── GoneTemplate.java │ │ ├── VisibleTemplate.java │ │ ├── InvisibleTemplate.java │ │ ├── FindViewByIdFieldTemplate.java │ │ ├── FindViewByIdVariableTemplate.java │ │ ├── VisibleGoneTemplate.java │ │ ├── LogDTemplate.java │ │ ├── FindViewByIdTemplate.java │ │ ├── LogTemplate.java │ │ ├── TextUtilsIsEmptyTemplate.java │ │ ├── ToastTemplate.java │ │ └── SnackbarTemplate.java │ ├── utils │ ├── AndroidClassName.java │ ├── ImportUtils.java │ └── AndroidPostfixTemplatesUtils.java │ ├── AndroidPostfixTemplateProvider.java │ ├── macro │ ├── ToStringIfNeedMacro.java │ ├── TagMacro.java │ └── FindViewByIdMacro.java │ └── internal │ └── AbstractRichStringBasedPostfixTemplate.java ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradle.properties ├── circle.yml ├── CONTRIBUTING.md ├── .gitignore ├── android-postfix-completion.iml ├── gradlew.bat ├── README.md ├── gradlew └── LICENSE /.idea/.name: -------------------------------------------------------------------------------- 1 | android-postfix-completion -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'android-postfix-plugin' -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /src/main/resources/postfixTemplates/LogTemplate/description.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Log message. 4 | 5 | 6 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/takahirom/android-postfix-plugin/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /src/main/resources/postfixTemplates/SnackbarTemplate/description.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Show a Snackbar. 4 | 5 | 6 | -------------------------------------------------------------------------------- /src/main/resources/postfixTemplates/GoneTemplate/description.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Change View's visibility. 4 | 5 | 6 | -------------------------------------------------------------------------------- /src/main/resources/postfixTemplates/ToastTemplate/description.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Create and show Toast. 4 | 5 | 6 | -------------------------------------------------------------------------------- /src/main/resources/postfixTemplates/InvisibleTemplate/description.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Change View's visibility. 4 | 5 | 6 | -------------------------------------------------------------------------------- /src/main/resources/postfixTemplates/VisibleTemplate/description.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Change View's visibility. 4 | 5 | 6 | -------------------------------------------------------------------------------- /src/main/resources/postfixTemplates/GoneTemplate/before.java.template: -------------------------------------------------------------------------------- 1 | public class Foo { 2 | void m(Object o) { 3 | o$key 4 | } 5 | } -------------------------------------------------------------------------------- /src/main/resources/postfixTemplates/LogDTemplate/before.java.template: -------------------------------------------------------------------------------- 1 | public class Foo { 2 | void m(Object o) { 3 | o$key 4 | } 5 | } -------------------------------------------------------------------------------- /src/main/resources/postfixTemplates/LogTemplate/before.java.template: -------------------------------------------------------------------------------- 1 | public class Foo { 2 | void m(Object o) { 3 | o$key 4 | } 5 | } -------------------------------------------------------------------------------- /src/main/resources/postfixTemplates/FindViewByIdTemplate/description.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Generate findViewById statement. 4 | 5 | 6 | -------------------------------------------------------------------------------- /src/main/resources/postfixTemplates/InvisibleTemplate/before.java.template: -------------------------------------------------------------------------------- 1 | public class Foo { 2 | void m(Object o) { 3 | o$key 4 | } 5 | } -------------------------------------------------------------------------------- /src/main/resources/postfixTemplates/LogDTemplate/description.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | If BuildConfig.DEBUG is true, Log message. 4 | 5 | 6 | -------------------------------------------------------------------------------- /src/main/resources/postfixTemplates/SnackbarTemplate/before.java.template: -------------------------------------------------------------------------------- 1 | public class Foo { 2 | void m(Object o) { 3 | o$key 4 | } 5 | } -------------------------------------------------------------------------------- /src/main/resources/postfixTemplates/ToastTemplate/before.java.template: -------------------------------------------------------------------------------- 1 | public class Foo { 2 | void m(Object o) { 3 | o$key 4 | } 5 | } -------------------------------------------------------------------------------- /src/main/resources/postfixTemplates/VisibleTemplate/before.java.template: -------------------------------------------------------------------------------- 1 | public class Foo { 2 | void m(Object o) { 3 | o$key 4 | } 5 | } -------------------------------------------------------------------------------- /src/main/resources/postfixTemplates/FindViewByIdFieldTemplate/description.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Generate findViewById statement. 4 | 5 | 6 | -------------------------------------------------------------------------------- /src/main/resources/postfixTemplates/FindViewByIdTemplate/before.java.template: -------------------------------------------------------------------------------- 1 | public class Foo { 2 | void m(Object o) { 3 | o$key 4 | } 5 | } -------------------------------------------------------------------------------- /src/main/resources/postfixTemplates/VisibleGoneTemplate/before.java.template: -------------------------------------------------------------------------------- 1 | public class Foo { 2 | void m(Object o) { 3 | o$key 4 | } 5 | } -------------------------------------------------------------------------------- /src/main/resources/postfixTemplates/FindViewByIdFieldTemplate/before.java.template: -------------------------------------------------------------------------------- 1 | public class Foo { 2 | void m(Object o) { 3 | o$key 4 | } 5 | } -------------------------------------------------------------------------------- /src/main/resources/postfixTemplates/FindViewByIdVariableTemplate/description.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Generate findViewById statement. 4 | 5 | 6 | -------------------------------------------------------------------------------- /src/main/resources/postfixTemplates/TextUtilsIsEmptyTemplate/before.java.template: -------------------------------------------------------------------------------- 1 | public class Foo { 2 | void m(Object o) { 3 | o$key 4 | } 5 | } -------------------------------------------------------------------------------- /src/main/resources/postfixTemplates/VisibleGoneTemplate/description.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Generate ternary operator of View visibility. 4 | 5 | 6 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | AndroidStudioPath=/Applications/Android Studio Canaly.app/Contents 2 | org.gradle.java.home=/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home -------------------------------------------------------------------------------- /src/main/resources/postfixTemplates/FindViewByIdVariableTemplate/before.java.template: -------------------------------------------------------------------------------- 1 | public class Foo { 2 | void m(Object o) { 3 | o$key 4 | } 5 | } -------------------------------------------------------------------------------- /src/main/resources/postfixTemplates/TextUtilsIsEmptyTemplate/description.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Returns true if the string is null or 0-length. 4 | 5 | 6 | -------------------------------------------------------------------------------- /src/main/resources/postfixTemplates/LogTemplate/after.java.template: -------------------------------------------------------------------------------- 1 | import com.google.common.base.Preconditions; 2 | 3 | public class Foo { 4 | void m(Object o) { 5 | Log.d(TAG, o); 6 | } 7 | } -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /src/main/resources/postfixTemplates/GoneTemplate/after.java.template: -------------------------------------------------------------------------------- 1 | import com.google.common.base.Preconditions; 2 | 3 | public class Foo { 4 | void m(Object o) { 5 | o.setVisibility(View.GONE); 6 | } 7 | } -------------------------------------------------------------------------------- /src/main/resources/postfixTemplates/VisibleTemplate/after.java.template: -------------------------------------------------------------------------------- 1 | import com.google.common.base.Preconditions; 2 | 3 | public class Foo { 4 | void m(Object o) { 5 | o.setVisibility(View.VISIBLE); 6 | } 7 | } -------------------------------------------------------------------------------- /circle.yml: -------------------------------------------------------------------------------- 1 | general: 2 | branches: 3 | only: ['master'] 4 | artifacts: 5 | - 'build/libs' 6 | machine: 7 | java: 8 | version: openjdk7 9 | test: 10 | override: 11 | - ./gradlew build --debug 12 | -------------------------------------------------------------------------------- /src/main/resources/postfixTemplates/FindViewByIdTemplate/after.java.template: -------------------------------------------------------------------------------- 1 | import com.google.common.base.Preconditions; 2 | 3 | public class Foo { 4 | void m(Object o) { 5 | (ViewType)findViewById(o); 6 | } 7 | } -------------------------------------------------------------------------------- /src/main/resources/postfixTemplates/InvisibleTemplate/after.java.template: -------------------------------------------------------------------------------- 1 | import com.google.common.base.Preconditions; 2 | 3 | public class Foo { 4 | void m(Object o) { 5 | o.setVisibility(View.INVISIBLE); 6 | } 7 | } -------------------------------------------------------------------------------- /src/main/resources/postfixTemplates/LogDTemplate/after.java.template: -------------------------------------------------------------------------------- 1 | import com.google.common.base.Preconditions; 2 | 3 | public class Foo { 4 | void m(Object o) { 5 | if (BuildConfig.DEBUG) Log.d(TAG, o); 6 | } 7 | } -------------------------------------------------------------------------------- /src/main/resources/postfixTemplates/VisibleGoneTemplate/after.java.template: -------------------------------------------------------------------------------- 1 | import com.google.common.base.Preconditions; 2 | 3 | public class Foo { 4 | void m(Object o) { 5 | (o) ? View.VISIBLE : View.GONE; 6 | } 7 | } -------------------------------------------------------------------------------- /src/main/resources/postfixTemplates/TextUtilsIsEmptyTemplate/after.java.template: -------------------------------------------------------------------------------- 1 | import com.google.common.base.Preconditions; 2 | 3 | public class Foo { 4 | void m(Object o) { 5 | TextUtils.isEmpty("test") 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/main/resources/postfixTemplates/FindViewByIdVariableTemplate/after.java.template: -------------------------------------------------------------------------------- 1 | import com.google.common.base.Preconditions; 2 | 3 | public class Foo { 4 | void m(Object o) { 5 | ViewType view = (ViewType)findViewById(o); 6 | } 7 | } -------------------------------------------------------------------------------- /src/main/resources/postfixTemplates/SnackbarTemplate/after.java.template: -------------------------------------------------------------------------------- 1 | import com.google.common.base.Preconditions; 2 | 3 | public class Foo { 4 | void m(Object o) { 5 | Snackbar.make(view, o, Snackbar.LENGTH_SHORT).show(); 6 | } 7 | } -------------------------------------------------------------------------------- /src/main/resources/postfixTemplates/ToastTemplate/after.java.template: -------------------------------------------------------------------------------- 1 | import com.google.common.base.Preconditions; 2 | 3 | public class Foo { 4 | void m(Object o) { 5 | Toast.makeText(MyActivity.this, "test", Toast.LENGTH_SHORT).show(); 6 | } 7 | } -------------------------------------------------------------------------------- /src/main/resources/postfixTemplates/FindViewByIdFieldTemplate/after.java.template: -------------------------------------------------------------------------------- 1 | import com.google.common.base.Preconditions; 2 | 3 | public class Foo { 4 | private ViewType view; 5 | void m(Object o) { 6 | view = (ViewType)findViewById(o); 7 | } 8 | } -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | Contribution 2 | ---------------- 3 | 1. Fork it ( http://github.com/takahirom/android-postfix-plugin/fork ) 4 | 2. Create your feature branch (git checkout -b my-new-feature) 5 | 3. Commit your changes (git commit -am 'Add some feature') 6 | 4. Push to the branch (git push origin my-new-feature) 7 | 5. Create new Pull Request -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.jar 2 | out/ 3 | .DS_Store 4 | 5 | # User-specific stuff: 6 | .idea/workspace.xml 7 | .idea/tasks.xml 8 | .idea/dictionaries 9 | 10 | # Sensitive or high-churn files: 11 | .idea/dataSources.ids 12 | .idea/dataSources.xml 13 | .idea/sqlDataSources.xml 14 | .idea/dynamic.xml 15 | .idea/uiDesigner.xml 16 | 17 | # Gradle: 18 | .idea/gradle.xml 19 | .idea/libraries 20 | 21 | # Mongo Explorer plugin: 22 | .idea/mongoSettings.xml 23 | 24 | 25 | ### Gradle template 26 | .gradle 27 | build/ 28 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 22 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (C) 2015 takahirom 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | #Tue Sep 15 22:19:36 JST 2015 18 | distributionBase=GRADLE_USER_HOME 19 | distributionPath=wrapper/dists 20 | zipStoreBase=GRADLE_USER_HOME 21 | zipStorePath=wrapper/dists 22 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.12-bin.zip 23 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /.idea/modules/android-postfix-plugin_main.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /src/main/java/com/kogitune/intellij/codeinsight/postfix/templates/surround/GoneTemplate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 takahirom 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.kogitune.intellij.codeinsight.postfix.templates.surround; 18 | 19 | import com.intellij.codeInsight.template.Template; 20 | import com.intellij.codeInsight.template.impl.TextExpression; 21 | import com.intellij.psi.PsiElement; 22 | import com.kogitune.intellij.codeinsight.postfix.internal.AbstractRichStringBasedPostfixTemplate; 23 | import org.jetbrains.annotations.NotNull; 24 | 25 | import static com.kogitune.intellij.codeinsight.postfix.utils.AndroidPostfixTemplatesUtils.IS_VIEW; 26 | 27 | /** 28 | * Postfix template for android View visibility. 29 | * 30 | * @author axlchen 31 | */ 32 | public class GoneTemplate extends AbstractRichStringBasedPostfixTemplate { 33 | 34 | public GoneTemplate() { 35 | this("gone"); 36 | } 37 | 38 | public GoneTemplate(@NotNull String alias) { 39 | super(alias, "view.setVisibility(View.GONE)", IS_VIEW); 40 | } 41 | 42 | @Override 43 | public String getTemplateString(@NotNull PsiElement element) { 44 | return "$view$.setVisibility(View.GONE)"; 45 | } 46 | 47 | @Override 48 | protected void addExprVariable(@NotNull PsiElement expr, Template template) { 49 | template.addVariable("view", new TextExpression(expr.getText()), false); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/kogitune/intellij/codeinsight/postfix/templates/surround/VisibleTemplate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 takahirom 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.kogitune.intellij.codeinsight.postfix.templates.surround; 18 | 19 | import com.intellij.codeInsight.template.Template; 20 | import com.intellij.codeInsight.template.impl.TextExpression; 21 | import com.intellij.psi.PsiElement; 22 | import com.kogitune.intellij.codeinsight.postfix.internal.AbstractRichStringBasedPostfixTemplate; 23 | import org.jetbrains.annotations.NotNull; 24 | 25 | import static com.kogitune.intellij.codeinsight.postfix.utils.AndroidPostfixTemplatesUtils.IS_VIEW; 26 | 27 | /** 28 | * Postfix template for android View visibility. 29 | * 30 | * @author axlchen 31 | */ 32 | public class VisibleTemplate extends AbstractRichStringBasedPostfixTemplate { 33 | 34 | public VisibleTemplate() { 35 | this("vsb"); 36 | } 37 | 38 | public VisibleTemplate(@NotNull String alias) { 39 | super(alias, "view.setVisibility(View.VISIBLE)", IS_VIEW); 40 | } 41 | 42 | @Override 43 | public String getTemplateString(@NotNull PsiElement element) { 44 | return "$view$.setVisibility(View.VISIBLE)"; 45 | } 46 | 47 | @Override 48 | protected void addExprVariable(@NotNull PsiElement expr, Template template) { 49 | template.addVariable("view", new TextExpression(expr.getText()), false); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/kogitune/intellij/codeinsight/postfix/templates/surround/InvisibleTemplate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 takahirom 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.kogitune.intellij.codeinsight.postfix.templates.surround; 18 | 19 | import com.intellij.codeInsight.template.Template; 20 | import com.intellij.codeInsight.template.impl.TextExpression; 21 | import com.intellij.psi.PsiElement; 22 | import com.kogitune.intellij.codeinsight.postfix.internal.AbstractRichStringBasedPostfixTemplate; 23 | import org.jetbrains.annotations.NotNull; 24 | 25 | import static com.kogitune.intellij.codeinsight.postfix.utils.AndroidPostfixTemplatesUtils.IS_VIEW; 26 | 27 | /** 28 | * Postfix template for android View visibility. 29 | * 30 | * @author axlchen 31 | */ 32 | public class InvisibleTemplate extends AbstractRichStringBasedPostfixTemplate { 33 | 34 | public InvisibleTemplate() { 35 | this("invsb"); 36 | } 37 | 38 | public InvisibleTemplate(@NotNull String alias) { 39 | super(alias, "view.setVisibility(View.INVISIBLE)", IS_VIEW); 40 | } 41 | 42 | @Override 43 | public String getTemplateString(@NotNull PsiElement element) { 44 | return "$view$.setVisibility(View.INVISIBLE)"; 45 | } 46 | 47 | @Override 48 | protected void addExprVariable(@NotNull PsiElement expr, Template template) { 49 | template.addVariable("view", new TextExpression(expr.getText()), false); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/kogitune/intellij/codeinsight/postfix/utils/AndroidClassName.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Bob Browning 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.kogitune.intellij.codeinsight.postfix.utils; 17 | 18 | import com.intellij.psi.PsiClass; 19 | import com.intellij.psi.PsiElement; 20 | import com.siyeh.ig.psiutils.ClassUtils; 21 | 22 | /** 23 | * Collection of static strings representing android class names. 24 | */ 25 | public enum AndroidClassName { 26 | TOAST("android.widget.Toast"), 27 | LOG("android.util.Log"), 28 | CONTEXT("android.content.Context"), 29 | TEXT_UTILS("android.text.TextUtils"), 30 | VIEW("android.view.View"), 31 | FRAGMENT("android.support.v4.app.Fragment"), 32 | ACTIVITY("android.app.Activity"), 33 | SNACKBAR("android.support.design.widget.Snackbar"); 34 | 35 | 36 | private final String fqClassName; 37 | 38 | AndroidClassName(String fqClassName) { 39 | this.fqClassName = fqClassName; 40 | } 41 | 42 | public String getClassName() { 43 | return fqClassName; 44 | } 45 | 46 | public PsiClass getPsiClass(PsiElement context) { 47 | return ClassUtils.findClass(fqClassName, context); 48 | } 49 | 50 | public String getQualifiedStaticMethodName(String methodName) { 51 | return fqClassName + "." + methodName; 52 | } 53 | 54 | @Override 55 | public String toString() { 56 | return fqClassName; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/kogitune/intellij/codeinsight/postfix/templates/surround/FindViewByIdFieldTemplate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 takahirom 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.kogitune.intellij.codeinsight.postfix.templates.surround; 18 | 19 | import com.intellij.codeInsight.template.Template; 20 | import com.intellij.codeInsight.template.TemplateManager; 21 | import com.intellij.openapi.actionSystem.ActionManager; 22 | import com.intellij.openapi.actionSystem.ActionPlaces; 23 | import com.intellij.openapi.actionSystem.AnAction; 24 | import com.intellij.openapi.actionSystem.impl.ActionManagerImpl; 25 | import com.intellij.openapi.editor.Editor; 26 | import com.intellij.openapi.ui.playback.commands.ActionCommand; 27 | 28 | /** 29 | * Postfix template for android findViewById. 30 | * 31 | * @author takahirom 32 | */ 33 | public class FindViewByIdFieldTemplate extends FindViewByIdTemplate { 34 | 35 | public FindViewByIdFieldTemplate() { 36 | super("findf", "mViewType = (ViewType)findViewById(expr);"); 37 | } 38 | 39 | @Override 40 | protected void onTemplateFinished(final TemplateManager manager, final Editor editor, Template template) { 41 | final ActionManager actionManager = ActionManagerImpl.getInstance(); 42 | final String editorCompleteStatementText = "IntroduceField"; 43 | final AnAction action = actionManager.getAction(editorCompleteStatementText); 44 | actionManager.tryToExecute(action, ActionCommand.getInputEvent(editorCompleteStatementText), null, ActionPlaces.UNKNOWN, true); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/com/kogitune/intellij/codeinsight/postfix/templates/surround/FindViewByIdVariableTemplate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 takahirom 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.kogitune.intellij.codeinsight.postfix.templates.surround; 18 | 19 | import com.intellij.codeInsight.template.Template; 20 | import com.intellij.codeInsight.template.TemplateManager; 21 | import com.intellij.openapi.actionSystem.ActionManager; 22 | import com.intellij.openapi.actionSystem.ActionPlaces; 23 | import com.intellij.openapi.actionSystem.AnAction; 24 | import com.intellij.openapi.actionSystem.impl.ActionManagerImpl; 25 | import com.intellij.openapi.editor.Editor; 26 | import com.intellij.openapi.ui.playback.commands.ActionCommand; 27 | 28 | /** 29 | * Postfix template for android findViewById. 30 | * 31 | * @author takahirom 32 | */ 33 | public class FindViewByIdVariableTemplate extends FindViewByIdTemplate { 34 | 35 | public FindViewByIdVariableTemplate() { 36 | super("findv", "ViewType viewType = (ViewType)findViewById(expr);"); 37 | } 38 | 39 | @Override 40 | protected void onTemplateFinished(final TemplateManager manager, final Editor editor, Template template) { 41 | final ActionManager actionManager = ActionManagerImpl.getInstance(); 42 | final String editorCompleteStatementText = "IntroduceVariable"; 43 | final AnAction action = actionManager.getAction(editorCompleteStatementText); 44 | actionManager.tryToExecute(action, ActionCommand.getInputEvent(editorCompleteStatementText), null, ActionPlaces.UNKNOWN, true); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/com/kogitune/intellij/codeinsight/postfix/templates/surround/VisibleGoneTemplate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 takahirom 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.kogitune.intellij.codeinsight.postfix.templates.surround; 18 | 19 | import com.intellij.codeInsight.template.Template; 20 | import com.intellij.codeInsight.template.impl.TextExpression; 21 | import com.intellij.psi.PsiElement; 22 | import com.kogitune.intellij.codeinsight.postfix.internal.AbstractRichStringBasedPostfixTemplate; 23 | import com.kogitune.intellij.codeinsight.postfix.utils.AndroidClassName; 24 | import org.jetbrains.annotations.NotNull; 25 | 26 | import static com.intellij.codeInsight.template.postfix.util.JavaPostfixTemplatesUtils.IS_BOOLEAN; 27 | 28 | /** 29 | * Postfix template for android View visibility. 30 | * 31 | * @author kikuchy 32 | */ 33 | public class VisibleGoneTemplate extends AbstractRichStringBasedPostfixTemplate { 34 | 35 | public VisibleGoneTemplate() { 36 | this("vg"); 37 | } 38 | 39 | public VisibleGoneTemplate(@NotNull String alias) { 40 | super(alias, "(expr) ? View.VISIBLE : View.GONE", IS_BOOLEAN); 41 | } 42 | 43 | @Override 44 | public String getTemplateString(@NotNull PsiElement element) { 45 | return "($expr$) ? " 46 | + getStaticPrefix(AndroidClassName.VIEW, "VISIBLE", element) 47 | + " : " 48 | + getStaticPrefix(AndroidClassName.VIEW, "GONE", element); 49 | } 50 | 51 | @Override 52 | protected void addExprVariable(@NotNull PsiElement expr, Template template) { 53 | template.addVariable("expr", new TextExpression(expr.getText()), false); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/kogitune/intellij/codeinsight/postfix/AndroidPostfixTemplateProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Bob Browning 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.kogitune.intellij.codeinsight.postfix; 17 | 18 | import com.intellij.codeInsight.template.postfix.templates.JavaPostfixTemplateProvider; 19 | import com.intellij.codeInsight.template.postfix.templates.PostfixTemplate; 20 | import com.intellij.util.containers.ContainerUtil; 21 | import com.kogitune.intellij.codeinsight.postfix.templates.surround.*; 22 | import org.jetbrains.annotations.NotNull; 23 | 24 | import java.util.HashSet; 25 | import java.util.Set; 26 | 27 | /** 28 | * Postfix template provider for extension point. 29 | * 30 | * @author takahirom 31 | */ 32 | public class AndroidPostfixTemplateProvider extends JavaPostfixTemplateProvider { 33 | 34 | private final HashSet templates; 35 | 36 | public AndroidPostfixTemplateProvider() { 37 | templates = ContainerUtil.newHashSet( 38 | new ToastTemplate(), 39 | new LogTemplate(), 40 | new LogDTemplate(), 41 | new TextUtilsIsEmptyTemplate(), 42 | new VisibleGoneTemplate(), 43 | new FindViewByIdTemplate(), 44 | new FindViewByIdVariableTemplate(), 45 | new FindViewByIdFieldTemplate(), 46 | new VisibleTemplate(), 47 | new InvisibleTemplate(), 48 | new GoneTemplate(), 49 | new SnackbarTemplate(), 50 | new SnackbarTemplate(true) 51 | ); 52 | } 53 | 54 | @NotNull 55 | @Override 56 | public Set getTemplates() { 57 | return templates; 58 | } 59 | 60 | 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/com/kogitune/intellij/codeinsight/postfix/templates/surround/LogDTemplate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Bob Browning 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.kogitune.intellij.codeinsight.postfix.templates.surround; 17 | 18 | import com.intellij.openapi.project.Project; 19 | import com.intellij.psi.PsiClass; 20 | import com.intellij.psi.PsiElement; 21 | import com.intellij.psi.search.GlobalSearchScope; 22 | import com.intellij.psi.search.PsiShortNamesCache; 23 | import com.kogitune.intellij.codeinsight.postfix.utils.AndroidPostfixTemplatesUtils; 24 | import org.jetbrains.annotations.NotNull; 25 | 26 | import static com.kogitune.intellij.codeinsight.postfix.utils.AndroidClassName.LOG; 27 | 28 | /** 29 | * Postfix template for android Log. 30 | * 31 | * @author takahirom 32 | */ 33 | public class LogDTemplate extends LogTemplate { 34 | 35 | public LogDTemplate() { 36 | this("logd"); 37 | } 38 | 39 | public LogDTemplate(@NotNull String alias) { 40 | super(alias, "if(BuildConfig.DEBUG) Log.d(TAG, expr);", AndroidPostfixTemplatesUtils.IS_NON_NULL); 41 | } 42 | 43 | @Override 44 | public String getTemplateString(@NotNull PsiElement element) { 45 | Project project = element.getProject(); 46 | final GlobalSearchScope resolveScope = element.getResolveScope(); 47 | PsiClass[] buildConfigClasses = PsiShortNamesCache.getInstance(project).getClassesByName("BuildConfig", resolveScope); 48 | 49 | String buildConfigDebug = "BuildConfig.DEBUG"; 50 | if (buildConfigClasses.length != 0) { 51 | // Get BuildConfig QualifiedName 52 | PsiClass buildConfig = buildConfigClasses[0]; 53 | String qualifiedName = buildConfig.getQualifiedName(); 54 | buildConfigDebug = qualifiedName + ".DEBUG"; 55 | } 56 | 57 | return "if (" + buildConfigDebug + ") " + getStaticPrefix(LOG, "d", element) + "($TAG$, $expr$)$END$"; 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/kogitune/intellij/codeinsight/postfix/macro/ToStringIfNeedMacro.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 takahirom 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.kogitune.intellij.codeinsight.postfix.macro; 18 | 19 | import com.intellij.codeInsight.template.*; 20 | import com.intellij.openapi.project.Project; 21 | import com.intellij.psi.*; 22 | import org.jetbrains.annotations.Nullable; 23 | 24 | public class ToStringIfNeedMacro extends Macro { 25 | 26 | 27 | public String getName() { 28 | return "to_string"; 29 | } 30 | 31 | public String getPresentableName() { 32 | return "to_string"; 33 | } 34 | 35 | @Nullable 36 | @Override 37 | public Result calculateResult(Expression[] expressions, ExpressionContext context) { 38 | if (expressions.length == 0) { 39 | return null; 40 | } 41 | Project project = context.getProject(); 42 | final String exprText = expressions[0].calculateResult(context).toString(); 43 | try { 44 | PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project); 45 | final PsiExpression expression = elementFactory.createExpressionFromText(exprText, context.getPsiElementAtStartOffset()); 46 | final PsiType type = expression.getType(); 47 | 48 | if ("java.lang.String".equals(type.getCanonicalText())) { 49 | // example "test:" + test 50 | return new TextResult(exprText); 51 | } 52 | if (expression instanceof PsiPolyadicExpression) { 53 | // example 1 + 1 54 | return new TextResult("\"" + exprText + ":\" + (" + exprText + ")"); 55 | } 56 | // example 1 57 | return new TextResult("\"" + exprText + ":\" + " + exprText); 58 | } catch (Exception e) { 59 | // ignored. because can use default result. 60 | e.printStackTrace(); 61 | } 62 | return new TextResult(exprText); 63 | 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /android-postfix-completion.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /src/main/java/com/kogitune/intellij/codeinsight/postfix/templates/surround/FindViewByIdTemplate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 takahirom 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.kogitune.intellij.codeinsight.postfix.templates.surround; 18 | 19 | import com.intellij.codeInsight.template.Template; 20 | import com.intellij.codeInsight.template.impl.ConstantNode; 21 | import com.intellij.codeInsight.template.impl.MacroCallNode; 22 | import com.intellij.openapi.util.Condition; 23 | import com.intellij.psi.PsiElement; 24 | import com.kogitune.intellij.codeinsight.postfix.internal.AbstractRichStringBasedPostfixTemplate; 25 | import com.kogitune.intellij.codeinsight.postfix.macro.FindViewByIdMacro; 26 | import com.kogitune.intellij.codeinsight.postfix.utils.AndroidPostfixTemplatesUtils; 27 | import org.jetbrains.annotations.NotNull; 28 | 29 | import static com.intellij.codeInsight.template.postfix.util.JavaPostfixTemplatesUtils.IS_NUMBER; 30 | 31 | /** 32 | * Postfix template for android findViewById. 33 | * 34 | * @author takahirom 35 | */ 36 | public class FindViewByIdTemplate extends AbstractRichStringBasedPostfixTemplate { 37 | 38 | public static final Condition IS_NON_NULL_NUMBER = new Condition() { 39 | @Override 40 | public boolean value(PsiElement element) { 41 | return IS_NUMBER.value(element) && !AndroidPostfixTemplatesUtils.isAnnotatedNullable(element); 42 | } 43 | 44 | }; 45 | 46 | public FindViewByIdTemplate() { 47 | this("find", "findViewById(expr);"); 48 | } 49 | 50 | public FindViewByIdTemplate(@NotNull String name, @NotNull String example) { 51 | super(name, example, IS_NON_NULL_NUMBER); 52 | } 53 | 54 | @Override 55 | public String getTemplateString(@NotNull PsiElement element) { 56 | return "$expr$$END$"; 57 | } 58 | 59 | @Override 60 | protected void addExprVariable(@NotNull PsiElement expr, Template template) { 61 | final FindViewByIdMacro toStringIfNeedMacro = new FindViewByIdMacro(); 62 | MacroCallNode macroCallNode = new MacroCallNode(toStringIfNeedMacro); 63 | macroCallNode.addParameter(new ConstantNode(expr.getText())); 64 | template.addVariable("expr", macroCallNode, false); 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # android-postfix-plugin 2 | ![build status](https://circleci.com/gh/takahirom/android-postfix-plugin/tree/master.svg?style=shield&circle-token=d7e574ba58679594e235b3ca326ef683eee7bf6f) 3 | 4 | Android postfix plugin for AndroidStudio 5 | 6 | ![image](https://cloud.githubusercontent.com/assets/1386930/10118005/8d45e898-64a6-11e5-8c32-8f38b0105177.gif) 7 | ![image](https://cloud.githubusercontent.com/assets/1386930/7507612/3c131392-f4ad-11e4-98a0-e56dbfab8c69.gif) 8 | ![image](https://cloud.githubusercontent.com/assets/1386930/7448067/c8f2ceb6-f24a-11e4-8711-c5f2a5d205d4.gif) 9 | 10 | Available templates: 11 | 12 | | Postfix Expression | Description | Example | 13 | | --------------------- | ---------------------------------------------------------------------------------------------------------------------- | -------------------------------------- | 14 | | .toast | Create and show Toast. | Toast.makeText(context, expr, Toast.LENGTH_SHORT).show() | 15 | | .log | Logging. If there is constant variable "TAG", it use "TAG" . Else it use class name. | Log.d(TAG, expr) | 16 | | .logd | Logging. If BuildConfig.DEBUG is true, Log message. | if (BuildConfig.DEBUG) Log.d(TAG, expr) | 17 | | .find | Typed FindView | (ViewType) findViewById(expr) | 18 | | .isemp | Check empty. | TextUtils.isEmpty(expr) | 19 | | .vg | Divide view visible or gone. | (expr) ? View.VISIBLE : View.GONE | 20 | | .vsb
.invsb
.gone | Change View's visibility. | view.setVisibility(View.VISIBLE) view.setVisibility(View.INVISIBLE) view.setVisibility(View.GONE) | 21 | | .snack
.snackaction | Show a Snackbar with or without an anction. | Snackbar.make(view, expr, Snackbar.LENGTH_SHORT).show()
Snackbar.make(view, expr, Snackbar.LENGTH_LONG).setAction(...).show() 22 | 23 | 24 | Plugin page 25 | --------------- 26 | [JetBrains plugin page](https://plugins.jetbrains.com/plugin/7775) 27 | 28 | 29 | Contributors 30 | --------------- 31 | * [kikuchy](http://www.github.com/kikuchy) 32 | * [Axlchen](http://www.github.com/Axlchen) 33 | 34 | How to run the repository code 35 | ---------------- 36 | 1. Clone 37 | 2. Run Gradle Wrapper 38 | In project root. 39 | ``` 40 | ./gradlew runIde 41 | ``` 42 | 43 | Thanks 44 | --------------- 45 | [guava-postfix-plugin](https://github.com/ukcrpb6/guava-postfix-plugin) 46 | 47 | 48 | License 49 | --------------- 50 | This project is released under the Apache License, Version 2.0. 51 | 52 | * [The Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0) 53 | -------------------------------------------------------------------------------- /src/main/java/com/kogitune/intellij/codeinsight/postfix/templates/surround/LogTemplate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Bob Browning 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.kogitune.intellij.codeinsight.postfix.templates.surround; 17 | 18 | import com.intellij.codeInsight.template.Template; 19 | import com.intellij.codeInsight.template.impl.ConstantNode; 20 | import com.intellij.codeInsight.template.impl.MacroCallNode; 21 | import com.intellij.openapi.util.Condition; 22 | import com.intellij.psi.PsiElement; 23 | import com.kogitune.intellij.codeinsight.postfix.internal.AbstractRichStringBasedPostfixTemplate; 24 | import com.kogitune.intellij.codeinsight.postfix.macro.TagMacro; 25 | import com.kogitune.intellij.codeinsight.postfix.macro.ToStringIfNeedMacro; 26 | import com.kogitune.intellij.codeinsight.postfix.utils.AndroidPostfixTemplatesUtils; 27 | import org.jetbrains.annotations.NotNull; 28 | 29 | import static com.kogitune.intellij.codeinsight.postfix.utils.AndroidClassName.LOG; 30 | 31 | /** 32 | * Postfix template for android Log. 33 | * 34 | * @author takahirom 35 | */ 36 | public class LogTemplate extends AbstractRichStringBasedPostfixTemplate { 37 | 38 | public LogTemplate() { 39 | this("log", "Log.d(TAG, expr);", AndroidPostfixTemplatesUtils.IS_NON_NULL); 40 | } 41 | 42 | public LogTemplate(@NotNull String name, @NotNull String example, @NotNull Condition typeChecker) { 43 | super(name, example, typeChecker); 44 | } 45 | 46 | @Override 47 | public String getTemplateString(@NotNull PsiElement element) { 48 | return getStaticPrefix(LOG, "d", element) + "($TAG$, $expr$)$END$"; 49 | } 50 | 51 | @Override 52 | protected void addExprVariable(@NotNull PsiElement expr, Template template) { 53 | final ToStringIfNeedMacro toStringIfNeedMacro = new ToStringIfNeedMacro(); 54 | MacroCallNode macroCallNode = new MacroCallNode(toStringIfNeedMacro); 55 | macroCallNode.addParameter(new ConstantNode(expr.getText())); 56 | template.addVariable("expr", macroCallNode, false); 57 | } 58 | 59 | @Override 60 | protected void setVariables(@NotNull Template template, @NotNull PsiElement element) { 61 | MacroCallNode node = new MacroCallNode(new TagMacro()); 62 | template.addVariable("TAG", node, new ConstantNode(""), false); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/com/kogitune/intellij/codeinsight/postfix/macro/TagMacro.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 takahirom 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.kogitune.intellij.codeinsight.postfix.macro; 18 | 19 | import com.intellij.codeInsight.template.*; 20 | import com.intellij.codeInsight.template.macro.ClassNameMacro; 21 | import com.intellij.codeInsight.template.macro.MacroUtil; 22 | import com.intellij.openapi.project.Project; 23 | import com.intellij.psi.*; 24 | import org.jetbrains.annotations.Nullable; 25 | 26 | /** 27 | * macro for android log TAG parameter. 28 | * 29 | * @author takahirom 30 | */ 31 | public class TagMacro extends Macro { 32 | 33 | 34 | public String getName() { 35 | return "tag"; 36 | } 37 | 38 | public String getPresentableName() { 39 | return "tag"; 40 | } 41 | 42 | @Nullable 43 | @Override 44 | public Result calculateResult(Expression[] expressions, ExpressionContext context) { 45 | if (isContainTagField(context)) { 46 | return new TextResult("TAG"); 47 | } else { 48 | String className = new ClassNameMacro().calculateResult(new Expression[]{}, context).toString(); 49 | if (className.length() > 23) { 50 | className = className.substring(0, 23); 51 | } 52 | return new TextResult("\"" + className + "\""); 53 | } 54 | } 55 | 56 | public boolean isAcceptableInContext(TemplateContextType context) { 57 | return context instanceof JavaCodeContextType; 58 | } 59 | 60 | 61 | public boolean isContainTagField(ExpressionContext context) { 62 | Project project = context.getProject(); 63 | int offset = context.getStartOffset(); 64 | PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(context.getEditor().getDocument()); 65 | PsiElement place = file.findElementAt(offset); 66 | PsiVariable[] variables = MacroUtil.getVariablesVisibleAt(place, ""); 67 | for (PsiVariable variable : variables) { 68 | if (variable instanceof PsiField && variable.hasModifierProperty("static")) { 69 | PsiField psiField = (PsiField) variable; 70 | if ("TAG".equals(psiField.getName())) { 71 | return true; 72 | } 73 | } 74 | 75 | } 76 | 77 | return false; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/main/java/com/kogitune/intellij/codeinsight/postfix/templates/surround/TextUtilsIsEmptyTemplate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 takahirom 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.kogitune.intellij.codeinsight.postfix.templates.surround; 18 | 19 | import com.intellij.codeInsight.template.Template; 20 | import com.intellij.codeInsight.template.TemplateManager; 21 | import com.intellij.codeInsight.template.impl.TextExpression; 22 | import com.intellij.openapi.editor.Editor; 23 | import com.intellij.openapi.util.Condition; 24 | import com.intellij.psi.PsiElement; 25 | import com.intellij.psi.PsiExpression; 26 | import com.intellij.psi.util.InheritanceUtil; 27 | import com.kogitune.intellij.codeinsight.postfix.internal.AbstractRichStringBasedPostfixTemplate; 28 | import com.kogitune.intellij.codeinsight.postfix.utils.AndroidPostfixTemplatesUtils; 29 | import org.jetbrains.annotations.NotNull; 30 | 31 | import static com.kogitune.intellij.codeinsight.postfix.utils.AndroidClassName.TEXT_UTILS; 32 | 33 | /** 34 | * Postfix template for android TextUtils class. 35 | * 36 | * @author kikuchy 37 | */ 38 | public class TextUtilsIsEmptyTemplate extends AbstractRichStringBasedPostfixTemplate { 39 | 40 | public static final Condition IS_NON_NULL_STRING = new Condition() { 41 | @Override 42 | public boolean value(PsiElement element) { 43 | return InheritanceUtil.isInheritor(((PsiExpression) element).getType(), "java.lang.CharSequence") && !AndroidPostfixTemplatesUtils.isAnnotatedNullable(element); 44 | } 45 | }; 46 | 47 | public TextUtilsIsEmptyTemplate() { 48 | this("isemp"); 49 | } 50 | 51 | public TextUtilsIsEmptyTemplate(@NotNull String alias) { 52 | super(alias, "TextUtils.isEmpty(expr)", IS_NON_NULL_STRING); 53 | } 54 | 55 | @Override 56 | protected void onTemplateFinished(TemplateManager manager, Editor editor, Template template) { 57 | // nothing 58 | // Prevent complete statement 59 | } 60 | 61 | @Override 62 | public String getTemplateString(@NotNull PsiElement element) { 63 | return getStaticPrefix(TEXT_UTILS, "isEmpty", element) + "($expr$)$END$"; 64 | } 65 | 66 | @Override 67 | protected void addExprVariable(@NotNull PsiElement expr, Template template) { 68 | template.addVariable("expr", new TextExpression(expr.getText()), false); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/com/kogitune/intellij/codeinsight/postfix/templates/surround/ToastTemplate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Bob Browning 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.kogitune.intellij.codeinsight.postfix.templates.surround; 17 | 18 | import com.intellij.codeInsight.template.Template; 19 | import com.intellij.codeInsight.template.impl.ConstantNode; 20 | import com.intellij.codeInsight.template.impl.MacroCallNode; 21 | import com.intellij.codeInsight.template.macro.VariableOfTypeMacro; 22 | import com.intellij.psi.PsiElement; 23 | import com.kogitune.intellij.codeinsight.postfix.internal.AbstractRichStringBasedPostfixTemplate; 24 | import com.kogitune.intellij.codeinsight.postfix.macro.ToStringIfNeedMacro; 25 | import com.kogitune.intellij.codeinsight.postfix.utils.AndroidPostfixTemplatesUtils; 26 | import org.jetbrains.annotations.NotNull; 27 | 28 | import static com.kogitune.intellij.codeinsight.postfix.utils.AndroidClassName.CONTEXT; 29 | import static com.kogitune.intellij.codeinsight.postfix.utils.AndroidClassName.TOAST; 30 | 31 | /** 32 | * Postfix template for android Toast. 33 | * 34 | * @author takahirom 35 | */ 36 | public class ToastTemplate extends AbstractRichStringBasedPostfixTemplate { 37 | 38 | public ToastTemplate() { 39 | this("toast"); 40 | } 41 | 42 | public ToastTemplate(@NotNull String alias) { 43 | super(alias, "Toast.makeText(context, expr, Toast.LENGTH_SHORT).show();", AndroidPostfixTemplatesUtils.IS_NON_NULL); 44 | } 45 | 46 | 47 | @Override 48 | public String getTemplateString(@NotNull PsiElement element) { 49 | return getStaticPrefix(TOAST, "makeText", element) + "($context$, $expr$, Toast.LENGTH_SHORT).show()$END$"; 50 | } 51 | 52 | @Override 53 | protected void addExprVariable(@NotNull PsiElement expr, Template template) { 54 | final ToStringIfNeedMacro toStringIfNeedMacro = new ToStringIfNeedMacro(); 55 | MacroCallNode macroCallNode = new MacroCallNode(toStringIfNeedMacro); 56 | macroCallNode.addParameter(new ConstantNode(expr.getText())); 57 | template.addVariable("expr", macroCallNode, false); 58 | } 59 | 60 | @Override 61 | protected void setVariables(@NotNull Template template, @NotNull PsiElement element) { 62 | MacroCallNode node = new MacroCallNode(new VariableOfTypeMacro()); 63 | node.addParameter(new ConstantNode(CONTEXT.toString())); 64 | template.addVariable("context", node, new ConstantNode(""), false); 65 | 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/com/kogitune/intellij/codeinsight/postfix/utils/ImportUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Bob Browning 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.kogitune.intellij.codeinsight.postfix.utils; 17 | 18 | import com.intellij.psi.*; 19 | import com.siyeh.ig.psiutils.ClassUtils; 20 | 21 | /** 22 | * Collection of method for supporting importing of members. 23 | * 24 | * @author Bob Browning 25 | */ 26 | public class ImportUtils { 27 | 28 | /** 29 | * Check whether the current context has a static member import, either on-demand or explicit. 30 | * 31 | * @param fqClassName The class to import from 32 | * @param memberName The class member to import 33 | * @param context The context to be imported into 34 | */ 35 | public static boolean hasImportStatic(String fqClassName, String memberName, PsiElement context) { 36 | final PsiFile file = context.getContainingFile(); 37 | if (!(file instanceof PsiJavaFile)) { 38 | return false; 39 | } 40 | final PsiJavaFile javaFile = (PsiJavaFile) file; 41 | final PsiImportList importList = javaFile.getImportList(); 42 | if (importList == null) { 43 | return false; 44 | } 45 | final PsiImportStaticStatement[] importStaticStatements = importList.getImportStaticStatements(); 46 | for (PsiImportStaticStatement importStaticStatement : importStaticStatements) { 47 | if (importStaticStatement.isOnDemand()) { 48 | PsiClass psiClass = ClassUtils.findClass(fqClassName, context); 49 | if (psiClass != null && psiClass.equals(importStaticStatement.resolveTargetClass())) { 50 | return true; 51 | } 52 | continue; 53 | } 54 | final String name = importStaticStatement.getReferenceName(); 55 | if (!memberName.equals(name)) { 56 | continue; 57 | } 58 | final PsiJavaCodeReferenceElement importReference = importStaticStatement.getImportReference(); 59 | if (importReference == null) { 60 | continue; 61 | } 62 | final PsiElement qualifier = importReference.getQualifier(); 63 | if (qualifier == null) { 64 | continue; 65 | } 66 | final String qualifierText = qualifier.getText(); 67 | if (fqClassName.equals(qualifierText)) { 68 | return true; 69 | } 70 | } 71 | return false; 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /src/main/resources/META-INF/plugin.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | 18 | com.kogitune.intellij.androidPostfixCompletion 19 | Android Postfix Completion 20 | 2.0.3 21 | takahirom 22 | 23 |
25 | 26 | Available templates:

27 | 28 |
    29 |
  • .toast
    Create and show Toast
    Toast.makeText(context, expr, Toast.LENGTH_SHORT).show();
  • 30 |
31 | 32 |
    33 |
  • .log
    Log
    Log.d("log", expr);
  • 34 |
35 |
    36 |
  • .logd
    If BuildConfig.DEBUG is true, Log message.
    Log.d("log", expr);
  • 37 |
38 |
    39 |
  • .find
    Typed FindView
    (ViewType) findViewById(expr);
  • 40 |
41 |
    42 |
  • .isemp
    isEmpty
    TextUtils.isEmpty(expr);
  • 43 |
44 |
    45 |
  • .vg
    ? View.VISIBLE : View.GONE;
    (expr) ? View.VISIBLE : View.GONE;
  • 46 |
47 |
    48 |
  • .vsb .invsb .gone
    49 | Change View's visibility.
    50 | view.setVisibility(View.VISIBLE); 51 | view.setVisibility(View.INVISIBLE); 52 | view.setVisibility(View.GONE); 53 |
  • 54 |
55 |
    56 |
  • .snack .snackaction
    57 | Show a Snackbar with or without an anction.
    58 | Snackbar.make(view, expr, Snackbar.LENGTH_SHORT).show();
    59 | Snackbar.make(view, expr, Snackbar.LENGTH_LONG).setAction(...).show();
    60 |
    61 |
  • 62 |
63 | 64 | ]]>
65 | 66 | 70 | 71 | 72 | 73 | 74 | 76 | 77 | com.intellij.modules.lang 78 | com.intellij.modules.java 79 | org.jetbrains.android 80 | 81 | 82 | 84 | 85 |
86 | -------------------------------------------------------------------------------- /src/main/java/com/kogitune/intellij/codeinsight/postfix/templates/surround/SnackbarTemplate.java: -------------------------------------------------------------------------------- 1 | package com.kogitune.intellij.codeinsight.postfix.templates.surround; 2 | 3 | import com.intellij.codeInsight.template.Template; 4 | import com.intellij.codeInsight.template.impl.ConstantNode; 5 | import com.intellij.codeInsight.template.impl.EmptyNode; 6 | import com.intellij.codeInsight.template.impl.MacroCallNode; 7 | import com.intellij.codeInsight.template.macro.VariableOfTypeMacro; 8 | import com.intellij.openapi.util.Condition; 9 | import com.intellij.psi.PsiElement; 10 | import com.kogitune.intellij.codeinsight.postfix.internal.AbstractRichStringBasedPostfixTemplate; 11 | import com.kogitune.intellij.codeinsight.postfix.macro.ToStringIfNeedMacro; 12 | import com.kogitune.intellij.codeinsight.postfix.utils.AndroidPostfixTemplatesUtils; 13 | import org.jetbrains.annotations.NotNull; 14 | import org.jetbrains.annotations.Nullable; 15 | 16 | import static com.kogitune.intellij.codeinsight.postfix.utils.AndroidClassName.SNACKBAR; 17 | import static com.kogitune.intellij.codeinsight.postfix.utils.AndroidClassName.VIEW; 18 | 19 | 20 | public class SnackbarTemplate extends AbstractRichStringBasedPostfixTemplate { 21 | 22 | private boolean mWithAction; 23 | 24 | public SnackbarTemplate() { 25 | this("snack", "Snackbar.make(view, expr, Snackbar.LENGTH_SHORT).show();", AndroidPostfixTemplatesUtils.IS_NON_NULL); 26 | } 27 | 28 | public SnackbarTemplate(boolean withAction) { 29 | this("snackaction", "Snackbar.make(view, expr, Snackbar.LENGTH_LONG)\n" + 30 | " .setAction(actionText, new View.OnClickListener() {\n" + 31 | " @Override\n" + 32 | " public void onClick(View v) {\n" + 33 | " " + 34 | " }\n" + 35 | " })\n" + 36 | " .show();", AndroidPostfixTemplatesUtils.IS_NON_NULL); 37 | mWithAction = withAction; 38 | } 39 | 40 | protected SnackbarTemplate(@NotNull String name, @NotNull String example, @NotNull Condition typeChecker) { 41 | super(name, example, typeChecker); 42 | } 43 | 44 | @Nullable 45 | @Override 46 | public String getTemplateString(@NotNull PsiElement element) { 47 | String prefix = getStaticPrefix(SNACKBAR, "make", element); 48 | if (mWithAction) { 49 | return prefix + "($view$, $expr$, Snackbar.LENGTH_LONG)\n" + 50 | " .setAction($actionText$, new View.OnClickListener() {\n" + 51 | " @Override\n" + 52 | " public void onClick(View v) {\n" + 53 | " $action$\n" + 54 | " }\n" + 55 | " })\n" + 56 | " .show();$END$"; 57 | } else { 58 | return prefix + "($view$, $expr$, Snackbar.LENGTH_SHORT).show();$END$"; 59 | } 60 | } 61 | 62 | @Override 63 | protected void addExprVariable(@NotNull PsiElement expr, Template template) { 64 | ToStringIfNeedMacro toStringIfNeedMacro = new ToStringIfNeedMacro(); 65 | MacroCallNode macroCallNode = new MacroCallNode(toStringIfNeedMacro); 66 | macroCallNode.addParameter(new ConstantNode(expr.getText())); 67 | 68 | MacroCallNode node = new MacroCallNode(new VariableOfTypeMacro()); 69 | node.addParameter(new ConstantNode(VIEW.toString())); 70 | template.addVariable("view", node, new EmptyNode(), false); 71 | 72 | template.addVariable("expr", macroCallNode, false); 73 | 74 | if (mWithAction) { 75 | template.addVariable("actionText", new EmptyNode(), false); 76 | template.addVariable("action", new EmptyNode(), false); 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /src/main/java/com/kogitune/intellij/codeinsight/postfix/macro/FindViewByIdMacro.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 takahirom 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.kogitune.intellij.codeinsight.postfix.macro; 18 | 19 | import com.android.ide.common.res2.ResourceItem; 20 | import com.android.resources.ResourceType; 21 | import com.android.tools.idea.res.LocalResourceRepository; 22 | import com.android.tools.idea.res.ProjectResourceRepository; 23 | import com.intellij.codeInsight.template.*; 24 | import com.intellij.codeInsight.template.impl.ConstantNode; 25 | import com.intellij.codeInsight.template.impl.MacroCallNode; 26 | import com.intellij.codeInsight.template.macro.VariableOfTypeMacro; 27 | import com.intellij.facet.Facet; 28 | import com.intellij.facet.FacetManager; 29 | import com.intellij.openapi.module.Module; 30 | import com.intellij.openapi.module.ModuleManager; 31 | import com.intellij.openapi.project.Project; 32 | import org.fest.reflect.exception.ReflectionError; 33 | import org.fest.reflect.reference.TypeRef; 34 | import org.jetbrains.android.facet.AndroidFacet; 35 | import org.jetbrains.annotations.Nullable; 36 | 37 | import java.util.Arrays; 38 | import java.util.HashMap; 39 | import java.util.List; 40 | 41 | import static com.kogitune.intellij.codeinsight.postfix.utils.AndroidClassName.ACTIVITY; 42 | import static com.kogitune.intellij.codeinsight.postfix.utils.AndroidClassName.VIEW; 43 | import static org.fest.reflect.core.Reflection.*; 44 | 45 | /** 46 | * Created by takam on 2015/05/05. 47 | */ 48 | public class FindViewByIdMacro extends Macro { 49 | 50 | 51 | private HashMap mFileIdMap; 52 | 53 | public String getName() { 54 | return "find_view"; 55 | } 56 | 57 | public String getPresentableName() { 58 | return "find_view"; 59 | } 60 | 61 | @Nullable 62 | @Override 63 | public Result calculateResult(Expression[] expressions, ExpressionContext context) { 64 | if (expressions.length == 0) { 65 | return null; 66 | } 67 | 68 | Project project = context.getProject(); 69 | Expression expression = expressions[0]; 70 | final String resource = expression.calculateResult(context).toString(); 71 | final TextResult defaultResult = new TextResult("findViewById(" + resource + ")"); 72 | if (!resource.startsWith("R.id.")) { 73 | return defaultResult; 74 | } 75 | 76 | final int index = resource.lastIndexOf("."); 77 | final String resourceId = resource.substring(index + 1); 78 | 79 | String viewTag = getViewTag(project, resourceId, context); 80 | if (viewTag == null) { 81 | return defaultResult; 82 | } 83 | final String contextVariable = getContextVariable(context); 84 | if (contextVariable == null) { 85 | return new TextResult("(" + viewTag + ")findViewById(" + resource + ")"); 86 | } else { 87 | return new TextResult("(" + viewTag + ")" + contextVariable + ".findViewById(" + resource + ")"); 88 | } 89 | 90 | 91 | } 92 | 93 | private String getContextVariable(ExpressionContext context) { 94 | Result calculateResult = getVariableByFQDN(context, ACTIVITY.toString()); 95 | if (calculateResult == null) { 96 | // Retry by view 97 | calculateResult = getVariableByFQDN(context, VIEW.toString()); 98 | if (calculateResult == null) { 99 | return null; 100 | } 101 | } 102 | final String result = calculateResult.toString(); 103 | if (result == null || "".equals(result)) { 104 | return null; 105 | } 106 | if ("this".equals(result)) { 107 | return null; 108 | } 109 | return result; 110 | } 111 | 112 | private Result getVariableByFQDN(ExpressionContext context, String fqn) { 113 | MacroCallNode callNode = new MacroCallNode(new VariableOfTypeMacro()); 114 | callNode.addParameter(new ConstantNode(fqn)); 115 | return callNode.calculateResult(context); 116 | } 117 | 118 | 119 | @Nullable 120 | public String getViewTag(Project project, String resourceId, ExpressionContext context) { 121 | final ModuleManager moduleManager = ModuleManager.getInstance(project); 122 | List modules = Arrays.asList(moduleManager.getModules()); 123 | AndroidFacet androidFacet = null; 124 | for (Module module : modules) { 125 | for (Facet facet : FacetManager.getInstance(module).getAllFacets()) { 126 | if (facet instanceof AndroidFacet) { 127 | androidFacet = (AndroidFacet) facet; 128 | } 129 | } 130 | if (androidFacet == null) { 131 | continue; 132 | } 133 | try { 134 | Class oldResourceClass = type("com.android.tools.idea.rendering.ProjectResourceRepository").load(); 135 | Object resources = staticMethod("getProjectResources").withReturnType(type("com.android.tools.idea.rendering.LocalResourceRepository").load()).withParameterTypes(androidFacet.getClass(), boolean.class).in(oldResourceClass).invoke(androidFacet, true); 136 | List items = method("getResourceItem").withReturnType(new TypeRef>() { 137 | }).withParameterTypes(ResourceType.class, String.class).in(resources).invoke(ResourceType.ID, resourceId); 138 | if (items == null || items.size() == 0) { 139 | continue; 140 | } 141 | final ResourceItem resourceItem = items.get(0); 142 | final String viewTag = method("getViewTag").withReturnType(new TypeRef() { 143 | }).withParameterTypes(ResourceItem.class).in(resources).invoke(resourceItem); 144 | return viewTag; 145 | } catch (ReflectionError error) { 146 | final LocalResourceRepository resources = ProjectResourceRepository.getProjectResources(androidFacet, true); 147 | List items = resources.getResourceItem(ResourceType.ID, resourceId); 148 | if (items == null || items.size() == 0) { 149 | continue; 150 | } 151 | final ResourceItem resourceItem = items.get(0); 152 | final String viewTag = resources.getViewTag(resourceItem); 153 | return viewTag; 154 | } 155 | } 156 | return null; 157 | } 158 | } 159 | -------------------------------------------------------------------------------- /src/main/java/com/kogitune/intellij/codeinsight/postfix/internal/AbstractRichStringBasedPostfixTemplate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Bob Browning 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.kogitune.intellij.codeinsight.postfix.internal; 17 | 18 | import com.intellij.codeInsight.template.Template; 19 | import com.intellij.codeInsight.template.TemplateEditingAdapter; 20 | import com.intellij.codeInsight.template.TemplateManager; 21 | import com.intellij.codeInsight.template.impl.TextExpression; 22 | import com.intellij.codeInsight.template.postfix.templates.PostfixTemplateWithExpressionSelector; 23 | import com.intellij.codeInsight.template.postfix.templates.PostfixTemplatesUtils; 24 | import com.intellij.codeInsight.template.postfix.templates.StringBasedPostfixTemplate; 25 | import com.intellij.codeInsight.template.postfix.util.JavaPostfixTemplatesUtils; 26 | import com.intellij.openapi.actionSystem.ActionManager; 27 | import com.intellij.openapi.actionSystem.ActionPlaces; 28 | import com.intellij.openapi.actionSystem.AnAction; 29 | import com.intellij.openapi.actionSystem.impl.ActionManagerImpl; 30 | import com.intellij.openapi.editor.Document; 31 | import com.intellij.openapi.editor.Editor; 32 | import com.intellij.openapi.project.Project; 33 | import com.intellij.openapi.ui.playback.commands.ActionCommand; 34 | import com.intellij.openapi.util.Condition; 35 | import com.intellij.psi.PsiElement; 36 | import com.kogitune.intellij.codeinsight.postfix.utils.AndroidClassName; 37 | import com.kogitune.intellij.codeinsight.postfix.utils.AndroidPostfixTemplatesUtils; 38 | import org.jetbrains.annotations.NotNull; 39 | import org.jetbrains.annotations.Nullable; 40 | 41 | /** 42 | * Base class that is a modified form of {@link StringBasedPostfixTemplate} that passes the project to {@link 43 | * AbstractRichStringBasedPostfixTemplate#createTemplate} to allow querying of project properties. 44 | * 45 | * @author Bob Browning 46 | */ 47 | public abstract class AbstractRichStringBasedPostfixTemplate extends PostfixTemplateWithExpressionSelector { 48 | 49 | protected AbstractRichStringBasedPostfixTemplate(@NotNull String name, 50 | @NotNull String example, 51 | @NotNull Condition typeChecker) { 52 | super(name, example, JavaPostfixTemplatesUtils.selectorAllExpressionsWithCurrentOffset(typeChecker)); 53 | } 54 | 55 | @Override 56 | protected final void expandForChooseExpression(@NotNull PsiElement expr, @NotNull final Editor editor) { 57 | Project project = expr.getProject(); 58 | Document document = editor.getDocument(); 59 | PsiElement elementForRemoving = shouldRemoveParent() ? expr.getParent() : expr; 60 | document.deleteString(elementForRemoving.getTextRange().getStartOffset(), 61 | elementForRemoving.getTextRange().getEndOffset()); 62 | final TemplateManager manager = TemplateManager.getInstance(project); 63 | 64 | String templateString = getTemplateString(expr); 65 | if (templateString == null) { 66 | PostfixTemplatesUtils.showErrorHint(expr.getProject(), editor); 67 | return; 68 | } 69 | 70 | Template template = createTemplate(project, manager, templateString); 71 | 72 | if (shouldAddExpressionToContext()) { 73 | addExprVariable(expr, template); 74 | } 75 | 76 | setVariables(template, expr); 77 | manager.startTemplate(editor, template, new TemplateEditingAdapter() { 78 | @Override 79 | public void templateFinished(Template template, boolean brokenOff) { 80 | onTemplateFinished(manager, editor, template); 81 | } 82 | }); 83 | } 84 | 85 | protected void onTemplateFinished(TemplateManager manager, Editor editor, Template template) { 86 | // format and add ; 87 | final ActionManager actionManager = ActionManagerImpl.getInstance(); 88 | final String editorCompleteStatementText = "EditorCompleteStatement"; 89 | final AnAction action = actionManager.getAction(editorCompleteStatementText); 90 | actionManager.tryToExecute(action, ActionCommand.getInputEvent(editorCompleteStatementText), null, ActionPlaces.UNKNOWN, true); 91 | } 92 | 93 | protected void addExprVariable(@NotNull PsiElement expr, Template template) { 94 | template.addVariable("expr", new TextExpression(expr.getText()), false); 95 | } 96 | 97 | /** 98 | * Add custom variables to the template. 99 | * 100 | * @param template The template 101 | * @param element The expression being replaced 102 | */ 103 | protected void setVariables(@NotNull Template template, @NotNull PsiElement element) { 104 | } 105 | 106 | @Nullable 107 | public abstract String getTemplateString(@NotNull PsiElement element); 108 | 109 | /** 110 | * Returns true if the {@code expr} variable should be added to the template by default. 111 | */ 112 | protected boolean shouldAddExpressionToContext() { 113 | return true; 114 | } 115 | 116 | /** 117 | * Returns true if the formatting manager should be applied to the generated code block. 118 | */ 119 | protected boolean shouldReformat() { 120 | return false; 121 | } 122 | 123 | 124 | /** 125 | * Returns true if the parent element should be removed, for example for topmost expression. 126 | */ 127 | protected boolean shouldRemoveParent() { 128 | return false; 129 | } 130 | 131 | 132 | /** 133 | * Create a new instance of a code template for the current postfix template. 134 | * 135 | * @param project The current project 136 | * @param manager The template manager 137 | * @param templateString The template string 138 | */ 139 | protected Template createTemplate(Project project, TemplateManager manager, String templateString) { 140 | Template template = manager.createTemplate("", "", templateString); 141 | template.setToReformat(shouldReformat()); 142 | template.setValue(Template.Property.USE_STATIC_IMPORT_IF_POSSIBLE, false); 143 | return template; 144 | } 145 | 146 | /** 147 | * Gets the static method prefix for the android static method. 148 | * 149 | * @param className The android class name 150 | * @param methodName The method name 151 | * @param context The context element 152 | */ 153 | protected String getStaticPrefix(@NotNull AndroidClassName className, 154 | @NotNull String methodName, 155 | @NotNull PsiElement context) { 156 | return getStaticPrefix(className.getClassName(), methodName, context); 157 | } 158 | 159 | /** 160 | * Gets the static method prefix for the android static method. 161 | * 162 | * @param className The android class name 163 | * @param methodName The method name 164 | * @param context The context element 165 | */ 166 | protected String getStaticPrefix(@NotNull String className, 167 | @NotNull String methodName, 168 | @NotNull PsiElement context) { 169 | return AndroidPostfixTemplatesUtils.getStaticPrefix(className, methodName, context); 170 | } 171 | } 172 | -------------------------------------------------------------------------------- /src/main/java/com/kogitune/intellij/codeinsight/postfix/utils/AndroidPostfixTemplatesUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Bob Browning 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.kogitune.intellij.codeinsight.postfix.utils; 17 | 18 | import com.intellij.codeInsight.NullableNotNullManager; 19 | import com.intellij.openapi.util.Condition; 20 | import com.intellij.psi.*; 21 | import com.intellij.psi.util.InheritanceUtil; 22 | import com.intellij.psi.util.PsiTreeUtil; 23 | import com.siyeh.ig.psiutils.ParenthesesUtils; 24 | import org.jetbrains.annotations.Contract; 25 | import org.jetbrains.annotations.NonNls; 26 | import org.jetbrains.annotations.NotNull; 27 | import org.jetbrains.annotations.Nullable; 28 | 29 | import static com.intellij.codeInsight.template.postfix.util.JavaPostfixTemplatesUtils.*; 30 | import static com.kogitune.intellij.codeinsight.postfix.utils.ImportUtils.hasImportStatic; 31 | 32 | /** 33 | * Collection of helper methods for surrounding an expression with android preconditions. 34 | * 35 | * @author Bob Browning 36 | */ 37 | public class AndroidPostfixTemplatesUtils { 38 | 39 | 40 | public static final Condition IS_NON_NULL = new Condition() { 41 | @Override 42 | public boolean value(PsiElement element) { 43 | return IS_NON_VOID.value(element) && !AndroidPostfixTemplatesUtils.isAnnotatedNullable(element); 44 | } 45 | }; 46 | /** 47 | * Condition that returns true if the element is a {@link java.util.Map}. 48 | */ 49 | public static final Condition IS_MAP = new Condition() { 50 | @Override 51 | public boolean value(PsiElement element) { 52 | return element instanceof PsiExpression 53 | && InheritanceUtil.isInheritor(((PsiExpression) element).getType(), CommonClassNames.JAVA_UTIL_MAP); 54 | } 55 | }; 56 | /** 57 | * Condition that returns true if the element is an iterable. 58 | */ 59 | public static final Condition IS_ITERABLE = new Condition() { 60 | @Override 61 | public boolean value(PsiElement element) { 62 | if (element instanceof PsiExpression) { 63 | PsiType type = ((PsiExpression) element).getType(); 64 | return isIterable(type); 65 | } 66 | return false; 67 | } 68 | }; 69 | /** 70 | * Condition that returns true if the element is an iterable or an iterator or an array. 71 | */ 72 | public static final Condition IS_ARRAY_OR_ITERABLE_OR_ITERATOR = new Condition() { 73 | @Override 74 | public boolean value(PsiElement element) { 75 | if (element instanceof PsiExpression) { 76 | PsiType type = ((PsiExpression) element).getType(); 77 | return isIterator(type) || isArray(type) || isIterable(type); 78 | } 79 | return false; 80 | } 81 | }; 82 | /** 83 | * Condition that returns true if the element is an iterable or an iterator or an object array or a collection. 84 | */ 85 | public static final Condition IS_NON_PRIMITIVE_ARRAY_OR_ITERABLE_OR_ITERATOR = 86 | new Condition() { 87 | @Override 88 | public boolean value(PsiElement element) { 89 | if (element instanceof PsiExpression) { 90 | PsiType type = ((PsiExpression) element).getType(); 91 | return isObjectArrayTypeExpression(type) 92 | || isIterable(type) 93 | || isIterator(type); 94 | } 95 | return false; 96 | } 97 | }; 98 | @NonNls 99 | private static final String JAVA_LANG_CHAR_SEQUENCE = "java.lang.CharSequence"; 100 | /** 101 | * Condition that returns true if the element is a {@link CharSequence}. 102 | */ 103 | public static final Condition IS_CHAR_SEQUENCE = new Condition() { 104 | @Override 105 | public boolean value(PsiElement element) { 106 | return element instanceof PsiExpression 107 | && isCharSequence(((PsiExpression) element).getType()); 108 | } 109 | }; 110 | 111 | /** 112 | * Condition that returns true if the element is a android.view.View. 113 | */ 114 | public static final Condition IS_VIEW = new Condition() { 115 | @Override 116 | public boolean value(PsiElement element) { 117 | return element instanceof PsiExpression 118 | && InheritanceUtil.isInheritor(((PsiExpression) element).getType(), AndroidClassName.VIEW.getClassName()); 119 | } 120 | }; 121 | 122 | @Contract("null -> false") 123 | public static boolean isCollection(@Nullable PsiType type) { 124 | return type != null && InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_UTIL_COLLECTION); 125 | } 126 | 127 | @Contract("null -> false") 128 | public static boolean isIterator(@Nullable PsiType type) { 129 | return type != null && InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_UTIL_ITERATOR); 130 | } 131 | 132 | 133 | @Contract("null -> false") 134 | public static boolean isCharSequence(@Nullable PsiType type) { 135 | return type != null && InheritanceUtil.isInheritor(type, JAVA_LANG_CHAR_SEQUENCE); 136 | } 137 | 138 | @Contract("null -> false") 139 | public static boolean isObjectArrayTypeExpression(@Nullable PsiType type) { 140 | return type instanceof PsiArrayType && 141 | !(((PsiArrayType) type).getComponentType() instanceof PsiPrimitiveType); 142 | } 143 | 144 | /** 145 | * Gets the expression for acquiring the size of the specified expression. 146 | * 147 | * @param expr The expression to evaluate 148 | */ 149 | @Nullable 150 | public static String getExpressionNumberOrArrayOrIterableBound(@NotNull PsiExpression expr) { 151 | PsiType type = expr.getType(); 152 | if (isNumber(type)) { 153 | return expr.getText(); 154 | } else if (isArray(type)) { 155 | return expr.getText() + ".length"; 156 | } else if (isCollection(type)) { 157 | return expr.getText() + ".size()"; 158 | } else if (isIterable(type)) { 159 | return "com.google.common.collect.Iterables.size(" + expr.getText() + ")"; 160 | } 161 | return null; 162 | } 163 | 164 | /** 165 | * Get the prefix required for the specified imported static method within the current context. 166 | * 167 | * @param fqClassName The qualified class name 168 | * @param methodName The method name 169 | * @param context The current context 170 | */ 171 | public static String getStaticPrefix(@NotNull String fqClassName, @NotNull String methodName, 172 | @NotNull PsiElement context) { 173 | return hasImportStatic(fqClassName, methodName, context) ? methodName : (fqClassName + "." + methodName); 174 | } 175 | 176 | public static boolean isTopmostExpression(@NotNull PsiElement element) { 177 | return element.equals(getTopmostExpression(element)); 178 | } 179 | 180 | @Contract("null -> false") 181 | public static boolean isSemicolonNeeded(PsiElement context) { 182 | PsiStatement statement = PsiTreeUtil.getParentOfType(context, PsiStatement.class); 183 | return statement != null && statement.getLastChild() instanceof PsiErrorElement; 184 | } 185 | 186 | public static boolean isAnnotatedNullable(PsiElement element) { 187 | PsiExpression expression; 188 | if (element instanceof PsiExpression) { 189 | expression = (PsiExpression) element; 190 | } else { 191 | expression = PsiTreeUtil.getParentOfType(element, PsiExpression.class, true); 192 | if (expression == null) { 193 | return false; 194 | } 195 | } 196 | expression = ParenthesesUtils.stripParentheses(expression); 197 | if (!(expression instanceof PsiReferenceExpression)) { 198 | return false; 199 | } 200 | final PsiReferenceExpression referenceExpression = (PsiReferenceExpression) expression; 201 | final PsiElement target = referenceExpression.resolve(); 202 | if (!(target instanceof PsiModifierListOwner)) { 203 | return false; 204 | } 205 | final PsiModifierListOwner modifierListOwner = (PsiModifierListOwner) target; 206 | return NullableNotNullManager.isNullable(modifierListOwner); 207 | } 208 | } 209 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2015 takahirom 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------