From eb7442a6c398684f4c5f3ed38f34bd72814eac16 Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Fri, 29 Aug 2014 17:31:32 +0400 Subject: [PATCH] PY-11481 Clean up in StringLiteralQuotesAnnotator, move messages to bundle --- .../com/jetbrains/python/PyBundle.properties | 3 ++ .../StringLiteralQuotesAnnotator.java | 35 ++++++++++--------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/python/src/com/jetbrains/python/PyBundle.properties b/python/src/com/jetbrains/python/PyBundle.properties index 482daa88b66c..d82e9a6fde62 100644 --- a/python/src/com/jetbrains/python/PyBundle.properties +++ b/python/src/com/jetbrains/python/PyBundle.properties @@ -676,6 +676,9 @@ ANN.tuple.py3=tuple parameter unpacking is not supported in Python 3 ANN.star.import.at.top.only='import *' only allowed at module level +ANN.missing.closing.quote=Missing closing quote [{0}] +ANN.missing.closing.triple.quotes=Missing closing triple quotes + ANN.method.$0.removed.use.$1=Method ''{0}'' has been removed, use ''{1}'' instead ANN.method.$0.removed=Method ''{0}'' removed diff --git a/python/src/com/jetbrains/python/validation/StringLiteralQuotesAnnotator.java b/python/src/com/jetbrains/python/validation/StringLiteralQuotesAnnotator.java index 0666e8319caa..4ae2bfd89a2c 100644 --- a/python/src/com/jetbrains/python/validation/StringLiteralQuotesAnnotator.java +++ b/python/src/com/jetbrains/python/validation/StringLiteralQuotesAnnotator.java @@ -18,8 +18,10 @@ package com.jetbrains.python.validation; import com.intellij.lang.ASTNode; import com.intellij.openapi.util.TextRange; import com.intellij.openapi.util.text.StringUtil; +import com.jetbrains.python.PyBundle; import com.jetbrains.python.psi.PyStringLiteralExpression; import com.jetbrains.python.psi.impl.PyStringLiteralExpressionImpl; +import org.jetbrains.annotations.NotNull; import java.util.List; @@ -29,17 +31,16 @@ import java.util.List; * @author dcheryasov */ public class StringLiteralQuotesAnnotator extends PyAnnotator { - public static final String MISSING_Q = "Missing closing quote"; private static final String TRIPLE_QUOTES = "\"\"\""; private static final String TRIPLE_APOS = "'''"; public void visitPyStringLiteralExpression(final PyStringLiteralExpression node) { - List stringNodes = node.getStringNodes(); + final List stringNodes = node.getStringNodes(); for (ASTNode stringNode : stringNodes) { - boolean foundError; - String nodeText = stringNode.getText(); - int index = PyStringLiteralExpressionImpl.getPrefixLength(nodeText); - String unprefixed = nodeText.substring(index); + final String nodeText = stringNode.getText(); + final int index = PyStringLiteralExpressionImpl.getPrefixLength(nodeText); + final String unprefixed = nodeText.substring(index); + final boolean foundError; if (StringUtil.startsWith(unprefixed, TRIPLE_QUOTES)) { foundError = checkTripleQuotedString(stringNode, unprefixed, TRIPLE_QUOTES); } @@ -49,23 +50,25 @@ public class StringLiteralQuotesAnnotator extends PyAnnotator { else { foundError = checkQuotedString(stringNode, unprefixed); } - if (foundError) break; + if (foundError) { + break; + } } } - private boolean checkQuotedString(ASTNode stringNode, String nodeText) { - char firstQuote = nodeText.charAt(0); - int lastChar = nodeText.length()-1; + private boolean checkQuotedString(@NotNull ASTNode stringNode, @NotNull String nodeText) { + final char firstQuote = nodeText.charAt(0); + final int lastChar = nodeText.length() - 1; if (lastChar == 0 || nodeText.charAt(lastChar) != firstQuote || - (nodeText.charAt(lastChar-1) == '\\' && (lastChar == 1 || nodeText.charAt(lastChar-2) != '\\'))) { - getHolder().createErrorAnnotation(stringNode, MISSING_Q + " [" + firstQuote + "]"); + (nodeText.charAt(lastChar - 1) == '\\' && (lastChar == 1 || nodeText.charAt(lastChar - 2) != '\\'))) { + getHolder().createErrorAnnotation(stringNode, PyBundle.message("ANN.missing.closing.quote", firstQuote)); return true; } return false; } - private boolean checkTripleQuotedString(ASTNode stringNode, String text, final String quotes) { - if (text.length() < 6 || !text.endsWith(quotes)) { + private boolean checkTripleQuotedString(@NotNull ASTNode stringNode, @NotNull String text, @NotNull String quotes) { + if (text.length() < 6 || !text.endsWith(quotes)) { int startOffset = StringUtil.trimTrailing(stringNode.getText()).lastIndexOf('\n'); if (startOffset < 0) { startOffset = stringNode.getTextRange().getStartOffset(); @@ -73,8 +76,8 @@ public class StringLiteralQuotesAnnotator extends PyAnnotator { else { startOffset = stringNode.getTextRange().getStartOffset() + startOffset + 1; } - TextRange highlightRange = new TextRange(startOffset, stringNode.getTextRange().getEndOffset()); - getHolder().createErrorAnnotation(highlightRange, "Missing closing triple quotes"); + final TextRange highlightRange = new TextRange(startOffset, stringNode.getTextRange().getEndOffset()); + getHolder().createErrorAnnotation(highlightRange, PyBundle.message("ANN.missing.closing.triple.quotes")); return true; } return false;