mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Closes PY-31 "Unclosed string literals are not highlighted as erorrs".
This commit is contained in:
@@ -36,6 +36,7 @@ public class PythonLanguage extends Language {
|
||||
_annotators.add(DocStringAnnotator.class);
|
||||
_annotators.add(ImportAnnotator.class);
|
||||
_annotators.add(UnresolvedReferenceAnnotator.class);
|
||||
_annotators.add(StringConstantAnnotator.class);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.jetbrains.python.validation;
|
||||
|
||||
import com.jetbrains.python.psi.PyStringLiteralExpression;
|
||||
|
||||
/**
|
||||
* Looks for well-formedness of string constants.
|
||||
* User: dcheryasov
|
||||
* Date: Jul 5, 2008
|
||||
* Time: 11:58:57 PM
|
||||
*/
|
||||
public class StringConstantAnnotator extends PyAnnotator {
|
||||
public void visitPyStringLiteralExpression(final PyStringLiteralExpression node) {
|
||||
String s = node.getText();
|
||||
String msg = "";
|
||||
boolean ok = true;
|
||||
boolean esc = false;
|
||||
int index = 0;
|
||||
// skip 'unicode' and 'raw' modifiers
|
||||
char first_quote = s.charAt(index);
|
||||
if ((first_quote == 'u') || (first_quote == 'U')) index += 1;
|
||||
first_quote = s.charAt(index);
|
||||
if ((first_quote == 'r') || (first_quote == 'R')) index += 1;
|
||||
first_quote = s.charAt(index);
|
||||
// s can't begin with non-quote, else parser would not say it's a string
|
||||
index += 1;
|
||||
if (index >= s.length()) { // sole opening quote
|
||||
msg = "No closing quote [" + first_quote + "]";
|
||||
ok = false;
|
||||
}
|
||||
else {
|
||||
while (ok && (index < s.length()-1)) {
|
||||
char c = s.charAt(index);
|
||||
if (esc) esc = false;
|
||||
else {
|
||||
if (c == first_quote) {
|
||||
msg = "Premature closing quote [" + first_quote + "]";
|
||||
ok = false;
|
||||
}
|
||||
else if (c == '\\') esc = true;
|
||||
}
|
||||
index += 1;
|
||||
}
|
||||
if (ok && (esc || (s.charAt(index) != first_quote))) {
|
||||
msg = "Missing closing quote [" + first_quote + "]";
|
||||
ok = false;
|
||||
}
|
||||
}
|
||||
//
|
||||
if (! ok) {
|
||||
getHolder().createErrorAnnotation(node, msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user