From adb8599d2168f5807baa07b6a78ac46f734893d4 Mon Sep 17 00:00:00 2001 From: Dmitriy Marmyshev Date: Thu, 16 Dec 2021 12:11:09 +0200 Subject: [PATCH] =?UTF-8?q?#817=20=D0=90=D0=B2=D1=82=D0=BE=D0=BC=D0=B0?= =?UTF-8?q?=D1=82=D0=B8=D1=87=D0=B5=D1=81=D0=BA=D0=BE=D0=B5=20=D0=B2=D0=BA?= =?UTF-8?q?=D0=BB=D1=8E=D1=87=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=81=D1=82=D1=80?= =?UTF-8?q?=D0=BE=D0=B3=D0=BE=D0=B9=20=D1=82=D0=B8=D0=BF=D0=B8=D0=B7=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D0=B8=20=D0=B2=20=D0=BD=D0=BE=D0=B2=D0=BE=D0=BC=20?= =?UTF-8?q?=D0=BC=D0=BE=D0=B4=D1=83=D0=BB=D0=B5=20(#880)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + bundles/com.e1c.v8codestyle.bsl.ui/plugin.xml | 81 ++++++++ .../internal/bsl/ui/preferences/Messages.java | 1 + .../ModuleStructurePreferencePage.java | 5 +- .../bsl/ui/preferences/messages.properties | 3 + .../bsl/ui/preferences/messages_ru.properties | 3 + .../internal/bsl/ui/properties/Messages.java | 1 + .../ModuleStructurePropertyPage.java | 27 ++- .../bsl/ui/properties/messages.properties | 3 + .../bsl/ui/properties/messages_ru.properties | 3 + ...ctModuleNewWizardRelatedModelsFactory.java | 141 +++++++++++++ ...ictTypesNewWizardRelatedModelsFactory.java | 195 ++++++++++++++++++ ...tructureNewWizardRelatedModelsFactory.java | 71 +------ .../bsl/strict/StrictTypeUtil.java | 33 +++ ...tructureProviderPreferenceInitializer.java | 4 + 15 files changed, 496 insertions(+), 76 deletions(-) create mode 100644 bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/wizard/AbstractModuleNewWizardRelatedModelsFactory.java create mode 100644 bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/wizard/ModuleStrictTypesNewWizardRelatedModelsFactory.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 5efec0e0..fb64af0b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ - Выбор подходящего тип общего модуля из списка при создании нового - Панель "Bsl Документирующий комментарий" - Автоматическое создание структуры модуля +- Автоматическое включение строгой типизации модулей ### Новые проверки diff --git a/bundles/com.e1c.v8codestyle.bsl.ui/plugin.xml b/bundles/com.e1c.v8codestyle.bsl.ui/plugin.xml index 794234b3..6a82f156 100644 --- a/bundles/com.e1c.v8codestyle.bsl.ui/plugin.xml +++ b/bundles/com.e1c.v8codestyle.bsl.ui/plugin.xml @@ -127,6 +127,87 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/preferences/Messages.java b/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/preferences/Messages.java index 7462c51b..33c14421 100644 --- a/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/preferences/Messages.java +++ b/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/preferences/Messages.java @@ -23,6 +23,7 @@ final class Messages { private static final String BUNDLE_NAME = "com.e1c.v8codestyle.internal.bsl.ui.preferences.messages"; //$NON-NLS-1$ public static String ModuleStructurePreferencePage_Automatically_create_module_structure; + public static String ModuleStructurePreferencePage_Automatically_create_strict_types_module; static { // initialize resource bundle diff --git a/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/preferences/ModuleStructurePreferencePage.java b/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/preferences/ModuleStructurePreferencePage.java index 81c2beaa..9846576a 100644 --- a/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/preferences/ModuleStructurePreferencePage.java +++ b/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/preferences/ModuleStructurePreferencePage.java @@ -21,6 +21,7 @@ import org.eclipse.ui.IWorkbenchPreferencePage; import org.eclipse.ui.preferences.ScopedPreferenceStore; import com.e1c.v8codestyle.bsl.IModuleStructureProvider; +import com.e1c.v8codestyle.bsl.strict.StrictTypeUtil; /** * The preference page of module structure settings. @@ -47,9 +48,11 @@ public class ModuleStructurePreferencePage @Override public void createFieldEditors() { + addField(new BooleanFieldEditor(StrictTypeUtil.PREF_KEY_CREATE_STRICT_TYPES, + Messages.ModuleStructurePreferencePage_Automatically_create_strict_types_module, getFieldEditorParent())); + addField(new BooleanFieldEditor(IModuleStructureProvider.PREF_KEY_CREATE_STRUCTURE, Messages.ModuleStructurePreferencePage_Automatically_create_module_structure, getFieldEditorParent())); - } @Override diff --git a/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/preferences/messages.properties b/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/preferences/messages.properties index 1794061a..abf683cc 100644 --- a/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/preferences/messages.properties +++ b/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/preferences/messages.properties @@ -1,3 +1,4 @@ +#Generated by ResourceBundle Editor (http://essiembre.github.io/eclipse-rbe/) ############################################################################### # Copyright (C) 2021, 1C-Soft LLC and others. # @@ -12,3 +13,5 @@ ############################################################################### ModuleStructurePreferencePage_Automatically_create_module_structure = &Automatically create module structure + +ModuleStructurePreferencePage_Automatically_create_strict_types_module = Automatically add module strict-types annotation diff --git a/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/preferences/messages_ru.properties b/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/preferences/messages_ru.properties index ef2c6e54..70246f20 100644 --- a/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/preferences/messages_ru.properties +++ b/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/preferences/messages_ru.properties @@ -1,3 +1,4 @@ +#Generated by ResourceBundle Editor (http://essiembre.github.io/eclipse-rbe/) ############################################################################### # Copyright (C) 2021, 1C-Soft LLC and others. # @@ -13,3 +14,5 @@ #Generated by ResourceBundle Editor (http://essiembre.github.io/eclipse-rbe/) ModuleStructurePreferencePage_Automatically_create_module_structure = &Автоматически создавать структуру модуля + +ModuleStructurePreferencePage_Automatically_create_strict_types_module = Автоматически добавлять аннотацию строгой типизации модуля diff --git a/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/properties/Messages.java b/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/properties/Messages.java index 04d1af61..d489f7fa 100644 --- a/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/properties/Messages.java +++ b/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/properties/Messages.java @@ -23,6 +23,7 @@ final class Messages { private static final String BUNDLE_NAME = "com.e1c.v8codestyle.internal.bsl.ui.properties.messages"; //$NON-NLS-1$ public static String ModuleStructurePropertyPage_Automatically_create_module_structure; + public static String ModuleStructurePropertyPage_Automatically_create_strict_types_module; public static String ModuleStructurePropertyPage_Open_template; public static String ModuleStructurePropertyPage_Open_template_tooltip; public static String ModuleStructurePropertyPage_Save_custom_template_to_project_settings; diff --git a/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/properties/ModuleStructurePropertyPage.java b/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/properties/ModuleStructurePropertyPage.java index 23457942..f1312c83 100644 --- a/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/properties/ModuleStructurePropertyPage.java +++ b/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/properties/ModuleStructurePropertyPage.java @@ -30,7 +30,6 @@ import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.preferences.IEclipsePreferences; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.preference.PreferenceDialog; -import org.eclipse.jface.preference.PreferencePage; import org.eclipse.jface.viewers.ArrayContentProvider; import org.eclipse.jface.viewers.CheckboxTableViewer; import org.eclipse.jface.viewers.IStructuredSelection; @@ -54,6 +53,7 @@ import com._1c.g5.v8.dt.core.platform.IV8ProjectManager; import com._1c.g5.v8.dt.metadata.mdclass.ScriptVariant; import com._1c.g5.v8.dt.ui.util.OpenHelper; import com.e1c.v8codestyle.bsl.IModuleStructureProvider; +import com.e1c.v8codestyle.bsl.strict.StrictTypeUtil; import com.e1c.v8codestyle.internal.bsl.ui.UiPlugin; import com.google.inject.Inject; @@ -74,12 +74,16 @@ public class ModuleStructurePropertyPage private Button createStructureButton; + private Button createStrictTypesButton; + private CheckboxTableViewer checkBoxViewer; private final Set existTemplates = new HashSet<>(); private boolean currentCreateStructure; + private boolean currentCreateStrictTypes; + private final OpenHelper openHelper = new OpenHelper(); /** @@ -145,8 +149,10 @@ public class ModuleStructurePropertyPage { ProjectScope scope = new ProjectScope(getProject()); IEclipsePreferences node = scope.getNode(IModuleStructureProvider.PREF_QUALIFIER); - boolean value = createStructureButton.getSelection(); - node.putBoolean(IModuleStructureProvider.PREF_KEY_CREATE_STRUCTURE, value); + boolean value1 = createStructureButton.getSelection(); + boolean value2 = createStrictTypesButton.getSelection(); + node.putBoolean(IModuleStructureProvider.PREF_KEY_CREATE_STRUCTURE, value1); + node.putBoolean(StrictTypeUtil.PREF_KEY_CREATE_STRICT_TYPES, value2); try { node.flush(); @@ -156,7 +162,8 @@ public class ModuleStructurePropertyPage UiPlugin.logError(e); return false; } - currentCreateStructure = value; + currentCreateStructure = value1; + currentCreateStrictTypes = value2; for (ModuleType type : ModuleType.VALUES) { if (checkBoxViewer.getChecked(type) && !existTemplates.contains(type)) @@ -232,6 +239,14 @@ public class ModuleStructurePropertyPage { Composite composite = createDefaultComposite(parent); + Label createStrictTypesLabel = new Label(composite, SWT.NONE); + createStrictTypesLabel.setText(Messages.ModuleStructurePropertyPage_Automatically_create_strict_types_module); + + currentCreateStrictTypes = StrictTypeUtil.canAddModuleStrictTypesAnnotation(getProject()); + + createStrictTypesButton = new Button(composite, SWT.CHECK); + createStrictTypesButton.setSelection(currentCreateStrictTypes); + Label createStructureLabel = new Label(composite, SWT.NONE); createStructureLabel.setText(Messages.ModuleStructurePropertyPage_Automatically_create_module_structure); @@ -239,7 +254,6 @@ public class ModuleStructurePropertyPage createStructureButton = new Button(composite, SWT.CHECK); createStructureButton.setSelection(currentCreateStructure); - } private void addSeparator(Composite parent) @@ -330,7 +344,8 @@ public class ModuleStructurePropertyPage private boolean isDirty() { - if (createStructureButton.getSelection() != currentCreateStructure) + if (createStructureButton.getSelection() != currentCreateStructure + || createStrictTypesButton.getSelection() != currentCreateStrictTypes) { return true; } diff --git a/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/properties/messages.properties b/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/properties/messages.properties index 6219ee82..dc6e996e 100644 --- a/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/properties/messages.properties +++ b/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/properties/messages.properties @@ -1,3 +1,4 @@ +#Generated by ResourceBundle Editor (http://essiembre.github.io/eclipse-rbe/) ############################################################################### # Copyright (C) 2021, 1C-Soft LLC and others. # @@ -13,6 +14,8 @@ ModuleStructurePropertyPage_Automatically_create_module_structure = Automatically create module structure +ModuleStructurePropertyPage_Automatically_create_strict_types_module = Automatically add module strict-types annotation + ModuleStructurePropertyPage_Open_template = Open template ModuleStructurePropertyPage_Open_template_tooltip = Open existing custom project template with saving settings. diff --git a/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/properties/messages_ru.properties b/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/properties/messages_ru.properties index 96b79ed8..d10949f4 100644 --- a/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/properties/messages_ru.properties +++ b/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/properties/messages_ru.properties @@ -1,3 +1,4 @@ +#Generated by ResourceBundle Editor (http://essiembre.github.io/eclipse-rbe/) ############################################################################### # Copyright (C) 2021, 1C-Soft LLC and others. # @@ -14,6 +15,8 @@ ModuleStructurePropertyPage_Automatically_create_module_structure = Автоматически создавать структуру модуля +ModuleStructurePropertyPage_Automatically_create_strict_types_module = Автоматически добавлять аннотацию строгой типизации модуля + ModuleStructurePropertyPage_Open_template = Открыть шаблон ModuleStructurePropertyPage_Open_template_tooltip = Открыть существующий шаблон проекта с сохранением настроек. diff --git a/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/wizard/AbstractModuleNewWizardRelatedModelsFactory.java b/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/wizard/AbstractModuleNewWizardRelatedModelsFactory.java new file mode 100644 index 00000000..be321b09 --- /dev/null +++ b/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/wizard/AbstractModuleNewWizardRelatedModelsFactory.java @@ -0,0 +1,141 @@ +/******************************************************************************* + * Copyright (C) 2021, 1C-Soft LLC and others. + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * 1C-Soft LLC - initial API and implementation + *******************************************************************************/ +package com.e1c.v8codestyle.internal.bsl.ui.wizard; + +import static com._1c.g5.v8.dt.bsl.model.BslPackage.Literals.MODULE; +import static com._1c.g5.v8.dt.metadata.mdclass.MdClassPackage.Literals.ABSTRACT_FORM__MODULE; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.Path; +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.InternalEObject; +import org.eclipse.emf.ecore.util.EcoreUtil; +import org.eclipse.xtext.naming.QualifiedName; + +import com._1c.g5.v8.bm.core.IBmObject; +import com._1c.g5.v8.dt.bsl.model.Module; +import com._1c.g5.v8.dt.common.FileUtil; +import com._1c.g5.v8.dt.common.StringUtils; +import com._1c.g5.v8.dt.core.filesystem.IQualifiedNameFilePathConverter; +import com._1c.g5.v8.dt.metadata.mdclass.AbstractForm; +import com._1c.g5.v8.dt.ui.wizards.IDtNewWizardRelatedModelsFactory; + +/** + * Abstract factory for creating module in new-wizard. + * + * @author Dmitriy Marmyshev + */ +public abstract class AbstractModuleNewWizardRelatedModelsFactory + implements IDtNewWizardRelatedModelsFactory +{ + + private static final String LINE_SEPARATOR_WIN = "\r\n"; //$NON-NLS-1$ + + private static final String LINE_SEPARATOR_LINUX = "\n"; //$NON-NLS-1$ + + protected final IQualifiedNameFilePathConverter qualifiedNameFilePathConverter; + + /** + * Instantiates a new abstract module new wizard related models factory. + * + * @param qualifiedNameFilePathConverter the qualified name file path converter, cannot be {@code null}. + */ + protected AbstractModuleNewWizardRelatedModelsFactory( + IQualifiedNameFilePathConverter qualifiedNameFilePathConverter) + { + this.qualifiedNameFilePathConverter = qualifiedNameFilePathConverter; + } + + /** + * Resolve line separator from text content. + * + * @param text the text, cannot be {@code null}. + * @return the string of line separator. + */ + protected String resolveLineSeparator(String text) + { + return text.contains(LINE_SEPARATOR_WIN) ? LINE_SEPARATOR_WIN : LINE_SEPARATOR_LINUX; + } + + /** + * Creates a new BSL proxy module object. + * + * @param bslFile the BSL file, cannot be {@code null}. + * @return the object of module, never returns {@code null}. + */ + protected EObject createBslProxyModule(IFile bslFile) + { + EObject module = EcoreUtil.create(MODULE); + URI uri = URI.createPlatformResourceURI(bslFile.getFullPath().toString(), true).appendFragment("/0"); //$NON-NLS-1$ + ((InternalEObject)module).eSetProxyURI(uri); + return module; + } + + /** + * Gets the module file. + * + * @param module the model, cannot be {@code null}. + * @return the module file or {@code null} if module URI is not platform URI. + */ + protected IFile getModuleFile(Module module) + { + URI uri = EcoreUtil.getURI(module); + + if (uri != null && uri.isPlatform()) + { + IPath path = new Path(uri.trimFragment().toPlatformString(true)); + return ResourcesPlugin.getWorkspace().getRoot().getFile(path); + } + return null; + } + + /** + * Gets the module file of form. + * + * @param form the form, cannot be {@code null}. + * @param project the project, cannot be {@code null}. + * @return the module file + */ + protected IFile getModuleFile(AbstractForm form, IProject project) + { + String fqn = ((IBmObject)form).bmGetFqn(); + + QualifiedName name = QualifiedName.create(fqn).append(StringUtils.capitalize(ABSTRACT_FORM__MODULE.getName())); + + IPath path = qualifiedNameFilePathConverter.getFilePath(name, MODULE); + + return project.getFile(path); + } + + /** + * Creates a parent directories of file without exceptions. + * + * @param bslFile the BSL file, cannot be {@code null}. + */ + protected void createParentFolders(IFile bslFile) + { + try + { + FileUtil.createParentFolders(bslFile); + } + catch (Exception e) + { + // skip if cannot create parent folders in other threads + } + } + +} diff --git a/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/wizard/ModuleStrictTypesNewWizardRelatedModelsFactory.java b/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/wizard/ModuleStrictTypesNewWizardRelatedModelsFactory.java new file mode 100644 index 00000000..ac24ab27 --- /dev/null +++ b/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/wizard/ModuleStrictTypesNewWizardRelatedModelsFactory.java @@ -0,0 +1,195 @@ +/******************************************************************************* + * Copyright (C) 2021, 1C-Soft LLC and others. + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * 1C-Soft LLC - initial API and implementation + *******************************************************************************/ +package com.e1c.v8codestyle.internal.bsl.ui.wizard; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.nio.charset.StandardCharsets; +import java.util.Iterator; +import java.util.Set; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.emf.ecore.EObject; + +import com._1c.g5.v8.dt.bsl.documentation.comment.IBslCommentToken; +import com._1c.g5.v8.dt.bsl.model.Module; +import com._1c.g5.v8.dt.common.PreferenceUtils; +import com._1c.g5.v8.dt.common.StringUtils; +import com._1c.g5.v8.dt.core.filesystem.IQualifiedNameFilePathConverter; +import com._1c.g5.v8.dt.metadata.mdclass.AbstractForm; +import com._1c.g5.v8.dt.ui.wizards.IDtNewWizardContext; +import com.e1c.v8codestyle.bsl.strict.StrictTypeUtil; +import com.e1c.v8codestyle.internal.bsl.ui.UiPlugin; +import com.google.common.io.CharStreams; +import com.google.inject.Inject; + +/** + * A factory for creating strict-types module when creating new object in wizard. + * + * @author Dmitriy Marmyshev + */ +public class ModuleStrictTypesNewWizardRelatedModelsFactory + extends AbstractModuleNewWizardRelatedModelsFactory +{ + + @Inject + public ModuleStrictTypesNewWizardRelatedModelsFactory( + IQualifiedNameFilePathConverter qualifiedNameFilePathConverter) + { + super(qualifiedNameFilePathConverter); + } + + @Override + public void createModels(IDtNewWizardContext context, Set createdModels) + { + IProject project = context.getV8project().getProject(); + if (!StrictTypeUtil.canAddModuleStrictTypesAnnotation(project)) + { + return; + } + + AbstractForm formToAddModule = null; + for (EObject model : context.getRelatedModels()) + { + if (model instanceof AbstractForm) + { + formToAddModule = (AbstractForm)model; + } + else if (model instanceof Module) + { + formToAddModule = null; + Module module = (Module)model; + IFile bslFile = getModuleFile(module); + if (bslFile != null) + { + createOrUpdateModule(bslFile, context); + } + } + } + + if (formToAddModule != null) + { + IFile bslFile = getModuleFile(formToAddModule, project); + if (bslFile != null) + { + createOrUpdateModule(bslFile, context); + + EObject module = createBslProxyModule(bslFile); + createdModels.add(module); + } + } + + } + + private void createOrUpdateModule(IFile bslFile, IDtNewWizardContext context) + { + try + { + if (bslFile.exists() && StrictTypeUtil.hasStrictTypeAnnotation(bslFile)) + { + return; + } + } + catch (CoreException | IOException e) + { + UiPlugin.logError(e); + } + + String currentCode = StringUtils.EMPTY; + if (bslFile.exists()) + { + try (InputStream in = bslFile.getContents(); + Reader reader = new InputStreamReader(in, StandardCharsets.UTF_8);) + { + currentCode = CharStreams.toString(reader); + } + catch (IOException | CoreException e) + { + IStatus status = UiPlugin.createErrorStatus("Can't read bsl file with name: " + bslFile.getName(), e); //$NON-NLS-1$ + UiPlugin.log(status); + } + } + + IProject project = context.getV8project().getProject(); + String preferedLineSeparator = PreferenceUtils.getLineSeparator(project); + StringBuilder sb = new StringBuilder(); + + int insertOffset = getInsertOffset(currentCode); + if (insertOffset > 0) + { + sb.append(currentCode.substring(0, insertOffset)); + sb.append(preferedLineSeparator); + } + + sb.append(IBslCommentToken.LINE_STARTER); + sb.append(" "); //$NON-NLS-1$ + sb.append(StrictTypeUtil.STRICT_TYPE_ANNOTATION); + sb.append(preferedLineSeparator); + sb.append(preferedLineSeparator); + sb.append(currentCode.substring(insertOffset)); + + try (InputStream in = new ByteArrayInputStream(sb.toString().getBytes(StandardCharsets.UTF_8));) + { + if (bslFile.exists()) + { + bslFile.setContents(in, true, true, new NullProgressMonitor()); + } + else + { + createParentFolders(bslFile); + bslFile.create(in, true, new NullProgressMonitor()); + } + } + catch (CoreException | IOException e) + { + IStatus status = UiPlugin.createErrorStatus("Can't create bsl file with name: " + bslFile.getName(), e); //$NON-NLS-1$ + UiPlugin.log(status); + } + } + + private int getInsertOffset(String currentCode) + { + int separator = resolveLineSeparator(currentCode).length(); + + int offset = 0; + for (Iterator iterator = currentCode.lines().iterator(); iterator.hasNext();) + { + if (offset > 0) + { + offset = offset + separator; + } + + String line = iterator.next(); + if (StringUtils.isBlank(line)) + { + return offset; + } + else if (!line.stripLeading().startsWith(IBslCommentToken.LINE_STARTER)) + { + return 0; + } + + offset = offset + line.length(); + + } + return 0; + } + +} diff --git a/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/wizard/ModuleStructureNewWizardRelatedModelsFactory.java b/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/wizard/ModuleStructureNewWizardRelatedModelsFactory.java index cedb3425..3e73d30c 100644 --- a/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/wizard/ModuleStructureNewWizardRelatedModelsFactory.java +++ b/bundles/com.e1c.v8codestyle.bsl.ui/src/com/e1c/v8codestyle/internal/bsl/ui/wizard/ModuleStructureNewWizardRelatedModelsFactory.java @@ -12,9 +12,6 @@ *******************************************************************************/ package com.e1c.v8codestyle.internal.bsl.ui.wizard; -import static com._1c.g5.v8.dt.bsl.model.BslPackage.Literals.MODULE; -import static com._1c.g5.v8.dt.metadata.mdclass.MdClassPackage.Literals.ABSTRACT_FORM__MODULE; - import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; @@ -26,30 +23,20 @@ import java.util.function.Supplier; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; -import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.NullProgressMonitor; -import org.eclipse.core.runtime.Path; -import org.eclipse.emf.common.util.URI; import org.eclipse.emf.ecore.EObject; -import org.eclipse.emf.ecore.InternalEObject; -import org.eclipse.emf.ecore.util.EcoreUtil; -import org.eclipse.xtext.naming.QualifiedName; -import com._1c.g5.v8.bm.core.IBmObject; import com._1c.g5.v8.dt.bsl.model.Module; import com._1c.g5.v8.dt.bsl.model.ModuleType; import com._1c.g5.v8.dt.bsl.util.BslUtil; -import com._1c.g5.v8.dt.common.FileUtil; import com._1c.g5.v8.dt.common.PreferenceUtils; import com._1c.g5.v8.dt.common.StringUtils; import com._1c.g5.v8.dt.core.filesystem.IQualifiedNameFilePathConverter; import com._1c.g5.v8.dt.metadata.mdclass.AbstractForm; import com._1c.g5.v8.dt.metadata.mdclass.ScriptVariant; import com._1c.g5.v8.dt.ui.wizards.IDtNewWizardContext; -import com._1c.g5.v8.dt.ui.wizards.IDtNewWizardRelatedModelsFactory; import com.e1c.v8codestyle.bsl.IModuleStructureProvider; import com.e1c.v8codestyle.internal.bsl.ui.UiPlugin; import com.google.common.io.CharStreams; @@ -61,17 +48,11 @@ import com.google.inject.Inject; * @author Dmitriy Marmyshev */ public class ModuleStructureNewWizardRelatedModelsFactory - implements IDtNewWizardRelatedModelsFactory + extends AbstractModuleNewWizardRelatedModelsFactory { private static final String CURRENT_CODE = "//%CURRENT_CODE%"; //$NON-NLS-1$ - private static final String LINE_SEPARATOR_WIN = "\r\n"; //$NON-NLS-1$ - - private static final String LINE_SEPARATOR_LINUX = "\n"; //$NON-NLS-1$ - - private final IQualifiedNameFilePathConverter qualifiedNameFilePathConverter; - private final IModuleStructureProvider moduleStructureProvider; /** @@ -84,7 +65,7 @@ public class ModuleStructureNewWizardRelatedModelsFactory public ModuleStructureNewWizardRelatedModelsFactory(IQualifiedNameFilePathConverter qualifiedNameFilePathConverter, IModuleStructureProvider moduleStructureProvider) { - this.qualifiedNameFilePathConverter = qualifiedNameFilePathConverter; + super(qualifiedNameFilePathConverter); this.moduleStructureProvider = moduleStructureProvider; } @@ -198,52 +179,4 @@ public class ModuleStructureNewWizardRelatedModelsFactory } } - private String resolveLineSeparator(String text) - { - return text.contains(LINE_SEPARATOR_WIN) ? LINE_SEPARATOR_WIN : LINE_SEPARATOR_LINUX; - } - - private EObject createBslProxyModule(IFile bslFile) - { - EObject module = EcoreUtil.create(MODULE); - URI uri = URI.createPlatformResourceURI(bslFile.getFullPath().toString(), true).appendFragment("/0"); //$NON-NLS-1$ - ((InternalEObject)module).eSetProxyURI(uri); - return module; - } - - private IFile getModuleFile(Module model) - { - URI uri = EcoreUtil.getURI(model); - - if (uri != null && uri.isPlatform()) - { - IPath path = new Path(uri.trimFragment().toPlatformString(true)); - return ResourcesPlugin.getWorkspace().getRoot().getFile(path); - } - return null; - } - - private IFile getModuleFile(AbstractForm model, IProject project) - { - String fqn = ((IBmObject)model).bmGetFqn(); - - QualifiedName name = QualifiedName.create(fqn).append(StringUtils.capitalize(ABSTRACT_FORM__MODULE.getName())); - - IPath path = qualifiedNameFilePathConverter.getFilePath(name, MODULE); - - return project.getFile(path); - } - - private void createParentFolders(IFile bslFile) - { - try - { - FileUtil.createParentFolders(bslFile); - } - catch (Exception e) - { - // skip if cannot create parent folders in other threads - } - } - } diff --git a/bundles/com.e1c.v8codestyle.bsl/src/com/e1c/v8codestyle/bsl/strict/StrictTypeUtil.java b/bundles/com.e1c.v8codestyle.bsl/src/com/e1c/v8codestyle/bsl/strict/StrictTypeUtil.java index a6eb01c7..984cc2a6 100644 --- a/bundles/com.e1c.v8codestyle.bsl/src/com/e1c/v8codestyle/bsl/strict/StrictTypeUtil.java +++ b/bundles/com.e1c.v8codestyle.bsl/src/com/e1c/v8codestyle/bsl/strict/StrictTypeUtil.java @@ -17,7 +17,14 @@ import java.io.IOException; import java.io.InputStreamReader; import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.ProjectScope; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.preferences.ConfigurationScope; +import org.eclipse.core.runtime.preferences.DefaultScope; +import org.eclipse.core.runtime.preferences.IScopeContext; +import org.eclipse.core.runtime.preferences.InstanceScope; import org.eclipse.emf.ecore.EObject; import org.eclipse.xtext.EcoreUtil2; import org.eclipse.xtext.nodemodel.ICompositeNode; @@ -28,6 +35,7 @@ import org.eclipse.xtext.nodemodel.util.NodeModelUtils; import com._1c.g5.v8.dt.bsl.documentation.comment.BslCommentUtils; import com._1c.g5.v8.dt.bsl.documentation.comment.IBslCommentToken; import com._1c.g5.v8.dt.bsl.model.Module; +import com.e1c.v8codestyle.internal.bsl.BslPlugin; /** * The utility class for strict-types system. @@ -42,8 +50,33 @@ public final class StrictTypeUtil public static final String BSL_FILE_EXTENSION = "bsl"; //$NON-NLS-1$ + /** The preference root qualifier. */ + public static final String PREF_QUALIFIER = BslPlugin.PLUGIN_ID; + + /** The key for preferences store the state of the creating module with {@code //@strict-types} annotation */ + public static final String PREF_KEY_CREATE_STRICT_TYPES = "addModuleStrictTypesAnnotation"; //$NON-NLS-1$ + + /** The default value of creating module with strict types. */ + public static final boolean PREF_DEFAULT_CREATE_STRICT_TYPES = true; + private static final int COMMENT_LENGTH = IBslCommentToken.LINE_STARTER.length(); + /** + * Can add module strict-types annotation for project. + * + * @param project the project, cannot be {@code null}. + * @return true, if can create strict-types module for the project + */ + public static boolean canAddModuleStrictTypesAnnotation(IProject project) + { + ProjectScope scope = new ProjectScope(project); + IScopeContext[] contexts = + new IScopeContext[] { scope, InstanceScope.INSTANCE, ConfigurationScope.INSTANCE, DefaultScope.INSTANCE }; + + return Platform.getPreferencesService() + .getBoolean(PREF_QUALIFIER, PREF_KEY_CREATE_STRICT_TYPES, PREF_DEFAULT_CREATE_STRICT_TYPES, contexts); + } + /** * Checks for {@code @strict-types} annotation in BSL module file. * diff --git a/bundles/com.e1c.v8codestyle.bsl/src/com/e1c/v8codestyle/internal/bsl/ModuleStructureProviderPreferenceInitializer.java b/bundles/com.e1c.v8codestyle.bsl/src/com/e1c/v8codestyle/internal/bsl/ModuleStructureProviderPreferenceInitializer.java index 1d28fa03..bebe07b6 100644 --- a/bundles/com.e1c.v8codestyle.bsl/src/com/e1c/v8codestyle/internal/bsl/ModuleStructureProviderPreferenceInitializer.java +++ b/bundles/com.e1c.v8codestyle.bsl/src/com/e1c/v8codestyle/internal/bsl/ModuleStructureProviderPreferenceInitializer.java @@ -16,6 +16,7 @@ import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer; import org.eclipse.core.runtime.preferences.DefaultScope; import com.e1c.v8codestyle.bsl.IModuleStructureProvider; +import com.e1c.v8codestyle.bsl.strict.StrictTypeUtil; /** * Initializer of default values for module structure service. @@ -33,6 +34,9 @@ public class ModuleStructureProviderPreferenceInitializer .putBoolean(IModuleStructureProvider.PREF_KEY_CREATE_STRUCTURE, IModuleStructureProvider.PREF_DEFAULT_CREATE_STRUCTURE); + DefaultScope.INSTANCE.getNode(StrictTypeUtil.PREF_QUALIFIER) + .putBoolean(StrictTypeUtil.PREF_KEY_CREATE_STRICT_TYPES, StrictTypeUtil.PREF_DEFAULT_CREATE_STRICT_TYPES); + } }