├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md ├── build.gradle ├── docs └── README-EN.md ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src └── main ├── java ├── NewProvider.java ├── helper │ ├── ProviderConfig.java │ └── ProviderTaoData.java ├── intention_action │ ├── SnippetType.java │ ├── Snippets.java │ ├── WrapHelper.kt │ ├── WrapWithAction.java │ ├── WrapWithChangeNotifierProviderAction.java │ ├── WrapWithConsumerAction.java │ ├── WrapWithSelectorAction.java │ └── WrapWithSelectorShouldRebuildAction.java ├── live_templates │ ├── ProviderContext.java │ └── ProviderTemplateProvider.java └── setting │ ├── SettingsComponent.java │ └── SettingsConfigurable.java └── resources ├── META-INF ├── plugin.xml └── pluginIcon.svg ├── image └── provider_icon.png ├── intentionDescriptions ├── WrapWithChangeNotifierProviderAction │ ├── after.java.template │ ├── before.java.template │ └── description.html ├── WrapWithConsumerAction │ ├── after.java.template │ ├── before.java.template │ └── description.html ├── WrapWithSelectorAction │ ├── after.java.template │ ├── before.java.template │ └── description.html └── WrapWithSelectorShouldRebuildAction │ ├── after.java.template │ ├── before.java.template │ └── description.html ├── liveTemplates └── provider.xml └── templates ├── high ├── provider.dart ├── state.dart └── view.dart ├── provider.dart └── view.dart /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /build/ 2 | /.gradle/ 3 | /.idea/ 4 | local.properties -------------------------------------------------------------------------------- /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 [yyyy] [name of copyright owner] 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # provider_template 2 | 3 | 语言: [中文简体](https://juejin.cn/post/6968272002515894303) 4 | 5 | ## provider usage 6 | 7 | - 掘金:[使用+源码剖析:Flutter Provider的另一面](https://juejin.cn/post/6968272002515894303) 8 | 9 | ## Renderings 10 | 11 | - Plug-in effect 12 | 13 | - There are some optional functions, so make it into a multi-button style 14 | - you can operate according to your own needs 15 | 16 | ![provider_plugin](https://cdn.jsdelivr.net/gh/CNAD666/MyData@master/pic/flutter/blog/20210521162031.gif) 17 | 18 | - Alt + Enter : Consumer、Selector、ChangeNotifirProvider 19 | 20 | ![image-20210605152137006](https://cdn.jsdelivr.net/gh/CNAD666/MyData@master/pic/android/flutter/blog/20210605152140.png) 21 | 22 | - enter the **provider** prefix 23 | 24 | ![image-20210605152343392](https://cdn.jsdelivr.net/gh/CNAD666/MyData@master/pic/android/flutter/blog/20210605152735.png) 25 | 26 | ## Description 27 | 28 | The description of the plugin 29 | 30 | - Model: generates the provider mode, 31 | 32 | - Default: the Default mode. two files are generated: view, provider 33 | - High: in simple mode, three files are generated: view, provider, state 34 | - Extended: a extended usage 35 | 36 | - Function: Function selection 37 | - useFolder: use a file. After you select it, a folder is generated. The Hump name is automatically converted to lowercase + underscore. 38 | - usePrefix: use the prefix, the generated file front prefix, prefix for: large hump named automatically converted to: lowercase + underline 39 | - null-safety: support to null-safety 40 | 41 | - Module Name: the Name of the Module. Use the hump Name. 42 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | id 'org.jetbrains.intellij' version '0.6.5' 4 | id 'org.jetbrains.kotlin.jvm' version '1.5.10' 5 | } 6 | 7 | apply plugin: 'org.jetbrains.intellij' 8 | apply plugin: 'java' 9 | apply plugin: 'kotlin' 10 | apply plugin: 'idea' 11 | 12 | group 'com.tao.provider' 13 | // If the plug-in related properties are set here, 14 | // the corresponding settings in Plugin.xml will be invalid 15 | // The two settings below are updated frequently, so put them here 16 | // The version number is modified here 17 | version '1.5.3' 18 | 19 | // must!!! 20 | sourceCompatibility = 1.8 21 | 22 | repositories { 23 | mavenCentral() 24 | } 25 | 26 | dependencies { 27 | compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8" 28 | testCompile group: 'junit', name: 'junit', version: '4.12' 29 | implementation "com.fleshgrinder.kotlin:case-format:0.2.0" 30 | } 31 | 32 | //compileKotlin { 33 | // kotlinOptions.jvmTarget = "1.8" 34 | //} 35 | //compileTestKotlin { 36 | // kotlinOptions.jvmTarget = "1.8" 37 | //} 38 | 39 | // See https://github.com/JetBrains/gradle-intellij-plugin/ 40 | // updateSinceUntilBuild no restrictions on plug-in installation corresponding 41 | // to the idea version 42 | intellij { 43 | version '2020.3.2' 44 | updateSinceUntilBuild false 45 | } 46 | 47 | 48 | // Update instructions are here 49 | patchPluginXml { 50 | changeNotes """ 51 |

1.5.x

52 | 57 | 58 |

1.3.x

59 | 65 | 66 |

1.1.x

67 | 71 | 72 |

1.0.x

73 | 83 | """ 84 | } 85 | 86 | -------------------------------------------------------------------------------- /docs/README-EN.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xdd666t/provider_template/4bf75e2bedc0451fa2b59a7ea9d16a707b463020/docs/README-EN.md -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xdd666t/provider_template/4bf75e2bedc0451fa2b59a7ea9d16a707b463020/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Jan 25 17:21:07 GMT+08:00 2021 2 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-all.zip 3 | distributionBase=GRADLE_USER_HOME 4 | distributionPath=wrapper/dists 5 | zipStorePath=wrapper/dists 6 | zipStoreBase=GRADLE_USER_HOME 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # 4 | # Copyright 2015 the original author or authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | ## 21 | ## Gradle start up script for UN*X 22 | ## 23 | ############################################################################## 24 | 25 | # Attempt to set APP_HOME 26 | # Resolve links: $0 may be a link 27 | PRG="$0" 28 | # Need this for relative symlinks. 29 | while [ -h "$PRG" ] ; do 30 | ls=`ls -ld "$PRG"` 31 | link=`expr "$ls" : '.*-> \(.*\)$'` 32 | if expr "$link" : '/.*' > /dev/null; then 33 | PRG="$link" 34 | else 35 | PRG=`dirname "$PRG"`"/$link" 36 | fi 37 | done 38 | SAVED="`pwd`" 39 | cd "`dirname \"$PRG\"`/" >/dev/null 40 | APP_HOME="`pwd -P`" 41 | cd "$SAVED" >/dev/null 42 | 43 | APP_NAME="Gradle" 44 | APP_BASE_NAME=`basename "$0"` 45 | 46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 48 | 49 | # Use the maximum available, or set MAX_FD != -1 to use that value. 50 | MAX_FD="maximum" 51 | 52 | warn () { 53 | echo "$*" 54 | } 55 | 56 | die () { 57 | echo 58 | echo "$*" 59 | echo 60 | exit 1 61 | } 62 | 63 | # OS specific support (must be 'true' or 'false'). 64 | cygwin=false 65 | msys=false 66 | darwin=false 67 | nonstop=false 68 | case "`uname`" in 69 | CYGWIN* ) 70 | cygwin=true 71 | ;; 72 | Darwin* ) 73 | darwin=true 74 | ;; 75 | MINGW* ) 76 | msys=true 77 | ;; 78 | NONSTOP* ) 79 | nonstop=true 80 | ;; 81 | esac 82 | 83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 84 | 85 | 86 | # Determine the Java command to use to start the JVM. 87 | if [ -n "$JAVA_HOME" ] ; then 88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 89 | # IBM's JDK on AIX uses strange locations for the executables 90 | JAVACMD="$JAVA_HOME/jre/sh/java" 91 | else 92 | JAVACMD="$JAVA_HOME/bin/java" 93 | fi 94 | if [ ! -x "$JAVACMD" ] ; then 95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 96 | 97 | Please set the JAVA_HOME variable in your environment to match the 98 | location of your Java installation." 99 | fi 100 | else 101 | JAVACMD="java" 102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 103 | 104 | Please set the JAVA_HOME variable in your environment to match the 105 | location of your Java installation." 106 | fi 107 | 108 | # Increase the maximum file descriptors if we can. 109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 110 | MAX_FD_LIMIT=`ulimit -H -n` 111 | if [ $? -eq 0 ] ; then 112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 113 | MAX_FD="$MAX_FD_LIMIT" 114 | fi 115 | ulimit -n $MAX_FD 116 | if [ $? -ne 0 ] ; then 117 | warn "Could not set maximum file descriptor limit: $MAX_FD" 118 | fi 119 | else 120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 121 | fi 122 | fi 123 | 124 | # For Darwin, add options to specify how the application appears in the dock 125 | if $darwin; then 126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 127 | fi 128 | 129 | # For Cygwin or MSYS, switch paths to Windows format before running java 130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then 131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 133 | 134 | JAVACMD=`cygpath --unix "$JAVACMD"` 135 | 136 | # We build the pattern for arguments to be converted via cygpath 137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 138 | SEP="" 139 | for dir in $ROOTDIRSRAW ; do 140 | ROOTDIRS="$ROOTDIRS$SEP$dir" 141 | SEP="|" 142 | done 143 | OURCYGPATTERN="(^($ROOTDIRS))" 144 | # Add a user-defined pattern to the cygpath arguments 145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 147 | fi 148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 149 | i=0 150 | for arg in "$@" ; do 151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 153 | 154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 156 | else 157 | eval `echo args$i`="\"$arg\"" 158 | fi 159 | i=`expr $i + 1` 160 | done 161 | case $i in 162 | 0) set -- ;; 163 | 1) set -- "$args0" ;; 164 | 2) set -- "$args0" "$args1" ;; 165 | 3) set -- "$args0" "$args1" "$args2" ;; 166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;; 167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 172 | esac 173 | fi 174 | 175 | # Escape application args 176 | save () { 177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 178 | echo " " 179 | } 180 | APP_ARGS=`save "$@"` 181 | 182 | # Collect all arguments for the java command, following the shell quoting and substitution rules 183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 184 | 185 | exec "$JAVACMD" "$@" 186 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto init 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto init 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :init 68 | @rem Get command-line arguments, handling Windows variants 69 | 70 | if not "%OS%" == "Windows_NT" goto win9xME_args 71 | 72 | :win9xME_args 73 | @rem Slurp the command line arguments. 74 | set CMD_LINE_ARGS= 75 | set _SKIP=2 76 | 77 | :win9xME_args_slurp 78 | if "x%~1" == "x" goto execute 79 | 80 | set CMD_LINE_ARGS=%* 81 | 82 | :execute 83 | @rem Setup the command line 84 | 85 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 86 | 87 | 88 | @rem Execute Gradle 89 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 90 | 91 | :end 92 | @rem End local scope for the variables with windows NT shell 93 | if "%ERRORLEVEL%"=="0" goto mainEnd 94 | 95 | :fail 96 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 97 | rem the _cmd.exe /c_ return code! 98 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 99 | exit /b 1 100 | 101 | :mainEnd 102 | if "%OS%"=="Windows_NT" endlocal 103 | 104 | :omega 105 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'provider_template' 2 | 3 | -------------------------------------------------------------------------------- /src/main/java/NewProvider.java: -------------------------------------------------------------------------------- 1 | import com.google.common.base.CaseFormat; 2 | import com.intellij.openapi.actionSystem.AnAction; 3 | import com.intellij.openapi.actionSystem.AnActionEvent; 4 | import com.intellij.openapi.actionSystem.PlatformDataKeys; 5 | import com.intellij.openapi.project.Project; 6 | import com.intellij.openapi.ui.Messages; 7 | import com.intellij.ui.JBColor; 8 | import helper.ProviderConfig; 9 | import helper.ProviderTaoData; 10 | 11 | import javax.swing.*; 12 | import java.awt.*; 13 | import java.awt.event.ActionEvent; 14 | import java.awt.event.ActionListener; 15 | import java.awt.event.KeyEvent; 16 | import java.awt.event.KeyListener; 17 | import java.io.*; 18 | 19 | 20 | public class NewProvider extends AnAction { 21 | private Project project; 22 | private String psiPath; 23 | private ProviderTaoData data; 24 | 25 | /** 26 | * Overall popup entity 27 | */ 28 | private JDialog jDialog; 29 | private JTextField nameTextField; 30 | private ButtonGroup templateGroup; 31 | /** 32 | * Checkbox 33 | * Use folder:default true 34 | * Use prefix:default false 35 | */ 36 | private JCheckBox folderBox, prefixBox; 37 | 38 | @Override 39 | public void actionPerformed(AnActionEvent event) { 40 | project = event.getProject(); 41 | psiPath = event.getData(PlatformDataKeys.PSI_ELEMENT).toString(); 42 | psiPath = psiPath.substring(psiPath.indexOf(":") + 1); 43 | initData(); 44 | initView(); 45 | } 46 | 47 | private void initData() { 48 | data = ProviderTaoData.getInstance(); 49 | jDialog = new JDialog(new JFrame(), "Provider Template Code Produce"); 50 | } 51 | 52 | private void initView() { 53 | //Set function button 54 | Container container = jDialog.getContentPane(); 55 | container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS)); 56 | 57 | //Set the main module style: mode, function 58 | //deal default value 59 | setMode(container); 60 | 61 | //Setting options: whether to use prefix 62 | //deal default value 63 | setCodeFile(container); 64 | 65 | //Generate module name and OK cancel button 66 | setModuleAndConfirm(container); 67 | 68 | //Choose a pop-up style 69 | setJDialog(); 70 | } 71 | 72 | /** 73 | * generate file 74 | */ 75 | private void save() { 76 | if (nameTextField.getText() == null || "".equals(nameTextField.getText().trim())) { 77 | Messages.showInfoMessage(project, "Please input the module name", "Info"); 78 | return; 79 | } 80 | dispose(); 81 | //Create a file 82 | createFile(); 83 | //Refresh project 84 | project.getBaseDir().refresh(false, true); 85 | } 86 | 87 | /** 88 | * Set the overall pop-up style 89 | */ 90 | private void setJDialog() { 91 | //The focus is on the current pop-up window, 92 | // and the focus will not shift even if you click on other areas 93 | jDialog.setModal(true); 94 | //Set padding 95 | ((JPanel) jDialog.getContentPane()).setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); 96 | jDialog.setSize(430, 350); 97 | jDialog.setLocationRelativeTo(null); 98 | jDialog.setVisible(true); 99 | } 100 | 101 | /** 102 | * Main module 103 | */ 104 | private void setMode(Container container) { 105 | //Two rows and two columns 106 | JPanel template = new JPanel(); 107 | template.setLayout(new GridLayout(1, 2)); 108 | //Set the main module style:mode, function 109 | template.setBorder(BorderFactory.createTitledBorder("Select Mode")); 110 | //default: high setting 111 | JRadioButton defaultBtn = new JRadioButton(ProviderConfig.modeDefault, data.defaultMode == 0); 112 | defaultBtn.setActionCommand(ProviderConfig.modeDefault); 113 | setPadding(defaultBtn, 5, 10); 114 | JRadioButton highBtn = new JRadioButton(ProviderConfig.modeHigh, data.defaultMode == 1); 115 | setPadding(highBtn, 5, 10); 116 | highBtn.setActionCommand(ProviderConfig.modeHigh); 117 | 118 | template.add(defaultBtn); 119 | template.add(highBtn); 120 | templateGroup = new ButtonGroup(); 121 | templateGroup.add(defaultBtn); 122 | templateGroup.add(highBtn); 123 | 124 | container.add(template); 125 | setDivision(container); 126 | } 127 | 128 | /** 129 | * Generate file 130 | */ 131 | private void setCodeFile(Container container) { 132 | //Select build file 133 | JPanel file = new JPanel(); 134 | file.setLayout(new GridLayout(1, 2)); 135 | file.setBorder(BorderFactory.createTitledBorder("Select Function")); 136 | 137 | //use folder 138 | folderBox = new JCheckBox("useFolder", data.useFolder); 139 | setMargin(folderBox, 5, 10); 140 | file.add(folderBox); 141 | 142 | //use prefix 143 | prefixBox = new JCheckBox("usePrefix", data.usePrefix); 144 | setMargin(prefixBox, 5, 10); 145 | file.add(prefixBox); 146 | 147 | container.add(file); 148 | setDivision(container); 149 | } 150 | 151 | 152 | /** 153 | * Generate file name and button 154 | */ 155 | private void setModuleAndConfirm(Container container) { 156 | JPanel nameField = new JPanel(); 157 | nameField.setLayout(new FlowLayout()); 158 | nameField.setBorder(BorderFactory.createTitledBorder("Module Name")); 159 | nameTextField = new JTextField(28); 160 | nameTextField.addKeyListener(keyListener); 161 | nameField.add(nameTextField); 162 | container.add(nameField); 163 | 164 | JPanel menu = new JPanel(); 165 | menu.setLayout(new FlowLayout()); 166 | 167 | //Set bottom spacing 168 | setDivision(container); 169 | 170 | //OK cancel button 171 | JButton cancel = new JButton("Cancel"); 172 | cancel.setForeground(JBColor.RED); 173 | cancel.addActionListener(actionListener); 174 | 175 | JButton ok = new JButton("OK"); 176 | ok.setForeground(JBColor.GREEN); 177 | ok.addActionListener(actionListener); 178 | menu.add(cancel); 179 | menu.add(ok); 180 | container.add(menu); 181 | } 182 | 183 | private void createFile() { 184 | String type = templateGroup.getSelection().getActionCommand(); 185 | //deal default value 186 | if (ProviderConfig.modeDefault.equals(type)) { 187 | data.defaultMode = 0; 188 | } else if (ProviderConfig.modeHigh.equals(type)) { 189 | data.defaultMode = 1; 190 | } 191 | data.useFolder = folderBox.isSelected(); 192 | data.usePrefix = prefixBox.isSelected(); 193 | 194 | 195 | String name = upperCase(nameTextField.getText()); 196 | String prefix = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, name); 197 | String folder = ""; 198 | String prefixName = ""; 199 | 200 | //Add folder 201 | if (data.useFolder) { 202 | folder = "/" + prefix; 203 | } 204 | 205 | //Add prefix 206 | if (data.usePrefix) { 207 | prefixName = prefix + "_"; 208 | } 209 | 210 | switch (type) { 211 | case ProviderConfig.modeDefault: 212 | generateDefault(folder, prefixName); 213 | break; 214 | case ProviderConfig.modeHigh: 215 | generateHigh(folder, prefixName); 216 | break; 217 | } 218 | } 219 | 220 | private void generateDefault(String folder, String prefixName) { 221 | String path = psiPath + folder; 222 | generateFile("view.dart", path, prefixName + data.viewFileName.toLowerCase() + ".dart"); 223 | generateFile("provider.dart", path, prefixName + data.logicName.toLowerCase() + ".dart"); 224 | } 225 | 226 | private void generateHigh(String folder, String prefixName) { 227 | String path = psiPath + folder; 228 | generateFile("high/view.dart", path, prefixName + data.viewFileName.toLowerCase() + ".dart"); 229 | generateFile("high/provider.dart", path, prefixName + data.logicName.toLowerCase() + ".dart"); 230 | generateFile("high/state.dart", path, prefixName + data.stateName.toLowerCase() + ".dart"); 231 | } 232 | 233 | 234 | private void generateFile(String inputFileName, String filePath, String outFileName) { 235 | //content deal 236 | String content = dealContent(inputFileName, outFileName); 237 | 238 | //Write file 239 | try { 240 | File folder = new File(filePath); 241 | // if file doesnt exists, then create it 242 | if (!folder.exists()) { 243 | folder.mkdirs(); 244 | } 245 | File file = new File(filePath + "/" + outFileName); 246 | if (!file.exists()) { 247 | file.createNewFile(); 248 | } 249 | 250 | FileWriter fw = new FileWriter(file.getAbsoluteFile()); 251 | BufferedWriter bw = new BufferedWriter(fw); 252 | bw.write(content); 253 | bw.close(); 254 | 255 | } catch (IOException e) { 256 | e.printStackTrace(); 257 | } 258 | } 259 | 260 | //content need deal 261 | private String dealContent(String inputFileName, String outFileName) { 262 | //name baseFolder 263 | String baseFolder = "/templates/"; 264 | 265 | //read file 266 | String content = ""; 267 | try { 268 | InputStream in = this.getClass().getResourceAsStream(baseFolder + inputFileName); 269 | content = new String(readStream(in)); 270 | } catch (Exception e) { 271 | } 272 | 273 | String prefixName = ""; 274 | //Adding a prefix requires modifying the imported class name 275 | if (data.usePrefix) { 276 | prefixName = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, upperCase(nameTextField.getText())) + "_"; 277 | } 278 | //replace logic 279 | if (outFileName.contains(data.logicName.toLowerCase())) { 280 | content = content.replaceAll("state.dart", prefixName + data.stateName.toLowerCase() + ".dart"); 281 | content = content.replaceAll("Provider", data.logicName); 282 | content = content.replaceAll("State", data.stateName); 283 | content = content.replaceAll("final state", "final " + data.stateName.toLowerCase()); 284 | } 285 | //replace state 286 | if (outFileName.contains(data.stateName.toLowerCase())) { 287 | content = content.replaceAll("State", data.stateName); 288 | } 289 | //replace view 290 | if (outFileName.contains(data.viewFileName.toLowerCase())) { 291 | content = content.replace("\'provider.dart\'", "\'" + prefixName + data.logicName.toLowerCase() + ".dart" + "\'"); 292 | content = content.replace("Page", data.viewName); 293 | content = content.replace("nameProvider", "name" + data.logicName); 294 | content = content.replace("final provider", "final " + data.logicName.toLowerCase()); 295 | content = content.replace("=> provider", "=> " + data.logicName.toLowerCase()); 296 | } 297 | 298 | content = content.replaceAll("\\$name", upperCase(nameTextField.getText())); 299 | 300 | return content; 301 | } 302 | 303 | private byte[] readStream(InputStream inStream) throws Exception { 304 | ByteArrayOutputStream outSteam = new ByteArrayOutputStream(); 305 | try { 306 | byte[] buffer = new byte[1024]; 307 | int len = -1; 308 | while ((len = inStream.read(buffer)) != -1) { 309 | outSteam.write(buffer, 0, len); 310 | System.out.println(new String(buffer)); 311 | } 312 | 313 | } catch (IOException e) { 314 | } finally { 315 | outSteam.close(); 316 | inStream.close(); 317 | } 318 | return outSteam.toByteArray(); 319 | } 320 | 321 | 322 | private final KeyListener keyListener = new KeyListener() { 323 | @Override 324 | public void keyTyped(KeyEvent e) { 325 | } 326 | 327 | @Override 328 | public void keyPressed(KeyEvent e) { 329 | if (e.getKeyCode() == KeyEvent.VK_ENTER) save(); 330 | if (e.getKeyCode() == KeyEvent.VK_ESCAPE) dispose(); 331 | } 332 | 333 | @Override 334 | public void keyReleased(KeyEvent e) { 335 | } 336 | }; 337 | 338 | private final ActionListener actionListener = new ActionListener() { 339 | @Override 340 | public void actionPerformed(ActionEvent e) { 341 | if (e.getActionCommand().equals("Cancel")) { 342 | dispose(); 343 | } else { 344 | save(); 345 | } 346 | } 347 | }; 348 | 349 | private void setPadding(JRadioButton btn, int top, int bottom) { 350 | btn.setBorder(BorderFactory.createEmptyBorder(top, 10, bottom, 0)); 351 | } 352 | 353 | private void setMargin(JCheckBox btn, int top, int bottom) { 354 | btn.setBorder(BorderFactory.createEmptyBorder(top, 10, bottom, 0)); 355 | } 356 | 357 | private void setDivision(Container container) { 358 | //Separate the spacing between modules 359 | JPanel margin = new JPanel(); 360 | container.add(margin); 361 | } 362 | 363 | private void dispose() { 364 | jDialog.dispose(); 365 | } 366 | 367 | private String upperCase(String str) { 368 | return str.substring(0, 1).toUpperCase() + str.substring(1); 369 | } 370 | } -------------------------------------------------------------------------------- /src/main/java/helper/ProviderConfig.java: -------------------------------------------------------------------------------- 1 | package helper; 2 | 3 | // set default value 4 | public class ProviderConfig { 5 | // use high mode 6 | // 0:default 1:high 7 | public static int defaultMode = 0; 8 | 9 | //default true 10 | public static boolean useFolder = true; 11 | 12 | //default false 13 | public static boolean usePrefix = false; 14 | 15 | //default false 16 | public static boolean nullSafety = false; 17 | 18 | 19 | //Logical layer name 20 | public static String logicName = "Provider"; 21 | 22 | //view layer name 23 | public static String viewName = "Page"; 24 | public static String viewFileName = "View"; 25 | 26 | //state layer name 27 | public static String stateName = "State"; 28 | 29 | //mode name 30 | public static final String modeDefault = "Default"; 31 | public static final String modeHigh = "High"; 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/helper/ProviderTaoData.java: -------------------------------------------------------------------------------- 1 | package helper; 2 | 3 | import com.intellij.openapi.components.PersistentStateComponent; 4 | import com.intellij.openapi.components.ServiceManager; 5 | import com.intellij.openapi.components.Storage; 6 | import com.intellij.util.xmlb.XmlSerializerUtil; 7 | 8 | //如果包名和类名和其他插件相同,会引起IDEA奔溃的坑比问题 9 | //故此处类名起的稍微与众不同点 10 | //custom save location 11 | @com.intellij.openapi.components.State( 12 | name = "ProviderDataService", 13 | storages = {@Storage(value = "ProviderDataService.xml") 14 | }) 15 | public class ProviderTaoData implements PersistentStateComponent { 16 | // 0:default 1:high 2:extended 17 | public int defaultMode = ProviderConfig.defaultMode; 18 | 19 | //default true 20 | public boolean useFolder = ProviderConfig.useFolder; 21 | 22 | //default false 23 | public boolean usePrefix = ProviderConfig.usePrefix; 24 | 25 | 26 | //Logical layer name 27 | public String logicName = ProviderConfig.logicName; 28 | 29 | //view layer name 30 | public String viewName = ProviderConfig.viewName; 31 | public String viewFileName = ProviderConfig.viewFileName; 32 | 33 | //state layer name 34 | public String stateName = ProviderConfig.stateName; 35 | 36 | public static ProviderTaoData getInstance() { 37 | return ServiceManager.getService(ProviderTaoData.class); 38 | } 39 | 40 | @Override 41 | public ProviderTaoData getState() { 42 | return this; 43 | } 44 | 45 | @Override 46 | public void loadState(ProviderTaoData state) { 47 | XmlSerializerUtil.copyBean(state, this); 48 | } 49 | } 50 | 51 | -------------------------------------------------------------------------------- /src/main/java/intention_action/SnippetType.java: -------------------------------------------------------------------------------- 1 | package intention_action; 2 | 3 | enum SnippetType { 4 | Consumer, Selector, SelectorShouldRebuild, ChangeNotifierProvider, 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/intention_action/Snippets.java: -------------------------------------------------------------------------------- 1 | package intention_action; 2 | 3 | import helper.ProviderTaoData; 4 | 5 | public class Snippets { 6 | public static final ProviderTaoData data = ProviderTaoData.getInstance(); 7 | 8 | public static final String PREFIX_SELECTION = "Subject"; 9 | public static final String SUFFIX1 = data.logicName; 10 | 11 | public static final String Provider_SNIPPET_KEY = PREFIX_SELECTION + SUFFIX1; 12 | 13 | 14 | static String getSnippet(SnippetType snippetType, String widget) { 15 | switch (snippetType) { 16 | case Consumer: 17 | return snippetConsumer(widget); 18 | case Selector: 19 | return snippetSelector(widget); 20 | case SelectorShouldRebuild: 21 | return snippetSelectorShouldRebuild(widget); 22 | case ChangeNotifierProvider: 23 | return snippetChangeNotifierProvider(widget); 24 | default: 25 | return ""; 26 | } 27 | } 28 | 29 | private static String snippetConsumer(String widget) { 30 | return String.format("Consumer<%1$s>(\n" + 31 | " builder: (context, provider, child) {\n" + 32 | " return %3$s;\n" + 33 | " },\n" + 34 | ")", Provider_SNIPPET_KEY, data.logicName.toLowerCase(), widget); 35 | } 36 | 37 | private static String snippetSelector(String widget) { 38 | return String.format("Selector<%1$s, Object>(\n" + 39 | " selector: (context, %2$s) => object,\n" + 40 | " builder: (context, value, child) {\n" + 41 | " return %3$s;\n" + 42 | " },\n" + 43 | ")", Provider_SNIPPET_KEY, data.logicName.toLowerCase(), widget); 44 | } 45 | 46 | private static String snippetSelectorShouldRebuild(String widget) { 47 | return String.format("Selector<%1$s, %1$s>(\n" + 48 | " shouldRebuild: (previous, next) {\n" + 49 | " return true;\n" + 50 | " },\n" + 51 | " selector: (context, %2$s) => %2$s,\n" + 52 | " builder: (context, value, child) {\n" + 53 | " return %3$s;\n" + 54 | " },\n" + 55 | ")", Provider_SNIPPET_KEY, data.logicName.toLowerCase(), widget); 56 | } 57 | 58 | private static String snippetChangeNotifierProvider(String widget) { 59 | return String.format("ChangeNotifierProvider(\n" + 60 | " create: (BuildContext context) => %1$s(),\n" + 61 | " child: %2$s,\n" + 62 | ")", Provider_SNIPPET_KEY, widget); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/intention_action/WrapHelper.kt: -------------------------------------------------------------------------------- 1 | package intention_action; 2 | 3 | import com.intellij.psi.PsiElement 4 | 5 | class WrapHelper { 6 | companion object { 7 | @JvmStatic 8 | fun callExpressionFinder(psiElement: PsiElement): PsiElement? { 9 | var psiElementFinder: PsiElement? = psiElement.parent 10 | 11 | for (i in 1..10) { 12 | if (psiElementFinder == null) { 13 | return null 14 | } 15 | 16 | if (psiElementFinder.toString() == "CALL_EXPRESSION") { 17 | if (psiElementFinder.text.startsWith(psiElement.text)) { 18 | return psiElementFinder 19 | } 20 | return null 21 | } 22 | psiElementFinder = psiElementFinder.parent 23 | } 24 | return null 25 | } 26 | 27 | @JvmStatic 28 | fun isSelectionValid(start: Int, end: Int): Boolean { 29 | if (start <= -1 || end <= -1) { 30 | return false 31 | } 32 | 33 | if (start >= end) { 34 | return false 35 | } 36 | 37 | return true 38 | } 39 | } 40 | } -------------------------------------------------------------------------------- /src/main/java/intention_action/WrapWithAction.java: -------------------------------------------------------------------------------- 1 | package intention_action; 2 | 3 | import com.intellij.codeInsight.intention.IntentionAction; 4 | import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction; 5 | import com.intellij.openapi.application.ApplicationManager; 6 | import com.intellij.openapi.command.WriteCommandAction; 7 | import com.intellij.openapi.editor.*; 8 | import com.intellij.openapi.project.Project; 9 | import com.intellij.openapi.util.TextRange; 10 | import com.intellij.psi.PsiDocumentManager; 11 | import com.intellij.psi.PsiElement; 12 | import com.intellij.psi.PsiFile; 13 | import com.intellij.psi.codeStyle.CodeStyleManager; 14 | import com.intellij.util.IncorrectOperationException; 15 | import org.jetbrains.annotations.NotNull; 16 | import org.jetbrains.annotations.Nullable; 17 | 18 | public abstract class WrapWithAction extends PsiElementBaseIntentionAction implements IntentionAction { 19 | final SnippetType snippetType; 20 | PsiElement callExpressionElement; 21 | 22 | public WrapWithAction(SnippetType snippetType) { 23 | this.snippetType = snippetType; 24 | } 25 | 26 | @NotNull 27 | public String getFamilyName() { 28 | return getText(); 29 | } 30 | 31 | 32 | @Override 33 | public boolean isAvailable(@NotNull Project project, Editor editor, @Nullable PsiElement psiElement) { 34 | if (psiElement == null) { 35 | return false; 36 | } 37 | 38 | final PsiFile currentFile = getCurrentFile(project, editor); 39 | if (currentFile != null && !currentFile.getName().endsWith(".dart")) { 40 | return false; 41 | } 42 | 43 | if (!psiElement.toString().equals("PsiElement(IDENTIFIER)")) { 44 | return false; 45 | } 46 | 47 | callExpressionElement = WrapHelper.callExpressionFinder(psiElement); 48 | if (callExpressionElement == null) { 49 | return false; 50 | } 51 | 52 | return true; 53 | } 54 | 55 | 56 | @Override 57 | public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) 58 | throws IncorrectOperationException { 59 | Runnable runnable = () -> invokeSnippetAction(project, editor, snippetType); 60 | WriteCommandAction.runWriteCommandAction(project, runnable); 61 | } 62 | 63 | protected void invokeSnippetAction(@NotNull Project project, Editor editor, SnippetType snippetType) { 64 | final Document document = editor.getDocument(); 65 | 66 | final PsiElement element = callExpressionElement; 67 | final TextRange elementSelectionRange = element.getTextRange(); 68 | final int offsetStart = elementSelectionRange.getStartOffset(); 69 | final int offsetEnd = elementSelectionRange.getEndOffset(); 70 | 71 | if (!WrapHelper.isSelectionValid(offsetStart, offsetEnd)) { 72 | return; 73 | } 74 | 75 | final String selectedText = document.getText(TextRange.create(offsetStart, offsetEnd)); 76 | final String replaceWith = Snippets.getSnippet(snippetType, selectedText); 77 | 78 | // wrap the widget: 79 | WriteCommandAction.runWriteCommandAction(project, () -> { 80 | document.replaceString(offsetStart, offsetEnd, replaceWith); 81 | } 82 | ); 83 | 84 | // place cursors to specify types: 85 | final String prefixSelection = Snippets.PREFIX_SELECTION; 86 | final String[] snippetArr = {Snippets.Provider_SNIPPET_KEY}; 87 | 88 | final CaretModel caretModel = editor.getCaretModel(); 89 | caretModel.removeSecondaryCarets(); 90 | 91 | for (String snippet : snippetArr) { 92 | if (!replaceWith.contains(snippet)) { 93 | continue; 94 | } 95 | 96 | final int caretOffset = offsetStart + replaceWith.indexOf(snippet); 97 | final VisualPosition visualPos = editor.offsetToVisualPosition(caretOffset); 98 | caretModel.addCaret(visualPos); 99 | 100 | // select snippet prefix keys: 101 | final Caret currentCaret = caretModel.getCurrentCaret(); 102 | currentCaret.setSelection(caretOffset, caretOffset + prefixSelection.length()); 103 | } 104 | 105 | final Caret initialCaret = caretModel.getAllCarets().get(0); 106 | if (!initialCaret.hasSelection()) { 107 | // initial position from where was triggered the intention action 108 | caretModel.removeCaret(initialCaret); 109 | } 110 | 111 | // reformat file: 112 | ApplicationManager.getApplication().runWriteAction(() -> { 113 | PsiDocumentManager.getInstance(project).commitDocument(document); 114 | final PsiFile currentFile = getCurrentFile(project, editor); 115 | if (currentFile != null) { 116 | final String unformattedText = document.getText(); 117 | final int unformattedLineCount = document.getLineCount(); 118 | 119 | CodeStyleManager.getInstance(project).reformat(currentFile); 120 | 121 | final int formattedLineCount = document.getLineCount(); 122 | 123 | // file was incorrectly formatted, revert formatting 124 | if (formattedLineCount > unformattedLineCount + 3) { 125 | document.setText(unformattedText); 126 | PsiDocumentManager.getInstance(project).commitDocument(document); 127 | } 128 | } 129 | }); 130 | } 131 | 132 | @Override 133 | public boolean startInWriteAction() { 134 | return true; 135 | } 136 | 137 | private PsiFile getCurrentFile(Project project, Editor editor) { 138 | return PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument()); 139 | } 140 | } 141 | -------------------------------------------------------------------------------- /src/main/java/intention_action/WrapWithChangeNotifierProviderAction.java: -------------------------------------------------------------------------------- 1 | package intention_action; 2 | 3 | import org.jetbrains.annotations.NotNull; 4 | 5 | public class WrapWithChangeNotifierProviderAction extends WrapWithAction { 6 | public WrapWithChangeNotifierProviderAction() { 7 | super(SnippetType.ChangeNotifierProvider); 8 | } 9 | 10 | @NotNull 11 | public String getText() { 12 | return "Wrap with ChangeNotifierProvider"; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/intention_action/WrapWithConsumerAction.java: -------------------------------------------------------------------------------- 1 | package intention_action; 2 | 3 | import org.jetbrains.annotations.NotNull; 4 | 5 | public class WrapWithConsumerAction extends WrapWithAction { 6 | public WrapWithConsumerAction() { 7 | super(SnippetType.Consumer); 8 | } 9 | 10 | @NotNull 11 | public String getText() { 12 | return "Wrap with Consumer"; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/intention_action/WrapWithSelectorAction.java: -------------------------------------------------------------------------------- 1 | package intention_action; 2 | 3 | import org.jetbrains.annotations.NotNull; 4 | 5 | public class WrapWithSelectorAction extends WrapWithAction { 6 | public WrapWithSelectorAction() { 7 | super(SnippetType.Selector); 8 | } 9 | 10 | @NotNull 11 | public String getText() { 12 | return "Wrap with Selector (selector)"; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/intention_action/WrapWithSelectorShouldRebuildAction.java: -------------------------------------------------------------------------------- 1 | package intention_action; 2 | 3 | import org.jetbrains.annotations.NotNull; 4 | 5 | public class WrapWithSelectorShouldRebuildAction extends WrapWithAction { 6 | public WrapWithSelectorShouldRebuildAction() { 7 | super(SnippetType.SelectorShouldRebuild); 8 | } 9 | 10 | @NotNull 11 | public String getText() { 12 | return "Wrap with Selector (shouldRebuild)"; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/live_templates/ProviderContext.java: -------------------------------------------------------------------------------- 1 | package live_templates; 2 | 3 | import com.intellij.codeInsight.template.TemplateContextType; 4 | import com.intellij.psi.PsiFile; 5 | import org.jetbrains.annotations.NotNull; 6 | 7 | public class ProviderContext extends TemplateContextType { 8 | protected ProviderContext() { 9 | super("FLUTTER", "Flutter"); 10 | } 11 | 12 | @Override 13 | public boolean isInContext(@NotNull PsiFile file, int offset) { 14 | return file.getName().endsWith(".dart"); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/live_templates/ProviderTemplateProvider.java: -------------------------------------------------------------------------------- 1 | package live_templates; 2 | 3 | import com.intellij.codeInsight.template.impl.DefaultLiveTemplatesProvider; 4 | import org.jetbrains.annotations.Nullable; 5 | 6 | public class ProviderTemplateProvider implements DefaultLiveTemplatesProvider { 7 | @Override 8 | public String[] getDefaultLiveTemplateFiles() { 9 | return new String[]{"liveTemplates/provider"}; 10 | } 11 | 12 | @Nullable 13 | @Override 14 | public String[] getHiddenLiveTemplateFiles() { 15 | return null; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/setting/SettingsComponent.java: -------------------------------------------------------------------------------- 1 | package setting; 2 | 3 | import com.intellij.ui.components.JBLabel; 4 | import com.intellij.ui.components.JBTextField; 5 | import com.intellij.util.ui.FormBuilder; 6 | 7 | import javax.swing.*; 8 | 9 | public class SettingsComponent { 10 | public JPanel mainPanel; 11 | public JBTextField logicName; 12 | public JBTextField stateName; 13 | public JBTextField viewName; 14 | public JBTextField viewFileName; 15 | 16 | public SettingsComponent() { 17 | logicName = new JBTextField(); 18 | stateName = new JBTextField(); 19 | viewName = new JBTextField(); 20 | viewFileName = new JBTextField(); 21 | 22 | mainPanel = FormBuilder.createFormBuilder() 23 | .addLabeledComponent(new JBLabel("Provider Name: "), logicName) 24 | .addLabeledComponent(new JBLabel("State Name: "), stateName) 25 | .addLabeledComponent(new JBLabel("View Name: "), viewName) 26 | .addLabeledComponent(new JBLabel("View File Name: "), viewFileName) 27 | .addComponentFillVertically(new JPanel(), 0) 28 | .getPanel(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/setting/SettingsConfigurable.java: -------------------------------------------------------------------------------- 1 | package setting; 2 | 3 | import com.intellij.openapi.options.Configurable; 4 | import helper.ProviderTaoData; 5 | import org.jetbrains.annotations.Nls; 6 | import org.jetbrains.annotations.Nullable; 7 | 8 | import javax.swing.*; 9 | 10 | public class SettingsConfigurable implements Configurable { 11 | 12 | private final ProviderTaoData data = ProviderTaoData.getInstance(); 13 | private SettingsComponent mSetting; 14 | 15 | @Nls(capitalization = Nls.Capitalization.Title) 16 | @Override 17 | public String getDisplayName() { 18 | return "Flutter Provider Setting"; 19 | } 20 | 21 | @Nullable 22 | @Override 23 | public JComponent createComponent() { 24 | mSetting = new SettingsComponent(); 25 | return mSetting.mainPanel; 26 | } 27 | 28 | @Override 29 | public boolean isModified() { 30 | boolean modified; 31 | modified = !mSetting.logicName.getText().equals(data.logicName) 32 | || !mSetting.stateName.getText().equals(data.stateName) 33 | || !mSetting.viewName.getText().equals(data.viewName) 34 | || !mSetting.viewFileName.getText().equals(data.viewFileName); 35 | return modified; 36 | } 37 | 38 | @Override 39 | public void apply() { 40 | data.logicName = mSetting.logicName.getText(); 41 | data.stateName = mSetting.stateName.getText(); 42 | data.viewName = mSetting.viewName.getText(); 43 | data.viewFileName = mSetting.viewFileName.getText(); 44 | } 45 | 46 | @Override 47 | public void reset() { 48 | mSetting.logicName.setText(data.logicName); 49 | mSetting.stateName.setText(data.stateName); 50 | mSetting.viewName.setText(data.viewName); 51 | mSetting.viewFileName.setText(data.viewFileName); 52 | } 53 | 54 | @Override 55 | public void disposeUIResources() { 56 | mSetting = null; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/resources/META-INF/plugin.xml: -------------------------------------------------------------------------------- 1 | 2 | com.tao.provider 3 | 4 | Flutter Provider 5 | 6 | 7 | 8 | 小呆呆666 9 | 10 | 13 | Used to generate the template code of provider framework 15 | 22 | ]]> 23 | 24 | 25 | 27 | com.intellij.modules.all 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | intention_action.WrapWithSelectorAction 58 | Provider 59 | 60 | 61 | intention_action.WrapWithSelectorShouldRebuildAction 62 | Provider 63 | 64 | 65 | 66 | intention_action.WrapWithConsumerAction 67 | Provider 68 | 69 | 70 | 71 | intention_action.WrapWithChangeNotifierProviderAction 72 | Provider 73 | 74 | 75 | -------------------------------------------------------------------------------- /src/main/resources/META-INF/pluginIcon.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/main/resources/image/provider_icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xdd666t/provider_template/4bf75e2bedc0451fa2b59a7ea9d16a707b463020/src/main/resources/image/provider_icon.png -------------------------------------------------------------------------------- /src/main/resources/intentionDescriptions/WrapWithChangeNotifierProviderAction/after.java.template: -------------------------------------------------------------------------------- 1 | class Counter extends StatelessWidget { 2 | @override 3 | Widget build(BuildContext context) { 4 | return Scaffold( 5 | body: ChangeNotifierProvider( 6 | create: (BuildContext context) => SubjectProvider(), 7 | child: Text('Counter'), 8 | ), 9 | ); 10 | } 11 | } -------------------------------------------------------------------------------- /src/main/resources/intentionDescriptions/WrapWithChangeNotifierProviderAction/before.java.template: -------------------------------------------------------------------------------- 1 | class Counter extends StatelessWidget { 2 | @override 3 | Widget build(BuildContext context) { 4 | return Scaffold( 5 | body: Text('Counter'), 6 | ); 7 | } 8 | } -------------------------------------------------------------------------------- /src/main/resources/intentionDescriptions/WrapWithChangeNotifierProviderAction/description.html: -------------------------------------------------------------------------------- 1 | 2 | Wraps the current widget in a ChangeNotifierProvider 3 | -------------------------------------------------------------------------------- /src/main/resources/intentionDescriptions/WrapWithConsumerAction/after.java.template: -------------------------------------------------------------------------------- 1 | class Counter extends StatelessWidget { 2 | @override 3 | Widget build(BuildContext context) { 4 | return Scaffold( 5 | body: Consumer( 6 | builder: (context, provider, child) { 7 | return Text('Counter'); 8 | }, 9 | ), 10 | ); 11 | } 12 | } -------------------------------------------------------------------------------- /src/main/resources/intentionDescriptions/WrapWithConsumerAction/before.java.template: -------------------------------------------------------------------------------- 1 | class Counter extends StatelessWidget { 2 | @override 3 | Widget build(BuildContext context) { 4 | return Scaffold( 5 | body: Text('Counter'), 6 | ); 7 | } 8 | } -------------------------------------------------------------------------------- /src/main/resources/intentionDescriptions/WrapWithConsumerAction/description.html: -------------------------------------------------------------------------------- 1 | 2 | Wraps the current widget in a Consumer 3 | -------------------------------------------------------------------------------- /src/main/resources/intentionDescriptions/WrapWithSelectorAction/after.java.template: -------------------------------------------------------------------------------- 1 | class Counter extends StatelessWidget { 2 | @override 3 | Widget build(BuildContext context) { 4 | return Scaffold( 5 | body: Selector( 6 | selector: (context, provider) => object, 7 | builder: (context, value, child) { 8 | return Text('Counter'); 9 | }, 10 | ), 11 | ); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/resources/intentionDescriptions/WrapWithSelectorAction/before.java.template: -------------------------------------------------------------------------------- 1 | class Counter extends StatelessWidget { 2 | @override 3 | Widget build(BuildContext context) { 4 | return Scaffold( 5 | body: Text('Counter'), 6 | ); 7 | } 8 | } -------------------------------------------------------------------------------- /src/main/resources/intentionDescriptions/WrapWithSelectorAction/description.html: -------------------------------------------------------------------------------- 1 | 2 | Wraps the current widget in a Selector 3 | -------------------------------------------------------------------------------- /src/main/resources/intentionDescriptions/WrapWithSelectorShouldRebuildAction/after.java.template: -------------------------------------------------------------------------------- 1 | class Counter extends StatelessWidget { 2 | @override 3 | Widget build(BuildContext context) { 4 | return Scaffold( 5 | body: Selector( 6 | shouldRebuild: (previous, next) { 7 | return true; 8 | }, 9 | selector: (context, provider) => provider, 10 | builder: (context, provider, child) { 11 | return Text('Counter'); 12 | }, 13 | ), 14 | ); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/resources/intentionDescriptions/WrapWithSelectorShouldRebuildAction/before.java.template: -------------------------------------------------------------------------------- 1 | class Counter extends StatelessWidget { 2 | @override 3 | Widget build(BuildContext context) { 4 | return Scaffold( 5 | body: Text('Counter'), 6 | ); 7 | } 8 | } -------------------------------------------------------------------------------- /src/main/resources/intentionDescriptions/WrapWithSelectorShouldRebuildAction/description.html: -------------------------------------------------------------------------------- 1 | 2 | Wraps the current widget in a Selector 3 | -------------------------------------------------------------------------------- /src/main/resources/liveTemplates/provider.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 20 | 28 | 29 | -------------------------------------------------------------------------------- /src/main/resources/templates/high/provider.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/material.dart'; 2 | 3 | import 'state.dart'; 4 | 5 | class $nameProvider extends ChangeNotifier { 6 | final state = $nameState(); 7 | } 8 | -------------------------------------------------------------------------------- /src/main/resources/templates/high/state.dart: -------------------------------------------------------------------------------- 1 | class $nameState { 2 | 3 | $nameState() { 4 | // init some variables 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /src/main/resources/templates/high/view.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/material.dart'; 2 | import 'package:provider/provider.dart'; 3 | 4 | import 'provider.dart'; 5 | 6 | class $namePage extends StatelessWidget { 7 | @override 8 | Widget build(BuildContext context) { 9 | return ChangeNotifierProvider( 10 | create: (BuildContext context) => $nameProvider(), 11 | builder: (context, child) => _buildPage(context), 12 | ); 13 | } 14 | 15 | Widget _buildPage(BuildContext context) { 16 | final provider = context.read<$nameProvider>(); 17 | final state = provider.state; 18 | 19 | return Container(); 20 | } 21 | } -------------------------------------------------------------------------------- /src/main/resources/templates/provider.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/material.dart'; 2 | 3 | class $nameProvider extends ChangeNotifier { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/main/resources/templates/view.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/material.dart'; 2 | import 'package:provider/provider.dart'; 3 | 4 | import 'provider.dart'; 5 | 6 | class $namePage extends StatelessWidget { 7 | @override 8 | Widget build(BuildContext context) { 9 | return ChangeNotifierProvider( 10 | create: (BuildContext context) => $nameProvider(), 11 | builder: (context, child) => _buildPage(context), 12 | ); 13 | } 14 | 15 | Widget _buildPage(BuildContext context) { 16 | final provider = context.read<$nameProvider>(); 17 | 18 | return Container(); 19 | } 20 | } 21 | 22 | --------------------------------------------------------------------------------