├── .gitignore
├── LICENSE
├── META-INF
└── plugin.xml
├── README.md
└── src
└── de
└── espend
└── idea
└── android
├── AndroidView.java
├── RelatedPopupGotoLineMarker.java
├── action
├── ExtractStringAction.java
├── generator
│ ├── AbstractActivityViewAction.java
│ ├── AbstractInflateViewAction.java
│ ├── ActivityViewFieldVariable.java
│ ├── ActivityViewMethodVariable.java
│ ├── FieldViewInflateViewAction.java
│ └── LocalViewAction.java
└── write
│ ├── InflateLocalVariableAction.java
│ ├── InflateThisExpressionAction.java
│ └── ReformatAndOptimizeImportsProcessor.java
├── annotator
├── InflateCastAnnotator.java
└── InflateViewAnnotator.java
├── linemarker
├── FragmentRelatedFileLineMarker.java
├── InflateLayoutLineMarkerProvider.java
└── ViewInflateLineMarker.java
├── symbol
└── AndroidSymbolContributor.java
└── utils
├── AndroidUtils.java
├── AndroidViewUtil.java
└── JavaPsiUtil.java
/.gitignore:
--------------------------------------------------------------------------------
1 | out/
2 | /.idea
3 | /php-annotation.jar
4 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 2, June 1991
3 |
4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6 | Everyone is permitted to copy and distribute verbatim copies
7 | of this license document, but changing it is not allowed.
8 |
9 | Preamble
10 |
11 | The licenses for most software are designed to take away your
12 | freedom to share and change it. By contrast, the GNU General Public
13 | License is intended to guarantee your freedom to share and change free
14 | software--to make sure the software is free for all its users. This
15 | General Public License applies to most of the Free Software
16 | Foundation's software and to any other program whose authors commit to
17 | using it. (Some other Free Software Foundation software is covered by
18 | the GNU Lesser General Public License instead.) You can apply it to
19 | your programs, too.
20 |
21 | When we speak of free software, we are referring to freedom, not
22 | price. Our General Public Licenses are designed to make sure that you
23 | have the freedom to distribute copies of free software (and charge for
24 | this service if you wish), that you receive source code or can get it
25 | if you want it, that you can change the software or use pieces of it
26 | in new free programs; and that you know you can do these things.
27 |
28 | To protect your rights, we need to make restrictions that forbid
29 | anyone to deny you these rights or to ask you to surrender the rights.
30 | These restrictions translate to certain responsibilities for you if you
31 | distribute copies of the software, or if you modify it.
32 |
33 | For example, if you distribute copies of such a program, whether
34 | gratis or for a fee, you must give the recipients all the rights that
35 | you have. You must make sure that they, too, receive or can get the
36 | source code. And you must show them these terms so they know their
37 | rights.
38 |
39 | We protect your rights with two steps: (1) copyright the software, and
40 | (2) offer you this license which gives you legal permission to copy,
41 | distribute and/or modify the software.
42 |
43 | Also, for each author's protection and ours, we want to make certain
44 | that everyone understands that there is no warranty for this free
45 | software. If the software is modified by someone else and passed on, we
46 | want its recipients to know that what they have is not the original, so
47 | that any problems introduced by others will not reflect on the original
48 | authors' reputations.
49 |
50 | Finally, any free program is threatened constantly by software
51 | patents. We wish to avoid the danger that redistributors of a free
52 | program will individually obtain patent licenses, in effect making the
53 | program proprietary. To prevent this, we have made it clear that any
54 | patent must be licensed for everyone's free use or not licensed at all.
55 |
56 | The precise terms and conditions for copying, distribution and
57 | modification follow.
58 |
59 | GNU GENERAL PUBLIC LICENSE
60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
61 |
62 | 0. This License applies to any program or other work which contains
63 | a notice placed by the copyright holder saying it may be distributed
64 | under the terms of this General Public License. The "Program", below,
65 | refers to any such program or work, and a "work based on the Program"
66 | means either the Program or any derivative work under copyright law:
67 | that is to say, a work containing the Program or a portion of it,
68 | either verbatim or with modifications and/or translated into another
69 | language. (Hereinafter, translation is included without limitation in
70 | the term "modification".) Each licensee is addressed as "you".
71 |
72 | Activities other than copying, distribution and modification are not
73 | covered by this License; they are outside its scope. The act of
74 | running the Program is not restricted, and the output from the Program
75 | is covered only if its contents constitute a work based on the
76 | Program (independent of having been made by running the Program).
77 | Whether that is true depends on what the Program does.
78 |
79 | 1. You may copy and distribute verbatim copies of the Program's
80 | source code as you receive it, in any medium, provided that you
81 | conspicuously and appropriately publish on each copy an appropriate
82 | copyright notice and disclaimer of warranty; keep intact all the
83 | notices that refer to this License and to the absence of any warranty;
84 | and give any other recipients of the Program a copy of this License
85 | along with the Program.
86 |
87 | You may charge a fee for the physical act of transferring a copy, and
88 | you may at your option offer warranty protection in exchange for a fee.
89 |
90 | 2. You may modify your copy or copies of the Program or any portion
91 | of it, thus forming a work based on the Program, and copy and
92 | distribute such modifications or work under the terms of Section 1
93 | above, provided that you also meet all of these conditions:
94 |
95 | a) You must cause the modified files to carry prominent notices
96 | stating that you changed the files and the date of any change.
97 |
98 | b) You must cause any work that you distribute or publish, that in
99 | whole or in part contains or is derived from the Program or any
100 | part thereof, to be licensed as a whole at no charge to all third
101 | parties under the terms of this License.
102 |
103 | c) If the modified program normally reads commands interactively
104 | when run, you must cause it, when started running for such
105 | interactive use in the most ordinary way, to print or display an
106 | announcement including an appropriate copyright notice and a
107 | notice that there is no warranty (or else, saying that you provide
108 | a warranty) and that users may redistribute the program under
109 | these conditions, and telling the user how to view a copy of this
110 | License. (Exception: if the Program itself is interactive but
111 | does not normally print such an announcement, your work based on
112 | the Program is not required to print an announcement.)
113 |
114 | These requirements apply to the modified work as a whole. If
115 | identifiable sections of that work are not derived from the Program,
116 | and can be reasonably considered independent and separate works in
117 | themselves, then this License, and its terms, do not apply to those
118 | sections when you distribute them as separate works. But when you
119 | distribute the same sections as part of a whole which is a work based
120 | on the Program, the distribution of the whole must be on the terms of
121 | this License, whose permissions for other licensees extend to the
122 | entire whole, and thus to each and every part regardless of who wrote it.
123 |
124 | Thus, it is not the intent of this section to claim rights or contest
125 | your rights to work written entirely by you; rather, the intent is to
126 | exercise the right to control the distribution of derivative or
127 | collective works based on the Program.
128 |
129 | In addition, mere aggregation of another work not based on the Program
130 | with the Program (or with a work based on the Program) on a volume of
131 | a storage or distribution medium does not bring the other work under
132 | the scope of this License.
133 |
134 | 3. You may copy and distribute the Program (or a work based on it,
135 | under Section 2) in object code or executable form under the terms of
136 | Sections 1 and 2 above provided that you also do one of the following:
137 |
138 | a) Accompany it with the complete corresponding machine-readable
139 | source code, which must be distributed under the terms of Sections
140 | 1 and 2 above on a medium customarily used for software interchange; or,
141 |
142 | b) Accompany it with a written offer, valid for at least three
143 | years, to give any third party, for a charge no more than your
144 | cost of physically performing source distribution, a complete
145 | machine-readable copy of the corresponding source code, to be
146 | distributed under the terms of Sections 1 and 2 above on a medium
147 | customarily used for software interchange; or,
148 |
149 | c) Accompany it with the information you received as to the offer
150 | to distribute corresponding source code. (This alternative is
151 | allowed only for noncommercial distribution and only if you
152 | received the program in object code or executable form with such
153 | an offer, in accord with Subsection b above.)
154 |
155 | The source code for a work means the preferred form of the work for
156 | making modifications to it. For an executable work, complete source
157 | code means all the source code for all modules it contains, plus any
158 | associated interface definition files, plus the scripts used to
159 | control compilation and installation of the executable. However, as a
160 | special exception, the source code distributed need not include
161 | anything that is normally distributed (in either source or binary
162 | form) with the major components (compiler, kernel, and so on) of the
163 | operating system on which the executable runs, unless that component
164 | itself accompanies the executable.
165 |
166 | If distribution of executable or object code is made by offering
167 | access to copy from a designated place, then offering equivalent
168 | access to copy the source code from the same place counts as
169 | distribution of the source code, even though third parties are not
170 | compelled to copy the source along with the object code.
171 |
172 | 4. You may not copy, modify, sublicense, or distribute the Program
173 | except as expressly provided under this License. Any attempt
174 | otherwise to copy, modify, sublicense or distribute the Program is
175 | void, and will automatically terminate your rights under this License.
176 | However, parties who have received copies, or rights, from you under
177 | this License will not have their licenses terminated so long as such
178 | parties remain in full compliance.
179 |
180 | 5. You are not required to accept this License, since you have not
181 | signed it. However, nothing else grants you permission to modify or
182 | distribute the Program or its derivative works. These actions are
183 | prohibited by law if you do not accept this License. Therefore, by
184 | modifying or distributing the Program (or any work based on the
185 | Program), you indicate your acceptance of this License to do so, and
186 | all its terms and conditions for copying, distributing or modifying
187 | the Program or works based on it.
188 |
189 | 6. Each time you redistribute the Program (or any work based on the
190 | Program), the recipient automatically receives a license from the
191 | original licensor to copy, distribute or modify the Program subject to
192 | these terms and conditions. You may not impose any further
193 | restrictions on the recipients' exercise of the rights granted herein.
194 | You are not responsible for enforcing compliance by third parties to
195 | this License.
196 |
197 | 7. If, as a consequence of a court judgment or allegation of patent
198 | infringement or for any other reason (not limited to patent issues),
199 | conditions are imposed on you (whether by court order, agreement or
200 | otherwise) that contradict the conditions of this License, they do not
201 | excuse you from the conditions of this License. If you cannot
202 | distribute so as to satisfy simultaneously your obligations under this
203 | License and any other pertinent obligations, then as a consequence you
204 | may not distribute the Program at all. For example, if a patent
205 | license would not permit royalty-free redistribution of the Program by
206 | all those who receive copies directly or indirectly through you, then
207 | the only way you could satisfy both it and this License would be to
208 | refrain entirely from distribution of the Program.
209 |
210 | If any portion of this section is held invalid or unenforceable under
211 | any particular circumstance, the balance of the section is intended to
212 | apply and the section as a whole is intended to apply in other
213 | circumstances.
214 |
215 | It is not the purpose of this section to induce you to infringe any
216 | patents or other property right claims or to contest validity of any
217 | such claims; this section has the sole purpose of protecting the
218 | integrity of the free software distribution system, which is
219 | implemented by public license practices. Many people have made
220 | generous contributions to the wide range of software distributed
221 | through that system in reliance on consistent application of that
222 | system; it is up to the author/donor to decide if he or she is willing
223 | to distribute software through any other system and a licensee cannot
224 | impose that choice.
225 |
226 | This section is intended to make thoroughly clear what is believed to
227 | be a consequence of the rest of this License.
228 |
229 | 8. If the distribution and/or use of the Program is restricted in
230 | certain countries either by patents or by copyrighted interfaces, the
231 | original copyright holder who places the Program under this License
232 | may add an explicit geographical distribution limitation excluding
233 | those countries, so that distribution is permitted only in or among
234 | countries not thus excluded. In such case, this License incorporates
235 | the limitation as if written in the body of this License.
236 |
237 | 9. The Free Software Foundation may publish revised and/or new versions
238 | of the General Public License from time to time. Such new versions will
239 | be similar in spirit to the present version, but may differ in detail to
240 | address new problems or concerns.
241 |
242 | Each version is given a distinguishing version number. If the Program
243 | specifies a version number of this License which applies to it and "any
244 | later version", you have the option of following the terms and conditions
245 | either of that version or of any later version published by the Free
246 | Software Foundation. If the Program does not specify a version number of
247 | this License, you may choose any version ever published by the Free Software
248 | Foundation.
249 |
250 | 10. If you wish to incorporate parts of the Program into other free
251 | programs whose distribution conditions are different, write to the author
252 | to ask for permission. For software which is copyrighted by the Free
253 | Software Foundation, write to the Free Software Foundation; we sometimes
254 | make exceptions for this. Our decision will be guided by the two goals
255 | of preserving the free status of all derivatives of our free software and
256 | of promoting the sharing and reuse of software generally.
257 |
258 | NO WARRANTY
259 |
260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
268 | REPAIR OR CORRECTION.
269 |
270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
278 | POSSIBILITY OF SUCH DAMAGES.
279 |
280 | END OF TERMS AND CONDITIONS
281 |
282 | How to Apply These Terms to Your New Programs
283 |
284 | If you develop a new program, and you want it to be of the greatest
285 | possible use to the public, the best way to achieve this is to make it
286 | free software which everyone can redistribute and change under these terms.
287 |
288 | To do so, attach the following notices to the program. It is safest
289 | to attach them to the start of each source file to most effectively
290 | convey the exclusion of warranty; and each file should have at least
291 | the "copyright" line and a pointer to where the full notice is found.
292 |
293 | {description}
294 | Copyright (C) {year} {fullname}
295 |
296 | This program is free software; you can redistribute it and/or modify
297 | it under the terms of the GNU General Public License as published by
298 | the Free Software Foundation; either version 2 of the License, or
299 | (at your option) any later version.
300 |
301 | This program is distributed in the hope that it will be useful,
302 | but WITHOUT ANY WARRANTY; without even the implied warranty of
303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
304 | GNU General Public License for more details.
305 |
306 | You should have received a copy of the GNU General Public License along
307 | with this program; if not, write to the Free Software Foundation, Inc.,
308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
309 |
310 | Also add information on how to contact you by electronic and paper mail.
311 |
312 | If the program is interactive, make it output a short notice like this
313 | when it starts in an interactive mode:
314 |
315 | Gnomovision version 69, Copyright (C) year name of author
316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
317 | This is free software, and you are welcome to redistribute it
318 | under certain conditions; type `show c' for details.
319 |
320 | The hypothetical commands `show w' and `show c' should show the appropriate
321 | parts of the General Public License. Of course, the commands you use may
322 | be called something other than `show w' and `show c'; they could even be
323 | mouse-clicks or menu items--whatever suits your program.
324 |
325 | You should also get your employer (if you work as a programmer) or your
326 | school, if any, to sign a "copyright disclaimer" for the program, if
327 | necessary. Here is a sample; alter the names:
328 |
329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program
330 | `Gnomovision' (which makes passes at compilers) written by James Hacker.
331 |
332 | {signature of Ty Coon}, 1 April 1989
333 | Ty Coon, President of Vice
334 |
335 | This General Public License does not permit incorporating your program into
336 | proprietary programs. If your program is a subroutine library, you may
337 | consider it more useful to permit linking proprietary applications with the
338 | library. If this is what you want to do, use the GNU Lesser General
339 | Public License instead of this License.
340 |
--------------------------------------------------------------------------------
/META-INF/plugin.xml:
--------------------------------------------------------------------------------
1 |
2 | de.espend.idea.android
3 | Android Studio Prettify
4 | 0.3.1
5 | espend
6 |
7 | Android Studio Prettify
9 |
10 | Issues
11 |
12 |
13 | Features
14 |
15 |
16 | - Generator for inflater and activity setContentView view variables
17 | - "Extract String resource" shortcut in context menu
18 | - LineMarker for view types
19 | - View cast checking annotator
20 | - Field and method variable generator for views
21 | - Related layout files for nested classes
22 | - Symbol search for layout id in xml files
23 |
24 |
25 | ]]>
26 |
27 |
28 |
29 | 0.3.1
32 |
33 | - Fix: Omits underscores in view ID #1
34 |
35 |
36 | 0.3
37 |
38 | - Add symbol search for layout ids in xml
39 |
40 |
41 | 0.2.1
42 |
43 | - make code generator compatible with latest intellij build
44 |
45 |
46 | 0.2
47 |
48 | - Implement support for activity setContentView
49 | - Optimize generator field detection
50 |
51 |
52 | 0.1
53 |
54 | - Add "Extract String resource" shortcut in context menu
55 | - Add LineMarker for view types
56 | - Add view cast checking annotator
57 | - Add field and method variable generator for views
58 | - Add related layout files for nested classes
59 |
60 |
61 | ]]>
62 |
63 |
64 |
65 |
66 |
67 |
69 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 | org.jetbrains.android
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Android Studio Prettify
2 | ==========================
3 | [](https://plugins.jetbrains.com/plugin/7405)
4 | [](https://plugins.jetbrains.com/plugin/7405)
5 | [](https://plugins.jetbrains.com/plugin/7405)
6 |
7 | IntelliJ IDEA / Android Studio plugin with some tools and usability improvements
8 |
9 | Plugin url : http://plugins.jetbrains.com/plugin/7405
10 |
11 | 
12 | 
13 | 
14 |
--------------------------------------------------------------------------------
/src/de/espend/idea/android/AndroidView.java:
--------------------------------------------------------------------------------
1 | package de.espend.idea.android;
2 |
3 | import com.intellij.psi.PsiElement;
4 | import org.jetbrains.annotations.NotNull;
5 |
6 | public class AndroidView {
7 |
8 | private String id;
9 | private String name;
10 | private PsiElement xmlTarget;
11 |
12 | public AndroidView(@NotNull String id, @NotNull String className, PsiElement xmlTarget) {
13 | this.xmlTarget = xmlTarget;
14 |
15 | if (id.startsWith("@+id/")) {
16 | this.id = ("R.id." + id.split("@\\+id/")[1]);
17 | } else if (id.contains(":")) {
18 | String[] s = id.split(":id/");
19 | String packageStr = s[0].substring(1, s[0].length());
20 | this.id = (packageStr + ".R.id." + s[1]);
21 | }
22 | if (className.contains("."))
23 | this.name = className;
24 | else if ((className.equals("View")) || (className.equals("ViewGroup")))
25 | this.name = String.format("android.view.%s", className);
26 | else
27 | this.name = String.format("android.widget.%s", className);
28 | }
29 |
30 | public PsiElement getXmlTarget() {
31 | return xmlTarget;
32 | }
33 |
34 | public String getId() {
35 | return this.id;
36 | }
37 |
38 | public String getName() {
39 | return this.name;
40 | }
41 |
42 | public String getFieldName() {
43 | String[] words = getId().split("_");
44 | StringBuilder fieldName = new StringBuilder();
45 | for (String word : words) {
46 | String[] idTokens = word.split("\\.");
47 | char[] chars = idTokens[(idTokens.length - 1)].toCharArray();
48 | fieldName.append(chars);
49 | }
50 | return fieldName.toString();
51 | }
52 | }
--------------------------------------------------------------------------------
/src/de/espend/idea/android/RelatedPopupGotoLineMarker.java:
--------------------------------------------------------------------------------
1 | package de.espend.idea.android;
2 |
3 | import com.intellij.codeInsight.daemon.GutterIconNavigationHandler;
4 | import com.intellij.ide.actions.GotoRelatedFileAction;
5 | import com.intellij.navigation.GotoRelatedItem;
6 | import com.intellij.psi.PsiElement;
7 | import com.intellij.ui.awt.RelativePoint;
8 | import org.jetbrains.annotations.NotNull;
9 | import org.jetbrains.annotations.Nullable;
10 |
11 | import javax.swing.*;
12 | import java.awt.event.MouseEvent;
13 | import java.util.List;
14 |
15 | public class RelatedPopupGotoLineMarker {
16 |
17 | public static class NavigationHandler implements GutterIconNavigationHandler {
18 |
19 | private List items;
20 |
21 | public NavigationHandler(List items){
22 | this.items = items;
23 | }
24 |
25 | public void navigate(MouseEvent e, PsiElement elt) {
26 | List items = this.items;
27 | if (items.size() == 1) {
28 | items.get(0).navigate();
29 | } else {
30 | GotoRelatedFileAction.createPopup(items, "Go to Related Files").show(new RelativePoint(e));
31 | }
32 |
33 | }
34 |
35 | }
36 |
37 | public static class PopupGotoRelatedItem extends GotoRelatedItem {
38 |
39 | private String customName;
40 | private Icon icon;
41 | private Icon smallIcon;
42 |
43 | public PopupGotoRelatedItem(@NotNull PsiElement element) {
44 | super(element);
45 | }
46 |
47 | public PopupGotoRelatedItem(@NotNull PsiElement element, String customName) {
48 | super(element);
49 | this.customName = customName;
50 | }
51 |
52 | @Nullable
53 | @Override
54 | public String getCustomName() {
55 | return customName;
56 | }
57 |
58 | @Nullable
59 | @Override
60 | public Icon getCustomIcon() {
61 | if(this.icon != null) {
62 | return this.icon;
63 | }
64 |
65 | return super.getCustomIcon();
66 | }
67 |
68 | public PopupGotoRelatedItem withIcon(Icon icon, Icon smallIcon) {
69 | this.icon = icon;
70 | this.smallIcon = smallIcon;
71 | return this;
72 | }
73 |
74 | public Icon getSmallIcon() {
75 | return smallIcon;
76 | }
77 |
78 | }
79 |
80 | }
81 |
--------------------------------------------------------------------------------
/src/de/espend/idea/android/action/ExtractStringAction.java:
--------------------------------------------------------------------------------
1 | package de.espend.idea.android.action;
2 |
3 | import com.intellij.openapi.actionSystem.AnActionEvent;
4 | import com.intellij.openapi.actionSystem.PlatformDataKeys;
5 | import com.intellij.openapi.application.ApplicationManager;
6 | import com.intellij.openapi.command.CommandProcessor;
7 | import com.intellij.openapi.editor.Editor;
8 | import com.intellij.openapi.project.DumbAwareAction;
9 | import com.intellij.openapi.project.Project;
10 | import com.intellij.psi.PsiElement;
11 | import com.intellij.psi.PsiFile;
12 | import com.intellij.psi.PsiLiteralExpression;
13 | import icons.AndroidIcons;
14 | import org.jetbrains.android.intentions.AndroidAddStringResourceAction;
15 | import org.jetbrains.annotations.Nullable;
16 |
17 | public class ExtractStringAction extends DumbAwareAction {
18 |
19 | public ExtractStringAction() {
20 | super("Extract String resource", "Extract string resource (string.xml)", AndroidIcons.Android);
21 | }
22 |
23 | @Override
24 | public void update(AnActionEvent event) {
25 | PsiElement psiElement = getPsiElement(event.getData(PlatformDataKeys.PSI_FILE), event.getData(PlatformDataKeys.EDITOR));
26 | event.getPresentation().setVisible(psiElement instanceof PsiLiteralExpression);
27 | }
28 |
29 | @Override
30 | public void actionPerformed(AnActionEvent event) {
31 |
32 | final PsiFile psiFile = event.getData(PlatformDataKeys.PSI_FILE);
33 | final Editor editor = event.getData(PlatformDataKeys.EDITOR);
34 | final Project project = event.getData(PlatformDataKeys.PROJECT);
35 |
36 | if(project == null) {
37 | return;
38 | }
39 |
40 | CommandProcessor.getInstance().executeCommand(project, new Runnable() {
41 | @Override
42 | public void run() {
43 | ApplicationManager.getApplication().runWriteAction(new Runnable() {
44 | @Override
45 | public void run() {
46 | new AndroidAddStringResourceAction().invoke(project, editor, psiFile);
47 | }
48 | });
49 |
50 | }
51 | }, "Extract string resource", "Android Studio Plugin");
52 |
53 | }
54 |
55 | @Nullable
56 | protected static PsiElement getPsiElement(@Nullable PsiFile file, @Nullable Editor editor) {
57 |
58 | if(file == null || editor == null) {
59 | return null;
60 | }
61 |
62 | int offset = editor.getCaretModel().getOffset();
63 | PsiElement element = file.findElementAt(offset);
64 | return element != null ? element.getParent() : null;
65 | }
66 |
67 | }
68 |
--------------------------------------------------------------------------------
/src/de/espend/idea/android/action/generator/AbstractActivityViewAction.java:
--------------------------------------------------------------------------------
1 | package de.espend.idea.android.action.generator;
2 |
3 | import com.intellij.codeInsight.generation.actions.BaseGenerateAction;
4 | import com.intellij.openapi.actionSystem.AnActionEvent;
5 | import com.intellij.openapi.editor.Editor;
6 | import com.intellij.openapi.project.Project;
7 | import com.intellij.patterns.PlatformPatterns;
8 | import com.intellij.psi.*;
9 | import com.intellij.psi.impl.source.PsiClassReferenceType;
10 | import com.intellij.psi.util.PsiTreeUtil;
11 | import com.intellij.psi.util.PsiUtilBase;
12 | import de.espend.idea.android.annotator.InflateViewAnnotator;
13 | import de.espend.idea.android.utils.AndroidUtils;
14 | import icons.AndroidIcons;
15 | import org.jetbrains.annotations.NotNull;
16 | import org.jetbrains.annotations.Nullable;
17 |
18 | abstract public class AbstractActivityViewAction extends BaseGenerateAction {
19 |
20 | public AbstractActivityViewAction() {
21 | super(null);
22 | }
23 |
24 | @Override
25 | protected boolean isValidForFile(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
26 | int offset = editor.getCaretModel().getOffset();
27 | PsiElement psiElement = file.findElementAt(offset);
28 |
29 | if(!PlatformPatterns.psiElement().inside(PsiMethodCallExpression.class).accepts(psiElement)) {
30 | return false;
31 | }
32 |
33 | PsiMethodCallExpression psiMethodCallExpression = PsiTreeUtil.getParentOfType(psiElement, PsiMethodCallExpression.class);
34 | if(psiMethodCallExpression == null) {
35 | return false;
36 | }
37 |
38 | PsiMethod psiMethod = psiMethodCallExpression.resolveMethod();
39 | if(psiMethod == null) {
40 | return false;
41 | }
42 |
43 | return "setContentView".equals(psiMethod.getName());
44 | }
45 |
46 | @Override
47 | public void actionPerformedImpl(@NotNull final Project project, final Editor editor) {
48 | PsiFile file = PsiUtilBase.getPsiFileInEditor(editor, project);
49 | if(file == null) {
50 | return;
51 | }
52 |
53 | int offset = editor.getCaretModel().getOffset();
54 | PsiElement psiElement = file.findElementAt(offset);
55 | if(psiElement == null) {
56 | return;
57 | }
58 |
59 | PsiMethodCallExpression psiMethodCallExpression = PsiTreeUtil.getParentOfType(psiElement, PsiMethodCallExpression.class);
60 | if(psiMethodCallExpression == null) {
61 | return;
62 | }
63 |
64 | PsiFile xmlFile = matchInflate(psiMethodCallExpression);
65 | generate(psiMethodCallExpression, xmlFile, editor, file);
66 | }
67 |
68 | @Nullable
69 | public static PsiFile matchInflate(PsiMethodCallExpression psiMethodCallExpression) {
70 |
71 | PsiExpression[] psiExpressions = psiMethodCallExpression.getArgumentList().getExpressions();
72 | if(psiExpressions.length == 0) {
73 | return null;
74 | }
75 |
76 | return AndroidUtils.findXmlResource((PsiReferenceExpression) psiExpressions[0]);
77 | }
78 |
79 | @Override
80 | public void update(AnActionEvent event) {
81 | super.update(event);
82 | event.getPresentation().setIcon(AndroidIcons.AndroidToolWindow);
83 | }
84 |
85 | abstract public void generate(PsiMethodCallExpression psiMethodCallExpression, PsiFile xmlFile, Editor editor, @NotNull PsiFile file);
86 |
87 | }
88 |
--------------------------------------------------------------------------------
/src/de/espend/idea/android/action/generator/AbstractInflateViewAction.java:
--------------------------------------------------------------------------------
1 | package de.espend.idea.android.action.generator;
2 |
3 | import com.intellij.codeInsight.generation.actions.BaseGenerateAction;
4 | import com.intellij.openapi.actionSystem.AnActionEvent;
5 | import com.intellij.openapi.editor.Editor;
6 | import com.intellij.openapi.project.Project;
7 | import com.intellij.patterns.PlatformPatterns;
8 | import com.intellij.psi.PsiElement;
9 | import com.intellij.psi.PsiFile;
10 | import com.intellij.psi.PsiLocalVariable;
11 | import com.intellij.psi.util.PsiTreeUtil;
12 | import com.intellij.psi.util.PsiUtilBase;
13 | import de.espend.idea.android.annotator.InflateViewAnnotator;
14 | import icons.AndroidIcons;
15 | import org.jetbrains.annotations.NotNull;
16 |
17 | abstract public class AbstractInflateViewAction extends BaseGenerateAction {
18 |
19 | public AbstractInflateViewAction() {
20 | super(null);
21 | }
22 |
23 | @Override
24 | protected boolean isValidForFile(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
25 | int offset = editor.getCaretModel().getOffset();
26 | PsiElement psiElement = file.findElementAt(offset);
27 |
28 | if(!PlatformPatterns.psiElement().inside(PsiLocalVariable.class).accepts(psiElement)) {
29 | return false;
30 | }
31 |
32 | PsiLocalVariable psiLocalVariable = PsiTreeUtil.getParentOfType(psiElement, PsiLocalVariable.class);
33 | return InflateViewAnnotator.matchInflate(psiLocalVariable) != null;
34 | }
35 |
36 | @Override
37 | public void actionPerformedImpl(@NotNull final Project project, final Editor editor) {
38 | PsiFile file = PsiUtilBase.getPsiFileInEditor(editor, project);
39 | if(file == null) {
40 | return;
41 | }
42 |
43 | int offset = editor.getCaretModel().getOffset();
44 | PsiElement psiElement = file.findElementAt(offset);
45 | if(psiElement == null) {
46 | return;
47 | }
48 |
49 | PsiLocalVariable psiLocalVariable = PsiTreeUtil.getParentOfType(psiElement, PsiLocalVariable.class);
50 | InflateViewAnnotator.InflateContainer inflateContainer = InflateViewAnnotator.matchInflate(psiLocalVariable);
51 | if(inflateContainer == null) {
52 | return;
53 | }
54 |
55 | generate(inflateContainer, editor, file);
56 | }
57 |
58 | @Override
59 | public void update(AnActionEvent event) {
60 | super.update(event);
61 | event.getPresentation().setIcon(AndroidIcons.AndroidToolWindow);
62 | }
63 |
64 | abstract public void generate(InflateViewAnnotator.InflateContainer inflateContainer, Editor editor, @NotNull PsiFile file);
65 |
66 | }
67 |
--------------------------------------------------------------------------------
/src/de/espend/idea/android/action/generator/ActivityViewFieldVariable.java:
--------------------------------------------------------------------------------
1 | package de.espend.idea.android.action.generator;
2 |
3 | import com.intellij.openapi.editor.Editor;
4 | import com.intellij.psi.PsiFile;
5 | import com.intellij.psi.PsiMethodCallExpression;
6 | import de.espend.idea.android.action.write.InflateLocalVariableAction;
7 | import de.espend.idea.android.action.write.InflateThisExpressionAction;
8 | import org.jetbrains.annotations.NotNull;
9 |
10 | public class ActivityViewFieldVariable extends AbstractActivityViewAction {
11 |
12 | @Override
13 | public void generate(PsiMethodCallExpression psiMethodCallExpression, PsiFile xmlFile, Editor editor, @NotNull PsiFile file) {
14 | new InflateThisExpressionAction(psiMethodCallExpression, xmlFile).invoke(file.getProject(), editor, file);
15 | }
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/src/de/espend/idea/android/action/generator/ActivityViewMethodVariable.java:
--------------------------------------------------------------------------------
1 | package de.espend.idea.android.action.generator;
2 |
3 | import com.intellij.openapi.editor.Editor;
4 | import com.intellij.psi.PsiFile;
5 | import com.intellij.psi.PsiMethodCallExpression;
6 | import de.espend.idea.android.action.write.InflateLocalVariableAction;
7 | import org.jetbrains.annotations.NotNull;
8 |
9 | public class ActivityViewMethodVariable extends AbstractActivityViewAction {
10 |
11 | @Override
12 | public void generate(PsiMethodCallExpression psiMethodCallExpression, PsiFile xmlFile, Editor editor, @NotNull PsiFile file) {
13 | new InflateLocalVariableAction(psiMethodCallExpression, xmlFile).invoke(file.getProject(), editor, file);
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/src/de/espend/idea/android/action/generator/FieldViewInflateViewAction.java:
--------------------------------------------------------------------------------
1 | package de.espend.idea.android.action.generator;
2 |
3 | import com.intellij.openapi.editor.Editor;
4 | import com.intellij.psi.PsiFile;
5 | import de.espend.idea.android.action.write.InflateThisExpressionAction;
6 | import de.espend.idea.android.annotator.InflateViewAnnotator;
7 | import org.jetbrains.annotations.NotNull;
8 |
9 | public class FieldViewInflateViewAction extends AbstractInflateViewAction {
10 |
11 | @Override
12 | public void generate(InflateViewAnnotator.InflateContainer inflateContainer, Editor editor, @NotNull PsiFile file) {
13 | new InflateThisExpressionAction(inflateContainer.getPsiLocalVariable(), inflateContainer.getXmlFile()).invoke(file.getProject(), editor, file);
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/src/de/espend/idea/android/action/generator/LocalViewAction.java:
--------------------------------------------------------------------------------
1 | package de.espend.idea.android.action.generator;
2 |
3 | import com.intellij.openapi.editor.Editor;
4 | import com.intellij.psi.PsiFile;
5 | import de.espend.idea.android.action.write.InflateLocalVariableAction;
6 | import de.espend.idea.android.annotator.InflateViewAnnotator;
7 | import org.jetbrains.annotations.NotNull;
8 |
9 | public class LocalViewAction extends AbstractInflateViewAction {
10 |
11 | @Override
12 | public void generate(InflateViewAnnotator.InflateContainer inflateContainer, Editor editor, @NotNull PsiFile file) {
13 | new InflateLocalVariableAction(inflateContainer.getPsiLocalVariable(), inflateContainer.getXmlFile()).invoke(file.getProject(), editor, file);
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/src/de/espend/idea/android/action/write/InflateLocalVariableAction.java:
--------------------------------------------------------------------------------
1 | package de.espend.idea.android.action.write;
2 |
3 | import com.intellij.codeInsight.intention.impl.BaseIntentionAction;
4 | import com.intellij.openapi.application.ApplicationManager;
5 | import com.intellij.openapi.editor.Editor;
6 | import com.intellij.openapi.project.Project;
7 | import com.intellij.psi.*;
8 | import com.intellij.psi.codeStyle.JavaCodeStyleManager;
9 | import com.intellij.psi.util.PsiElementFilter;
10 | import com.intellij.psi.util.PsiTreeUtil;
11 | import com.intellij.util.DocumentUtil;
12 | import com.intellij.util.IncorrectOperationException;
13 | import de.espend.idea.android.AndroidView;
14 | import de.espend.idea.android.utils.AndroidUtils;
15 | import org.jetbrains.annotations.NotNull;
16 | import org.jetbrains.annotations.Nullable;
17 |
18 | import java.util.HashSet;
19 | import java.util.List;
20 | import java.util.Set;
21 |
22 | public class InflateLocalVariableAction extends BaseIntentionAction {
23 |
24 | final private PsiFile xmlFile;
25 | final private PsiElement psiElement;
26 |
27 | @Nullable
28 | private String variableName = null;
29 |
30 | public InflateLocalVariableAction(PsiLocalVariable psiLocalVariable, PsiFile xmlFile) {
31 | this.xmlFile = xmlFile;
32 | this.psiElement = psiLocalVariable;
33 | this.variableName = psiLocalVariable.getName();
34 | }
35 |
36 | public InflateLocalVariableAction(PsiElement psiElement, PsiFile xmlFile) {
37 | this.xmlFile = xmlFile;
38 | this.psiElement = psiElement;
39 | }
40 |
41 | @NotNull
42 | @Override
43 | public String getFamilyName() {
44 | return "Android Studio Prettify";
45 | }
46 |
47 | @Override
48 | public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile psiFile) {
49 | return true;
50 | }
51 |
52 | @Override
53 | public void invoke(@NotNull Project project, Editor editor, PsiFile psiFile) throws IncorrectOperationException {
54 | DocumentUtil.writeInRunUndoTransparentAction(new Runnable() {
55 | @Override
56 | public void run() {
57 | List androidViews = AndroidUtils.getIDsFromXML(xmlFile);
58 |
59 | PsiStatement psiStatement = PsiTreeUtil.getParentOfType(psiElement, PsiStatement.class);
60 | if (psiStatement == null) {
61 | return;
62 | }
63 |
64 | PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(psiStatement.getProject());
65 |
66 | PsiElement[] localVariables = PsiTreeUtil.collectElements(psiStatement.getParent(), new PsiElementFilter() {
67 | @Override
68 | public boolean isAccepted(PsiElement element) {
69 | return element instanceof PsiLocalVariable;
70 | }
71 | });
72 |
73 | Set variables = new HashSet();
74 | for (PsiElement localVariable : localVariables) {
75 | variables.add(((PsiLocalVariable) localVariable).getName());
76 | }
77 |
78 | for (AndroidView v : androidViews) {
79 | if (!variables.contains(v.getFieldName())) {
80 | String sb1;
81 |
82 | if (variableName != null) {
83 | sb1 = String.format("%s %s = (%s) %s.findViewById(%s);", v.getName(), v.getFieldName(), v.getName(), variableName, v.getId());
84 | } else {
85 | sb1 = String.format("%s %s = (%s) findViewById(%s);", v.getName(), v.getFieldName(), v.getName(), v.getId());
86 | }
87 |
88 | PsiStatement statementFromText = elementFactory.createStatementFromText(sb1, null);
89 | psiStatement.getParent().addAfter(statementFromText, psiStatement);
90 | }
91 | }
92 |
93 | JavaCodeStyleManager.getInstance(psiStatement.getProject()).shortenClassReferences(psiStatement.getParent());
94 | new ReformatAndOptimizeImportsProcessor(psiStatement.getProject(), psiStatement.getContainingFile(), true).run();
95 |
96 | }
97 | });
98 |
99 | }
100 |
101 | @NotNull
102 | @Override
103 | public String getText() {
104 | return "Local View Variables";
105 | }
106 | }
107 |
--------------------------------------------------------------------------------
/src/de/espend/idea/android/action/write/InflateThisExpressionAction.java:
--------------------------------------------------------------------------------
1 | package de.espend.idea.android.action.write;
2 |
3 | import com.intellij.codeInsight.intention.impl.BaseIntentionAction;
4 | import com.intellij.openapi.application.ApplicationManager;
5 | import com.intellij.util.DocumentUtil;
6 | import com.intellij.openapi.editor.Editor;
7 | import com.intellij.openapi.project.Project;
8 | import com.intellij.psi.*;
9 | import com.intellij.psi.codeStyle.JavaCodeStyleManager;
10 | import com.intellij.psi.search.PsiElementProcessor;
11 | import com.intellij.psi.util.PsiTreeUtil;
12 | import com.intellij.util.IncorrectOperationException;
13 | import de.espend.idea.android.AndroidView;
14 | import de.espend.idea.android.utils.AndroidUtils;
15 | import org.jetbrains.annotations.NotNull;
16 | import org.jetbrains.annotations.Nullable;
17 |
18 | import java.util.HashSet;
19 | import java.util.List;
20 | import java.util.Set;
21 |
22 | public class InflateThisExpressionAction extends BaseIntentionAction {
23 |
24 | final private PsiFile xmlFile;
25 | final private PsiElement psiElement;
26 |
27 | @Nullable
28 | private String variableName = null;
29 |
30 | public InflateThisExpressionAction(PsiLocalVariable psiLocalVariable, PsiFile xmlFile) {
31 | this.xmlFile = xmlFile;
32 | this.psiElement = psiLocalVariable;
33 | this.variableName = psiLocalVariable.getName();
34 | }
35 |
36 | public InflateThisExpressionAction(PsiElement psiElement, PsiFile xmlFile) {
37 | this.xmlFile = xmlFile;
38 | this.psiElement = psiElement;
39 | }
40 |
41 | @NotNull
42 | @Override
43 | public String getFamilyName() {
44 | return "Android Studio Prettify";
45 | }
46 |
47 | @Override
48 | public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile psiFile) {
49 | return true;
50 | }
51 |
52 | @Override
53 | public void invoke(@NotNull Project project, Editor editor, PsiFile psiFile) throws IncorrectOperationException {
54 |
55 | DocumentUtil.writeInRunUndoTransparentAction(new Runnable() {
56 | @Override
57 | public void run() {
58 | List androidViews = AndroidUtils.getIDsFromXML(xmlFile);
59 |
60 | PsiStatement psiStatement = PsiTreeUtil.getParentOfType(psiElement, PsiStatement.class);
61 | if(psiStatement == null) {
62 | return;
63 | }
64 |
65 | // collection class field
66 | // check if we need to set them
67 | PsiClass psiClass = PsiTreeUtil.getParentOfType(psiStatement, PsiClass.class);
68 | Set fieldSet = new HashSet();
69 | for(PsiField field: psiClass.getFields()) {
70 | fieldSet.add(field.getName());
71 | }
72 |
73 | // collect this.foo = "" and (this.)foo = ""
74 | // collection already init variables
75 | final Set thisSet = new HashSet();
76 | PsiTreeUtil.processElements(psiStatement.getParent(), new PsiElementProcessor() {
77 |
78 | @Override
79 | public boolean execute(@NotNull PsiElement element) {
80 |
81 | if(element instanceof PsiThisExpression) {
82 | attachFieldName(element.getParent());
83 | } else if(element instanceof PsiAssignmentExpression) {
84 | attachFieldName(((PsiAssignmentExpression) element).getLExpression());
85 | }
86 |
87 | return true;
88 | }
89 |
90 | private void attachFieldName(PsiElement psiExpression) {
91 |
92 | if(!(psiExpression instanceof PsiReferenceExpression)) {
93 | return;
94 | }
95 |
96 | PsiElement psiField = ((PsiReferenceExpression) psiExpression).resolve();
97 | if(psiField instanceof PsiField) {
98 | thisSet.add(((PsiField) psiField).getName());
99 | }
100 | }
101 | });
102 |
103 |
104 | PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(psiStatement.getProject());
105 | for (AndroidView v: androidViews) {
106 |
107 | if(!fieldSet.contains(v.getFieldName())) {
108 | String sb = "private " + v.getName() + " " + v.getFieldName() + ";";
109 | psiClass.add(elementFactory.createFieldFromText(sb, psiClass));
110 | }
111 |
112 | if(!thisSet.contains(v.getFieldName())) {
113 |
114 | String sb1;
115 | if(variableName != null) {
116 | sb1 = String.format("this.%s = (%s) %s.findViewById(%s);", v.getFieldName(), v.getName(), variableName, v.getId());
117 | } else {
118 | sb1 = String.format("this.%s = (%s) findViewById(%s);", v.getFieldName(), v.getName(), v.getId());
119 | }
120 |
121 |
122 | PsiStatement statementFromText = elementFactory.createStatementFromText(sb1, null);
123 |
124 | psiStatement.getParent().addAfter(statementFromText, psiStatement);
125 | }
126 |
127 | }
128 |
129 | JavaCodeStyleManager.getInstance(psiStatement.getProject()).shortenClassReferences(psiStatement.getParent());
130 | new ReformatAndOptimizeImportsProcessor(psiStatement.getProject(), psiStatement.getContainingFile(), true).run();
131 |
132 | }
133 | });
134 |
135 | }
136 |
137 | @NotNull
138 | @Override
139 | public String getText() {
140 | return "Field View Variables";
141 | }
142 | }
143 |
--------------------------------------------------------------------------------
/src/de/espend/idea/android/action/write/ReformatAndOptimizeImportsProcessor.java:
--------------------------------------------------------------------------------
1 | package de.espend.idea.android.action.write;
2 |
3 | import com.intellij.codeInsight.CodeInsightBundle;
4 | import com.intellij.codeInsight.actions.AbstractLayoutCodeProcessor;
5 | import com.intellij.codeInsight.actions.OptimizeImportsProcessor;
6 | import com.intellij.codeInsight.actions.ReformatCodeProcessor;
7 | import com.intellij.openapi.module.Module;
8 | import com.intellij.openapi.project.Project;
9 | import com.intellij.psi.PsiDirectory;
10 | import com.intellij.psi.PsiFile;
11 | import com.intellij.psi.impl.source.codeStyle.CodeStyleManagerImpl;
12 | import com.intellij.util.IncorrectOperationException;
13 | import org.jetbrains.annotations.NotNull;
14 |
15 | import java.util.concurrent.Callable;
16 | import java.util.concurrent.FutureTask;
17 |
18 | /**
19 | * @author max
20 | */
21 | public class ReformatAndOptimizeImportsProcessor extends AbstractLayoutCodeProcessor {
22 | public static final String COMMAND_NAME = CodeInsightBundle.message("progress.reformat.and.optimize.common.command.text");
23 | private static final String PROGRESS_TEXT = CodeInsightBundle.message("reformat.and.optimize.progress.common.text");
24 |
25 | private final OptimizeImportsProcessor myOptimizeImportsProcessor;
26 | private final ReformatCodeProcessor myReformatCodeProcessor;
27 |
28 | public ReformatAndOptimizeImportsProcessor(Project project, PsiFile file, boolean processChangedTextOnly) {
29 | super(project, file, PROGRESS_TEXT, COMMAND_NAME, processChangedTextOnly);
30 | myOptimizeImportsProcessor = new OptimizeImportsProcessor(project, file);
31 | myReformatCodeProcessor = new ReformatCodeProcessor(project, file, null, processChangedTextOnly);
32 | }
33 |
34 | @NotNull
35 | @Override
36 | protected FutureTask prepareTask(@NotNull PsiFile psiFile, boolean b) throws IncorrectOperationException {
37 | return new FutureTask(new Callable() {
38 | @Override
39 | public Boolean call() throws Exception {
40 | return true;
41 | }
42 | });
43 | }
44 |
45 | @Override
46 | @NotNull
47 | public FutureTask preprocessFile(@NotNull PsiFile file, boolean processChangedTextOnly) throws IncorrectOperationException {
48 | final FutureTask reformatTask = myReformatCodeProcessor.preprocessFile(file, processChangedTextOnly);
49 | final FutureTask optimizeImportsTask = myOptimizeImportsProcessor.preprocessFile(file, false);
50 | return new FutureTask(new Callable() {
51 | @Override
52 | public Boolean call() throws Exception {
53 | reformatTask.run();
54 | if (!reformatTask.get() || reformatTask.isCancelled()) {
55 | return false;
56 | }
57 |
58 | CodeStyleManagerImpl.setSequentialProcessingAllowed(false);
59 | try {
60 | optimizeImportsTask.run();
61 | return optimizeImportsTask.get() && !optimizeImportsTask.isCancelled();
62 | }
63 | finally {
64 | CodeStyleManagerImpl.setSequentialProcessingAllowed(true);
65 | }
66 | }
67 | });
68 | }
69 | }
--------------------------------------------------------------------------------
/src/de/espend/idea/android/annotator/InflateCastAnnotator.java:
--------------------------------------------------------------------------------
1 | package de.espend.idea.android.annotator;
2 |
3 | import com.intellij.lang.annotation.AnnotationHolder;
4 | import com.intellij.lang.annotation.Annotator;
5 | import com.intellij.psi.*;
6 | import com.intellij.psi.util.PsiTreeUtil;
7 | import de.espend.idea.android.AndroidView;
8 | import de.espend.idea.android.utils.AndroidUtils;
9 | import de.espend.idea.android.utils.AndroidViewUtil;
10 | import de.espend.idea.android.utils.JavaPsiUtil;
11 | import org.jetbrains.annotations.NotNull;
12 |
13 | public class InflateCastAnnotator implements Annotator {
14 |
15 | @Override
16 | public void annotate(@NotNull PsiElement psiElement, @NotNull AnnotationHolder annotationHolder) {
17 |
18 | // check cast:
19 | // this.newButton = (CheckBox) rootView.findViewById(R.id.newButton);
20 | // CheckBox newButton = (CheckBox) rootView.findViewById(R.id.newButton);
21 | if(psiElement instanceof PsiTypeElement) {
22 | PsiElement castExpression = psiElement.getParent();
23 | if(castExpression instanceof PsiTypeCastExpression) {
24 | PsiExpression psiMethodCallExpression = ((PsiTypeCastExpression) castExpression).getOperand();
25 | if(psiMethodCallExpression instanceof PsiMethodCallExpression) {
26 | PsiJavaCodeReferenceElement psiJavaCodeReferenceElement = ((PsiTypeElement) psiElement).getInnermostComponentReferenceElement();
27 | if(psiJavaCodeReferenceElement != null) {
28 | PsiElement castClass = psiJavaCodeReferenceElement.resolve();
29 | if(castClass instanceof PsiClass) {
30 | AndroidView androidView = AndroidViewUtil.getAndroidView((PsiMethodCallExpression) psiMethodCallExpression);
31 | if(androidView != null) {
32 | PsiClass viewClass = JavaPsiUtil.getClass(psiElement.getProject(), androidView.getName());
33 | if(viewClass != null) {
34 | if(!JavaPsiUtil.isInstanceOf(viewClass, (PsiClass) castClass)) {
35 | annotationHolder.createErrorAnnotation(psiElement, String.format("Wrong cast need %s", viewClass.getQualifiedName()));
36 | }
37 | }
38 | }
39 | }
40 | }
41 | }
42 | }
43 |
44 | // activity layouts
45 | // @TODO: simplify, use view callback :)
46 | if(castExpression instanceof PsiTypeCastExpression) {
47 | PsiExpression psiMethodCallExpression = ((PsiTypeCastExpression) castExpression).getOperand();
48 | if(psiMethodCallExpression instanceof PsiMethodCallExpression) {
49 | PsiJavaCodeReferenceElement psiJavaCodeReferenceElement = ((PsiTypeElement) psiElement).getInnermostComponentReferenceElement();
50 | if(psiJavaCodeReferenceElement != null) {
51 | PsiElement castClass = psiJavaCodeReferenceElement.resolve();
52 | if(castClass instanceof PsiClass) {
53 |
54 | PsiExpression[] psiExpressions = ((PsiMethodCallExpression) psiMethodCallExpression).getArgumentList().getExpressions();
55 | if(psiExpressions.length > 0 && psiExpressions[0].getText().startsWith("R.id")) {
56 | String viewId = psiExpressions[0].getText();
57 |
58 | PsiMethod psiMethod = PsiTreeUtil.getParentOfType(psiMethodCallExpression, PsiMethod.class);
59 | if(psiMethod != null) {
60 | for(PsiFile psiFile : AndroidViewUtil.findLayoutFilesInsideMethod(psiMethod)) {
61 | AndroidView androidView = AndroidUtils.getViewType(psiFile, viewId);
62 | if(androidView != null) {
63 | PsiClass viewClass = JavaPsiUtil.getClass(psiElement.getProject(), androidView.getName());
64 | if(viewClass != null) {
65 | if(!JavaPsiUtil.isInstanceOf(viewClass, (PsiClass) castClass)) {
66 | annotationHolder.createErrorAnnotation(psiElement, String.format("Wrong cast need %s", viewClass.getQualifiedName()));
67 | }
68 | }
69 | }
70 | }
71 | }
72 |
73 | }
74 |
75 | }
76 | }
77 | }
78 | }
79 |
80 | }
81 |
82 | }
83 |
84 | }
85 |
86 |
--------------------------------------------------------------------------------
/src/de/espend/idea/android/annotator/InflateViewAnnotator.java:
--------------------------------------------------------------------------------
1 | package de.espend.idea.android.annotator;
2 |
3 | import com.intellij.lang.annotation.Annotation;
4 | import com.intellij.lang.annotation.AnnotationHolder;
5 | import com.intellij.lang.annotation.Annotator;
6 | import com.intellij.psi.*;
7 | import com.intellij.psi.impl.source.PsiClassReferenceType;
8 | import com.intellij.psi.impl.source.tree.java.PsiIdentifierImpl;
9 | import com.intellij.psi.util.PsiTreeUtil;
10 | import de.espend.idea.android.action.write.InflateLocalVariableAction;
11 | import de.espend.idea.android.action.write.InflateThisExpressionAction;
12 | import de.espend.idea.android.utils.AndroidUtils;
13 | import de.espend.idea.android.utils.JavaPsiUtil;
14 | import org.jetbrains.annotations.NotNull;
15 | import org.jetbrains.annotations.Nullable;
16 |
17 | public class InflateViewAnnotator implements Annotator {
18 |
19 | @Override
20 | public void annotate(@NotNull PsiElement psiElement, @NotNull AnnotationHolder annotationHolder) {
21 |
22 | InflateContainer inflateContainer = matchInflate(psiElement);
23 | if(inflateContainer == null) {
24 | return;
25 | }
26 |
27 | Annotation inflateLocal = annotationHolder.createWeakWarningAnnotation(psiElement, null);
28 | inflateLocal.setHighlightType(null);
29 | inflateLocal.registerFix(new InflateLocalVariableAction(inflateContainer.getPsiLocalVariable(), inflateContainer.getXmlFile()));
30 |
31 | Annotation inflateThis = annotationHolder.createWeakWarningAnnotation(psiElement, null);
32 | inflateThis.setHighlightType(null);
33 | inflateThis.registerFix(new InflateThisExpressionAction(inflateContainer.getPsiLocalVariable(), inflateContainer.getXmlFile()));
34 |
35 | }
36 |
37 | @Nullable
38 | public static InflateContainer matchInflate(@Nullable PsiElement psiElement) {
39 | if(!(psiElement instanceof PsiIdentifierImpl)) {
40 | return null;
41 | }
42 |
43 | // View "rootView" = inflater.inflate(R.layout.fragment_main, container, false);
44 | PsiElement psiLocalVariable = psiElement.getParent();
45 | if(psiLocalVariable instanceof PsiLocalVariable) {
46 | return matchInflate((PsiLocalVariable) psiLocalVariable);
47 | }
48 |
49 | return null;
50 |
51 | }
52 |
53 | @Nullable
54 | public static InflateContainer matchInflate(PsiLocalVariable psiLocalVariable) {
55 | PsiType psiType = psiLocalVariable.getType();
56 | if(psiType instanceof PsiClassReferenceType) {
57 | PsiMethodCallExpression psiMethodCallExpression = PsiTreeUtil.findChildOfType(psiLocalVariable, PsiMethodCallExpression.class);
58 | if(psiMethodCallExpression != null) {
59 | PsiMethod psiMethod = psiMethodCallExpression.resolveMethod();
60 |
61 | // @TODO: replace "inflate"; resolve method and check nethod calls
62 | if(psiMethod != null && psiMethod.getName().equals("inflate")) {
63 | PsiExpression[] expressions = psiMethodCallExpression.getArgumentList().getExpressions();
64 | if(expressions.length > 0 && expressions[0] instanceof PsiReferenceExpression) {
65 | PsiFile xmlFile = AndroidUtils.findXmlResource((PsiReferenceExpression) expressions[0]);
66 | if(xmlFile != null) {
67 | return new InflateContainer(xmlFile, ((PsiLocalVariable) psiLocalVariable));
68 | }
69 | }
70 | }
71 | }
72 | }
73 |
74 | return null;
75 | }
76 |
77 | public static class InflateContainer {
78 |
79 | final private PsiFile xmlFile;
80 | final private PsiLocalVariable psiLocalVariable;
81 |
82 | public InflateContainer(PsiFile xmlFile, PsiLocalVariable psiLocalVariable) {
83 | this.xmlFile = xmlFile;
84 | this.psiLocalVariable = psiLocalVariable;
85 | }
86 |
87 | public PsiLocalVariable getPsiLocalVariable() {
88 | return psiLocalVariable;
89 | }
90 |
91 | public PsiFile getXmlFile() {
92 | return xmlFile;
93 | }
94 |
95 | }
96 |
97 |
98 |
99 |
100 | }
101 |
102 |
--------------------------------------------------------------------------------
/src/de/espend/idea/android/linemarker/FragmentRelatedFileLineMarker.java:
--------------------------------------------------------------------------------
1 | package de.espend.idea.android.linemarker;
2 |
3 | import com.intellij.codeInsight.daemon.LineMarkerInfo;
4 | import com.intellij.codeInsight.daemon.LineMarkerProvider;
5 | import com.intellij.navigation.GotoRelatedItem;
6 | import com.intellij.psi.*;
7 | import com.intellij.psi.util.PsiTreeUtil;
8 | import com.intellij.util.ConstantFunction;
9 | import de.espend.idea.android.RelatedPopupGotoLineMarker;
10 | import de.espend.idea.android.utils.AndroidUtils;
11 | import icons.AndroidIcons;
12 | import org.jetbrains.android.AndroidLineMarkerProvider;
13 | import org.jetbrains.annotations.NotNull;
14 | import org.jetbrains.annotations.Nullable;
15 |
16 | import java.util.ArrayList;
17 | import java.util.Collection;
18 | import java.util.List;
19 |
20 | public class FragmentRelatedFileLineMarker implements LineMarkerProvider {
21 |
22 | @Nullable
23 | @Override
24 | public LineMarkerInfo getLineMarkerInfo(@NotNull PsiElement element) {
25 | return null;
26 | }
27 |
28 | @Override
29 | public void collectSlowLineMarkers(@NotNull List elements, @NotNull Collection result) {
30 |
31 | for(PsiElement psiElement : elements) {
32 |
33 | List gotoRelatedItems = new ArrayList();
34 | List psiFiles = new ArrayList();
35 |
36 | // android studio provide line marker with xml targets only on root classes not on class inside classes like fragments
37 | // we support all of them :)
38 | if(psiElement instanceof PsiIdentifier && psiElement.getParent() instanceof PsiClass && !(psiElement.getParent().getParent() instanceof PsiFile)) {
39 |
40 | // simple hack activity provide this on core
41 | if(isFragmentClass((PsiClass) psiElement.getParent())) {
42 | Collection PsiMethodCallExpressions = PsiTreeUtil.collectElementsOfType(psiElement.getParent(), PsiMethodCallExpression.class);
43 | for(PsiMethodCallExpression methodCallExpression: PsiMethodCallExpressions) {
44 | PsiMethod psiMethod = methodCallExpression.resolveMethod();
45 | if(psiMethod != null && psiMethod.getName().equals("inflate")) {
46 | PsiExpression[] expressions = methodCallExpression.getArgumentList().getExpressions();
47 | if(expressions.length > 0 && expressions[0] instanceof PsiReferenceExpression) {
48 | PsiFile xmlFile = AndroidUtils.findXmlResource((PsiReferenceExpression) expressions[0]);
49 | if(xmlFile != null && !psiFiles.contains(xmlFile)) {
50 | psiFiles.add(xmlFile);
51 | gotoRelatedItems.add(new GotoRelatedItem(xmlFile));
52 | }
53 | }
54 | }
55 | }
56 | }
57 |
58 | }
59 |
60 | if(gotoRelatedItems.size() > 0) {
61 | result.add(new LineMarkerInfo(psiElement, psiElement.getTextOffset(), AndroidIcons.AndroidToolWindow, 6, new ConstantFunction("Related Files"), new RelatedPopupGotoLineMarker.NavigationHandler(gotoRelatedItems)));
62 | }
63 |
64 | }
65 |
66 | }
67 |
68 | private boolean isFragmentClass(PsiClass psiClass) {
69 |
70 | PsiReferenceList extendsList = psiClass.getExtendsList();
71 | if(extendsList == null) {
72 | return false;
73 | }
74 |
75 | // @TODO: replace this one with instance check
76 | PsiClassType[] tests = extendsList.getReferencedTypes();
77 | for(PsiClassType psiClassType: tests) {
78 | if(psiClassType.getClassName().contains("Activity")) {
79 | return false;
80 | }
81 | if(psiClassType.getClassName().contains("Fragment")) {
82 | return true;
83 | }
84 | }
85 |
86 | return false;
87 | }
88 |
89 | }
90 |
--------------------------------------------------------------------------------
/src/de/espend/idea/android/linemarker/InflateLayoutLineMarkerProvider.java:
--------------------------------------------------------------------------------
1 | package de.espend.idea.android.linemarker;
2 |
3 | import com.intellij.codeInsight.daemon.LineMarkerInfo;
4 | import com.intellij.codeInsight.daemon.LineMarkerProvider;
5 | import com.intellij.codeInsight.navigation.NavigationGutterIconBuilder;
6 | import com.intellij.psi.*;
7 | import de.espend.idea.android.utils.AndroidUtils;
8 | import icons.AndroidIcons;
9 | import org.jetbrains.annotations.NotNull;
10 | import org.jetbrains.annotations.Nullable;
11 |
12 | import java.util.Collection;
13 | import java.util.List;
14 |
15 | public class InflateLayoutLineMarkerProvider implements LineMarkerProvider {
16 |
17 | @Nullable
18 | @Override
19 | public LineMarkerInfo getLineMarkerInfo(@NotNull PsiElement element) {
20 | return null;
21 | }
22 |
23 | @Override
24 | public void collectSlowLineMarkers(@NotNull List elements, @NotNull Collection result) {
25 | for(PsiElement psiElement : elements) {
26 |
27 | if(psiElement instanceof PsiMethodCallExpression) {
28 | PsiMethod psiMethod = ((PsiCallExpression) psiElement).resolveMethod();
29 | if(psiMethod != null && ("inflate".equals(psiMethod.getName()) || "setContentView".equals(psiMethod.getName()))) {
30 | PsiExpressionList psiExpressionList = ((PsiCallExpression) psiElement).getArgumentList();
31 | if(psiExpressionList != null) {
32 | PsiExpression[] psiExpressions = psiExpressionList.getExpressions();
33 | if(psiExpressions.length > 0 && psiExpressions[0].getText().startsWith("R.layout")) {
34 | String layoutText = psiExpressions[0].getText();
35 |
36 | PsiFile xmlFile = AndroidUtils.findXmlResource(psiElement.getProject(), layoutText);
37 | if(xmlFile != null) {
38 |
39 | NavigationGutterIconBuilder builder = NavigationGutterIconBuilder.create(AndroidIcons.Views.Fragment).
40 | setTooltipText(xmlFile.getName()).
41 | setTargets(xmlFile);
42 |
43 | result.add(builder.createLineMarkerInfo(psiElement));
44 | }
45 |
46 | }
47 | }
48 | }
49 | }
50 |
51 | }
52 | }
53 | }
54 |
55 |
--------------------------------------------------------------------------------
/src/de/espend/idea/android/linemarker/ViewInflateLineMarker.java:
--------------------------------------------------------------------------------
1 | package de.espend.idea.android.linemarker;
2 |
3 | import com.intellij.codeInsight.daemon.LineMarkerInfo;
4 | import com.intellij.codeInsight.daemon.LineMarkerProvider;
5 | import com.intellij.codeInsight.navigation.NavigationGutterIconBuilder;
6 | import com.intellij.psi.*;
7 | import com.intellij.psi.search.GlobalSearchScope;
8 | import com.intellij.psi.util.PsiTreeUtil;
9 | import de.espend.idea.android.AndroidView;
10 | import de.espend.idea.android.utils.AndroidUtils;
11 | import de.espend.idea.android.utils.AndroidViewUtil;
12 | import icons.AndroidIcons;
13 | import org.jetbrains.annotations.NotNull;
14 | import org.jetbrains.annotations.Nullable;
15 |
16 | import javax.swing.*;
17 | import java.util.Collection;
18 | import java.util.List;
19 |
20 | public class ViewInflateLineMarker implements LineMarkerProvider {
21 |
22 | @Nullable
23 | @Override
24 | public LineMarkerInfo getLineMarkerInfo(@NotNull PsiElement element) {
25 | return null;
26 | }
27 |
28 | @Override
29 | public void collectSlowLineMarkers(@NotNull List elements, @NotNull Collection result) {
30 |
31 | for(PsiElement psiElement : elements) {
32 | if(psiElement instanceof PsiMethodCallExpression) {
33 | attachFindViewByIdInflate((PsiMethodCallExpression) psiElement, result);
34 | attachFindViewByIdSetContentViews((PsiMethodCallExpression) psiElement, result);
35 | }
36 | }
37 |
38 | }
39 | private void attachFindViewByIdInflate(PsiMethodCallExpression psiMethodCallExpression, @NotNull Collection result) {
40 | AndroidView androidView = AndroidViewUtil.getAndroidView(psiMethodCallExpression);
41 | if(androidView != null) {
42 | attachLineIcon(androidView, psiMethodCallExpression, result);
43 | }
44 | }
45 |
46 | private void attachFindViewByIdSetContentViews(PsiMethodCallExpression psiMethodCallExpression, @NotNull Collection result) {
47 |
48 | PsiExpression[] psiExpressions = psiMethodCallExpression.getArgumentList().getExpressions();
49 | if(psiExpressions.length == 0) {
50 | return;
51 | }
52 |
53 | PsiMethod psiMethodResolved = psiMethodCallExpression.resolveMethod();
54 | if(psiMethodResolved == null) {
55 | return;
56 | }
57 |
58 | if("findViewById".equals(psiMethodResolved.getName())) {
59 | String viewId = psiExpressions[0].getText();
60 |
61 | PsiMethod psiMethod = PsiTreeUtil.getParentOfType(psiMethodCallExpression, PsiMethod.class);
62 | if(psiMethod != null) {
63 | for(PsiFile psiFile : AndroidViewUtil.findLayoutFilesInsideMethod(psiMethod)) {
64 | AndroidView androidView = AndroidUtils.getViewType(psiFile, viewId);
65 | if(androidView != null) {
66 | attachLineIcon(androidView, psiMethodCallExpression, result);
67 | }
68 | }
69 | }
70 | }
71 |
72 | }
73 |
74 | private void attachLineIcon(@NotNull AndroidView view, @NotNull PsiElement psiElement, @NotNull Collection result) {
75 |
76 | JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(psiElement.getProject());
77 | PsiClass psiClass = psiFacade.findClass(view.getName(), GlobalSearchScope.allScope(psiElement.getProject()));
78 | if(psiClass == null) {
79 | return;
80 | }
81 |
82 | Icon icon = AndroidViewUtil.getCoreIconWithExtends(view, psiClass);
83 | if(icon == null) {
84 | icon = AndroidIcons.Views.View;
85 | }
86 |
87 | NavigationGutterIconBuilder builder = NavigationGutterIconBuilder.create(icon).
88 | setTooltipText(view.getName()).
89 | setTargets(view.getXmlTarget());
90 |
91 | result.add(builder.createLineMarkerInfo(psiElement));
92 | }
93 |
94 | }
95 |
--------------------------------------------------------------------------------
/src/de/espend/idea/android/symbol/AndroidSymbolContributor.java:
--------------------------------------------------------------------------------
1 | package de.espend.idea.android.symbol;
2 |
3 | import com.intellij.ide.util.PsiNavigationSupport;
4 | import com.intellij.navigation.ChooseByNameContributor;
5 | import com.intellij.navigation.ItemPresentation;
6 | import com.intellij.navigation.NavigationItem;
7 | import com.intellij.openapi.project.Project;
8 | import com.intellij.pom.Navigatable;
9 | import com.intellij.psi.PsiElement;
10 | import com.intellij.psi.PsiFile;
11 | import de.espend.idea.android.AndroidView;
12 | import de.espend.idea.android.utils.AndroidUtils;
13 | import de.espend.idea.android.utils.AndroidViewUtil;
14 | import org.jetbrains.annotations.NotNull;
15 | import org.jetbrains.annotations.Nullable;
16 |
17 | import javax.swing.*;
18 | import java.util.ArrayList;
19 | import java.util.HashSet;
20 | import java.util.List;
21 | import java.util.Set;
22 |
23 | public class AndroidSymbolContributor implements ChooseByNameContributor {
24 |
25 | @NotNull
26 | @Override
27 | public String[] getNames(Project project, boolean b) {
28 | List androidViewList = AndroidUtils.getProjectViews(project);
29 |
30 | Set names = new HashSet();
31 | for(AndroidView androidView : androidViewList) {
32 | String id = androidView.getId();
33 | if(id.startsWith("R.id.")) {
34 | names.add(id.substring("R.id.".length()));
35 | }
36 | }
37 |
38 | return names.toArray(new String[names.size()]);
39 | }
40 |
41 | @NotNull
42 | @Override
43 | public NavigationItem[] getItemsByName(String s, String s2, Project project, boolean b) {
44 |
45 | List navigationItems = new ArrayList();
46 |
47 | List androidViewList = AndroidUtils.getProjectViews(project);
48 |
49 |
50 | Set names = new HashSet();
51 | for(AndroidView androidView : androidViewList) {
52 | String id = androidView.getId();
53 | if(id.startsWith("R.id.")) {
54 | id = id.substring("R.id.".length());
55 | }
56 |
57 | if(id.equals(s)) {
58 | navigationItems.add(new NavigationItemEx(androidView.getXmlTarget(), s, AndroidViewUtil.getViewIcon(project, androidView) , androidView.getName()));
59 | }
60 |
61 | }
62 |
63 | return navigationItems.toArray(new NavigationItem[navigationItems.size()]);
64 | }
65 |
66 | public class NavigationItemEx implements NavigationItem, ItemPresentation {
67 |
68 | private PsiElement psiElement;
69 | private String name;
70 | private Icon icon;
71 | private String locationString;
72 | private boolean appendBundleLocation = true;
73 |
74 | public NavigationItemEx(PsiElement psiElement, final String name, final Icon icon, final String locationString) {
75 | this.psiElement = psiElement;
76 | this.name = name;
77 | this.icon = icon;
78 | this.locationString = locationString;
79 | }
80 |
81 | @Nullable
82 | @Override
83 | public String getName() {
84 | return this.name;
85 | }
86 |
87 | @Nullable
88 | @Override
89 | public ItemPresentation getPresentation() {
90 | return this;
91 | }
92 |
93 | @Override
94 | public void navigate(boolean requestFocus) {
95 | final Navigatable descriptor = PsiNavigationSupport.getInstance().getDescriptor(this.psiElement);
96 | if (descriptor != null) {
97 | descriptor.navigate(requestFocus);
98 | }
99 | }
100 |
101 | @Override
102 | public boolean canNavigate() {
103 | return PsiNavigationSupport.getInstance().canNavigate(this.psiElement);
104 | }
105 |
106 | @Override
107 | public boolean canNavigateToSource() {
108 | return canNavigate();
109 | }
110 |
111 | @Override
112 | public String toString() {
113 | return this.name;
114 | }
115 |
116 | @Nullable
117 | @Override
118 | public String getPresentableText() {
119 | return name;
120 | }
121 |
122 | @Nullable
123 | @Override
124 | public String getLocationString() {
125 |
126 | if(!this.appendBundleLocation) {
127 | return this.locationString;
128 | }
129 |
130 | PsiFile psiFile = psiElement.getContainingFile();
131 |
132 | if(psiFile == null) {
133 | return this.locationString;
134 | }
135 |
136 | String locationPathString = this.locationString;
137 | return locationPathString + " " + psiFile.getName();
138 | }
139 |
140 | @Nullable
141 | @Override
142 | public Icon getIcon(boolean b) {
143 | return icon;
144 | }
145 |
146 | }
147 |
148 | }
149 |
--------------------------------------------------------------------------------
/src/de/espend/idea/android/utils/AndroidUtils.java:
--------------------------------------------------------------------------------
1 | package de.espend.idea.android.utils;
2 |
3 | import com.intellij.openapi.project.Project;
4 | import com.intellij.openapi.vfs.VfsUtil;
5 | import com.intellij.openapi.vfs.VirtualFile;
6 | import com.intellij.psi.*;
7 | import com.intellij.psi.search.FilenameIndex;
8 | import com.intellij.psi.search.GlobalSearchScope;
9 | import com.intellij.psi.xml.XmlAttribute;
10 | import com.intellij.psi.xml.XmlTag;
11 | import de.espend.idea.android.AndroidView;
12 | import org.jetbrains.annotations.NotNull;
13 | import org.jetbrains.annotations.Nullable;
14 |
15 | import java.util.ArrayList;
16 | import java.util.List;
17 |
18 |
19 | public class AndroidUtils {
20 |
21 | private AndroidUtils() {
22 | }
23 |
24 | @Nullable
25 | public static PsiFile findXmlResource(@Nullable PsiReferenceExpression referenceExpression) {
26 | if (referenceExpression == null) return null;
27 |
28 | PsiElement firstChild = referenceExpression.getFirstChild();
29 | if (firstChild == null || !"R.layout".equals(firstChild.getText())) {
30 | return null;
31 | }
32 |
33 | PsiElement lastChild = referenceExpression.getLastChild();
34 | if(lastChild == null) {
35 | return null;
36 | }
37 |
38 | String name = String.format("%s.xml", lastChild.getText());
39 | PsiFile[] foundFiles = FilenameIndex.getFilesByName(referenceExpression.getProject(), name, GlobalSearchScope.allScope(referenceExpression.getProject()));
40 | if (foundFiles.length <= 0) {
41 | return null;
42 | }
43 |
44 | return foundFiles[0];
45 | }
46 |
47 | public static List getProjectViews(Project project) {
48 |
49 | List androidViews = new ArrayList();
50 | for(PsiFile psiFile: getLayoutFiles(project)) {
51 | androidViews.addAll(getIDsFromXML(psiFile));
52 | }
53 |
54 | return androidViews;
55 | }
56 |
57 | public static List getLayoutFiles(Project project) {
58 |
59 | List psiFileList = new ArrayList();
60 |
61 | for (VirtualFile virtualFile : FilenameIndex.getAllFilesByExt(project, "xml")) {
62 | VirtualFile parent = virtualFile.getParent();
63 | if (parent != null && "layout".equals(parent.getName())) {
64 | String relative = VfsUtil.getRelativePath(virtualFile, project.getBaseDir(), '/');
65 | if (relative != null) {
66 | PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
67 | if (psiFile != null) {
68 | psiFileList.add(psiFile);
69 | }
70 | }
71 | }
72 | }
73 |
74 | return psiFileList;
75 | }
76 |
77 | @Nullable
78 | public static PsiFile findXmlResource(Project project, String layoutName) {
79 |
80 | if (!layoutName.startsWith("R.layout.")) {
81 | return null;
82 | }
83 |
84 | layoutName = layoutName.substring("R.layout.".length());
85 |
86 | String name = String.format("%s.xml", layoutName);
87 | PsiFile[] foundFiles = FilenameIndex.getFilesByName(project, name, GlobalSearchScope.allScope(project));
88 | if (foundFiles.length <= 0) {
89 | return null;
90 | }
91 |
92 | return foundFiles[0];
93 | }
94 |
95 | @NotNull
96 | public static List getIDsFromXML(@NotNull PsiFile f) {
97 | final ArrayList ret = new ArrayList();
98 | f.accept(new XmlRecursiveElementVisitor() {
99 | @Override
100 | public void visitElement(final PsiElement element) {
101 | super.visitElement(element);
102 | if (element instanceof XmlTag) {
103 | XmlTag t = (XmlTag) element;
104 | XmlAttribute id = t.getAttribute("android:id", null);
105 | if (id == null) {
106 | return;
107 | }
108 | final String val = id.getValue();
109 | if (val == null) {
110 | return;
111 | }
112 | ret.add(new AndroidView(val, t.getName(), id));
113 |
114 | }
115 |
116 | }
117 | });
118 |
119 | return ret;
120 | }
121 |
122 | @Nullable
123 | public static AndroidView getViewType(@NotNull PsiFile f, String findId) {
124 |
125 | // @TODO: replace dup for
126 | List views = getIDsFromXML(f);
127 |
128 | for(AndroidView view: views) {
129 | if(findId.equals(view.getId())) {
130 | return view;
131 | }
132 | }
133 |
134 | return null;
135 | }
136 |
137 | }
138 |
--------------------------------------------------------------------------------
/src/de/espend/idea/android/utils/AndroidViewUtil.java:
--------------------------------------------------------------------------------
1 | package de.espend.idea.android.utils;
2 |
3 |
4 | import com.intellij.openapi.project.Project;
5 | import com.intellij.psi.*;
6 | import com.intellij.psi.impl.PsiClassImplUtil;
7 | import com.intellij.psi.search.GlobalSearchScope;
8 | import com.intellij.psi.search.PsiElementProcessor;
9 | import com.intellij.psi.util.PsiTreeUtil;
10 | import de.espend.idea.android.AndroidView;
11 | import de.espend.idea.android.annotator.InflateViewAnnotator;
12 | import icons.AndroidIcons;
13 | import org.jetbrains.annotations.NotNull;
14 | import org.jetbrains.annotations.Nullable;
15 |
16 | import javax.swing.*;
17 | import java.lang.reflect.Field;
18 | import java.util.ArrayList;
19 | import java.util.List;
20 |
21 | public class AndroidViewUtil {
22 |
23 | private AndroidViewUtil() {
24 | }
25 |
26 | public static Icon getCoreIconWithExtends(AndroidView view, PsiClass psiClass) {
27 |
28 | Field[] declaredFields = AndroidIcons.Views.class.getDeclaredFields();
29 |
30 | // direct class
31 | if(view.getName().equals(psiClass.getQualifiedName())) {
32 | String viewName = view.getName();
33 | Icon icon = getCoreIconName(declaredFields, viewName);
34 | if(icon != null) {
35 | return icon;
36 | }
37 | }
38 |
39 | // extended
40 | for(PsiClassType classType : PsiClassImplUtil.getExtendsListTypes(psiClass)) {
41 | PsiClass resolve = classType.resolve();
42 | if(resolve != null) {
43 | String viewName = resolve.getQualifiedName();
44 | Icon icon = getCoreIconName(declaredFields, viewName);
45 | if(icon != null) {
46 | return icon;
47 | }
48 | }
49 | }
50 |
51 | return null;
52 | }
53 |
54 | @Nullable
55 | private static Icon getCoreIconName(Field[] declaredFields, String viewName) {
56 |
57 | if(viewName.contains(".")) {
58 | viewName = viewName.substring(viewName.lastIndexOf(".") +1 );
59 | for(Field field: declaredFields) {
60 | if(field.getName().equalsIgnoreCase(viewName) && java.lang.reflect.Modifier.isStatic(field.getModifiers()) && field.getType() == Icon.class) {
61 | try {
62 | return (Icon) field.get(null);
63 | } catch (IllegalAccessException e) {
64 | e.printStackTrace();
65 | }
66 | }
67 | }
68 | }
69 |
70 | return null;
71 | }
72 |
73 | @Nullable
74 | public static AndroidView getAndroidView(PsiMethodCallExpression psiMethodCallExpression) {
75 |
76 | PsiExpression[] psiExpressions = psiMethodCallExpression.getArgumentList().getExpressions();
77 | if(psiExpressions.length > 0) {
78 | String viewId = psiExpressions[0].getText();
79 |
80 | PsiMethod psiMethod = psiMethodCallExpression.resolveMethod();
81 | if(psiMethod != null && psiMethod.getName().equals("findViewById")) {
82 |
83 | // methodExpression direct catch!?
84 | PsiElement methodExpression = psiMethodCallExpression.getFirstChild();
85 | if(methodExpression instanceof PsiReferenceExpression) {
86 | PsiExpression psiExpression = ((PsiReferenceExpression) methodExpression).getQualifierExpression();
87 | if(psiExpression != null) {
88 | PsiReference psiLocalVariableReference = psiExpression.getReference();
89 | if(psiLocalVariableReference != null) {
90 | PsiElement psiLocalVariable = psiLocalVariableReference.resolve();
91 | if(psiLocalVariable instanceof PsiLocalVariable) {
92 | InflateViewAnnotator.InflateContainer inflateContainer = InflateViewAnnotator.matchInflate((PsiLocalVariable) psiLocalVariable);
93 | if(inflateContainer != null) {
94 | PsiFile psiFile = inflateContainer.getXmlFile();
95 | return AndroidUtils.getViewType(psiFile, viewId);
96 | }
97 | }
98 | }
99 | }
100 | }
101 | }
102 | }
103 |
104 | return null;
105 |
106 |
107 | }
108 |
109 | public static List findLayoutFilesInsideMethod(PsiMethod psiMethod) {
110 | final List xmlFiles = new ArrayList();
111 |
112 | PsiTreeUtil.processElements(psiMethod, new PsiElementProcessor() {
113 |
114 | @Override
115 | public boolean execute(@NotNull PsiElement element) {
116 |
117 | if (element instanceof PsiMethodCallExpression) {
118 | PsiMethod psiMethodResolved = ((PsiMethodCallExpression) element).resolveMethod();
119 | if (psiMethodResolved != null) {
120 | String methodName = psiMethodResolved.getName();
121 | // @TODO: implement instance check
122 | if ("setContentView".equals(methodName)) {
123 | PsiExpression[] expressions = ((PsiMethodCallExpression) element).getArgumentList().getExpressions();
124 | if (expressions.length > 0 && expressions[0] instanceof PsiReferenceExpression) {
125 | PsiFile xmlFile = AndroidUtils.findXmlResource((PsiReferenceExpression) expressions[0]);
126 | if (xmlFile != null) {
127 | xmlFiles.add(xmlFile);
128 | }
129 | }
130 |
131 | }
132 |
133 | }
134 |
135 | }
136 |
137 | return true;
138 | }
139 | });
140 |
141 |
142 | return xmlFiles;
143 | }
144 |
145 | @NotNull
146 | public static Icon getViewIcon(Project project, AndroidView androidView) {
147 | JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
148 | PsiClass psiClass = psiFacade.findClass(androidView.getName(), GlobalSearchScope.allScope(project));
149 | if(psiClass == null) {
150 | return AndroidIcons.Views.View;
151 | }
152 |
153 | Icon icon = AndroidViewUtil.getCoreIconWithExtends(androidView, psiClass);
154 |
155 | return icon != null ? icon : AndroidIcons.Views.View;
156 | }
157 |
158 | }
159 |
--------------------------------------------------------------------------------
/src/de/espend/idea/android/utils/JavaPsiUtil.java:
--------------------------------------------------------------------------------
1 | package de.espend.idea.android.utils;
2 |
3 | import com.intellij.openapi.project.Project;
4 | import com.intellij.psi.JavaPsiFacade;
5 | import com.intellij.psi.PsiClass;
6 | import com.intellij.psi.PsiClassType;
7 | import com.intellij.psi.impl.PsiClassImplUtil;
8 | import com.intellij.psi.search.GlobalSearchScope;
9 | import org.jetbrains.annotations.Nullable;
10 |
11 | public class JavaPsiUtil {
12 |
13 | private JavaPsiUtil() {
14 | }
15 |
16 | @Nullable
17 | public static PsiClass getClass(Project project, String qualifiedClassName) {
18 | JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
19 | return psiFacade.findClass(qualifiedClassName, GlobalSearchScope.allScope(project));
20 | }
21 |
22 | public static boolean isInstanceOf(PsiClass instance, PsiClass interfaceExtendsClass) {
23 |
24 | String className = interfaceExtendsClass.getQualifiedName();
25 | if(className == null) {
26 | return true;
27 | }
28 |
29 | if(className.equals(instance.getQualifiedName())) {
30 | return true;
31 | }
32 |
33 | for(PsiClassType psiClassType: PsiClassImplUtil.getExtendsListTypes(instance)) {
34 | PsiClass resolve = psiClassType.resolve();
35 | if(resolve != null) {
36 | if(className.equals(resolve.getQualifiedName())) {
37 | return true;
38 | }
39 | }
40 | }
41 |
42 | for(PsiClass psiInterface: PsiClassImplUtil.getInterfaces(instance)) {
43 | if(className.equals(psiInterface.getQualifiedName())) {
44 | return true;
45 | }
46 | }
47 |
48 | return false;
49 | }
50 | }
51 |
--------------------------------------------------------------------------------