forked from JetBrains/intellij-community
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HTML: add unknown boolean attribute inspection
- Loading branch information
Showing
12 changed files
with
278 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
xml/impl/src/com/intellij/codeInspection/htmlInspections/AddAttributeValueIntentionFix.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright 2000-2013 JetBrains s.r.o. | ||
* | ||
* Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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.intellij.codeInspection.htmlInspections; | ||
|
||
import com.intellij.codeInsight.AutoPopupController; | ||
import com.intellij.codeInsight.FileModificationService; | ||
import com.intellij.codeInsight.daemon.XmlErrorMessages; | ||
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement; | ||
import com.intellij.openapi.application.Result; | ||
import com.intellij.openapi.command.WriteCommandAction; | ||
import com.intellij.openapi.editor.Editor; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiFile; | ||
import com.intellij.psi.XmlElementFactory; | ||
import com.intellij.psi.util.PsiTreeUtil; | ||
import com.intellij.psi.xml.XmlAttribute; | ||
import com.intellij.psi.xml.XmlAttributeValue; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public class AddAttributeValueIntentionFix extends LocalQuickFixAndIntentionActionOnPsiElement { | ||
public AddAttributeValueIntentionFix(@Nullable PsiElement element) { | ||
super(element); | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public String getText() { | ||
return XmlErrorMessages.message("add.attribute.value.quickfix.text"); | ||
} | ||
|
||
@Override | ||
@NotNull | ||
public String getFamilyName() { | ||
return getName(); | ||
} | ||
|
||
@Override | ||
public void invoke(@NotNull Project project, | ||
@NotNull PsiFile file, | ||
@Nullable("is null when called from inspection") final Editor editor, | ||
@NotNull PsiElement startElement, | ||
@NotNull PsiElement endElement) { | ||
final XmlAttribute attribute = PsiTreeUtil.getNonStrictParentOfType(startElement, XmlAttribute.class); | ||
if (attribute == null || attribute.getValue() != null) { | ||
return; | ||
} | ||
|
||
if (!FileModificationService.getInstance().prepareFileForWrite(attribute.getContainingFile())) { | ||
return; | ||
} | ||
|
||
new WriteCommandAction(project) { | ||
@Override | ||
protected void run(@NotNull final Result result) { | ||
final XmlAttribute attributeWithValue = XmlElementFactory.getInstance(getProject()).createXmlAttribute(attribute.getName(), ""); | ||
final PsiElement newAttribute = attribute.replace(attributeWithValue); | ||
|
||
if (editor != null && newAttribute != null && newAttribute instanceof XmlAttribute && newAttribute.isValid()) { | ||
final XmlAttributeValue valueElement = ((XmlAttribute)newAttribute).getValueElement(); | ||
if (valueElement != null) { | ||
editor.getCaretModel().moveToOffset(valueElement.getTextOffset()); | ||
AutoPopupController.getInstance(newAttribute.getProject()).scheduleAutoPopup(editor); | ||
} | ||
} | ||
} | ||
}.execute(); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...rc/com/intellij/codeInspection/htmlInspections/HtmlUnknownBooleanAttributeInspection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright 2000-2013 JetBrains s.r.o. | ||
* | ||
* Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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.intellij.codeInspection.htmlInspections; | ||
|
||
import org.jetbrains.annotations.Nullable; | ||
|
||
import javax.swing.*; | ||
|
||
public class HtmlUnknownBooleanAttributeInspection extends HtmlUnknownBooleanAttributeInspectionBase { | ||
|
||
public HtmlUnknownBooleanAttributeInspection() { | ||
super(""); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public JComponent createOptionsPanel() { | ||
return HtmlUnknownTagInspection.createOptionsPanel(this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
xml/xml-analysis-impl/resources/inspectionDescriptions/HtmlUnknownBooleanAttribute.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<html> | ||
<body> | ||
This inspection highlights HTML none-boolean tag attributes without value as invalid, and lets mark such attributes as Custom to avoid highlighting them as | ||
invalid.<br> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
...om/intellij/codeInspection/htmlInspections/HtmlUnknownBooleanAttributeInspectionBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Copyright 2000-2013 JetBrains s.r.o. | ||
* | ||
* Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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.intellij.codeInspection.htmlInspections; | ||
|
||
import com.intellij.codeInsight.daemon.XmlErrorMessages; | ||
import com.intellij.codeInspection.LocalQuickFix; | ||
import com.intellij.codeInspection.ProblemsHolder; | ||
import com.intellij.codeInspection.XmlQuickFixFactory; | ||
import com.intellij.openapi.diagnostic.Logger; | ||
import com.intellij.openapi.util.Key; | ||
import com.intellij.psi.html.HtmlTag; | ||
import com.intellij.psi.xml.XmlAttribute; | ||
import com.intellij.psi.xml.XmlTag; | ||
import com.intellij.xml.XmlAttributeDescriptor; | ||
import com.intellij.xml.XmlBundle; | ||
import com.intellij.xml.XmlElementDescriptor; | ||
import com.intellij.xml.impl.schema.AnyXmlElementDescriptor; | ||
import com.intellij.xml.util.HtmlUtil; | ||
import org.jetbrains.annotations.Nls; | ||
import org.jetbrains.annotations.NonNls; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public abstract class HtmlUnknownBooleanAttributeInspectionBase extends HtmlUnknownElementInspection { | ||
private static final Key<HtmlUnknownElementInspection> BOOLEAN_ATTRIBUTE_KEY = Key.create(BOOLEAN_ATTRIBUTE_SHORT_NAME); | ||
private static final Logger LOG = Logger.getInstance(HtmlUnknownBooleanAttributeInspectionBase.class); | ||
|
||
public HtmlUnknownBooleanAttributeInspectionBase(String defaultValues) { | ||
super(defaultValues); | ||
} | ||
|
||
@Override | ||
@Nls | ||
@NotNull | ||
public String getDisplayName() { | ||
return XmlBundle.message("html.inspections.unknown.boolean.attribute"); | ||
} | ||
|
||
@Override | ||
@NonNls | ||
@NotNull | ||
public String getShortName() { | ||
return BOOLEAN_ATTRIBUTE_SHORT_NAME; | ||
} | ||
|
||
@Override | ||
protected String getCheckboxTitle() { | ||
return XmlBundle.message("html.inspections.unknown.tag.boolean.attribute.checkbox.title"); | ||
} | ||
|
||
@NotNull | ||
@Override | ||
protected String getPanelTitle() { | ||
return XmlBundle.message("html.inspections.unknown.tag.boolean.attribute.title"); | ||
} | ||
|
||
@Override | ||
@NotNull | ||
protected Logger getLogger() { | ||
return LOG; | ||
} | ||
|
||
@Override | ||
protected void checkAttribute(@NotNull final XmlAttribute attribute, @NotNull final ProblemsHolder holder, final boolean isOnTheFly) { | ||
if (attribute.getValueElement() == null) { | ||
final XmlTag tag = attribute.getParent(); | ||
|
||
if (tag instanceof HtmlTag) { | ||
XmlElementDescriptor elementDescriptor = tag.getDescriptor(); | ||
if (elementDescriptor == null || elementDescriptor instanceof AnyXmlElementDescriptor) { | ||
return; | ||
} | ||
|
||
XmlAttributeDescriptor attributeDescriptor = elementDescriptor.getAttributeDescriptor(attribute); | ||
if (attributeDescriptor != null) { | ||
String name = attribute.getName(); | ||
if (!HtmlUtil.isBooleanAttribute(attributeDescriptor) && (!isCustomValuesEnabled() || !isCustomValue(name))) { | ||
LocalQuickFix[] quickFixes = new LocalQuickFix[]{ | ||
new AddCustomHtmlElementIntentionAction(BOOLEAN_ATTRIBUTE_KEY, name, XmlBundle.message("add.custom.html.boolean.attribute", name)), | ||
XmlQuickFixFactory.getInstance().addAttributeValueFix(attribute), | ||
new RemoveAttributeIntentionAction(name), | ||
}; | ||
|
||
registerProblemOnAttributeName(attribute, XmlErrorMessages.message("attribute.is.not.boolean", attribute.getName()), holder, | ||
quickFixes); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.