Skip to content

Commit

Permalink
Merge pull request #1359 from EightM/fix/generateRegionsAction
Browse files Browse the repository at this point in the history
Fix/generate regions action
  • Loading branch information
nixel2007 authored Sep 14, 2020
2 parents 3ef973e + 94f3ea9 commit 48010a0
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -48,6 +55,12 @@
@Component
public class GenerateStandardRegionsSupplier implements CodeActionSupplier {

private final LanguageServerConfiguration languageServerConfiguration;

public GenerateStandardRegionsSupplier(LanguageServerConfiguration languageServerConfiguration) {
this.languageServerConfiguration = languageServerConfiguration;
}

/**
* При необходимости создает {@code CodeAction} для генерации отсутствующих
* стандартных областей 1С
Expand All @@ -62,13 +75,14 @@ public List<CodeAction> getCodeActions(CodeActionParams params, DocumentContext

ModuleType moduleType = documentContext.getModuleType();
FileType fileType = documentContext.getFileType();
ScriptVariant configurationLanguage = documentContext.getServerContext().getConfiguration().getScriptVariant();

ScriptVariant regionsLanguage = getRegionsLanguage(documentContext, fileType);
Set<String> 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<String> documentRegionsNames = documentContext.getSymbolTree().getModuleLevelRegions().stream()
Expand All @@ -81,12 +95,12 @@ public List<CodeAction> 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<String, List<TextEdit>> changes = Map.of(documentContext.getUri().toString(),
Expand All @@ -99,4 +113,48 @@ public List<CodeAction> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -142,15 +143,17 @@ public Set<Pattern> getStandardRegionsPatternsByModuleType(ModuleType moduleType
*/
public static Set<String> getOneScriptStandardRegions(ScriptVariant configurationLanguage) {

Set<String> regionsName = new HashSet<>();
Set<String> 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);
Expand All @@ -170,7 +173,7 @@ public Set<String> getStandardRegionsNamesByModuleType(ModuleType moduleType, Sc
}

private static Set<String> getStandardRegionNames(ModuleType moduleType, ScriptVariant language) {
Set<String> regionsName = new HashSet<>();
Set<String> regionsName = new LinkedHashSet<>();
switch (moduleType) {
case FormModule:
addFormModuleRegionsNames(regionsName, language);
Expand Down

0 comments on commit 48010a0

Please sign in to comment.