// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Extensions;
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer
{
///
/// Class to provide information about an edit
///
public class TextEdit : Range
{
///
/// 1-based line number on which the text, which needs to be replaced, starts.
///
public int StartLineNumber { get { return this.Start.Line; } }
///
/// 1-based offset on start line at which the text, which needs to be replaced, starts.
/// This includes the first character of the text.
///
public int StartColumnNumber { get { return this.Start.Column; } }
///
/// 1-based line number on which the text, which needs to be replace, ends.
///
public int EndLineNumber { get { return this.End.Line; } }
///
/// 1-based offset on end line at which the text, which needs to be replaced, ends.
/// This offset value is 1 more than the offset of the last character of the text.
///
public int EndColumnNumber { get { return this.End.Column; } }
///
/// The text that will replace the text bounded by the Line/Column number properties.
///
public string Text { get; }
public string[] Lines { get; }
///
/// Constructs a TextEdit object.
///
/// 1-based line number on which the text, which needs to be replaced, starts.
/// 1-based offset on start line at which the text, which needs to be replaced, starts. This includes the first character of the text.
/// 1-based line number on which the text, which needs to be replace, ends.
/// 1-based offset on end line at which the text, which needs to be replaced, ends. This offset value is 1 more than the offset of the last character of the text.
/// The text that will replace the text bounded by the Line/Column number properties.
public TextEdit(
int startLineNumber,
int startColumnNumber,
int endLineNumber,
int endColumnNumber,
string newText)
: base(startLineNumber, startColumnNumber, endLineNumber, endColumnNumber)
{
if (newText == null)
{
throw new ArgumentNullException(nameof(newText));
}
Text = newText;
Lines = Text.GetLines().ToArray();
}
///
/// Constructs a TextEdit object.
///
/// 1-based line number on which the text, which needs to be replaced, starts.
/// 1-based offset on start line at which the text, which needs to be replaced, starts. This includes the first character of the text.
/// 1-based line number on which the text, which needs to be replace, ends.
/// 1-based offset on end line at which the text, which needs to be replaced, ends. This offset value is 1 more than the offset of the last character of the text.
/// The contiguous lines that will replace the text bounded by the Line/Column number properties.
public TextEdit(
int startLineNumber,
int startColumnNumber,
int endLineNumber,
int endColumnNumber,
IEnumerable lines)
: base(startLineNumber, startColumnNumber, endLineNumber, endColumnNumber)
{
if (lines == null)
{
throw new ArgumentNullException(nameof(lines));
}
if (lines.Any(line => line == null))
{
throw new ArgumentException(
String.Format(CultureInfo.CurrentCulture,
Strings.TextEditNoNullItem), nameof(lines));
}
Lines = lines.ToArray();
Text = String.Join(Environment.NewLine, Lines);
}
}
}