2222package com .github ._1c_syntax .bsl .languageserver .diagnostics ;
2323
2424import com .github ._1c_syntax .bsl .languageserver .context .symbol .SymbolTree ;
25- import com .github ._1c_syntax .bsl .languageserver .context .symbol .VariableSymbol ;
2625import com .github ._1c_syntax .bsl .languageserver .diagnostics .metadata .DiagnosticMetadata ;
2726import com .github ._1c_syntax .bsl .languageserver .diagnostics .metadata .DiagnosticParameter ;
2827import com .github ._1c_syntax .bsl .languageserver .diagnostics .metadata .DiagnosticSeverity ;
3635
3736import java .util .Arrays ;
3837import java .util .HashSet ;
38+ import java .util .Locale ;
3939import java .util .Map ;
4040import java .util .Optional ;
4141import java .util .Set ;
@@ -91,6 +91,8 @@ public class MagicDateDiagnostic extends AbstractVisitorDiagnostic {
9191 "\\ D"
9292 );
9393
94+ private static final Pattern digitsPattern = Pattern .compile ("\\ d+" );
95+
9496 private static final Pattern STRUCTURE_METHOD_PATTERN = CaseInsensitivePattern .compile (
9597 "(?i)Вставить|Insert"
9698 );
@@ -106,6 +108,8 @@ public class MagicDateDiagnostic extends AbstractVisitorDiagnostic {
106108 private static final int MAX_SECOND = 59 ;
107109 private static final int DATE_LENGTH = 8 ;
108110 private static final int DATETIME_LENGTH = 14 ;
111+ private static final int NAVIGATION_LEVELS_TO_METHOD_CALL = 4 ;
112+ private static final int NAVIGATION_LEVELS_TO_ASSIGNMENT = 4 ;
109113
110114 @ DiagnosticParameter (
111115 type = String .class ,
@@ -146,7 +150,7 @@ public void configure(Map<String, Object> configuration) {
146150 * @return true, если строка является валидной датой, иначе false
147151 */
148152 private static boolean isValidDateString (String dateString ) {
149- return dateString .matches (" \\ d+" ) && (dateString .length () == DATE_LENGTH || dateString .length () == DATETIME_LENGTH );
153+ return digitsPattern . matcher ( dateString ) .matches () && (dateString .length () == DATE_LENGTH || dateString .length () == DATETIME_LENGTH );
150154 }
151155
152156
@@ -331,7 +335,7 @@ private static boolean insideReturnSimpleDate(BSLParser.ExpressionContext expres
331335 * @return true, если выражение находится внутри присваивания с методом Дата(), иначе false
332336 */
333337 private static boolean insideAssignmentWithDateMethodForSimpleDate (BSLParser .ExpressionContext expression ) {
334- BSLParserRuleContext current = navigateToParent (expression , 4 );
338+ BSLParserRuleContext current = navigateToParent (expression , NAVIGATION_LEVELS_TO_METHOD_CALL );
335339 if (!(current instanceof BSLParser .GlobalMethodCallContext )) {
336340 return false ;
337341 }
@@ -341,7 +345,7 @@ private static boolean insideAssignmentWithDateMethodForSimpleDate(BSLParser.Exp
341345 return false ;
342346 }
343347
344- current = navigateToParent (current , 4 );
348+ current = navigateToParent (current , NAVIGATION_LEVELS_TO_ASSIGNMENT );
345349 return current instanceof BSLParser .AssignmentContext ;
346350 }
347351
@@ -353,7 +357,7 @@ private static boolean insideAssignmentWithDateMethodForSimpleDate(BSLParser.Exp
353357 * @return родительский контекст или null, если навигация невозможна
354358 */
355359 private static BSLParserRuleContext navigateToParent (BSLParserRuleContext context , int levels ) {
356- BSLParserRuleContext current = context ;
360+ var current = context ;
357361 for (int i = 0 ; i < levels ; i ++) {
358362 if (current == null ) {
359363 return null ;
@@ -413,7 +417,7 @@ private boolean isStructureMethodCall(BSLParser.ExpressionContext expr) {
413417 return false ;
414418 }
415419
416- String callText = callStatement .getText ();
420+ var callText = callStatement .getText ();
417421 if (!isValidCallText (callText )) {
418422 return false ;
419423 }
@@ -432,14 +436,14 @@ private boolean isStructureMethodCall(BSLParser.ExpressionContext expr) {
432436 * @return true, если это вызов метода структуры, иначе false
433437 */
434438 private boolean isStructureObjectCall (String callText ) {
435- int dotIndex = callText .indexOf ('.' );
436- String objectName = callText .substring (0 , dotIndex ).trim ();
437- String methodPart = callText .substring (dotIndex + 1 );
439+ var dotIndex = callText .indexOf ('.' );
440+ var methodPart = callText .substring (dotIndex + 1 );
438441
439442 if (!STRUCTURE_METHOD_PATTERN .matcher (methodPart ).find ()) {
440443 return false ;
441444 }
442445
446+ var objectName = callText .substring (0 , dotIndex ).trim ();
443447 return isVariableOfStructureType (objectName );
444448 }
445449
@@ -450,7 +454,7 @@ private boolean isStructureObjectCall(String callText) {
450454 * @return true, если текст валиден, иначе false
451455 */
452456 private static boolean isValidCallText (String callText ) {
453- int dotIndex = callText .indexOf ('.' );
457+ var dotIndex = callText .indexOf ('.' );
454458 return dotIndex > 0 && callText .contains ("(" );
455459 }
456460
@@ -470,8 +474,8 @@ private boolean isVariableOfStructureType(String variableName) {
470474
471475 return variableSymbol .get ().getDescription ()
472476 .map (description -> description .getDescription ())
473- .map (description -> {
474- var lowerDescription = description .toLowerCase ();
477+ .map (( String description ) -> {
478+ var lowerDescription = description .toLowerCase (Locale . ROOT );
475479 return lowerDescription .contains ("структура" ) || lowerDescription .contains ("structure" );
476480 })
477481 .orElse (false );
0 commit comments