public ApplySuggestionOnSelectionAction(ScopeAwareRichTextBox txt, Suggestion sug)
            : base(txt, txt.ActiveScope)
        {
            selStart = txt.SelectionStart;
            selLength = txt.SelectionLength;

            Title = sug.Description;
            suggestion = sug;
        }
Beispiel #2
0
 public MultilineSuggestionDecorator(Suggestion sug)
     : base(sug)
 {
     m_sug.RegexMatchFlags.Add(MatchFlags.Multiline);
 }
Beispiel #3
0
 public GroupedSuggestionDecorator(Suggestion inner, Scope namedScope)
     : base(inner)
 {
     scopeName     = namedScope.Name;
     baseRegexText = inner.RegexText;
 }
        public void Learn(Suggestion suggestion)
        {
            if (possibleMatches.Count == 0)
            {
                possibleMatches.Add(suggestion);
                return;
            }
            if (matchAlreadyExists(suggestion))
            {
                string text = "A suggestion with the regex '"
                                + suggestion.RegexText
                                + "' already exists";
                throw new InvalidOperationException(text);
            }

            if (AutoLineStartEndSymbolOnSuggestions)
            {
                if (!suggestion.RegexText.StartsWith("^"))
                {
                    suggestion.RegexText = "^" + suggestion.RegexText;
                }

                if (!suggestion.RegexText.EndsWith("$"))
                {
                    suggestion.RegexText += "$";
                }
            }

            addSuggestionSortedByHighestProbability(suggestion);
        }
 public void GroupedSuggestion_CreateWithNoScopeName_ReturnsOriginalRegex()
 {
     Scope unnamedScope = new Scope("a");
     Suggestion s = new Suggestion(@"a", "desc");
     GroupedSuggestionDecorator decorator = new GroupedSuggestionDecorator(s, unnamedScope);
     Assert.AreEqual(@"a", decorator.RegexText);
 }
 public GroupedSuggestionDecorator(Suggestion inner, Scope namedScope)
     : base(inner)
 {
     scopeName = namedScope.Name;
     baseRegexText = inner.RegexText;
 }
 public SuggestionDecorator(Suggestion sug)
 {
     m_sug = sug;
 }
Beispiel #8
0
 public void LearnAutomaticRules(Suggestion suggestion)
 {
     initAutoAdvisor();
     autoAdvisor.Learn(suggestion);
 }
 private bool isMatch(Suggestion possibility, string input)
 {
     try
     {
         bool success = Regex.IsMatch(input, possibility.RegexText, RegexOptions.None);
         return success;
     }
     catch (Exception)
     {
         return false;
     }
 }
        private void inferMultipleMatchesSuggestions(string input, List<Suggestion> matches, Suggestion possibility)
        {
            bool IsMatchingOneOrMore =
                possibility.RegexText.EndsWith(@"+$") ||
                possibility.RegexText.EndsWith(@"+?$");

            if (!(IsMatchingOneOrMore))
            {
                return;
            }
            int howManyChars = input.Length;
            string[] descWords = possibility.Description.Split(' ');
            string lastWord = descWords[descWords.Length - 1];

            inferMultiple(matches, possibility, howManyChars, lastWord);
            inferMultipleExplicitChar(input, matches, howManyChars, lastWord);
        }
        private void inferMultiple(List<Suggestion> matches, Suggestion possibility, int howManyChars, string lastWord)
        {
            string newMatchSuffix = "{" + howManyChars + "}$";
            string newRegex = possibility.RegexText + newMatchSuffix;
            newRegex = newRegex.Replace("+$" + newMatchSuffix, newMatchSuffix);
            newRegex = newRegex.Replace("+?$" + newMatchSuffix, newMatchSuffix);

            matches.Add(new Suggestion(newRegex, howManyChars + " " + lastWord));
        }
 private void addSuggestionSortedByHighestProbability(Suggestion suggestion)
 {
     for (int i = 0; i < possibleMatches.Count; i++)
     {
         Suggestion curSuggestion = possibleMatches[i];
         if (curSuggestion.Probability < suggestion.Probability)
         {
             possibleMatches.Insert(i, suggestion);
             return;
         }
     }
     possibleMatches.Add(suggestion);
 }
        public List<Suggestion> Suggest(Scope scope)
        {
            Thread.SpinWait(1);
            List<Suggestion> list = new List<Suggestion>();
            if (scope == null)
            {
                return list;
            }
            bool scopeAlreadyHasSuggestions = (scope.Suggestions.Count > 0);
            if (scopeAlreadyHasSuggestions && scope.IsFlat)
            {
                if (useGroupsForUnnamedScopes)
                {
                    useUnnamedGroupedSuggestion(scope.Suggestions, scope, scope.Suggestions[0]);
                }
                useGroupedSuggestionIfNeeded(scope.Suggestions, scope, scope.Suggestions[0]);
                return scope.Suggestions;
            }

            if (scope.IsFlat)
            {
                List<Suggestion> results = Suggest(scope.Text);
                if (useGroupsForUnnamedScopes)
                {
                    useUnnamedGroupedSuggestion(results, scope, results[0]);
                }
                useGroupedSuggestionIfNeeded(results, scope, results[0]);
                return results;
            }

            for (int i = 0; i < 2; i++)
            {
                StringBuilder combinedSuggestions = new StringBuilder();
                int suggestionIndex = i;
                processInnerScope(combinedSuggestions, scope.InnerLeftScope, suggestionIndex);
                processInnerScope(combinedSuggestions, scope.InnerMiddleScope, suggestionIndex);
                processInnerScope(combinedSuggestions, scope.InnerRightScope, suggestionIndex);

                Suggestion suggestion = new Suggestion(combinedSuggestions.ToString(), "Combined");
                list.Add(suggestion);
                if (useGroupsForUnnamedScopes)
                {
                    useUnnamedGroupedSuggestion(list, scope, suggestion);
                }
                useGroupedSuggestionIfNeeded(list, scope, list[0]);
                scope.VisitedByGroup = false;
            }

            scope.Suggestions = list;
            return list;
        }
 public void LearnAutomaticRules(Suggestion suggestion)
 {
     initAutoAdvisor();
     autoAdvisor.Learn(suggestion);
 }
        public virtual void Learn(string suggestionsList)
        {
            if (suggestionsList == string.Empty)
            {
                return;
            }

            string[] lines = Regex.Split(suggestionsList, "\n");
            foreach (string line in lines)
            {
                if (line == String.Empty)
                {
                    continue;
                }
                string[] splitted = Regex.Split(line, "\t\t");
                if (splitted.Length < 2)
                {
                    continue;
                }
                Suggestion suggestion = new Suggestion(splitted[0], splitted[1]);
                if (splitted.Length >= 3)
                {
                    try
                    {
                        suggestion.Probability = int.Parse(splitted[2]);
                    }
                    catch (Exception)
                    {
                        continue;
                    }
                }
                if (splitted.Length == 4)
                {
                    try
                    {
                        suggestion.Arity = splitted[3].Replace(Environment.NewLine, "");
                    }
                    catch (Exception)
                    {
                        continue;
                    }
                }
                try
                {
                    Learn(suggestion);
                }
                catch (Exception)
                {
                }
            }
        }
Beispiel #16
0
        private void useUnnamedGroupedSuggestion(List <Suggestion> results, Scope scope, Suggestion toReplace)
        {
            if (!(toReplace is UnNamedGroupedSuggestionDecorator))
            {
                UnNamedGroupedSuggestionDecorator groupedSug = new UnNamedGroupedSuggestionDecorator(toReplace, scope);

                for (int i = 0; i < results.Count; i++)
                {
                    if (results[i] == toReplace)
                    {
                        results.Remove(toReplace);
                        results.Insert(i, groupedSug);
                    }
                    return;
                }
            }
        }
Beispiel #17
0
        private void inferMultipleMatchesSuggestions(string input, List <Suggestion> matches, Suggestion possibility)
        {
            bool IsMatchingOneOrMore =
                possibility.RegexText.EndsWith(@"+$") ||
                possibility.RegexText.EndsWith(@"+?$");

            if (!(IsMatchingOneOrMore))
            {
                return;
            }
            int howManyChars = input.Length;

            string[] descWords = possibility.Description.Split(' ');
            string   lastWord  = descWords[descWords.Length - 1];

            inferMultiple(matches, possibility, howManyChars, lastWord);
            inferMultipleExplicitChar(input, matches, howManyChars, lastWord);
        }
        private bool isMultilineMatch(Suggestion possibility, string input)
        {
            try
            {
                Regex r = new Regex(possibility.RegexText, RegexOptions.Singleline);
                Match m = r.Match(input);
                return m.Success;

            }
            catch (Exception)
            {

                return false;

            }
        }
Beispiel #19
0
        private void useGroupedSuggestionIfNeeded(List <Suggestion> results, Scope scope, Suggestion toReplace)
        {
            if (scope.VisitedByGroup)
            {
                Console.Write("break here");
            }
            if (/*!scope.VisitedByGroup && */ scope.Name != string.Empty && !(toReplace is GroupedSuggestionDecorator))
            {
                scope.VisitedByGroup = true;
                GroupedSuggestionDecorator groupedSug = null;
                groupedSug = new GroupedSuggestionDecorator(toReplace, scope);


                for (int i = 0; i < results.Count; i++)
                {
                    if (results[i] == toReplace)
                    {
                        results.Remove(toReplace);
                        results.Insert(i, groupedSug);
                    }
                    return;
                }
            }
            if ((toReplace is GroupedSuggestionDecorator))
            {
                ((GroupedSuggestionDecorator)toReplace).GroupName = scope.Name;
            }
        }
 private bool matchAlreadyExists(Suggestion newSuggestion)
 {
     foreach (Suggestion existingSuggestions in possibleMatches)
     {
         if (existingSuggestions.RegexText == newSuggestion.RegexText)
         {
             return true;
         }
     }
     return false;
 }
 public UnNamedGroupedSuggestionDecorator(Suggestion inner, Scope namedScope)
     : base(inner)
 {
     scopeName = namedScope.Name;
     innersuggestion = inner;
     originalText = inner.RegexText;
 }
        private void useGroupedSuggestionIfNeeded(List<Suggestion> results, Scope scope, Suggestion toReplace)
        {
            if (scope.VisitedByGroup)
            {
                Console.Write("break here");
            }
            if (/*!scope.VisitedByGroup && */scope.Name != string.Empty && !(toReplace is GroupedSuggestionDecorator))
            {
                scope.VisitedByGroup = true;
                GroupedSuggestionDecorator groupedSug = null;
                groupedSug = new GroupedSuggestionDecorator(toReplace, scope);

                for (int i = 0; i < results.Count; i++)
                {
                    if (results[i] == toReplace)
                    {
                        results.Remove(toReplace);
                        results.Insert(i, groupedSug);
                    }
                    return;
                }
            }
            if ((toReplace is GroupedSuggestionDecorator))
            {
                ((GroupedSuggestionDecorator)toReplace).GroupName = scope.Name;
            }
        }
 public MultilineSuggestionDecorator(Suggestion sug)
     : base(sug)
 {
     m_sug.RegexMatchFlags.Add(MatchFlags.Multiline);
 }
        private void useUnnamedGroupedSuggestion(List<Suggestion> results, Scope scope, Suggestion toReplace)
        {
            if (!(toReplace is UnNamedGroupedSuggestionDecorator))
            {
                UnNamedGroupedSuggestionDecorator groupedSug = new UnNamedGroupedSuggestionDecorator(toReplace, scope);

                for (int i = 0; i < results.Count; i++)
                {
                    if (results[i] == toReplace)
                    {
                        results.Remove(toReplace);
                        results.Insert(i, groupedSug);
                    }
                    return;
                }
            }
        }
 public void GroupedSuggestion_Create_ContainsGoodRegex2()
 {
     Scope scope = new Scope("a", "Name");
     Suggestion s = new Suggestion(@"b", "desc");
     GroupedSuggestionDecorator decorator = new GroupedSuggestionDecorator(s, scope);
     Assert.AreEqual(@"(?<Name>b)", decorator.RegexText);
 }
Beispiel #26
0
 public SuggestionDecorator(Suggestion sug)
 {
     m_sug = sug;
 }