Esempio n. 1
0
        void HandleTextReplacing(object sender, Core.Text.TextChangeEventArgs e)
        {
            var handler = TextChanged;

            if (handler != null)
            {
                lock (replaceLock) {
                    var oldText      = CurrentText;
                    var changes      = new Microsoft.CodeAnalysis.Text.TextChange[e.TextChanges.Count];
                    var changeRanges = new TextChangeRange[e.TextChanges.Count];
                    for (int i = 0; i < e.TextChanges.Count; ++i)
                    {
                        var c    = e.TextChanges[i];
                        var span = new TextSpan(c.Offset, c.RemovalLength);
                        changes[i]      = new Microsoft.CodeAnalysis.Text.TextChange(span, c.InsertedText.Text);
                        changeRanges[i] = new TextChangeRange(span, c.InsertionLength);
                    }
                    var newText = oldText.WithChanges(changes);
                    currentText = newText;
                    try {
                        handler(this, new Microsoft.CodeAnalysis.Text.TextChangeEventArgs(oldText, newText, changeRanges));
                    } catch (ArgumentException ae) {
                        LoggingService.LogWarning(ae.Message + " re opening " + editor.FileName + " as roslyn source text.");
                        workspace.InformDocumentClose(Id, editor.FileName);
                        Dispose();                          // 100% ensure that this object is disposed
                        if (workspace.GetDocument(Id) != null)
                        {
                            TypeSystemService.InformDocumentOpen(Id, editor);
                        }
                    } catch (Exception ex) {
                        LoggingService.LogError("Error while text replacing", ex);
                    }
                }
            }
        }
        void HandleTextReplacing(object sender, Core.Text.TextChangeEventArgs e)
        {
            var handler = TextChanged;

            if (handler != null)
            {
                lock (replaceLock) {
                    var oldText      = CurrentText;
                    var changes      = new Microsoft.CodeAnalysis.Text.TextChange[e.TextChanges.Count];
                    var changeRanges = new TextChangeRange[e.TextChanges.Count];
                    for (int i = 0; i < e.TextChanges.Count; ++i)
                    {
                        var c    = e.TextChanges[i];
                        var span = new TextSpan(c.Offset, c.RemovalLength);
                        changes[i]      = new Microsoft.CodeAnalysis.Text.TextChange(span, c.InsertedText.Text);
                        changeRanges[i] = new TextChangeRange(span, c.InsertionLength);
                    }
                    var newText = oldText.WithChanges(changes);
                    currentText = newText;
                    try {
                        handler(this, new Microsoft.CodeAnalysis.Text.TextChangeEventArgs(oldText, newText, changeRanges));
                    } catch (Exception ex) {
                        LoggingService.LogError("Error while text replacing", ex);
                    }
                }
            }
        }
Esempio n. 3
0
        static TextChange ChangeAtOffset(int offset, string text)
        {
            TextSpan   span   = new TextSpan(offset, 0);
            TextChange change = new TextChange(span, text);

            return(change);
        }
Esempio n. 4
0
        static TextChange ChangeFromBounds(int offset, int endOffset, string text)
        {
            TextSpan   span   = new TextSpan(offset, endOffset - offset);
            TextChange change = new TextChange(span, text);

            return(change);
        }
Esempio n. 5
0
        static void Apply_TrimTrailingWhitespace(
            TextEditor editor,
            IDocumentLine line,
            List <TextChange> changes)
        {
            int offset = line.EndOffset;

            for (; offset > line.Offset; --offset)
            {
                char c = editor.GetCharAt(offset - 1);

                if (!char.IsWhiteSpace(c))
                {
                    break;
                }
            }

            TextChange change = ChangeFromBounds(offset, line.EndOffset, string.Empty);

            if (change.Span.Length == 0)
            {
                return;
            }

            changes.Add(change);
        }
Esempio n. 6
0
        static TextSpan GetChangeSpan(SourceText inText, TextChange change, int offset)
        {
            var startLine = inText.Lines.GetLineFromPosition(change.Span.Start + offset);

            var maxEndOffset = Math.Min(inText.Length, change.Span.End + offset);
            var endLine      = inText.Lines.GetLineFromPosition(maxEndOffset);

            var changedLength = endLine.End - startLine.Start;

            return(new TextSpan(startLine.Start, changedLength));
        }
Esempio n. 7
0
        static void Apply_EndOfLine(
            TextEditor editor,
            FileConfiguration config,
            IDocumentLine line,
            List <TextChange> changes)
        {
            string eolMarker = null;

            switch (config.EndOfLine.Value)
            {
            case EndOfLine.CR:
                if (line.UnicodeNewline != UnicodeNewline.CR)
                {
                    eolMarker = "\r";
                }
                break;

            case EndOfLine.LF:
                if (line.UnicodeNewline != UnicodeNewline.LF)
                {
                    eolMarker = "\n";
                }
                break;

            case EndOfLine.CRLF:
                if (line.UnicodeNewline != UnicodeNewline.CRLF)
                {
                    eolMarker = "\r\n";
                }
                break;
            }
            if (eolMarker == null)
            {
                return;
            }

            TextChange change = ChangeFromBounds(line.EndOffset, line.EndOffsetIncludingDelimiter, eolMarker);

            changes.Add(change);
        }
Esempio n. 8
0
        static void Apply_InsertFinalNewline(
            TextEditor editor,
            FileConfiguration config,
            List <TextChange> changes)
        {
            IDocumentLine lastLine = editor.GetLine(editor.LineCount);

            if (config.InsertFinalNewline.Value)
            {
                if (lastLine.Length == 0)
                {
                    return;
                }

                string     newlineString = GetBestNewlineString(editor, config, lastLine);
                TextChange change        = ChangeAtOffset(lastLine.EndOffset, newlineString);
                changes.Add(change);
            }
            else
            {
                int offset = lastLine.EndOffset;
                for (int i = editor.LineCount; i > 0; --i)
                {
                    IDocumentLine currLine = editor.GetLine(i);
                    if (currLine.Length != 0)
                    {
                        offset = currLine.EndOffset;
                        break;
                    }
                }

                // remove all trailing empty lines in one change
                TextChange change = ChangeFromBounds(offset, lastLine.EndOffset, string.Empty);
                changes.Add(change);
            }
        }
Esempio n. 9
0
        void SetChangedEditorText(SourceText baseText, IEnumerable <TextChange> textChanges, TextChange currentChange, int offset)
        {
            var changedText = baseText.WithChanges(textChanges);

            changedEditor.Text = changedText.ToString();
            if (currentChange == default)
            {
                return;
            }

            var changeSpan = GetChangeSpan(changedText, currentChange, offset);

            changedEditor.CenterTo(currentChange.Span.Start);
            changedEditor.SelectionRange = new TextSegment(changeSpan.Start, changeSpan.Length);
        }