Skip to content

Commit

Permalink
Antidote 4.0.3.2: Fix tracking changes issue
Browse files Browse the repository at this point in the history
  • Loading branch information
ealbu committed Sep 16, 2024
1 parent c4933e0 commit 2170c51
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 169 deletions.
Original file line number Diff line number Diff line change
@@ -1,143 +1,139 @@
using System.Collections.Generic;
using System.Linq;
using Sdl.Community.AntidoteVerifier.Extensions;
using Sdl.Community.AntidoteVerifier.Extensions;
using Sdl.Community.AntidoteVerifier.Utils;
using Sdl.FileTypeSupport.Framework.BilingualApi;
using Sdl.TranslationStudioAutomation.IntegrationApi;
using Sdl.TranslationStudioAutomation.IntegrationApi.Extensions;
using System.Collections.Generic;
using System.Linq;

namespace Sdl.Community.AntidoteVerifier
{
public struct RangeOfCharacterInfos
{
public int length;
public int start;
}

public class EditorService : IEditorService
{
private readonly IStudioDocument _document;
private readonly Dictionary<int, KeyValuePair<string, string>> _segmentMetadata;

public EditorService(IStudioDocument document)
{
_document = document;
_segmentMetadata = new Dictionary<int, KeyValuePair<string, string>>();
Initialize();
}

public void ActivateDocument()
{
//Commented because Activate is not thread-safe and it will crash.
//EditorController editorController = SdlTradosStudio.Application.GetController<EditorController>();
//editorController.Activate(_document);
}

public bool CanReplace(int segmentId, int startPosition, int endPosition, string origString, string displayLanguage, ref string message, ref string explication)
{
var ret = false;
var segmentPair = GetSegmentPair(segmentId);
if (segmentPair != null)
{
ret = segmentPair.Target.CanReplace(startPosition, endPosition, origString, displayLanguage, ref message, ref explication);
}
return ret;
}

public int GetActiveSegmentId()
{
var segmentId = int.Parse(_document.ActiveSegmentPair.Properties.Id.Id);
var paragraphUnitId = _document.ActiveSegmentPair.GetParagraphUnitProperties().ParagraphUnitId.Id;
foreach (var kvp in _segmentMetadata)
{
if (kvp.Value.Key.Equals(segmentId) && kvp.Value.Value.Equals(paragraphUnitId))
{
return kvp.Key;
}
}

return 1;
}

public int GetCurrentSegmentId(int segmentNumber)
{
return segmentNumber;
}

public int GetDocumentId()
{
return DocumentIdGenerator.Instance.GetDocumentId(_document.ActiveFile.Id);
}

public string GetDocumentName()
{
return _document.ActiveFile.Name;
}

public int GetDocumentNoOfSegments()
{
return _segmentMetadata.Count();
}

public string GetSegmentText(int index)
{
var segmentPair = GetSegmentPair(index);
return segmentPair.Target.GetString();
}

public string GetSelection()
public struct RangeOfCharacterInfos
{
public int length;
public int start;
}

public class EditorService : IEditorService
{
private readonly IStudioDocument _document;
private readonly Dictionary<int, KeyValuePair<string, string>> _segmentMetadata;

public EditorService(IStudioDocument document)
{
_document = document;
_segmentMetadata = new Dictionary<int, KeyValuePair<string, string>>();
Initialize();
}

public void ActivateDocument()
{
EditorController editorController = SdlTradosStudio.Application.GetController<EditorController>();
editorController.Activate(_document);
}

public bool CanReplace(int segmentId, int startPosition, int endPosition, string origString, string displayLanguage, ref string message, ref string explication)
{
var ret = false;
var segmentPair = GetSegmentPair(segmentId);
if (segmentPair != null)
{
ret = segmentPair.Target.CanReplace(startPosition, endPosition, origString, displayLanguage, ref message, ref explication);
}
return ret;
}

public int GetActiveSegmentId()
{
var segmentId = int.Parse(_document.ActiveSegmentPair.Properties.Id.Id);
var paragraphUnitId = _document.ActiveSegmentPair.GetParagraphUnitProperties().ParagraphUnitId.Id;
foreach (var kvp in _segmentMetadata)
{
if (kvp.Value.Key.Equals(segmentId) && kvp.Value.Value.Equals(paragraphUnitId))
{
return kvp.Key;
}
}

return 1;
}

public int GetCurrentSegmentId(int segmentNumber)
{
return segmentNumber;
}

public int GetDocumentId()
{
return DocumentIdGenerator.Instance.GetDocumentId(_document.ActiveFile.Id);
}

public string GetDocumentName()
{
return _document.ActiveFile.Name;
}

public int GetDocumentNoOfSegments()
{
return _segmentMetadata.Count();
}

public string GetSegmentText(int index)
{
var segmentPair = GetSegmentPair(index);
return segmentPair.Target.GetString();
}

public string GetSelection()
{
return _document.Selection.Target.ToString();
}

public void ReplaceTextInSegment(int segmentId, int startPosition, int endPosition, string replacementText)
{
var segmentPair = GetSegmentPair(segmentId);

segmentPair?.Target.Replace(startPosition, endPosition, replacementText);

_document.UpdateSegmentPair(segmentPair);
}

public void SelectText(int index, int startPosition, int endPosition)
{
var segmentPair = GetSegmentPair(index);

var paragraphUnitId = segmentPair.GetParagraphUnitProperties().ParagraphUnitId.Id;

_document.SetActiveSegmentPair(paragraphUnitId, segmentPair.Properties.Id.Id);
}

private ISegmentPair GetSegmentPair(int index)
{
var segmentUniqueIdentifier = _segmentMetadata[index];

return _document.SegmentPairs
.FirstOrDefault(
segmentPair =>
{
var segmentIdFound = segmentPair.Properties.Id.Id.Equals(segmentUniqueIdentifier.Key.ToString());
var paragraphUnitId = segmentPair.GetParagraphUnitProperties().ParagraphUnitId.Id;
return segmentIdFound && paragraphUnitId.Equals(segmentUniqueIdentifier.Value);
});
}

private void Initialize()
{
_segmentMetadata.Clear();

var index = 1;
for (var i = 0; i < _document?.FilteredSegmentPairsCount; i++)
{
var segmentPair = _document.FilteredSegmentPairs.ToList()[i];
var paragraphUnitId = segmentPair.GetParagraphUnitProperties().ParagraphUnitId.Id;
var currentId = segmentPair.Properties.Id.Id;

if (string.IsNullOrEmpty(segmentPair.Target.GetString()))
continue;
_segmentMetadata.Add(index, new KeyValuePair<string, string>(currentId, paragraphUnitId));
index++;
}
}
}
public void ReplaceTextInSegment(int segmentId, int startPosition, int endPosition, string replacementText)
{
var segmentPair = GetSegmentPair(segmentId);
segmentPair?.Target.Replace(startPosition, endPosition, replacementText);
_document.UpdateSegmentPair(segmentPair);
}

public void SelectText(int index, int startPosition, int endPosition)
{
var segmentPair = GetSegmentPair(index);

var paragraphUnitId = segmentPair.GetParagraphUnitProperties().ParagraphUnitId.Id;

_document.SetActiveSegmentPair(paragraphUnitId, segmentPair.Properties.Id.Id);
}

private ISegmentPair GetSegmentPair(int index)
{
var segmentUniqueIdentifier = _segmentMetadata[index];

return _document.SegmentPairs
.FirstOrDefault(
segmentPair =>
{
var segmentIdFound = segmentPair.Properties.Id.Id.Equals(segmentUniqueIdentifier.Key.ToString());
var paragraphUnitId = segmentPair.GetParagraphUnitProperties().ParagraphUnitId.Id;
return segmentIdFound && paragraphUnitId.Equals(segmentUniqueIdentifier.Value);
});
}

private void Initialize()
{
_segmentMetadata.Clear();

var index = 1;
for (var i = 0; i < _document?.FilteredSegmentPairsCount; i++)
{
var segmentPair = _document.FilteredSegmentPairs.ToList()[i];
var paragraphUnitId = segmentPair.GetParagraphUnitProperties().ParagraphUnitId.Id;
var currentId = segmentPair.Properties.Id.Id;

if (string.IsNullOrEmpty(segmentPair.Target.GetString()))
continue;
_segmentMetadata.Add(index, new KeyValuePair<string, string>(currentId, paragraphUnitId));
index++;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
using Sdl.Community.AntidoteVerifier.Utils;
using Sdl.FileTypeSupport.Framework.BilingualApi;
using System;
using Sdl.FileTypeSupport.Framework.BilingualApi;
using System.Collections.Generic;

namespace Sdl.Community.AntidoteVerifier.Extensions
{
public static class SegmentExtensions
{
public static string GetString(this ISegment segment, bool includeSegments = false) =>
segment.ToString();

//displayLanguage = language of the message or the explication
//displayLanguage = language of the message or the explication
public static bool CanReplace(this ISegment segment, int startIndex, int endPosition, string origString, string displayLanguage, ref string message, ref string explication)
{
bool ret;
Expand All @@ -27,7 +23,6 @@ public static bool CanReplace(this ISegment segment, int startIndex, int endPosi
message = "Antidote cannot proceed with the correction because the segment is read-only.";
explication = "";
}

}
else
{
Expand Down Expand Up @@ -68,43 +63,39 @@ public static bool CanReplace(this ISegment segment, int startIndex, int endPosi
return ret;
}

public static void Replace(this ISegment segment, int startIndex, int endPosition, string replacementText)
public static string GetString(this ISegment segment, bool includeSegments = false)
{
if (segment == null)
throw new ArgumentNullException(nameof(segment));
var textVisitor = new CustomTextCollectionVisitor(segment);

var currentIndex = 0;

foreach (var element in segment)
foreach (var item in segment)
{
if (element is IText textElement)
{
var elementLength = textElement.Properties.Text.Length;
item.AcceptVisitor(textVisitor);
}

// Check if the replacement range falls within this text element
if (currentIndex <= startIndex && currentIndex + elementLength > endPosition)
{
// Split the text within this element
var beforeText = textElement.Properties.Text.Substring(0, startIndex - currentIndex);
var afterText = textElement.Properties.Text.Substring(endPosition - currentIndex);
return textVisitor.CollectedText;
}

// Set the new text with the replacement
textElement.Properties.Text = beforeText + replacementText + afterText;
public static void Replace(this ISegment segment, int startIndex, int endPosition, string replacementText)
{
var textVisitor = new CustomTextCollectionVisitor(segment, startIndex, endPosition);
foreach (var item in segment)
item.AcceptVisitor(textVisitor);

break;
}
textVisitor.ReplaceText(replacementText);
}

if (currentIndex <= startIndex && currentIndex + elementLength == endPosition)
private static IEnumerable<IAbstractMarkupData> GetFlattenedElements(this ISegment segment)
{
foreach (var element in segment)
{
if (element is IAbstractMarkupDataContainer container)
foreach (var subElement in container)
{
var beforeText = textElement.Properties.Text.Substring(0, startIndex - currentIndex);
textElement.Properties.Text = beforeText + replacementText;
yield return subElement;
}

currentIndex += elementLength;
}
else
currentIndex += element.ToString().Length;
yield return element;
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("4.0.0.0")]
[assembly: AssemblyFileVersion("4.0.3.1")]
[assembly: AssemblyFileVersion("4.0.3.2")]
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<PluginPackage xmlns="http://www.sdl.com/Plugins/PluginPackage/1.0">
<PlugInName>Antidote Verifier</PlugInName>
<Version>4.0.3.1</Version>
<Version>4.0.3.2</Version>
<Description>Studio verifier that will verify the text in Antidote</Description>
<Author>Trados AppStore Team</Author>
<RequiredProduct name="TradosStudio" minversion="18.0" maxversion="18.9" />
Expand Down

0 comments on commit 2170c51

Please sign in to comment.