Esempio n. 1
0
        private static IEnumerable <ComparisonUnit> ComparisonUnitDifferences(TrackedDocuments trackedDocument, List <ContentSection> targetSectionsCurrent, KeyStroke keyStroke)
        {
            //compare at a character level to understand what was added or removed (no transposition)
            var textComparer = new TextComparer {
                Type = TextComparer.ComparisonType.Characters
            };
            var comparisonUnits = textComparer.GetComparisonTextUnits(trackedDocument.ActiveSegment.CurrentTargetSections,
                                                                      targetSectionsCurrent, false);

            // clean up the removed selection from the current placeholder; we are only looking for what is new from this holder
            if (trackedDocument.ActiveSegment.CurrentTargetSelection.Length <= 2)
            {
                return(comparisonUnits);
            }

            try
            {
                var indexCharDiffStart = 0;
                foreach (var comparisonUnit in comparisonUnits)
                {
                    if (comparisonUnit.Type == ComparisonUnit.ComparisonType.Identical)
                    {
                        indexCharDiffStart += comparisonUnit.Text.Length;
                    }
                    else
                    {
                        break;
                    }
                }
                if (indexCharDiffStart > trackedDocument.ActiveSegment.CurrentTargetSelection.Length)
                {
                    indexCharDiffStart = indexCharDiffStart - trackedDocument.ActiveSegment.CurrentTargetSelection.Length;
                }

                var indexCharDiffCounter = 0;
                foreach (var contentSection in trackedDocument.ActiveSegment.CurrentTargetSections)
                {
                    indexCharDiffCounter += contentSection.Content.Length;
                    if (indexCharDiffCounter < indexCharDiffStart)
                    {
                        continue;
                    }

                    var indexA = indexCharDiffCounter - contentSection.Content.Length;
                    var indexB = indexA;
                    if (indexCharDiffStart > indexA)
                    {
                        indexB = indexCharDiffStart - indexA;
                    }

                    var textSeed      = contentSection.Content.Substring(0, indexB);
                    var textSelection = contentSection.Content.Substring(indexB);

                    if (textSelection.IndexOf(trackedDocument.ActiveSegment.CurrentTargetSelection, StringComparison.Ordinal) <= -1)
                    {
                        continue;
                    }

                    //remove the selection
                    var index      = textSelection.IndexOf(keyStroke.Selection, StringComparison.Ordinal);
                    var textBefore = textSelection.Substring(0, index);
                    var textAfter  = textSelection.Substring(index + trackedDocument.ActiveSegment.CurrentTargetSelection.Length);

                    contentSection.Content = textSeed + textBefore + textAfter;

                    //redo the comparison
                    comparisonUnits = textComparer.GetComparisonTextUnits(trackedDocument.ActiveSegment.CurrentTargetSections, targetSectionsCurrent, false);
                    break;
                }
            }
            catch
            {
                //ignore here for now
            }
            return(comparisonUnits);
        }
        private static void AddKeyStrokeData(KeyStroke keyStroke, IEnumerable <ComparisonUnit> comparisonUnits, TrackedDocuments trackedDocuments)
        {
            // identify the starting position of content, added, removed or replaced.
            keyStroke.Text     = GetTypedText(comparisonUnits);
            keyStroke.Position = Convert.ToInt32(GetTargetCursorPosition());
            keyStroke.X        = Cursor.Position.X;
            keyStroke.Y        = Cursor.Position.Y;

            // needs to be revised!
            // 1. exclude translations from providers
            // 2. exclude suggestions from termbase? no api handler here...
            // 3. if content that has changed is greater than 1 char in length, then evaluate
            //  - 3.a. if the char == tab or return, then we assume that this was derived from auto-suggest; however
            //         this does not take into account when the user has selected the auto-suggestion via mouse etc...
            if (string.Compare(keyStroke.Key, @"[Tab]", StringComparison.OrdinalIgnoreCase) == 0 && keyStroke.Text.Length > 1)
            {
                keyStroke.OriginType = @"auto-suggest";
            }

            //if the user hit the back key then attempt to get the selection from the comparison if it is not already present
            if (string.IsNullOrEmpty(keyStroke.Selection))
            {
                keyStroke.Selection = GetSelectedText(comparisonUnits);
            }

            if (string.IsNullOrEmpty(keyStroke.Text) && string.IsNullOrEmpty(keyStroke.Selection))
            {
                return;
            }

            //add the key stroke object to the list
            trackedDocuments.ActiveSegment.CurrentTranslationKeyStokeObjectId     = keyStroke.Id;
            trackedDocuments.ActiveSegment.CurrentTranslationKeyStrokeObjectCheck = true;
            trackedDocuments.ActiveSegment.CurrentKeyStrokes.Add(keyStroke);
        }
Esempio n. 3
0
        private static void AddKeyStrokeData(KeyStroke keyStroke, string typedText, string removedText, int position, TrackedDocuments trackedDocuments)
        {
            keyStroke.Text     = typedText;
            keyStroke.Position = position;
            keyStroke.X        = Cursor.Position.X;
            keyStroke.Y        = Cursor.Position.Y;

            // needs to be revised!
            // 1. exclude translations from providers
            // 2. exclude suggestions from termbase? no api handler here...
            // 3. if content that has changed is greater than 1 char in length, then evaluate
            //  - 3.a. if the char == tab or return, then we assume that this was derived from auto-suggest; however
            //         this does not take into account when the user has selected the auto-suggestion via mouse etc...
            if (string.Compare(keyStroke.Key, @"[Tab]", StringComparison.OrdinalIgnoreCase) == 0 && keyStroke.Text.Length > 1)
            {
                keyStroke.OriginType = @"auto-suggest";
            }

            keyStroke.Selection = removedText;

            if (string.IsNullOrEmpty(keyStroke.Text) && string.IsNullOrEmpty(keyStroke.Selection))
            {
                return;
            }

            //add the key stroke object to the list
            trackedDocuments.ActiveSegment.CurrentTranslationKeyStokeObjectId     = keyStroke.Id;
            trackedDocuments.ActiveSegment.CurrentTranslationKeyStrokeObjectCheck = true;
            trackedDocuments.ActiveSegment.CurrentKeyStrokes.Add(keyStroke);
        }