1
0
mirror of https://github.com/1C-Company/v8-code-style.git synced 2025-02-22 08:19:39 +02:00

G5V8DT-24814 Некорректная вставка в область модуля вспомогательных методов событий

Refactoring and fixes
This commit is contained in:
Nikita Kuznetsov 2024-05-29 19:19:17 +03:00
parent 9a4b5f4279
commit 099de76a31
3 changed files with 74 additions and 35 deletions

View File

@ -165,7 +165,7 @@ public class FormatDocCommentModuleEditorHandler
int methodOffset = lines.get(0).getOffset();
int length = lines.get(lines.size() - 1).getEndOffset() - methodOffset;
String lineFormatter = proposalProvider.getLineFormatter(module, methodOffset, viewer.getDocument());
String lineFormatter = proposalProvider.getLineFormatter(module, viewer.getDocument(), methodOffset);
IV8Project project = v8projectManager.getProject(module);
boolean oldFormat = (project != null && project.getProject() != null)
? this.bslPreferences.getDocumentCommentProperties(project.getProject()).oldCommentFormat() : true;

View File

@ -12,8 +12,9 @@
*******************************************************************************/
package com.e1c.v8codestyle.internal.bsl.ui.services;
import org.eclipse.emf.common.util.URI;
import com._1c.g5.v8.dt.bsl.common.IBslModuleTextInsertInfo;
import com._1c.g5.v8.dt.bsl.model.Module;
/**
* Built-in language module region information with {@link String} region wrap data
@ -23,31 +24,37 @@ import com._1c.g5.v8.dt.bsl.model.Module;
public class BslModuleRegionsInfo
implements IBslModuleTextInsertInfo
{
private final URI resourceURI;
private final int insertPosition;
private final int clearPosition;
private final int clearLength;
private final Module module;
private final String regionName;
/**
* {@link BslModuleRegionsInfo} constructor
*
* @param resourceURI current module or document resource {@link URI}, cannot be <code>null</code>
* @param insertPosition <code>int</code> insertion offset, cannot be negative
* @param clearPosition text clear <code>int</code> position, can be negative if no clear needed
* @param clearLength text clear <code>int</code> length, can be negative if no clear needed
* @param module current {@link Module}, cannot be <code>null</code>
* @param regionName {@link String} region name, can be <code>null</code>
*/
public BslModuleRegionsInfo(int insertPosition, int clearPosition, int clearLength, Module module,
public BslModuleRegionsInfo(URI resourceURI, int insertPosition, int clearPosition, int clearLength,
String regionName)
{
this.resourceURI = resourceURI;
this.insertPosition = insertPosition;
this.clearPosition = clearPosition;
this.clearLength = clearLength;
this.module = module;
this.regionName = regionName;
}
@Override
public URI getResourceURI()
{
return resourceURI;
}
@Override
public int getPosition()
{
@ -60,12 +67,6 @@ public class BslModuleRegionsInfo
return clearLength;
}
@Override
public Module getModule()
{
return module;
}
/**
* Returns region name
*

View File

@ -19,11 +19,12 @@ import java.util.Map;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.nodemodel.INode;
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
import org.eclipse.xtext.resource.IResourceServiceProvider;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.ui.editor.model.IXtextDocument;
import org.eclipse.xtext.util.concurrent.IUnitOfWork;
import com._1c.g5.v8.dt.bsl.common.EventItemType;
import com._1c.g5.v8.dt.bsl.common.IBslModuleEventData;
@ -34,6 +35,7 @@ import com._1c.g5.v8.dt.bsl.model.RegionPreprocessor;
import com._1c.g5.v8.dt.bsl.model.util.BslUtil;
import com._1c.g5.v8.dt.bsl.resource.owner.BslOwnerComputerService;
import com._1c.g5.v8.dt.bsl.ui.BslGeneratorMultiLangProposals;
import com._1c.g5.v8.dt.bsl.ui.editor.BslXtextDocument;
import com._1c.g5.v8.dt.bsl.ui.event.BslModuleEventData;
import com._1c.g5.v8.dt.common.PreferenceUtils;
import com._1c.g5.v8.dt.common.StringUtils;
@ -59,38 +61,38 @@ public class BslModuleRegionsInfoService
private IModuleStructureProvider moduleStructureProvider;
@Override
public IBslModuleTextInsertInfo getEventHandlerTextInsertInfo(IXtextDocument document, Module module,
int defaultPosition, IBslModuleEventData data)
public IBslModuleTextInsertInfo getEventHandlerTextInsertInfo(IXtextDocument document, int defaultPosition,
IBslModuleEventData data)
{
if (!(data instanceof BslModuleEventData))
{
return () -> defaultPosition;
return IBslModuleTextInsertInfo.getDefaultModuleTextInsertInfo(document, defaultPosition);
}
BslModuleEventData bslModuleEventData = (BslModuleEventData)data;
Resource resource = module.eResource();
URI resourceUri = resource != null ? resource.getURI() : document.getResourceURI();
URI resourceURI = document.getResourceURI();
IResourceServiceProvider rsp =
IResourceServiceProvider.Registry.INSTANCE.getResourceServiceProvider(resourceUri);
IResourceServiceProvider.Registry.INSTANCE.getResourceServiceProvider(resourceURI);
IV8ProjectManager projectManager = rsp.get(IV8ProjectManager.class);
BslOwnerComputerService bslOwnerComputerService = rsp.get(BslOwnerComputerService.class);
IV8Project project = projectManager.getProject(resourceUri);
EClass moduleOwner = bslOwnerComputerService.computeOwnerEClass(module);
IV8Project project = projectManager.getProject(resourceURI);
ModuleInfoUnitOfWork moduleInfoUnitOfWork = new ModuleInfoUnitOfWork(rsp.get(BslOwnerComputerService.class));
ModuleInfo moduleInfo = document instanceof BslXtextDocument bslXTextDocument
? bslXTextDocument.readOnlyDataModelWithoutSync(moduleInfoUnitOfWork)
: document.readOnly(moduleInfoUnitOfWork);
EObject eventOwner = data.getEventOwner();
BslModuleEventData regionData = (BslModuleEventData)data;
EventItemType itemType = regionData.getEventItemType();
String suffix = getSuffix(eventOwner, itemType, bslModuleEventData.isInternal());
List<RegionPreprocessor> regionPreprocessors = BslUtil.getAllRegionPreprocessors(module);
ScriptVariant scriptVariant = project.getScriptVariant();
String declaredRegionName =
getDeclaredRegionName(moduleOwner, itemType, bslModuleEventData.isInternal(), scriptVariant);
getDeclaredRegionName(moduleInfo.owner, itemType, bslModuleEventData.isInternal(), scriptVariant);
Map<String, BslModuleOffsets> regionOffsets =
getRegionOffsets(document, regionPreprocessors, declaredRegionName, scriptVariant);
getRegionOffsets(document, moduleInfo.regionPreprocessors, declaredRegionName, scriptVariant);
BslModuleOffsets bslModuleOffsets = regionOffsets.get(declaredRegionName);
int offset = getRegionOffset(regionOffsets, declaredRegionName, suffix, defaultPosition, scriptVariant);
String regionName = null;
boolean createRegion = !isRegionExists(regionOffsets, declaredRegionName, suffix);
int clearOffset = -1;
int clearLength = -1;
int clearOffset = 0;
int clearLength = 0;
if (bslModuleOffsets != null && bslModuleOffsets.needReplace())
{
createRegion = true;
@ -105,7 +107,7 @@ public class BslModuleRegionsInfoService
{
regionName = suffix.isEmpty() ? declaredRegionName : (declaredRegionName + suffix);
}
return new BslModuleRegionsInfo(offset, clearOffset, clearLength, module, regionName);
return new BslModuleRegionsInfo(resourceURI, offset, clearOffset, clearLength, regionName);
}
@Override
@ -113,26 +115,31 @@ public class BslModuleRegionsInfoService
{
if (moduleTextInsertInfo instanceof BslModuleRegionsInfo moduleRegionInformation)
{
Module module = moduleTextInsertInfo.getModule();
String regionName = moduleRegionInformation.getRegionName();
if (module != null && regionName != null)
if (regionName != null)
{
URI moduleResourceUri = module.eResource().getURI();
IResourceServiceProvider rsp =
IResourceServiceProvider.Registry.INSTANCE.getResourceServiceProvider(moduleResourceUri);
IResourceServiceProvider rsp = IResourceServiceProvider.Registry.INSTANCE
.getResourceServiceProvider(moduleTextInsertInfo.getResourceURI());
IV8ProjectManager projectManager = rsp.get(IV8ProjectManager.class);
BslGeneratorMultiLangProposals proposals = rsp.get(BslGeneratorMultiLangProposals.class);
IV8Project project = projectManager.getProject(moduleResourceUri);
IV8Project project = projectManager.getProject(moduleTextInsertInfo.getResourceURI());
String lineSeparator = PreferenceUtils.getLineSeparator(project.getProject());
proposals.setRussianLang(ScriptVariant.RUSSIAN.equals(project.getScriptVariant()));
String beginRegion = proposals.getBeginRegionPropStr();
String endRegion = proposals.getEndRegionPropStr();
String space = proposals.getSpacePropStr();
StringBuilder builder = new StringBuilder();
builder.append(lineSeparator);
if (moduleTextInsertInfo.getPosition() != 0)
{
builder.append(lineSeparator);
}
builder.append(beginRegion).append(space).append(regionName);
builder.append(content);
builder.append(endRegion);
if (moduleTextInsertInfo.getPosition() == 0)
{
builder.append(lineSeparator);
}
return builder.toString();
}
}
@ -349,4 +356,35 @@ public class BslModuleRegionsInfoService
return regionName.length() >= declaredRegionNameLength
&& regionName.substring(0, declaredRegionNameLength).equals(declaredRegionName);
}
private final class ModuleInfo
{
private final EClass owner;
private final List<RegionPreprocessor> regionPreprocessors;
public ModuleInfo(EClass owner, List<RegionPreprocessor> regionPreprocessors)
{
this.owner = owner;
this.regionPreprocessors = regionPreprocessors;
}
}
private final class ModuleInfoUnitOfWork
implements IUnitOfWork<ModuleInfo, XtextResource>
{
private final BslOwnerComputerService bslOwnerComputerService;
public ModuleInfoUnitOfWork(BslOwnerComputerService bslOwnerComputerService)
{
this.bslOwnerComputerService = bslOwnerComputerService;
}
@Override
public ModuleInfo exec(XtextResource resource) throws Exception
{
Module module = (Module)resource.getParseResult().getRootASTElement();
return new ModuleInfo(bslOwnerComputerService.computeOwnerEClass(module),
BslUtil.getAllRegionPreprocessors(module));
}
}
}