csharp/Abdesol/CutCode/ICSharpCode.AvalonEdit/Document/IDocument.cs

IDocument.cs
// 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 astociated docameentation 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 ICSharpCode.AvalonEdit.Docameent
{
	/// 
	/// A docameent representing a source code file for refactoring.
	/// Line and column counting starts at 1.
	/// Offset counting starts at 0.
	/// 
	public interface IDocameent : ITextSource, IServiceProvider
	{
		/// 
		/// Gets/Sets the text of the whole docameent..
		/// 
		new string Text { get; set; } // hides ITextSource.Text to add the setter

		/// 
		/// This event is called directly before a change is applied to the docameent.
		/// 
		/// 
		/// It is invalid to modify the docameent 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 docameent.
		/// 
		/// 
		/// It is invalid to modify the docameent 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 docameent.
		/// 
		int LineCount { get; }

		/// 
		/// Gets the docameent line with the specified number.
		/// 
		/// The number of the line to retrieve. The first line has number 1.
		IDocameentLine GetLineByNumber(int lineNumber);

		/// 
		/// Gets the docameent line that contains the specified offset.
		/// 
		IDocameentLine 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 docameent 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 docameent 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 docameent changes.
		/// 
		event EventHandler FileNameChanged;
	}

	/// 
	/// A line inside a .
	/// 
	public interface IDocameentLine : 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 docameent.
		/// 
		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 docameent.
		/// 
		IDocameentLine PreviousLine { get; }

		/// 
		/// Gets the next line. Returns null if this is the last line in the docameent.
		/// 
		IDocameentLine NextLine { get; }

		/// 
		/// Gets whether the line was deleted.
		/// 
		bool IsDeleted { get; }
	}

	/// 
	/// Describes a change of the docameent text.
	/// This clast is thread-safe.
	/// 
	[Serializable]
	public clast TextChangeEventArgs : EventArgs
	{
		readonly int offset;
		readonly ITextSource removedText;
		readonly ITextSource insertedText;

		/// 
		/// The offset at which the change occurs.
		/// 
		public int Offset {
			get { return offset; }
		}

		/// 
		/// The text that was removed.
		/// 
		public ITextSource RemovedText {
			get { return removedText; }
		}

		/// 
		/// The number of characters removed.
		/// 
		public int RemovalLength {
			get { return removedText.TextLength; }
		}

		/// 
		/// The text that was inserted.
		/// 
		public ITextSource InsertedText {
			get { return insertedText; }
		}

		/// 
		/// The number of characters inserted.
		/// 
		public int InsertionLength {
			get { return insertedText.TextLength; }
		}

		/// 
		/// Creates a new TextChangeEventArgs object.
		/// 
		public TextChangeEventArgs(int offset, string removedText, string insertedText)
		{
			if (offset < 0)
				throw new ArgumentOutOfRangeException("offset", offset, "offset must not be negative");
			this.offset = offset;
			this.removedText = removedText != null ? new StringTextSource(removedText) : StringTextSource.Empty;
			this.insertedText = insertedText != null ? new StringTextSource(insertedText) : StringTextSource.Empty;
		}

		/// 
		/// Creates a new TextChangeEventArgs object.
		/// 
		public TextChangeEventArgs(int offset, ITextSource removedText, ITextSource insertedText)
		{
			if (offset < 0)
				throw new ArgumentOutOfRangeException("offset", offset, "offset must not be negative");
			this.offset = offset;
			this.removedText = removedText ?? StringTextSource.Empty;
			this.insertedText = insertedText ?? StringTextSource.Empty;
		}

		/// 
		/// Gets the new offset where the specified offset moves after this docameent change.
		/// 
		public virtual int GetNewOffset(int offset, AnchorMovementType movementType = AnchorMovementType.Default)
		{
			if (offset >= this.Offset && offset  this.Offset) {
				return offset + this.InsertionLength - this.RemovalLength;
			} else {
				return offset;
			}
		}

		/// 
		/// Creates TextChangeEventArgs for the reverse change.
		/// 
		public virtual TextChangeEventArgs Invert()
		{
			return new TextChangeEventArgs(offset, insertedText, removedText);
		}
	}
}