update copyright
[fedora-idea.git] / xml / impl / src / com / intellij / codeInsight / daemon / impl / analysis / XmlErrorQuickFixProvider.java
blob9372cb3ab07f199afcb36034ada1e5c1715070b4
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.codeInsight.daemon.impl.analysis;
18 import com.intellij.psi.PsiErrorElement;
19 import com.intellij.psi.PsiFile;
20 import com.intellij.psi.xml.XmlTag;
21 import com.intellij.psi.util.PsiTreeUtil;
22 import com.intellij.codeInsight.daemon.impl.HighlightInfo;
23 import com.intellij.codeInsight.daemon.impl.quickfix.QuickFixAction;
24 import com.intellij.codeInsight.daemon.XmlErrorMessages;
25 import com.intellij.codeInsight.intention.IntentionAction;
26 import com.intellij.codeInsight.CodeInsightUtilBase;
27 import com.intellij.openapi.project.Project;
28 import com.intellij.openapi.editor.Editor;
29 import org.jetbrains.annotations.NonNls;
30 import org.jetbrains.annotations.NotNull;
32 public class XmlErrorQuickFixProvider implements ErrorQuickFixProvider {
33 @NonNls private static final String AMP_ENTITY = "&";
35 public void registerErrorQuickFix(final PsiErrorElement element, final HighlightInfo highlightInfo) {
36 if (PsiTreeUtil.getParentOfType(element, XmlTag.class) != null) {
37 registerXmlErrorQuickFix(element,highlightInfo);
41 private static void registerXmlErrorQuickFix(final PsiErrorElement element, final HighlightInfo highlightInfo) {
42 final String text = element.getErrorDescription();
43 if (text != null && text.startsWith(XmlErrorMessages.message("unescaped.ampersand"))) {
44 QuickFixAction.registerQuickFixAction(highlightInfo, new IntentionAction() {
45 @NotNull
46 public String getText() {
47 return XmlErrorMessages.message("escape.ampersand.quickfix");
50 @NotNull
51 public String getFamilyName() {
52 return getText();
55 public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
56 return true;
59 public void invoke(@NotNull Project project, Editor editor, PsiFile file) {
60 if (!CodeInsightUtilBase.prepareFileForWrite(file)) return;
61 final int textOffset = element.getTextOffset();
62 editor.getDocument().replaceString(textOffset,textOffset + 1,AMP_ENTITY);
65 public boolean startInWriteAction() {
66 return true;
68 });