public static void ContentChanged(object sender, DocumentContentEventArgs e) { Tracked.TrackerLastActivity = DateTime.Now; if (Tracked.ActiveDocument == null) { return; } var projectFile = Tracked.ActiveDocument.Files.FirstOrDefault(); if (projectFile == null) { return; } var projectFileId = projectFile.Id.ToString(); // Warning message to user -> if content changes and activity tracking is turned off | WarningMessageTrackingNotRunning(sender, projectFileId); if (!Tracked.DictCacheDocumentItems.ContainsKey(projectFileId)) { return; } //grab the latest keys selected var keyStroke = (KeyStroke)Viewer.KsCache.Clone(); keyStroke.Created = DateTime.Now; //get the cache document item var trackedDocument = Tracked.DictCacheDocumentItems[projectFile.Id.ToString()]; trackedDocument.ActiveSegment.CurrentSegmentContentHasChanged = true; keyStroke.Selection = trackedDocument.ActiveSegment.CurrentTargetSelection; //reset the class Viewer.KsCache = new KeyStroke(); //get the latest version of the target content TrackedController.ContentProcessor.ProcessSegment(e.Segments.FirstOrDefault(), true, new List <string>()); var trackingSegmentContentTrg = string.Empty; var targetSectionsCurrent = TrackedController.GetRecordContentSections( TrackedController.ContentProcessor.SegmentSections , ContentSection.LanguageType.Target , ref trackingSegmentContentTrg); //remove the revision marker for this test foreach (var contentSection in targetSectionsCurrent) { if (contentSection.RevisionMarker == null || contentSection.RevisionMarker.RevType != RevisionMarker.RevisionType.Delete) { continue; } contentSection.Content = string.Empty; contentSection.RevisionMarker = null; } try { #if DEBUG var temp1 = new List <ContentSection>(); var temp2 = new List <ContentSection>(); trackedDocument.ActiveSegment.CurrentTargetSections.ForEach(x => temp1.Add(x.Clone() as ContentSection)); targetSectionsCurrent.ForEach(x => temp2.Add(x.Clone() as ContentSection)); #endif // Compare the before/after text, to work out (as much as we can) what was typed/pasted/etc. and // what selection (if any) was replaced as a result. // We can assume that the change will affect a contiguous span of text, because this event fires in response // to whatever change, and the target segment can't have multiple selected spans. // Even if we have a segment with the same word repeated in different places, and we perform 'replace all' // in Studio, the two replacements fire separately here. var newSections = TextComparer.ConcatenateComparableContentSections(trackedDocument.ActiveSegment.CurrentTargetSections); var previousSections = TextComparer.ConcatenateComparableContentSections(targetSectionsCurrent); var newTextWithMarkup = SimpleParse(newSections, true); var oldTextWithMarkup = SimpleParse(previousSections, true); var newText = SimpleParse(newSections, false); var oldText = SimpleParse(previousSections, false); // Find the shared prefix and suffix to identify the affected span int prefixLength = CommonPrefixLength(newTextWithMarkup, oldTextWithMarkup); int suffixLength = 0; if (prefixLength < Math.Min(newTextWithMarkup.Length, oldTextWithMarkup.Length)) { suffixLength = CommonSuffixLength(newTextWithMarkup, oldTextWithMarkup, prefixLength); } // Extract typed/pasted and removed (selected) text string typedText = TypedText(newTextWithMarkup, oldTextWithMarkup, prefixLength, suffixLength); string removedText = RemovedText(newTextWithMarkup, oldTextWithMarkup, prefixLength, suffixLength); // For logging, report the position in the text without tags, because that's a value that's // most likely to be usable when analysing int prefixLengthPlain = CommonPrefixLength(newText, oldText); Debug.WriteLine("Got typed text of <" + typedText + ">"); Debug.WriteLine("Got removed text of <" + removedText + ">"); Debug.WriteLine("newTextWithMarkup: " + newTextWithMarkup); Debug.WriteLine("c2: " + oldTextWithMarkup); // Try to detect case where (say) 'Parliament' was selected in the text, then the user typed 'P'; // at this point, text comparison will have left typedText empty, and selection one character short ('P') if (!string.IsNullOrEmpty(keyStroke.Key) && typedText.Length == 0 && prefixLength > 0 && !keyStroke.Ctrl && !keyStroke.Alt) { string altTypedText = TypedText(newTextWithMarkup, oldTextWithMarkup, prefixLength - 1, suffixLength); if (altTypedText.Length > 0 && altTypedText.ToLower()[0] == keyStroke.Key.ToLower()[0]) { typedText = altTypedText; removedText = RemovedText(newTextWithMarkup, oldTextWithMarkup, prefixLength - 1, suffixLength); Debug.WriteLine("Detected overtype of selected text"); Debug.WriteLine("Got typed text of <" + typedText + ">"); Debug.WriteLine("Got removed text of <" + removedText + ">"); } } // add the key stroke data | AddKeyStrokeData(keyStroke, typedText, removedText, prefixLengthPlain, trackedDocument); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { //update the current cause selection list trackedDocument.ActiveSegment.CurrentTargetSections = new List <ContentSection>(); foreach (var section in targetSectionsCurrent) { trackedDocument.ActiveSegment.CurrentTargetSections.Add((ContentSection)section.Clone()); } //remove the cach holder for current seleciton trackedDocument.ActiveSegment.CurrentTargetSelection = string.Empty; } }
public static void ContentChanged(object sender, DocumentContentEventArgs e) { Tracked.TrackerLastActivity = DateTime.Now; if (Tracked.ActiveDocument == null) { return; } var projectFile = Tracked.ActiveDocument.Files.FirstOrDefault(); if (projectFile == null) { return; } var projectFileId = projectFile.Id.ToString(); // Warning message to user -> if content changes and activity tracking is turned off | WarningMessageTrackingNotRunning(sender, projectFileId); if (!Tracked.DictCacheDocumentItems.ContainsKey(projectFileId)) { return; } //grab the latest keys selected var keyStroke = (KeyStroke)Viewer.KsCache.Clone(); keyStroke.Created = DateTime.Now; //get the cache document item var trackedDocument = Tracked.DictCacheDocumentItems[projectFile.Id.ToString()]; trackedDocument.ActiveSegment.CurrentSegmentContentHasChanged = true; keyStroke.Selection = trackedDocument.ActiveSegment.CurrentTargetSelection; //reset the class Viewer.KsCache = new KeyStroke(); //get the latest version of the target content TrackedController.ContentProcessor.ProcessSegment(e.Segments.FirstOrDefault(), true, new List <string>()); var trackingSegmentContentTrg = string.Empty; var targetSectionsCurrent = TrackedController.GetRecordContentSections(TrackedController.ContentProcessor.SegmentSections , ContentSection.LanguageType.Target , ref trackingSegmentContentTrg); //remove the revision marker for this test foreach (var contentSection in targetSectionsCurrent) { if (contentSection.RevisionMarker == null || contentSection.RevisionMarker.RevType != RevisionMarker.RevisionType.Delete) { continue; } contentSection.Content = string.Empty; contentSection.RevisionMarker = null; } var comparisonUnits = ComparisonUnitDifferences(trackedDocument, targetSectionsCurrent, keyStroke); try { // add the key stroke data | AddKeyStrokeData(keyStroke, comparisonUnits, trackedDocument); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { //update the current cause selection list trackedDocument.ActiveSegment.CurrentTargetSections = new List <ContentSection>(); foreach (var section in targetSectionsCurrent) { trackedDocument.ActiveSegment.CurrentTargetSections.Add((ContentSection)section.Clone()); } //remove the cach holder for current seleciton trackedDocument.ActiveSegment.CurrentTargetSelection = string.Empty; } }
public static void ContentChanged(object sender, DocumentContentEventArgs e) { Tracked.TrackerLastActivity = DateTime.Now; #region | warning message to user -> if content changes and activity tracking is turned off | var firstOrDefault = Tracked.ActiveDocument.Files.FirstOrDefault(); if (firstOrDefault != null && Convert.ToBoolean(Tracked.Settings.GetTrackingProperty(@"warningMessageActivityTrackingNotRunning").Value) && Tracked.ActiveDocument != null && Tracked.DictCacheDocumentItems.ContainsKey(firstOrDefault.Id.ToString())) { var ignoreWarning = false; var projectFile = Tracked.ActiveDocument.Files.FirstOrDefault(); if (projectFile != null && Tracked.WarningMessageDocumentsIgnoreActivityNotRunning.ContainsKey(projectFile.Id.ToString())) { var orDefault = Tracked.ActiveDocument.Files.FirstOrDefault(); if (orDefault != null) { ignoreWarning = Tracked.WarningMessageDocumentsIgnoreActivityNotRunning[orDefault.Id.ToString()]; } } if (!ignoreWarning) { var dr = MessageBox.Show(PluginResources.The_activity_tracker_is_not_running_ + "\r\n\r\n" + PluginResources.Note_The_current_change_will_be_ignored, Application.ProductName, MessageBoxButtons.YesNo); if (dr == DialogResult.Yes) { Tracked.TrackingState = Tracked.TimerState.Started; if (!Tracked.TrackingTimer.IsRunning) { Tracked.TrackingTimer.Start(); } var orDefault = Tracked.ActiveDocument.Files.FirstOrDefault(); if (orDefault != null && !Tracked.DictCacheDocumentItems.ContainsKey(orDefault.Id.ToString())) { TrackedController.TrackNewDocumentEntry(Tracked.ActiveDocument); } var @default = Tracked.ActiveDocument.Files.FirstOrDefault(); if (@default != null) { var trackedDocuments = Tracked.DictCacheDocumentItems[@default.Id.ToString()]; if (Tracked.TrackingState == Tracked.TimerState.Started) { trackedDocuments.ActiveDocument.DocumentTimer.Start(); } } TrackedController.InitializeDocumentTracking(Tracked.ActiveDocument); ActiveSegmentChanged(sender, null); Tracked.TrackingIsDirtyC0 = true; Tracked.TrackingIsDirtyC1 = true; Tracked.TrackingIsDirtyC2 = true; } else { //remember the decision from the user var orDefault = Tracked.ActiveDocument.Files.FirstOrDefault(); if (orDefault != null && !Tracked.WarningMessageDocumentsIgnoreActivityNotRunning.ContainsKey(orDefault.Id.ToString())) { var @default = Tracked.ActiveDocument.Files.FirstOrDefault(); if (@default != null) { Tracked.WarningMessageDocumentsIgnoreActivityNotRunning.Add(@default.Id.ToString(), true); } } else { var file = Tracked.ActiveDocument.Files.FirstOrDefault(); if (file != null) { Tracked.WarningMessageDocumentsIgnoreActivityNotRunning[file.Id.ToString()] = true; } } } } } #endregion var o = Tracked.ActiveDocument.Files.FirstOrDefault(); if (o == null || Tracked.ActiveDocument == null || !Tracked.DictCacheDocumentItems.ContainsKey(o.Id.ToString())) { return; } { //grab the latest keys selected var keyStroke = (KeyStroke)Viewer.KsCache.Clone(); keyStroke.Created = DateTime.Now; //get the cache document item var projectFile = Tracked.ActiveDocument.Files.FirstOrDefault(); if (projectFile == null) { return; } var trackedDocuments = Tracked.DictCacheDocumentItems[projectFile.Id.ToString()]; trackedDocuments.ActiveSegment.CurrentSegmentContentHasChanged = true; keyStroke.Selection = trackedDocuments.ActiveSegment.CurrentTargetSelection; //reset the class Viewer.KsCache = new KeyStroke(); //get the latest version of the target content TrackedController.ContentProcessor.ProcessSegment(e.Segments.FirstOrDefault(), true, new List <string>()); var trackingSegmentContentTrg = string.Empty; var targetSectionsCurrent = TrackedController.GetRecordContentSections(TrackedController.ContentProcessor.SegmentSections , ContentSection.LanguageType.Target , ref trackingSegmentContentTrg); //remove the revision marker for this test foreach (var t in targetSectionsCurrent) { if (t.RevisionMarker == null || t.RevisionMarker.RevType != RevisionMarker.RevisionType.Delete) { continue; } t.Content = string.Empty; t.RevisionMarker = null; } #region | compare the content | //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(trackedDocuments.ActiveSegment.CurrentTargetSections, targetSectionsCurrent, false); #region | fix the selection removal from the cache | // clean up the removed selection from the current placeholder; we are only looking for what is new from this holder if (trackedDocuments.ActiveSegment.CurrentTargetSelection.Length > 2) { try { var indexCharDiffStart = 0; foreach (var t in comparisonUnits) { if (t.Type == ComparisonUnit.ComparisonType.Identical) { indexCharDiffStart += t.Text.Length; } else { break; } } if (indexCharDiffStart > trackedDocuments.ActiveSegment.CurrentTargetSelection.Length) { indexCharDiffStart = indexCharDiffStart - trackedDocuments.ActiveSegment.CurrentTargetSelection.Length; } var indexCharDiffCounter = 0; foreach (var t in trackedDocuments.ActiveSegment.CurrentTargetSections) { indexCharDiffCounter += t.Content.Length; if (indexCharDiffCounter < indexCharDiffStart) { continue; } var indexStartingPointA = indexCharDiffCounter - t.Content.Length; var indexStartingPointB = indexStartingPointA; if (indexCharDiffStart > indexStartingPointA) { indexStartingPointB = indexCharDiffStart - indexStartingPointA; } var indexStartingPointBBefore = t.Content.Substring(0, indexStartingPointB); var indexStartingPointBAfter = t.Content.Substring(indexStartingPointB); if (indexStartingPointBAfter.IndexOf(trackedDocuments.ActiveSegment.CurrentTargetSelection, StringComparison.Ordinal) <= -1) { continue; } //remove the selection var indexBefore = indexStartingPointBAfter.IndexOf(keyStroke.Selection, StringComparison.Ordinal); var strBefore = indexStartingPointBAfter.Substring(0, indexBefore); var strAfter = indexStartingPointBAfter.Substring(indexBefore + trackedDocuments.ActiveSegment.CurrentTargetSelection.Length); t.Content = indexStartingPointBBefore + strBefore + strAfter; //redo the comparison comparisonUnits = textComparer.GetComparisonTextUnits(trackedDocuments.ActiveSegment.CurrentTargetSections, targetSectionsCurrent, false); break; } } catch { //ignore here for now } } #endregion #endregion try { #region | add the key stroke data | var textDelete = string.Empty; keyStroke.Text = string.Empty; foreach (var comparisonUnit in comparisonUnits) { switch (comparisonUnit.Type) { case ComparisonUnit.ComparisonType.New: foreach (var trgu in comparisonUnit.Section) { keyStroke.Text += trgu.Content; } break; case ComparisonUnit.ComparisonType.Removed: textDelete = comparisonUnit.Section.Aggregate(textDelete, (current, trgu) => current + trgu.Content); break; } } // logical deduction // needs to be revised! // 1. exclude translations from providers // 2. exclude suggestions from termbase? no api handler here... TODO // 3. if content changed is greater than 1 char in length then check the key char // - 3.a. if the char == tab or return, then we can 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 selection 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.Compare(keyStroke.Key, @"[Back]", StringComparison.OrdinalIgnoreCase) == 0 || string.Compare(keyStroke.Key, @"[Delete]", StringComparison.OrdinalIgnoreCase) == 0 && keyStroke.Text == string.Empty && keyStroke.Selection == string.Empty && textDelete != string.Empty) { keyStroke.Selection = textDelete; } if (keyStroke.Text == string.Empty && keyStroke.Selection == string.Empty) { return; } //add the key stroke object to the list trackedDocuments.ActiveSegment.CurrentTranslationKeyStokeObjectId = keyStroke.Id; trackedDocuments.ActiveSegment.CurrentTranslationKeyStrokeObjectCheck = true; trackedDocuments.ActiveSegment.CurrentKeyStrokes.Add(keyStroke); #endregion } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { //update the current cause selection list trackedDocuments.ActiveSegment.CurrentTargetSections = new List <ContentSection>(); foreach (var section in targetSectionsCurrent) { trackedDocuments.ActiveSegment.CurrentTargetSections.Add((ContentSection)section.Clone()); } //remove the cach holder for current seleciton trackedDocuments.ActiveSegment.CurrentTargetSelection = string.Empty; } } }