use of com.github._1c_syntax.bsl.languageserver.context.DocumentContext in project bsl-language-server by 1c-syntax.
the class BSLTextDocumentService method didChange.
@Override
public void didChange(DidChangeTextDocumentParams params) {
// TODO: Place to optimize -> migrate to #TextDocumentSyncKind.INCREMENTAL and build changed parse tree
DocumentContext documentContext = context.getDocument(params.getTextDocument().getUri());
if (documentContext == null) {
return;
}
documentContext.rebuild(params.getContentChanges().get(0).getText(), params.getTextDocument().getVersion());
if (configuration.getDiagnosticsOptions().getComputeTrigger() == ComputeTrigger.ONTYPE) {
validate(documentContext);
}
}
use of com.github._1c_syntax.bsl.languageserver.context.DocumentContext in project bsl-language-server by 1c-syntax.
the class SymbolTreeComputer method compute.
@Override
public SymbolTree compute() {
ModuleSymbol moduleSymbol = new ModuleSymbolComputer(documentContext).compute();
List<MethodSymbol> methods = new MethodSymbolComputer(documentContext).compute();
List<RegionSymbol> regions = new RegionSymbolComputer(documentContext).compute();
List<VariableSymbol> variables = new VariableSymbolComputer(documentContext).compute();
List<SourceDefinedSymbol> allOfThem = new ArrayList<>(methods);
allOfThem.addAll(regions);
allOfThem.addAll(variables);
allOfThem.sort(Comparator.comparingInt(symbol -> symbol.getRange().getStart().getLine()));
List<SourceDefinedSymbol> topLevelSymbols = new ArrayList<>();
SourceDefinedSymbol currentParent = moduleSymbol;
for (SourceDefinedSymbol symbol : allOfThem) {
currentParent = placeSymbol(topLevelSymbols, currentParent, symbol);
}
return new SymbolTree(moduleSymbol);
}
use of com.github._1c_syntax.bsl.languageserver.context.DocumentContext in project bsl-language-server by 1c-syntax.
the class FixAllCodeActionSupplier method getFixAllCodeAction.
private List<CodeAction> getFixAllCodeAction(Either<String, Integer> diagnosticCode, CodeActionParams params, DocumentContext documentContext) {
Optional<Class<? extends QuickFixProvider>> quickFixClass = quickFixSupplier.getQuickFixClass(diagnosticCode);
if (quickFixClass.isEmpty()) {
return Collections.emptyList();
}
List<Diagnostic> suitableDiagnostics = documentContext.getComputedDiagnostics().stream().filter(diagnostic -> diagnosticCode.equals(diagnostic.getCode())).collect(Collectors.toList());
// if incomingDiagnostics list has size = 1 - it will be displayed as regular quick fix
if (suitableDiagnostics.size() < ADD_FIX_ALL_DIAGNOSTICS_THRESHOLD) {
return Collections.emptyList();
}
CodeActionContext fixAllContext = new CodeActionContext();
fixAllContext.setDiagnostics(suitableDiagnostics);
fixAllContext.setOnly(Collections.singletonList(CodeActionKind.QuickFix));
CodeActionParams fixAllParams = new CodeActionParams();
fixAllParams.setTextDocument(params.getTextDocument());
fixAllParams.setRange(params.getRange());
fixAllParams.setContext(fixAllContext);
Class<? extends QuickFixProvider> quickFixProviderClass = quickFixClass.get();
QuickFixProvider quickFixInstance = quickFixSupplier.getQuickFixInstance(quickFixProviderClass);
return quickFixInstance.getQuickFixes(suitableDiagnostics, fixAllParams, documentContext);
}
use of com.github._1c_syntax.bsl.languageserver.context.DocumentContext in project bsl-language-server by 1c-syntax.
the class GenerateStandardRegionsSupplier method getCodeActions.
/**
* При необходимости создает {@code CodeAction} для генерации отсутствующих
* стандартных областей 1С
*
* @param params параметры вызова генерации {@code codeAction}
* @param documentContext представление программного модуля
* @return {@code List<CodeAction>} если модуль не содержит всех стандартных областей,
* пустой {@code List} если генерация областей не требуется
*/
@Override
public List<CodeAction> getCodeActions(CodeActionParams params, DocumentContext documentContext) {
ModuleType moduleType = documentContext.getModuleType();
FileType fileType = documentContext.getFileType();
ScriptVariant regionsLanguage = getRegionsLanguage(documentContext, fileType);
Set<String> neededStandardRegions;
if (fileType == FileType.BSL) {
neededStandardRegions = Regions.getStandardRegionsNamesByModuleType(moduleType, regionsLanguage);
} else {
neededStandardRegions = Regions.getOneScriptStandardRegions(regionsLanguage);
}
Set<String> documentRegionsNames = documentContext.getSymbolTree().getModuleLevelRegions().stream().map(RegionSymbol::getName).collect(Collectors.toSet());
neededStandardRegions.removeAll(documentRegionsNames);
if (neededStandardRegions.isEmpty()) {
return Collections.emptyList();
}
String regionFormat = regionsLanguage == ScriptVariant.ENGLISH ? "#Region %s%n%n#EndRegion%n" : "#Область %s%n%n#КонецОбласти%n";
String result = neededStandardRegions.stream().map(s -> String.format(regionFormat, s)).collect(Collectors.joining("\n"));
TextEdit textEdit = new TextEdit(calculateFixRange(params.getRange()), result);
WorkspaceEdit edit = new WorkspaceEdit();
Map<String, List<TextEdit>> changes = Map.of(documentContext.getUri().toString(), Collections.singletonList(textEdit));
edit.setChanges(changes);
CodeAction codeAction = new CodeAction("Generate missing regions");
codeAction.setDiagnostics(new ArrayList<>());
codeAction.setKind(CodeActionKind.Refactor);
codeAction.setEdit(edit);
return List.of(codeAction);
}
use of com.github._1c_syntax.bsl.languageserver.context.DocumentContext in project bsl-language-server by 1c-syntax.
the class CognitiveComplexityComputerTest method compute.
@Test
void compute() {
// given
DocumentContext documentContext = TestUtils.getDocumentContextFromFile("./src/test/resources/context/computer/CognitiveComplexityComputerTest.bsl");
// when
Computer<ComplexityData> cognitiveComplexityComputer = new CognitiveComplexityComputer(documentContext);
ComplexityData data = cognitiveComplexityComputer.compute();
final Map<MethodSymbol, Integer> methodsComplexity = data.getMethodsComplexity();
// then
MethodSymbol example1 = documentContext.getSymbolTree().getMethods().get(0);
Integer example1Complexity = methodsComplexity.get(example1);
assertThat(example1Complexity).isEqualTo(19);
MethodSymbol example2 = documentContext.getSymbolTree().getMethods().get(1);
Integer example2Complexity = methodsComplexity.get(example2);
assertThat(example2Complexity).isEqualTo(33);
MethodSymbol example3 = documentContext.getSymbolTree().getMethods().get(2);
Integer example3Complexity = methodsComplexity.get(example3);
assertThat(example3Complexity).isEqualTo(20);
MethodSymbol example4 = documentContext.getSymbolTree().getMethods().get(3);
Integer example4Complexity = methodsComplexity.get(example4);
assertThat(example4Complexity).isEqualTo(15);
}
Aggregations