// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team // // Permission is hereby granted, free of charge, to any person obtaining a copy of this // software and associated documentation files (the "Software"), to deal in the Software // without restriction, including without limitation the rights to use, copy, modify, merge, // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons // to whom the Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. using System; namespace Unity.CodeEditor.Document { /// /// A document representing a source code file for refactoring. /// Line and column counting starts at 1. /// Offset counting starts at 0. /// internal interface IDocument : ITextSource, IServiceProvider { /// /// Gets/Sets the text of the whole document.. /// new string Text { get; set; } // hides ITextSource.Text to add the setter /// /// This event is called directly before a change is applied to the document. /// /// /// It is invalid to modify the document within this event handler. /// Aborting the change (by throwing an exception) is likely to cause corruption of data structures /// that listen to the Changing and Changed events. /// event EventHandler TextChanging; /// /// This event is called directly after a change is applied to the document. /// /// /// It is invalid to modify the document within this event handler. /// Aborting the event handler (by throwing an exception) is likely to cause corruption of data structures /// that listen to the Changing and Changed events. /// event EventHandler TextChanged; /// /// This event is called after a group of changes is completed. /// /// event EventHandler ChangeCompleted; /// /// Gets the number of lines in the document. /// int LineCount { get; } /// /// Gets the document line with the specified number. /// /// The number of the line to retrieve. The first line has number 1. IDocumentLine GetLineByNumber(int lineNumber); /// /// Gets the document line that contains the specified offset. /// IDocumentLine GetLineByOffset(int offset); /// /// Gets the offset from a text location. /// /// int GetOffset(int line, int column); /// /// Gets the offset from a text location. /// /// int GetOffset(TextLocation location); /// /// Gets the location from an offset. /// /// TextLocation GetLocation(int offset); /// /// Inserts text. /// /// The offset at which the text is inserted. /// The new text. /// /// Anchors positioned exactly at the insertion offset will move according to their movement type. /// For AnchorMovementType.Default, they will move behind the inserted text. /// The caret will also move behind the inserted text. /// void Insert(int offset, string text); /// /// Inserts text. /// /// The offset at which the text is inserted. /// The new text. /// /// Anchors positioned exactly at the insertion offset will move according to their movement type. /// For AnchorMovementType.Default, they will move behind the inserted text. /// The caret will also move behind the inserted text. /// void Insert(int offset, ITextSource text); /// /// Inserts text. /// /// The offset at which the text is inserted. /// The new text. /// /// Anchors positioned exactly at the insertion offset will move according to the anchor's movement type. /// For AnchorMovementType.Default, they will move according to the movement type specified by this parameter. /// The caret will also move according to the parameter. /// void Insert(int offset, string text, AnchorMovementType defaultAnchorMovementType); /// /// Inserts text. /// /// The offset at which the text is inserted. /// The new text. /// /// Anchors positioned exactly at the insertion offset will move according to the anchor's movement type. /// For AnchorMovementType.Default, they will move according to the movement type specified by this parameter. /// The caret will also move according to the parameter. /// void Insert(int offset, ITextSource text, AnchorMovementType defaultAnchorMovementType); /// /// Removes text. /// /// Starting offset of the text to be removed. /// Length of the text to be removed. void Remove(int offset, int length); /// /// Replaces text. /// /// The starting offset of the text to be replaced. /// The length of the text to be replaced. /// The new text. void Replace(int offset, int length, string newText); /// /// Replaces text. /// /// The starting offset of the text to be replaced. /// The length of the text to be replaced. /// The new text. void Replace(int offset, int length, ITextSource newText); /// /// Make the document combine the following actions into a single /// action for undo purposes. /// void StartUndoableAction(); /// /// Ends the undoable action started with . /// void EndUndoableAction(); /// /// Creates an undo group. Dispose the returned value to close the undo group. /// /// An object that closes the undo group when Dispose() is called. IDisposable OpenUndoGroup(); /// /// Creates a new at the specified offset. /// /// ITextAnchor CreateAnchor(int offset); /// /// Gets the name of the file the document is stored in. /// Could also be a non-existent dummy file name or null if no name has been set. /// string FileName { get; } /// /// Fired when the file name of the document changes. /// event EventHandler FileNameChanged; } /// /// A line inside a . /// internal interface IDocumentLine : ISegment { /// /// Gets the length of this line, including the line delimiter. /// int TotalLength { get; } /// /// Gets the length of the line terminator. /// Returns 1 or 2; or 0 at the end of the document. /// int DelimiterLength { get; } /// /// Gets the number of this line. /// The first line has the number 1. /// int LineNumber { get; } /// /// Gets the previous line. Returns null if this is the first line in the document. /// IDocumentLine PreviousLine { get; } /// /// Gets the next line. Returns null if this is the last line in the document. /// IDocumentLine NextLine { get; } /// /// Gets whether the line was deleted. /// bool IsDeleted { get; } } /// /// Describes a change of the document text. /// This class is thread-safe. /// internal class TextChangeEventArgs : EventArgs { /// /// The offset at which the change occurs. /// internal int Offset { get; } /// /// The text that was removed. /// internal ITextSource RemovedText { get; } /// /// The number of characters removed. /// internal int RemovalLength => RemovedText.TextLength; /// /// The text that was inserted. /// internal ITextSource InsertedText { get; } /// /// The number of characters inserted. /// internal int InsertionLength => InsertedText.TextLength; /// /// Creates a new TextChangeEventArgs object. /// internal TextChangeEventArgs(int offset, string removedText, string insertedText) { if (offset < 0) throw new ArgumentOutOfRangeException(nameof(offset), offset, "offset must not be negative"); Offset = offset; RemovedText = removedText != null ? new StringTextSource(removedText) : StringTextSource.Empty; InsertedText = insertedText != null ? new StringTextSource(insertedText) : StringTextSource.Empty; } /// /// Creates a new TextChangeEventArgs object. /// internal TextChangeEventArgs(int offset, ITextSource removedText, ITextSource insertedText) { if (offset < 0) throw new ArgumentOutOfRangeException(nameof(offset), offset, "offset must not be negative"); Offset = offset; RemovedText = removedText ?? StringTextSource.Empty; InsertedText = insertedText ?? StringTextSource.Empty; } /// /// Gets the new offset where the specified offset moves after this document change. /// internal virtual int GetNewOffset(int offset, AnchorMovementType movementType = AnchorMovementType.Default) { if (offset >= Offset && offset <= Offset + RemovalLength) { if (movementType == AnchorMovementType.BeforeInsertion) return Offset; return Offset + InsertionLength; } if (offset > Offset) { return offset + InsertionLength - RemovalLength; } return offset; } /// /// Creates TextChangeEventArgs for the reverse change. /// internal virtual TextChangeEventArgs Invert() { return new TextChangeEventArgs(Offset, InsertedText, RemovedText); } } }