aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/build/AaptQuickFix.java
blob: 3db380832db4d0ce9d9e8a5c69e7988d7d86c6f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.eclipse.org/org/documents/epl-v10.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.ide.eclipse.adt.internal.build;

import static com.android.SdkConstants.ANDROID_URI;
import static com.android.SdkConstants.XMLNS_ANDROID;
import static com.android.SdkConstants.XMLNS_URI;

import com.android.ide.common.resources.ResourceUrl;
import com.android.ide.eclipse.adt.AdtConstants;
import com.android.ide.eclipse.adt.AdtPlugin;
import com.android.ide.eclipse.adt.AdtUtils;
import com.android.ide.eclipse.adt.internal.editors.AndroidXmlEditor;
import com.android.ide.eclipse.adt.internal.resources.ResourceHelper;
import com.android.resources.ResourceType;
import com.android.utils.Pair;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.jface.text.quickassist.IQuickAssistInvocationContext;
import org.eclipse.jface.text.quickassist.IQuickAssistProcessor;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.ui.IMarkerResolution;
import org.eclipse.ui.IMarkerResolution2;
import org.eclipse.ui.IMarkerResolutionGenerator2;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.editors.text.TextFileDocumentProvider;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.wst.sse.core.StructuredModelManager;
import org.eclipse.wst.sse.core.internal.provisional.IModelManager;
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.util.List;

/**
 * Shared handler for both quick assist processors (Control key handler) and quick fix
 * marker resolution (Problem view handling), since there is a lot of overlap between
 * these two UI handlers.
 */
@SuppressWarnings("restriction") // XML model
public class AaptQuickFix implements IMarkerResolutionGenerator2, IQuickAssistProcessor {

    public AaptQuickFix() {
    }

    /** Returns the error message from aapt that signals missing resources */
    private static String getTargetMarkerErrorMessage() {
        return "No resource found that matches the given name";
    }

    /** Returns the error message from aapt that signals a missing namespace declaration */
    private static String getUnboundErrorMessage() {
        return "Error parsing XML: unbound prefix";
    }

    // ---- Implements IMarkerResolution2 ----

    @Override
    public boolean hasResolutions(IMarker marker) {
        String message = null;
        try {
            message = (String) marker.getAttribute(IMarker.MESSAGE);
        } catch (CoreException e) {
            AdtPlugin.log(e, null);
        }

        return message != null
                && (message.contains(getTargetMarkerErrorMessage())
                        || message.contains(getUnboundErrorMessage()));
    }

    @Override
    public IMarkerResolution[] getResolutions(IMarker marker) {
        IResource markerResource = marker.getResource();
        IProject project = markerResource.getProject();
        try {
            String message = (String) marker.getAttribute(IMarker.MESSAGE);
            if (message.contains(getUnboundErrorMessage()) && markerResource instanceof IFile) {
                return new IMarkerResolution[] {
                        new CreateNamespaceFix((IFile) markerResource)
                    };
            }
        } catch (CoreException e1) {
            AdtPlugin.log(e1, null);
        }

        int start = marker.getAttribute(IMarker.CHAR_START, 0);
        int end = marker.getAttribute(IMarker.CHAR_END, 0);
        if (end > start) {
            int length = end - start;
            IDocumentProvider provider = new TextFileDocumentProvider();
            try {
                provider.connect(markerResource);
                IDocument document = provider.getDocument(markerResource);
                String resource = document.get(start, length);
                if (ResourceHelper.canCreateResource(resource)) {
                    return new IMarkerResolution[] {
                        new CreateResourceProposal(project, resource)
                    };
                }
            } catch (Exception e) {
                AdtPlugin.log(e, "Can't find range information for %1$s", markerResource);
            } finally {
                provider.disconnect(markerResource);
            }
        }

        return null;
    }

    // ---- Implements IQuickAssistProcessor ----

    @Override
    public boolean canAssist(IQuickAssistInvocationContext invocationContext) {
        return true;
    }

    @Override
    public boolean canFix(Annotation annotation) {
        return true;
    }

    @Override
    public ICompletionProposal[] computeQuickAssistProposals(
            IQuickAssistInvocationContext invocationContext) {

        // We have to find the corresponding project/file (so we can look up the aapt
        // error markers). Unfortunately, an IQuickAssistProcessor only gets
        // access to an ISourceViewer which has no hooks back to the surrounding
        // editor.
        //
        // However, the IQuickAssistProcessor will only be used interactively by a file
        // being edited, so we can cheat like the hyperlink detector and simply
        // look up the currently active file in the IDE. To be on the safe side,
        // we'll make sure that that editor has the same sourceViewer such that
        // we are indeed looking at the right file:
        ISourceViewer sourceViewer = invocationContext.getSourceViewer();
        AndroidXmlEditor editor = AndroidXmlEditor.fromTextViewer(sourceViewer);
        if (editor != null) {
            IFile file = editor.getInputFile();
            if (file == null) {
                return null;
            }
            IDocument document = sourceViewer.getDocument();
            List<IMarker> markers = AdtUtils.findMarkersOnLine(AdtConstants.MARKER_AAPT_COMPILE,
                    file, document, invocationContext.getOffset());
            try {
                for (IMarker marker : markers) {
                    String message = marker.getAttribute(IMarker.MESSAGE, ""); //$NON-NLS-1$
                    if (message.contains(getTargetMarkerErrorMessage())) {
                        int start = marker.getAttribute(IMarker.CHAR_START, 0);
                        int end = marker.getAttribute(IMarker.CHAR_END, 0);
                        int length = end - start;
                        String resource = document.get(start, length);
                        // Can only offer create value for non-framework value
                        // resources
                        if (ResourceHelper.canCreateResource(resource)) {
                            IProject project = editor.getProject();
                            return new ICompletionProposal[] {
                                new CreateResourceProposal(project, resource)
                            };
                        }
                    } else if (message.contains(getUnboundErrorMessage())) {
                        return new ICompletionProposal[] {
                            new CreateNamespaceFix(null)
                        };
                    }
                }
            } catch (BadLocationException e) {
                AdtPlugin.log(e, null);
            }
        }

        return null;
    }

    @Override
    public String getErrorMessage() {
        return null;
    }

    /** Quick fix to insert namespace binding when missing */
    private final static class CreateNamespaceFix
            implements ICompletionProposal, IMarkerResolution2 {
        private IFile mFile;

        public CreateNamespaceFix(IFile file) {
            mFile = file;
        }

        private IndexedRegion perform(IDocument doc) {
            IModelManager manager = StructuredModelManager.getModelManager();
            IStructuredModel model = manager.getExistingModelForEdit(doc);
            if (model != null) {
                try {
                    perform(model);
                } finally {
                    model.releaseFromEdit();
                }
            }

            return null;
        }

        private IndexedRegion perform(IFile file) {
            IModelManager manager = StructuredModelManager.getModelManager();
            IStructuredModel model;
            try {
                model = manager.getModelForEdit(file);
                if (model != null) {
                    try {
                        perform(model);
                    } finally {
                        model.releaseFromEdit();
                    }
                }
            } catch (Exception e) {
                AdtPlugin.log(e, "Can't look up XML model");
            }

            return null;
        }

        private IndexedRegion perform(IStructuredModel model) {
            if (model instanceof IDOMModel) {
                IDOMModel domModel = (IDOMModel) model;
                Document document = domModel.getDocument();
                Element element = document.getDocumentElement();
                Attr attr = document.createAttributeNS(XMLNS_URI, XMLNS_ANDROID);
                attr.setValue(ANDROID_URI);
                element.getAttributes().setNamedItemNS(attr);
                return (IndexedRegion) attr;
            }

            return null;
        }

        // ---- Implements ICompletionProposal ----

        @Override
        public void apply(IDocument document) {
            perform(document);
        }

        @Override
        public String getAdditionalProposalInfo() {
            return "Adds an Android namespace declaratiopn to the root element.";
        }

        @Override
        public IContextInformation getContextInformation() {
            return null;
        }

        @Override
        public String getDisplayString() {
            return "Insert namespace binding";
        }

        @Override
        public Image getImage() {
            return AdtPlugin.getAndroidLogo();
        }

        @Override
        public Point getSelection(IDocument doc) {
            return null;
        }


        // ---- Implements MarkerResolution2 ----

        @Override
        public String getLabel() {
            return getDisplayString();
        }

        @Override
        public void run(IMarker marker) {
            try {
                AdtPlugin.openFile(mFile, null);
            } catch (PartInitException e) {
                AdtPlugin.log(e, "Can't open file %1$s", mFile.getName());
            }

            IndexedRegion indexedRegion = perform(mFile);
            if (indexedRegion != null) {
                try {
                    IRegion region =
                        new Region(indexedRegion.getStartOffset(), indexedRegion.getLength());
                    AdtPlugin.openFile(mFile, region);
                } catch (PartInitException e) {
                    AdtPlugin.log(e, "Can't open file %1$s", mFile.getName());
                }
            }
        }

        @Override
        public String getDescription() {
            return getAdditionalProposalInfo();
        }
    }

    private static class CreateResourceProposal
            implements ICompletionProposal, IMarkerResolution2 {
        private final IProject mProject;
        private final String mResource;

        CreateResourceProposal(IProject project, String resource) {
            super();
            mProject = project;
            mResource = resource;
        }

        private void perform() {
            ResourceUrl resource = ResourceUrl.parse(mResource);
            if (resource == null) {
                return;
            }
            ResourceType type = resource.type;
            String name = resource.name;
            assert !resource.framework;
            String value = ""; //$NON-NLS-1$

            // Try to pick a reasonable first guess. The new value will be highlighted and
            // selected for editing, but if we have an initial value then the new file
            // won't show an error.
            switch (type) {
                case STRING: value = "TODO"; break; //$NON-NLS-1$
                case DIMEN: value = "1dp"; break; //$NON-NLS-1$
                case BOOL: value = "true"; break; //$NON-NLS-1$
                case COLOR: value = "#000000"; break; //$NON-NLS-1$
                case INTEGER: value = "1"; break; //$NON-NLS-1$
                case ARRAY: value = "<item>1</item>"; break; //$NON-NLS-1$
            }

            Pair<IFile, IRegion> location =
                ResourceHelper.createResource(mProject, type, name, value);
            if (location != null) {
                IFile file = location.getFirst();
                IRegion region = location.getSecond();
                try {
                    AdtPlugin.openFile(file, region);
                } catch (PartInitException e) {
                    AdtPlugin.log(e, "Can't open file %1$s", file.getName());
                }
            }
        }

        // ---- Implements ICompletionProposal ----

        @Override
        public void apply(IDocument document) {
            perform();
        }

        @Override
        public String getAdditionalProposalInfo() {
            return "Creates an XML file entry for the given missing resource "
                    + "and opens it in the editor.";
        }

        @Override
        public IContextInformation getContextInformation() {
            return null;
        }

        @Override
        public String getDisplayString() {
            return String.format("Create resource %1$s", mResource);
        }

        @Override
        public Image getImage() {
            return AdtPlugin.getAndroidLogo();
        }

        @Override
        public Point getSelection(IDocument document) {
            return null;
        }

        // ---- Implements MarkerResolution2 ----

        @Override
        public String getLabel() {
            return getDisplayString();
        }

        @Override
        public void run(IMarker marker) {
            perform();
        }

        @Override
        public String getDescription() {
            return getAdditionalProposalInfo();
        }
    }
}