55import com .intellij .codeInspection .ProblemsHolder ;
66import com .intellij .psi .PsiElement ;
77import com .intellij .psi .PsiElementVisitor ;
8+ import com .intellij .psi .PsiErrorElement ;
89import fr .adrienbrault .idea .symfony2plugin .Symfony2ProjectComponent ;
910import fr .adrienbrault .idea .symfony2plugin .util .SymfonyUtil ;
1011import org .apache .commons .lang .StringUtils ;
@@ -19,30 +20,36 @@ public class YamlQuotedEscapedInspection extends LocalInspectionTool {
1920 @ NotNull
2021 public PsiElementVisitor buildVisitor (final @ NotNull ProblemsHolder holder , boolean isOnTheFly ) {
2122
22- if (!Symfony2ProjectComponent .isEnabled (holder .getProject ())) {
23+ if (!Symfony2ProjectComponent .isEnabled (holder .getProject ())) {
2324 return super .buildVisitor (holder , isOnTheFly );
2425 }
2526
2627 return new PsiElementVisitor () {
2728 @ Override
2829 public void visitElement (PsiElement element ) {
29- if (element .getNode ().getElementType () == YAMLTokenTypes .SCALAR_DSTRING && SymfonyUtil .isVersionGreaterThenEquals (element .getProject (), "2.8" )) {
30+ if (element .getNode ().getElementType () == YAMLTokenTypes .SCALAR_DSTRING && SymfonyUtil .isVersionGreaterThenEquals (element .getProject (), "2.8" )) {
3031 // "Foo\Foo" -> "Foo\\Foo"
3132 String text = StringUtils .strip (element .getText (), "\" " );
3233
3334 // dont check to long strings
3435 // ascii chars that need to be escape; some @see Symfony\Component\Yaml\Unescaper
35- if (text .length () < 255 && text .matches (".*[^\\ \\ ]\\ \\ [^\\ \\ 0abtnvfre \" /N_LPxuU].*" )) {
36+ if (text .length () < 255 && text .matches (".*[^\\ \\ ]\\ \\ [^\\ \\ 0abtnvfre \" /N_LPxuU].*" )) {
3637 holder .registerProblem (element , "Not escaping a backslash in a double-quoted string is deprecated" , ProblemHighlightType .WEAK_WARNING );
3738 }
3839 } else if (element .getNode ().getElementType () == YAMLTokenTypes .TEXT && SymfonyUtil .isVersionGreaterThenEquals (element .getProject (), "2.8" )) {
3940 // @foo -> "@foo"
40- String text = element .getText ();
41- if (text .length () > 1 ) {
41+ String text ;
42+ if (parentIsErrorAndHasPreviousElement (element )) {
43+ text = element .getParent ().getPrevSibling ().getText ();
44+ } else {
45+ text = element .getText ();
46+ }
47+
48+ if (text .length () > 1 || (parentIsErrorAndHasPreviousElement (element ) && text .length () >= 1 )) {
4249 String startChar = text .substring (0 , 1 );
43- if (startChar .equals ("@" ) || startChar .equals ("`" ) || startChar .equals ("|" ) || startChar .equals (">" )) {
50+ if (startChar .equals ("@" ) || startChar .equals ("`" ) || startChar .equals ("|" ) || startChar .equals (">" )) {
4451 holder .registerProblem (element , String .format ("Deprecated usage of '%s' at the beginning of unquoted string" , startChar ), ProblemHighlightType .WEAK_WARNING );
45- } else if (startChar .equals ("%" )) {
52+ } else if (startChar .equals ("%" )) {
4653 // deprecated in => "3.1"; but as most user will need to migrate in 2.8 let them know it already
4754 holder .registerProblem (element , "Not quoting a scalar starting with the '%' indicator character is deprecated since Symfony 3.1" , ProblemHighlightType .WEAK_WARNING );
4855 }
@@ -53,4 +60,9 @@ public void visitElement(PsiElement element) {
5360 };
5461 }
5562
63+ private boolean parentIsErrorAndHasPreviousElement (@ NotNull PsiElement element ) {
64+ PsiElement parent = element .getParent ();
65+
66+ return parent instanceof PsiErrorElement && parent .getPrevSibling () != null ;
67+ }
5668}
0 commit comments