com.github._1c_syntax.bsl.languageserver.diagnostics.PairingBrokenTransactionDiagnostic Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bsl-language-server Show documentation
Show all versions of bsl-language-server Show documentation
Language Server Protocol implementation for 1C (BSL) - 1C:Enterprise 8 and OneScript languages.
The newest version!
/*
* This file is a part of BSL Language Server.
*
* Copyright (c) 2018-2024
* Alexey Sosnoviy , Nikita Fedkin and contributors
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*
* BSL Language Server is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* BSL Language Server is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with BSL Language Server.
*/
package com.github._1c_syntax.bsl.languageserver.diagnostics;
import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticMetadata;
import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticSeverity;
import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticTag;
import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticType;
import com.github._1c_syntax.bsl.languageserver.utils.Trees;
import com.github._1c_syntax.bsl.parser.BSLParser;
import com.github._1c_syntax.utils.CaseInsensitivePattern;
import org.antlr.v4.runtime.tree.ParseTree;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@DiagnosticMetadata(
type = DiagnosticType.ERROR,
severity = DiagnosticSeverity.MAJOR,
minutesToFix = 15,
tags = {
DiagnosticTag.STANDARD
}
)
public class PairingBrokenTransactionDiagnostic extends AbstractVisitorDiagnostic {
private final Pattern beginTransaction = CaseInsensitivePattern.compile(
"НачатьТранзакцию|BeginTransaction"
);
private final Pattern beginEndTransaction = CaseInsensitivePattern.compile(
"ЗафиксироватьТранзакцию|CommitTransaction|НачатьТранзакцию|BeginTransaction"
);
private final Pattern beginCancelTransaction = CaseInsensitivePattern.compile(
"ОтменитьТранзакцию|RollbackTransaction|НачатьТранзакцию|BeginTransaction"
);
private final HashMap pairMethodsBeginEnd = new HashMap<>();
private final HashMap pairMethodsBeginCancel = new HashMap<>();
public PairingBrokenTransactionDiagnostic() {
pairMethodsBeginEnd.put("НАЧАТЬТРАНЗАКЦИЮ", "ЗафиксироватьТранзакцию");
pairMethodsBeginEnd.put("ЗАФИКСИРОВАТЬТРАНЗАКЦИЮ", "НачатьТранзакцию");
pairMethodsBeginEnd.put("BEGINTRANSACTION", "CommitTransaction");
pairMethodsBeginEnd.put("COMMITTRANSACTION", "BeginTransaction");
pairMethodsBeginCancel.put("НАЧАТЬТРАНЗАКЦИЮ", "ОтменитьТранзакцию");
pairMethodsBeginCancel.put("ОТМЕНИТЬТРАНЗАКЦИЮ", "НачатьТранзакцию");
pairMethodsBeginCancel.put("BEGINTRANSACTION", "RollbackTransaction");
pairMethodsBeginCancel.put("ROLLBACKTRANSACTION", "BeginTransaction");
}
@Override
public ParseTree visitSub(BSLParser.SubContext ctx) {
findAndAddDiagnostic(ctx, beginEndTransaction, pairMethodsBeginEnd);
findAndAddDiagnostic(ctx, beginCancelTransaction, pairMethodsBeginCancel);
return ctx;
}
private void findAndAddDiagnostic(ParseTree ctx, Pattern pattern, HashMap pairMethods) {
ArrayList allTranCalls = new ArrayList<>();
Trees.findAllRuleNodes(ctx, BSLParser.RULE_globalMethodCall)
.stream()
.filter(node -> pattern.matcher((
(BSLParser.GlobalMethodCallContext) node).methodName().getText()).matches())
.collect(Collectors.toCollection(() -> allTranCalls));
ArrayDeque beginCalls = new ArrayDeque<>();
for (ParseTree tranCall : allTranCalls) {
if (beginTransaction.matcher(((BSLParser.GlobalMethodCallContext) tranCall).methodName().getText()).matches()) {
beginCalls.add(tranCall);
} else if (!beginCalls.isEmpty()) {
beginCalls.pop();
} else {
addDiagnosticWithMessage(tranCall, pairMethods);
}
}
if (!beginCalls.isEmpty()) {
beginCalls.forEach(tranCall -> addDiagnosticWithMessage(tranCall, pairMethods));
}
}
private void addDiagnosticWithMessage(ParseTree tranCall, HashMap pairMethods) {
String methodName = ((BSLParser.GlobalMethodCallContext) tranCall).methodName().getText();
diagnosticStorage.addDiagnostic((BSLParser.GlobalMethodCallContext) tranCall,
info.getMessage(pairMethods.get(methodName.toUpperCase(Locale.ENGLISH)), methodName));
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy