From acc583e096e4d393c7473d6be477f977ea64ebca Mon Sep 17 00:00:00 2001 From: Ekaterina Tuzova Date: Mon, 2 Dec 2013 19:11:48 +0400 Subject: [PATCH] fixed PY-10998 Wrong highlight in rst (restructured texts) for headlines --- python/rest/resources/META-INF/plugin.xml | 10 ++++-- python/rest/rest.iml | 2 +- .../com/jetbrains/rest/RestBundle.properties | 1 + .../src/com/jetbrains/rest/RestLanguage.java | 6 ++-- .../src/com/jetbrains/rest/psi/RestTitle.java | 30 ++++++++++++++--- .../rest/validation/RestTitleAnnotator.java | 32 +++++++++++++++++++ 6 files changed, 68 insertions(+), 13 deletions(-) create mode 100644 python/rest/src/com/jetbrains/rest/validation/RestTitleAnnotator.java diff --git a/python/rest/resources/META-INF/plugin.xml b/python/rest/resources/META-INF/plugin.xml index 06cd1f494960..76e572763830 100644 --- a/python/rest/resources/META-INF/plugin.xml +++ b/python/rest/resources/META-INF/plugin.xml @@ -3,12 +3,16 @@ org.jetbrains.plugins.rest This plugin enables support for reStructuredText files (*.rst) JetBrains - 132.SNAPSHOT - + 134.SNAPSHOT + com.intellij.modules.lang - + +
  • Added inspection for title & underline length math (PY-10998)
  • + +]]>
    diff --git a/python/rest/rest.iml b/python/rest/rest.iml index b9ccb2c7f453..0310775a2f43 100644 --- a/python/rest/rest.iml +++ b/python/rest/rest.iml @@ -1,5 +1,5 @@ - + diff --git a/python/rest/src/com/jetbrains/rest/RestBundle.properties b/python/rest/src/com/jetbrains/rest/RestBundle.properties index 24b98c7431f4..192f44308c13 100644 --- a/python/rest/src/com/jetbrains/rest/RestBundle.properties +++ b/python/rest/src/com/jetbrains/rest/RestBundle.properties @@ -9,6 +9,7 @@ QFIX.ignore.role=Ignore undefined role ''{0}'' ### Annotators ### ANN.unknown.target=Unknown target name ''{0}'' ANN.duplicate.target=Duplicate explicit target name ''{0}'' +ANN.title.length=Title length must match the underline ANN.unusable.anonymous.target=Anonymous hyperlink target has no reference ANN.inline.block=Blank line is required after a literal block diff --git a/python/rest/src/com/jetbrains/rest/RestLanguage.java b/python/rest/src/com/jetbrains/rest/RestLanguage.java index c451e379f437..df056590efcc 100644 --- a/python/rest/src/com/jetbrains/rest/RestLanguage.java +++ b/python/rest/src/com/jetbrains/rest/RestLanguage.java @@ -17,10 +17,7 @@ package com.jetbrains.rest; import com.intellij.lang.Language; import com.intellij.psi.templateLanguages.TemplateLanguage; -import com.jetbrains.rest.validation.RestAnnotator; -import com.jetbrains.rest.validation.RestHyperlinksAnnotator; -import com.jetbrains.rest.validation.RestInlineBlockAnnotator; -import com.jetbrains.rest.validation.RestReferenceTargetAnnotator; +import com.jetbrains.rest.validation.*; import java.util.Set; import java.util.concurrent.CopyOnWriteArraySet; @@ -48,6 +45,7 @@ public class RestLanguage extends Language implements TemplateLanguage { _annotators.add(RestHyperlinksAnnotator.class); _annotators.add(RestReferenceTargetAnnotator.class); _annotators.add(RestInlineBlockAnnotator.class); + _annotators.add(RestTitleAnnotator.class); } public Set> getAnnotators() { diff --git a/python/rest/src/com/jetbrains/rest/psi/RestTitle.java b/python/rest/src/com/jetbrains/rest/psi/RestTitle.java index eb93cbee77ac..05c2e66c2c52 100644 --- a/python/rest/src/com/jetbrains/rest/psi/RestTitle.java +++ b/python/rest/src/com/jetbrains/rest/psi/RestTitle.java @@ -16,6 +16,7 @@ package com.jetbrains.rest.psi; import com.intellij.lang.ASTNode; +import com.jetbrains.rest.validation.RestElementVisitor; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -37,15 +38,13 @@ public class RestTitle extends RestElement { @Nullable public String getName() { - final String text = getNode().getText(); - if (text.length() == 0) return null; + final String text = getNode().getText().trim(); + if (text.length() < 2) return null; final char adorn = text.charAt(text.length()-2); final CharacterIterator it = new StringCharacterIterator(text); int finish = 0; for (char ch = it.last(); ch != CharacterIterator.DONE; ch = it.previous()) { - if (finish == 0) - finish++; - else if (ch != adorn) { + if (ch != adorn) { finish = it.getIndex(); break; } @@ -63,4 +62,25 @@ public class RestTitle extends RestElement { return null; return text.substring(start, finish).trim(); } + + @Nullable + public String getUnderline() { + final String text = getNode().getText().trim(); + if (text.length() < 2) return null; + final char adorn = text.charAt(text.length()-2); + final CharacterIterator it = new StringCharacterIterator(text); + int start = 0; + for (char ch = it.last(); ch != CharacterIterator.DONE; ch = it.previous()) { + if (ch != adorn) { + start = it.getIndex() + 1; + break; + } + } + return text.substring(start, text.length()); + } + + @Override + protected void acceptRestVisitor(RestElementVisitor visitor) { + visitor.visitTitle(this); + } } diff --git a/python/rest/src/com/jetbrains/rest/validation/RestTitleAnnotator.java b/python/rest/src/com/jetbrains/rest/validation/RestTitleAnnotator.java new file mode 100644 index 000000000000..c906e311af83 --- /dev/null +++ b/python/rest/src/com/jetbrains/rest/validation/RestTitleAnnotator.java @@ -0,0 +1,32 @@ +/* + * 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.jetbrains.rest.validation; + +import com.jetbrains.rest.RestBundle; +import com.jetbrains.rest.psi.RestTitle; + +public class RestTitleAnnotator extends RestAnnotator { + @Override + public void visitTitle(final RestTitle node) { + final String name = node.getName(); + if (name == null) return; + int nameLen = name.length(); + final String underline = node.getUnderline(); + if (underline != null && nameLen != underline.length()) { + getHolder().createWarningAnnotation(node, RestBundle.message("ANN.title.length")); + } + } +}