ReplaceSelectionWithText() 개인적인 메소드

private ReplaceSelectionWithText ( string newText ) : void
newText string
리턴 void
예제 #1
0
        private static void OnPaste(object target, ExecutedRoutedEventArgs args)
        {
            TextArea textArea = GetTextArea(target);

            if (textArea != null && textArea.Document != null)
            {
                IDataObject dataObject;
                try
                {
                    dataObject = Clipboard.GetDataObject();
                }
                catch (ExternalException)
                {
                    return;
                }
                if (dataObject == null)
                {
                    return;
                }

                var pastingEventArgs = new DataObjectPastingEventArgs(dataObject, false, DataFormats.UnicodeText);
                textArea.RaiseEvent(pastingEventArgs);
                if (pastingEventArgs.CommandCancelled)
                {
                    return;
                }

                string text = GetTextToPaste(pastingEventArgs, textArea);

                if (!string.IsNullOrEmpty(text))
                {
                    dataObject = pastingEventArgs.DataObject;
                    bool fullLine    = textArea.Options.CutCopyWholeLine && dataObject.GetDataPresent(LineSelectedType);
                    bool rectangular = dataObject.GetDataPresent(RectangleSelection.RectangularSelectionDataType);

                    if (fullLine)
                    {
                        DocumentLine currentLine = textArea.Document.GetLineByNumber(textArea.Caret.Line);
                        if (textArea.ReadOnlySectionProvider.CanInsert(currentLine.Offset))
                        {
                            textArea.Document.Insert(currentLine.Offset, text);
                        }
                    }
                    else if (rectangular && textArea.Selection.IsEmpty && !(textArea.Selection is RectangleSelection))
                    {
                        if (!RectangleSelection.PerformRectangularPaste(textArea, textArea.Caret.Position, text, false))
                        {
                            textArea.ReplaceSelectionWithText(text);
                        }
                    }
                    else
                    {
                        textArea.ReplaceSelectionWithText(text);
                    }
                }
                textArea.Caret.BringCaretToView();
                args.Handled = true;
            }
        }
예제 #2
0
        static void OnPaste(object target, ExecutedRoutedEventArgs args)
        {
            TextArea textArea = GetTextArea(target);

            if (textArea != null && textArea.Document != null)
            {
                IDataObject dataObject;
                try {
                    dataObject = Clipboard.GetDataObject();
                } catch (ExternalException) {
                    return;
                }
                if (dataObject == null)
                {
                    return;
                }
                Debug.WriteLine(dataObject.GetData(DataFormats.Html) as string);

                // convert text back to correct newlines for this document
                string newLine = TextUtilities.GetNewLineFromDocument(textArea.Document, textArea.Caret.Line);
                string text;
                try {
                    text = (string)dataObject.GetData(DataFormats.UnicodeText);
                    text = TextUtilities.NormalizeNewLines(text, newLine);
                } catch (OutOfMemoryException) {
                    return;
                }

                if (!string.IsNullOrEmpty(text))
                {
                    bool fullLine    = textArea.Options.CutCopyWholeLine && dataObject.GetDataPresent(LineSelectedType);
                    bool rectangular = dataObject.GetDataPresent(RectangleSelection.RectangularSelectionDataType);
                    if (fullLine)
                    {
                        DocumentLine currentLine = textArea.Document.GetLineByNumber(textArea.Caret.Line);
                        if (textArea.ReadOnlySectionProvider.CanInsert(currentLine.Offset))
                        {
                            textArea.Document.Insert(currentLine.Offset, text);
                        }
                    }
                    else if (rectangular && textArea.Selection.IsEmpty && !(textArea.Selection is RectangleSelection))
                    {
                        if (!RectangleSelection.PerformRectangularPaste(textArea, textArea.Caret.Position, text, false))
                        {
                            textArea.ReplaceSelectionWithText(text);
                        }
                    }
                    else
                    {
                        textArea.ReplaceSelectionWithText(text);
                    }
                }
                textArea.Caret.BringCaretToView();
                args.Handled = true;
            }
        }
예제 #3
0
        static void OnTab(object target, ExecutedRoutedEventArgs args)
        {
            TextArea textArea = GetTextArea(target);

            if (textArea != null && textArea.Document != null)
            {
                using (textArea.Document.RunUpdate()) {
                    if (textArea.Selection.IsMultiline(textArea.Document))
                    {
                        DocumentLine start = textArea.Document.GetLineByOffset(textArea.Selection.SurroundingSegment.Offset);
                        DocumentLine end   = textArea.Document.GetLineByOffset(textArea.Selection.SurroundingSegment.EndOffset);
                        while (true)
                        {
                            int offset = start.Offset;
                            if (textArea.ReadOnlySectionProvider.CanInsert(offset))
                            {
                                textArea.Document.Insert(offset, textArea.Options.IndentationString);
                            }
                            if (start == end)
                            {
                                break;
                            }
                            start = start.NextLine;
                        }
                    }
                    else
                    {
                        string indentationString = textArea.Options.GetIndentationString(textArea.Caret.Column);
                        textArea.ReplaceSelectionWithText(indentationString);
                    }
                }
                textArea.Caret.BringCaretToView();
                args.Handled = true;
            }
        }
예제 #4
0
        static void OnPaste(object target, ExecutedRoutedEventArgs args)
        {
            TextArea textArea = GetTextArea(target);

            if (textArea != null && textArea.Document != null)
            {
                Debug.WriteLine(Clipboard.GetText(TextDataFormat.Html));

                // convert text back to correct newlines for this document
                string newLine = TextUtilities.GetNewLineFromDocument(textArea.Document, textArea.Caret.Line);
                string text    = TextUtilities.NormalizeNewLines(Clipboard.GetText(), newLine);

                if (!string.IsNullOrEmpty(text))
                {
                    bool fullLine    = textArea.Options.CutCopyWholeLine && Clipboard.ContainsData(LineSelectedType);
                    bool rectangular = Clipboard.ContainsData(RectangleSelection.RectangularSelectionDataType);
                    if (fullLine)
                    {
                        DocumentLine currentLine = textArea.Document.GetLineByNumber(textArea.Caret.Line);
                        if (textArea.ReadOnlySectionProvider.CanInsert(currentLine.Offset))
                        {
                            textArea.Document.Insert(currentLine.Offset, text);
                        }
                    }
                    else if (rectangular && textArea.Selection.IsEmpty)
                    {
                        if (!RectangleSelection.PerformRectangularPaste(textArea, textArea.Caret.Offset, text, false))
                        {
                            textArea.ReplaceSelectionWithText(text);
                        }
                    }
                    else
                    {
                        textArea.ReplaceSelectionWithText(text);
                    }
                }
                textArea.Caret.BringCaretToView();
                args.Handled = true;
            }
        }
예제 #5
0
        static void OnTab(object target, ExecutedRoutedEventArgs args)
        {
            TextArea textArea = GetTextArea(target);

            if (textArea != null && textArea.Document != null)
            {
                using (textArea.Document.RunUpdate())
                {
                    if (textArea.Selection.IsMultiline)
                    {
                        var          segment = textArea.Selection.SurroundingSegment;
                        DocumentLine start   = textArea.Document.GetLineByOffset(segment.Offset);
                        DocumentLine end     = textArea.Document.GetLineByOffset(segment.EndOffset);
                        // don't include the last line if no characters on it are selected
                        if (start != end && end.Offset == segment.EndOffset)
                        {
                            end = end.PreviousLine;
                        }
                        DocumentLine current = start;
                        while (true)
                        {
                            int offset = current.Offset;
                            if (textArea.ReadOnlySectionProvider.CanInsert(offset))
                            {
                                textArea.Document.Replace(offset, 0, textArea.Options.IndentationString, OffsetChangeMappingType.KeepAnchorBeforeInsertion);
                            }
                            if (current == end)
                            {
                                break;
                            }
                            current = current.NextLine;
                        }
                    }
                    else
                    {
                        string indentationString = textArea.Options.GetIndentationString(textArea.Caret.Column);
                        textArea.ReplaceSelectionWithText(indentationString);
                    }
                }
                textArea.Caret.BringCaretToView();
                args.Handled = true;
            }
        }
예제 #6
0
 //--------------------------------------------------------------
 /// <inheritdoc/>
 public void Complete(TextArea textArea, ISegment completionSegment, EventArgs insertionRequestEventArgs)
 {
     if (!textArea.Selection.IsEmpty)
     {
         // ----- PasteMultiple:
         // By default the completion data does not affect the current selection.
         // But in TextEditor.OnPasteMultiple the completion should behave like
         // the Paste command.
         textArea.Document.Remove(completionSegment);
         textArea.ReplaceSelectionWithText(Text);
     }
     else
     {
         textArea.Document.Replace(completionSegment, Text);
     }
 }
예제 #7
0
        static void OnPaste(object target, ExecutedRoutedEventArgs args)
        {
            TextArea textArea = GetTextArea(target);

            if (textArea != null && textArea.Document != null)
            {
                IDataObject dataObject;
                try {
                    dataObject = Clipboard.GetDataObject();
                } catch (ExternalException) {
                    return;
                }
                if (dataObject == null)
                {
                    return;
                }

                var pastingEventArgs = new DataObjectPastingEventArgs(dataObject, false, DataFormats.UnicodeText);
                textArea.RaiseEvent(pastingEventArgs);
                if (pastingEventArgs.CommandCancelled)
                {
                    return;
                }

                dataObject = pastingEventArgs.DataObject;
                if (dataObject == null)
                {
                    return;
                }

                // convert text back to correct newlines for this document
                string newLine = TextUtilities.GetNewLineFromDocument(textArea.Document, textArea.Caret.Line);
                string text;
                try {
                    // Try retrieving the text as one of:
                    //  - the FormatToApply
                    //  - UnicodeText
                    //  - Text
                    // (but don't try the same format twice)
                    if (pastingEventArgs.FormatToApply != null && dataObject.GetDataPresent(pastingEventArgs.FormatToApply))
                    {
                        text = (string)dataObject.GetData(pastingEventArgs.FormatToApply);
                    }
                    else if (pastingEventArgs.FormatToApply != DataFormats.UnicodeText && dataObject.GetDataPresent(DataFormats.UnicodeText))
                    {
                        text = (string)dataObject.GetData(DataFormats.UnicodeText);
                    }
                    else if (pastingEventArgs.FormatToApply != DataFormats.Text && dataObject.GetDataPresent(DataFormats.Text))
                    {
                        text = (string)dataObject.GetData(DataFormats.Text);
                    }
                    else
                    {
                        return;                         // no text data format
                    }
                    text = TextUtilities.NormalizeNewLines(text, newLine);
                } catch (OutOfMemoryException) {
                    // may happen when trying to paste a huge string
                    return;
                }

                if (!string.IsNullOrEmpty(text))
                {
                    bool fullLine    = textArea.Options.CutCopyWholeLine && dataObject.GetDataPresent(LineSelectedType);
                    bool rectangular = dataObject.GetDataPresent(RectangleSelection.RectangularSelectionDataType);

                    if (fullLine)
                    {
                        DocumentLine currentLine = textArea.Document.GetLineByNumber(textArea.Caret.Line);
                        if (textArea.ReadOnlySectionProvider.CanInsert(currentLine.Offset))
                        {
                            textArea.Document.Insert(currentLine.Offset, text);
                        }
                    }
                    else if (rectangular && textArea.Selection.IsEmpty && !(textArea.Selection is RectangleSelection))
                    {
                        if (!RectangleSelection.PerformRectangularPaste(textArea, textArea.Caret.Position, text, false))
                        {
                            textArea.ReplaceSelectionWithText(text);
                        }
                    }
                    else
                    {
                        textArea.ReplaceSelectionWithText(text);
                    }
                }
                textArea.Caret.BringCaretToView();
                args.Handled = true;
            }
        }