private static void DetectInsertDeleteInsertPattern(ILogProvider logProvider, List <DocumentChange> dcList, List <PatternInstance> detectedPatterns, int i, DocumentChange dc)
        {
            if (dc is Insert && logProvider.CausedByInsertString(dc) && !string.IsNullOrWhiteSpace(((Insert)dc).Text))
            {
                int offset = dc.Offset;
                int length = dc.Length;

                if (i + 1 < dcList.Count && dcList[i + 1] is Delete && (dcList[i + 1].Timestamp - dc.Timestamp) < TIME_THRESHOLD)
                {
                    int offset2 = dcList[i + 1].Offset;
                    int length2 = dcList[i + 1].Length;

                    if (offset <= offset2 && offset2 + length2 <= offset + length)
                    {
                        if (i + 2 < dcList.Count && dcList[i + 2] is Insert)
                        {
                            int offset3 = dcList[i + 2].Offset;

                            if (offset2 == offset3)
                            {
                                var result = new PatternInstance(
                                    dc,
                                    3,
                                    "Type #1: \"" + ((Insert)dcList[i]).Text + "\" - \"" +
                                    ((Delete)dcList[i + 1]).Text + "\" + \"" +
                                    ((Insert)dcList[i + 2]).Text + "\""
                                    );

                                detectedPatterns.Add(result);
                            }
                        }
                    }
                }
            }
        }
        private static void DetectInsertReplaceInsertPattern(ILogProvider logProvider, List <Event> completeList, List <DocumentChange> dcList, List <PatternInstance> detectedPatterns, int i, DocumentChange dc)
        {
            if (dc is Insert && logProvider.CausedByInsertString(dc) && !string.IsNullOrWhiteSpace(((Insert)dc).Text))
            {
                int offset = dc.Offset;
                int length = dc.Length;

                if (i + 1 < dcList.Count && dcList[i + 1] is Replace &&
                    !logProvider.CausedByAutoIndent(dcList[i + 1]) &&
                    (dcList[i + 1].Timestamp - dc.Timestamp) < TIME_THRESHOLD)
                {
                    Replace replace = (Replace)dcList[i + 1];

                    // Check the prev/next command
                    int indexOfReplace = completeList.IndexOf(replace);
                    Debug.Assert(indexOfReplace >= 0);

                    // If previous command is content assist, don't count it.
                    // If next command is PasteCommand, don't count it.
                    if (!(completeList[indexOfReplace - 1] is AssistCommand) &&
                        !(completeList.Count > indexOfReplace + 1 && completeList[indexOfReplace + 1] is PasteCommand))
                    {
                        int offset2 = replace.Offset;
                        int length2 = replace.Length;

                        if (offset <= offset2 && offset2 + length2 <= offset + length)
                        {
                            if (i + 2 < dcList.Count && dcList[i + 2] is Insert)
                            {
                                int offset3 = dcList[i + 2].Offset;

                                if (offset3 == offset2 + replace.InsertionLength)
                                {
                                    var result = new PatternInstance(
                                        dc,
                                        3,
                                        "Type #2: \"" + ((Insert)dcList[i]).Text + "\" - \"" +
                                        replace.DeletedText + "\" + \"" +
                                        replace.InsertedText +
                                        ((Insert)dcList[i + 2]).Text + "\""
                                        );

                                    detectedPatterns.Add(result);
                                }
                            }
                            else
                            {
                                var result = new PatternInstance(
                                    dc,
                                    2,
                                    "Type #3: \"" + ((Insert)dcList[i]).Text + "\" - \"" +
                                    ((Replace)dcList[i + 1]).DeletedText + "\" + \"" +
                                    ((Replace)dcList[i + 1]).InsertedText + "\""
                                    );

                                detectedPatterns.Add(result);
                            }
                        }
                    }
                }
            }
        }
Example #3
0
        public override IEnumerable <PatternInstance> DetectAsPatternInstances(ILogProvider logProvider)
        {
            // we only consider "Create" because people often do not close the application at all.
            List <Event> list = logProvider.LoggedEvents.Where(x => x is DocumentChange || (x is RunCommand && !((RunCommand)x).IsTerminate)).ToList();

            Event lastRun = null;

            List <ParameterTuneElement> deleteOffsets = new List <ParameterTuneElement>();

            foreach (int i in Enumerable.Range(0, list.Count))
            {
                if (list[i] is RunCommand)
                {
                    RunCommand currentRun = (RunCommand)list[i];

                    if (lastRun != null)
                    {
                        // TODO: Make it as an option! Current value is 1 min.
                        if (currentRun.Timestamp - lastRun.Timestamp <= 45000)
                        {
                            foreach (var element in deleteOffsets)
                            {
                                if (CheckElement(element))
                                {
                                    var result = new PatternInstance(
                                        lastRun,
                                        -1,
                                        "Deleted: \"" + element.DeletedText + "\", Inserted: \"" + element.InsertedText + "\""
                                        );
                                    yield return(result);
                                }
                            }
                        }
                    }

                    lastRun = currentRun;
                    deleteOffsets.Clear();
                }
                else if (list[i] is DocumentChange)
                {
                    DocumentChange dc = (DocumentChange)list[i];
                    if (dc is Delete)
                    {
                        Delete delete = (Delete)dc;

                        // TODO: Merge delete offsets if necessary!
                        foreach (int j in Enumerable.Range(0, deleteOffsets.Count))
                        {
                            if (deleteOffsets[j].Confirmed)
                            {
                                continue;
                            }

                            if (deleteOffsets[j].DeleteOffset > delete.Offset)
                            {
                                deleteOffsets[j].DeleteOffset -= delete.Length;
                            }
                        }

                        deleteOffsets.Add(new ParameterTuneElement(delete));
                    }
                    else if (dc is Insert)
                    {
                        Insert insert = (Insert)dc;

                        foreach (int j in Enumerable.Range(0, deleteOffsets.Count))
                        {
                            if (deleteOffsets[j].Confirmed)
                            {
                                continue;
                            }

                            if (deleteOffsets[j].DeleteOffset > insert.Offset)
                            {
                                deleteOffsets[j].DeleteOffset += insert.Length;
                            }
                            else if (deleteOffsets[j].DeleteOffset == insert.Offset)
                            {
                                deleteOffsets[j].InsertedText = insert.Text;
                                deleteOffsets[j].Confirmed    = true;
                            }
                        }
                    }
                    else if (dc is Replace && !logProvider.CausedByAutoIndent(dc) && !logProvider.CausedByAssist(dc))
                    {
                        Replace replace = (Replace)dc;

                        deleteOffsets.Add(new ParameterTuneElement(replace));

                        foreach (int j in Enumerable.Range(0, deleteOffsets.Count))
                        {
                            if (deleteOffsets[j].Confirmed)
                            {
                                continue;
                            }

                            if (deleteOffsets[j].DeleteOffset == replace.Offset)
                            {
                                deleteOffsets[j].DeleteOffset -= replace.Length;
                                deleteOffsets[j].DeleteOffset += replace.InsertionLength;
                            }
                        }
                    }
                }
            }
        }