diff --git a/src/main/java/com/github/_1c_syntax/bsl/languageserver/codeactions/GenerateStandardRegionsSupplier.java b/src/main/java/com/github/_1c_syntax/bsl/languageserver/codeactions/GenerateStandardRegionsSupplier.java index a6a0ffefe48..55983bbc3b2 100644 --- a/src/main/java/com/github/_1c_syntax/bsl/languageserver/codeactions/GenerateStandardRegionsSupplier.java +++ b/src/main/java/com/github/_1c_syntax/bsl/languageserver/codeactions/GenerateStandardRegionsSupplier.java @@ -21,17 +21,24 @@ */ package com.github._1c_syntax.bsl.languageserver.codeactions; +import com.github._1c_syntax.bsl.languageserver.configuration.Language; +import com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration; import com.github._1c_syntax.bsl.languageserver.context.DocumentContext; import com.github._1c_syntax.bsl.languageserver.context.FileType; import com.github._1c_syntax.bsl.languageserver.context.symbol.RegionSymbol; import com.github._1c_syntax.bsl.languageserver.utils.Regions; +import com.github._1c_syntax.mdclasses.metadata.Configuration; +import com.github._1c_syntax.mdclasses.metadata.additional.ConfigurationSource; import com.github._1c_syntax.mdclasses.metadata.additional.ModuleType; import com.github._1c_syntax.mdclasses.metadata.additional.ScriptVariant; import org.eclipse.lsp4j.CodeAction; import org.eclipse.lsp4j.CodeActionKind; import org.eclipse.lsp4j.CodeActionParams; +import org.eclipse.lsp4j.Position; +import org.eclipse.lsp4j.Range; import org.eclipse.lsp4j.TextEdit; import org.eclipse.lsp4j.WorkspaceEdit; +import org.jetbrains.annotations.NotNull; import org.springframework.stereotype.Component; import java.util.ArrayList; @@ -48,6 +55,12 @@ @Component public class GenerateStandardRegionsSupplier implements CodeActionSupplier { + private final LanguageServerConfiguration languageServerConfiguration; + + public GenerateStandardRegionsSupplier(LanguageServerConfiguration languageServerConfiguration) { + this.languageServerConfiguration = languageServerConfiguration; + } + /** * При необходимости создает {@code CodeAction} для генерации отсутствующих * стандартных областей 1С @@ -62,13 +75,14 @@ public List getCodeActions(CodeActionParams params, DocumentContext ModuleType moduleType = documentContext.getModuleType(); FileType fileType = documentContext.getFileType(); - ScriptVariant configurationLanguage = documentContext.getServerContext().getConfiguration().getScriptVariant(); + + ScriptVariant regionsLanguage = getRegionsLanguage(documentContext, fileType); Set neededStandardRegions; if (fileType == FileType.BSL) { - neededStandardRegions = Regions.getStandardRegionsNamesByModuleType(moduleType, configurationLanguage); + neededStandardRegions = Regions.getStandardRegionsNamesByModuleType(moduleType, regionsLanguage); } else { - neededStandardRegions = Regions.getOneScriptStandardRegions(configurationLanguage); + neededStandardRegions = Regions.getOneScriptStandardRegions(regionsLanguage); } Set documentRegionsNames = documentContext.getSymbolTree().getModuleLevelRegions().stream() @@ -81,12 +95,12 @@ public List getCodeActions(CodeActionParams params, DocumentContext } String regionFormat = - configurationLanguage == ScriptVariant.ENGLISH ? "#Region %s%n%n#EndRegion%n" : "#Область %s%n%n#КонецОбласти%n"; + 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(params.getRange(), result); + TextEdit textEdit = new TextEdit(calculateFixRange(params.getRange()), result); WorkspaceEdit edit = new WorkspaceEdit(); Map> changes = Map.of(documentContext.getUri().toString(), @@ -99,4 +113,48 @@ public List getCodeActions(CodeActionParams params, DocumentContext codeAction.setEdit(edit); return List.of(codeAction); } + + private ScriptVariant getRegionsLanguage(DocumentContext documentContext, FileType fileType) { + + ScriptVariant regionsLanguage; + Configuration configuration = documentContext.getServerContext().getConfiguration(); + if (configuration.getConfigurationSource() == ConfigurationSource.EMPTY || fileType == FileType.OS) { + regionsLanguage = getScriptVariantFromConfigLanguage(); + } else { + regionsLanguage = documentContext.getServerContext().getConfiguration().getScriptVariant(); + } + return regionsLanguage; + } + + @NotNull + private ScriptVariant getScriptVariantFromConfigLanguage() { + ScriptVariant regionsLanguage; + if (languageServerConfiguration.getLanguage() == Language.EN) { + regionsLanguage = ScriptVariant.ENGLISH; + } else { + regionsLanguage = ScriptVariant.RUSSIAN; + } + return regionsLanguage; + } + + private Range calculateFixRange(Range range) { + + Position start = range.getStart(); + if (start == null) { + start = new Position(0, 0); + } else { + start.setCharacter(0); + } + + Position end = range.getEnd(); + if (end == null) { + end = new Position(0, 0); + } else { + end.setCharacter(0); + } + + range.setStart(start); + range.setEnd(end); + return range; + } } diff --git a/src/main/java/com/github/_1c_syntax/bsl/languageserver/utils/Regions.java b/src/main/java/com/github/_1c_syntax/bsl/languageserver/utils/Regions.java index 9393c92e219..630da54c302 100644 --- a/src/main/java/com/github/_1c_syntax/bsl/languageserver/utils/Regions.java +++ b/src/main/java/com/github/_1c_syntax/bsl/languageserver/utils/Regions.java @@ -28,6 +28,7 @@ import java.util.Collections; import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.Set; import java.util.regex.Pattern; @@ -142,15 +143,17 @@ public Set getStandardRegionsPatternsByModuleType(ModuleType moduleType */ public static Set getOneScriptStandardRegions(ScriptVariant configurationLanguage) { - Set regionsName = new HashSet<>(); + Set regionsName = new LinkedHashSet<>(); if (configurationLanguage == ScriptVariant.ENGLISH) { + regionsName.add(Keywords.VARIABLES_REGION_EN); regionsName.add(Keywords.PUBLIC_REGION_EN); regionsName.add(Keywords.INTERNAL_REGION_EN); regionsName.add(Keywords.PRIVATE_REGION_EN); return regionsName; } + regionsName.add(Keywords.VARIABLES_REGION_RU); regionsName.add(Keywords.PUBLIC_REGION_RU); regionsName.add(Keywords.INTERNAL_REGION_RU); regionsName.add(Keywords.PRIVATE_REGION_RU); @@ -170,7 +173,7 @@ public Set getStandardRegionsNamesByModuleType(ModuleType moduleType, Sc } private static Set getStandardRegionNames(ModuleType moduleType, ScriptVariant language) { - Set regionsName = new HashSet<>(); + Set regionsName = new LinkedHashSet<>(); switch (moduleType) { case FormModule: addFormModuleRegionsNames(regionsName, language);