Search in sources :

Example 1 with ScriptVariant

use of com.github._1c_syntax.mdclasses.mdo.support.ScriptVariant in project bsl-language-server by 1c-syntax.

the class GenerateStandardRegionsSupplier method getRegionsLanguage.

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;
}
Also used : Configuration(com.github._1c_syntax.mdclasses.Configuration) LanguageServerConfiguration(com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration) ScriptVariant(com.github._1c_syntax.mdclasses.mdo.support.ScriptVariant)

Example 2 with ScriptVariant

use of com.github._1c_syntax.mdclasses.mdo.support.ScriptVariant 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);
}
Also used : ScriptVariant(com.github._1c_syntax.mdclasses.mdo.support.ScriptVariant) Language(com.github._1c_syntax.bsl.languageserver.configuration.Language) Range(org.eclipse.lsp4j.Range) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) Regions(com.github._1c_syntax.bsl.languageserver.utils.Regions) FileType(com.github._1c_syntax.bsl.languageserver.context.FileType) ArrayList(java.util.ArrayList) RegionSymbol(com.github._1c_syntax.bsl.languageserver.context.symbol.RegionSymbol) Configuration(com.github._1c_syntax.mdclasses.Configuration) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) TextEdit(org.eclipse.lsp4j.TextEdit) LanguageServerConfiguration(com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration) ConfigurationSource(com.github._1c_syntax.mdclasses.common.ConfigurationSource) Map(java.util.Map) Position(org.eclipse.lsp4j.Position) CodeAction(org.eclipse.lsp4j.CodeAction) Set(java.util.Set) ModuleType(com.github._1c_syntax.mdclasses.mdo.support.ModuleType) Collectors(java.util.stream.Collectors) Component(org.springframework.stereotype.Component) List(java.util.List) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) CodeActionKind(org.eclipse.lsp4j.CodeActionKind) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) ModuleType(com.github._1c_syntax.mdclasses.mdo.support.ModuleType) FileType(com.github._1c_syntax.bsl.languageserver.context.FileType) TextEdit(org.eclipse.lsp4j.TextEdit) CodeAction(org.eclipse.lsp4j.CodeAction) ScriptVariant(com.github._1c_syntax.mdclasses.mdo.support.ScriptVariant) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

LanguageServerConfiguration (com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration)2 Configuration (com.github._1c_syntax.mdclasses.Configuration)2 ScriptVariant (com.github._1c_syntax.mdclasses.mdo.support.ScriptVariant)2 Language (com.github._1c_syntax.bsl.languageserver.configuration.Language)1 DocumentContext (com.github._1c_syntax.bsl.languageserver.context.DocumentContext)1 FileType (com.github._1c_syntax.bsl.languageserver.context.FileType)1 RegionSymbol (com.github._1c_syntax.bsl.languageserver.context.symbol.RegionSymbol)1 Regions (com.github._1c_syntax.bsl.languageserver.utils.Regions)1 ConfigurationSource (com.github._1c_syntax.mdclasses.common.ConfigurationSource)1 ModuleType (com.github._1c_syntax.mdclasses.mdo.support.ModuleType)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 CodeAction (org.eclipse.lsp4j.CodeAction)1 CodeActionKind (org.eclipse.lsp4j.CodeActionKind)1 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)1 Position (org.eclipse.lsp4j.Position)1