Ejemplo n.º 1
0
        private List<string> ExtractListOfWords(Paragraph paragraph)
        {
            var caretIsWithin = (this.CaretPosition != null &&
                                 this.CaretPosition.CompareTo(paragraph.ContentStart) >= 0 &&
                                 this.CaretPosition.CompareTo(paragraph.ContentEnd) <= 0);

            var list = new List<string>();

            if (caretIsWithin)
            {
                var beforeText = paragraph.GetTextBefore(this.CaretPosition);
                var afterText = paragraph.GetTextAfter(this.CaretPosition);
                // enter key was pressed
                if (beforeText == "" && afterText == "")
                {

                    if (this.DisableAddingWhiteSpacesOnEnter)
                    {
                        return null;
                    }
                    // add white space to the new line - same as the previous line
                    var prev = paragraph.PreviousBlock as Paragraph;
                    if (prev != null)
                    {
                        var previousLine = prev.GetText();
                        var spaces = new StringBuilder();
                        foreach (var ch in previousLine)
                        {
                            if (ch == ' ' || ch == '\t')
                            {
                                spaces.Append(ch);
                            }
                            else
                            {
                                break;
                            }
                        }
                        if (spaces.Length > 0)
                        {
                            beforeText = spaces.ToString();
                        }
                        else
                        {
                            return null;
                        }
                    }
                    else
                    {
                        return null;
                    }
                }
                // else

                if (beforeText != "") list.AddRange(this._paragraphProcessor.SplitWordsRegex.Split(beforeText));
                list.Add(null);
                if (afterText != "") list.AddRange(this._paragraphProcessor.SplitWordsRegex.Split(afterText));
            }
            else
            {
                var text = paragraph.GetText();
                var array = this._paragraphProcessor.SplitWordsRegex.Split(text);
                list.AddRange(array);
            }
            return list;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="textBox"></param>
        /// <param name="paragraph"></param>
        /// <returns></returns>
        private List<string> ExtractListOfWords(RichTextBox textBox, Paragraph paragraph)
        {
            // Check if caret position is within the word
            var caretIsWithin = (textBox.CaretPosition != null &&
                                 textBox.CaretPosition.CompareTo(paragraph.ContentStart) >= 0 &&
                                 textBox.CaretPosition.CompareTo(paragraph.ContentEnd) <= 0);

            var list = new List<string>();

            // If caret is within the paragraph, we need to break it up into Inline objects and format the line
            if (caretIsWithin) {

                var beforeText = paragraph.GetTextBefore(textBox.CaretPosition);
                var afterText = paragraph.GetTextAfter(textBox.CaretPosition);

                // If there's no text on either side of the caret, we're on an empty line
                if (beforeText == "" && afterText == "") {

                    if (this.DisableAddingWhiteSpacesOnEnter) {
                        return null;
                    }

                    // Add white space to the new line - same as the previous line
                    var prev = paragraph.PreviousBlock as Paragraph;
                    if (prev != null) {
                        var previousLine = prev.GetText();
                        var spaces = new StringBuilder();

                        foreach (var ch in previousLine) {
                            if (ch == ' ' || ch == '\t') {
                                spaces.Append(ch);
                            }
                            else {
                                break;
                            }
                        }

                        if (spaces.Length > 0) {
                            beforeText = spaces.ToString();
                        }
                        else {
                            return null;
                        }
                    }
                    else {
                        return null;
                    }
                }

                // If theres text before or after the caret, split the words and add them to the list
                if (beforeText != "") {
                    list.AddRange(this.syntaxProcessor.SplitWordsRegex.Split(beforeText));
                }

                // Add null representing the caret
                list.Add(null);

                if (afterText != "") {
                    list.AddRange(this.syntaxProcessor.SplitWordsRegex.Split(afterText));
                }
            }
            else {
                // If caret is not in the paragraph, just add it as-is.
                // It would already be formatted.
                var text = paragraph.GetText();
                var array = this.syntaxProcessor.SplitWordsRegex.Split(text);
                list.AddRange(array);
            }

            return list;
        }