Example #1
0
            private CamelCaseResult?TryConsumePatternOrMatchNextHump(
                int patternIndex, int humpIndex, bool contiguous, int chunkOffset)
            {
                var bestResult = default(CamelCaseResult?);

                var candidateHump = _candidateHumps[humpIndex];

                var maxPatternHumpLength   = _patternText.Length - patternIndex;
                var maxCandidateHumpLength = candidateHump.Length;

                var maxHumpMatchLength = Math.Min(maxPatternHumpLength, maxCandidateHumpLength);

                for (var possibleHumpMatchLength = 1; possibleHumpMatchLength <= maxHumpMatchLength; possibleHumpMatchLength++)
                {
                    if (!LowercaseSubstringsMatch(
                            _candidate, candidateHump.Start,
                            _patternText, patternIndex, possibleHumpMatchLength))
                    {
                        // Stop trying to consume once the pattern contents no longer matches
                        // against the current candidate hump.
                        break;
                    }

                    // The pattern substring 'f' has matched against 'F', or 'fi' has matched
                    // against 'Fi'.  recurse and let the rest of the pattern match the remainder
                    // of the candidate.

                    var resultOpt = TryMatch(
                        patternIndex + possibleHumpMatchLength, humpIndex + 1, contiguous, chunkOffset);

                    if (resultOpt == null)
                    {
                        // Didn't match.  Try the next longer pattern chunk.
                        continue;
                    }

                    var result = resultOpt.Value;
                    // If this is our first hump add a 'from start' bonus.
                    if (humpIndex == 0)
                    {
                        result = result.WithFromStart(true);
                    }

                    // If this is our last hump, add a 'to end' bonus.
                    if (humpIndex == _candidateHumps.GetCount() - 1)
                    {
                        result = result.WithToEnd(true);
                    }

                    // This is the span of the hump of the candidate we matched.
                    var matchSpanToAdd = new TextSpan(chunkOffset + candidateHump.Start, possibleHumpMatchLength);
                    if (UpdateBestResultIfBetter(result, ref bestResult, matchSpanToAdd))
                    {
                        // We found the best result so far.  We can stop immediately.
                        break;
                    }
                }

                return(bestResult);
            }
Example #2
0
        void UpdateLinkText(TextLink link, Microsoft.VisualStudio.Text.ITextEdit edit)
        {
            for (int i = 0; (i < link.Links.Count); ++i)
            {
                var s      = link.Links[i];
                int offset = s.Offset + baseOffset;
                if (offset < 0 || s.Length < 0 || offset + s.Length > Editor.Document.Length)
                {
                    // This should never happen since it implies a corrupted link/bad update following a text change.
                    continue;
                }

                var span = new Microsoft.VisualStudio.Text.Span(offset, s.Length);
                if (edit.Snapshot.GetText(span) != link.CurrentText)
                {
                    edit.Replace(span, link.CurrentText);
                }
            }
        }
Example #3
0
        /// <summary>
        /// This is used to check the spelling in the text box's content
        /// </summary>
        /// <param name="textToSplit">The text to split into words and check for misspellings</param>
        private void CheckSpelling(string textToSplit)
        {
            List <Match> rangeExclusions = null;
            TextSpan     errorSpan, deleteWordSpan, lastWord;
            string       actualWord, textToCheck;
            int          mnemonicPos;

            misspelledWords.Clear();

            // Always ignore URLs
            rangeExclusions = WordSplitter.Url.Matches(textToSplit).Cast <Match>().ToList();

            // Note the location of all XML elements if needed
            if (wordSplitter.Configuration.IgnoreXmlElementsInText)
            {
                rangeExclusions.AddRange(WordSplitter.XmlElement.Matches(textToSplit).Cast <Match>());
            }

            // Add exclusions from the configuration if any
            foreach (var exclude in wordSplitter.Configuration.ExclusionExpressions)
            {
                try
                {
                    rangeExclusions.AddRange(exclude.Matches(textToSplit).Cast <Match>());
                }
                catch (RegexMatchTimeoutException ex)
                {
                    // Ignore expression timeouts
                    System.Diagnostics.Debug.WriteLine(ex);
                }
            }

            lastWord = new TextSpan();

            foreach (var word in wordSplitter.GetWordsInText(textToSplit))
            {
                actualWord  = textToSplit.Substring(word.Start, word.Length);
                mnemonicPos = actualWord.IndexOf(wordSplitter.Mnemonic);

                if (mnemonicPos == -1)
                {
                    textToCheck = actualWord;
                }
                else
                {
                    textToCheck = actualWord.Substring(0, mnemonicPos) + actualWord.Substring(mnemonicPos + 1);
                }

                // Spell check the word if it looks like one and is not ignored
                if (wordSplitter.IsProbablyARealWord(textToCheck) && (rangeExclusions.Count == 0 ||
                                                                      !rangeExclusions.Any(match => word.Start >= match.Index &&
                                                                                           word.Start <= match.Index + match.Length - 1)))
                {
                    errorSpan = new TextSpan(word.Start, word.Length);

                    // Check for a doubled word
                    if (wordSplitter.Configuration.DetectDoubledWords && lastWord.Length != 0 &&
                        textToSplit.Substring(lastWord.Start, lastWord.Length).Equals(actualWord,
                                                                                      StringComparison.OrdinalIgnoreCase) && String.IsNullOrWhiteSpace(textToSplit.Substring(
                                                                                                                                                           lastWord.Start + lastWord.Length, word.Start - lastWord.Start - lastWord.Length)))
                    {
                        // Delete the whitespace ahead of it too
                        deleteWordSpan = new TextSpan(lastWord.Start + lastWord.Length,
                                                      word.Length + word.Start - lastWord.Start - lastWord.Length);

                        misspelledWords.Add(new FileMisspelling(errorSpan, deleteWordSpan, actualWord));

                        lastWord = word;
                        continue;
                    }

                    lastWord = word;

                    // If the word is not being ignored, perform the other checks
                    if (!dictionary.ShouldIgnoreWord(textToCheck))
                    {
                        if (!dictionary.IsSpelledCorrectly(textToCheck))
                        {
                            // Sometimes it flags a word as misspelled if it ends with "'s".  Try checking the
                            // word without the "'s".  If ignored or correct without it, don't flag it.  This
                            // appears to be caused by the definitions in the dictionary rather than Hunspell.
                            if (textToCheck.EndsWith("'s", StringComparison.OrdinalIgnoreCase))
                            {
                                textToCheck = textToCheck.Substring(0, textToCheck.Length - 2);

                                if (dictionary.ShouldIgnoreWord(textToCheck) ||
                                    dictionary.IsSpelledCorrectly(textToCheck))
                                {
                                    continue;
                                }

                                textToCheck += "'s";
                            }

                            // Some dictionaries include a trailing period on certain words such as "etc." which
                            // we don't include.  If the word is followed by a period, try it with the period to
                            // see if we get a match.  If so, consider it valid.
                            if (word.Start + word.Length < textToSplit.Length && textToSplit[word.Start + word.Length] == '.')
                            {
                                if (dictionary.ShouldIgnoreWord(textToCheck + ".") ||
                                    dictionary.IsSpelledCorrectly(textToCheck + "."))
                                {
                                    continue;
                                }
                            }

                            misspelledWords.Add(new FileMisspelling(errorSpan, actualWord));
                        }
                    }
                }
            }
        }
Example #4
0
 public CamelCaseResult WithAddedMatchedSpan(TextSpan value)
 {
     MatchedSpansInReverse?.Add(value);
     return(new CamelCaseResult(FromStart, Contiguous, ToEnd, MatchCount + 1, MatchedSpansInReverse, ChunkOffset));
 }
Example #5
0
 public ArraySlice(T[] array, TextSpan span) : this(array, span.Start, span.Length)
 {
 }