cleanup

alphons <alphons@heijden.com> 3 Sep 2024, 20:35
7b9b868d00e9f11de583d040ac2a1b3ff9053a42
6 files changed
  • src/InteractiveReadLine/ConsoleReadLine.cs
  • src/InteractiveReadLine/LineState.cs
  • src/InteractiveReadLine/ReadLineConfig.cs
  • src/InteractiveReadLine/ReadLineHandler.cs
  • src/InteractiveReadLine/Tokenizing/CommonLexers.cs
  • src/InteractiveReadLine/Tokenizing/TokenizedLine.cs
diff --git a/src/InteractiveReadLine/ConsoleReadLine.cs b/src/InteractiveReadLine/ConsoleReadLine.cs
index c2a3aea..01dd548 100644
--- a/src/InteractiveReadLine/ConsoleReadLine.cs
+++ b/src/InteractiveReadLine/ConsoleReadLine.cs
@@ -14,15 +14,15 @@ namespace InteractiveReadLine
/// </summary>
public class ConsoleReadLine : IReadLineProvider
{
- private readonly IConsole _console;
- private FormattedText _lastWrittenText;
- private int _lastWrittenCursor;
+ private readonly IConsole console;
+ private FormattedText lastWrittenText;
+ private int lastWrittenCursor;
- private int _startingRow;
- private int _startingCol;
+ private int startingRow;
+ private int startingCol;
public ConsoleReadLine(IConsole console = null)
{
- _console = console ?? new SystemConsoleWrapper();
+ this.console = console ?? new SystemConsoleWrapper();
this.Start();
}
@@ -32,7 +32,7 @@ namespace InteractiveReadLine
/// <returns></returns>
public ConsoleKeyInfo ReadKey()
{
- return _console.ReadKey();
+ return console.ReadKey();
}
/// <summary>
@@ -61,9 +61,9 @@ namespace InteractiveReadLine
// First, we should determine what the new line needs to look like. If the new line is
// longer than the old line, we will write the new line exactly. If it's shorter, we'll
// need to pad it out with empty characters
- var writeText = (totalText.Length >= _lastWrittenText.Length)
+ var writeText = (totalText.Length >= lastWrittenText.Length)
? totalText
- : totalText + new string(' ', _lastWrittenText.Length - totalText.Length);
+ : totalText + new string(' ', lastWrittenText.Length - totalText.Length);
// Sweep through each character in the text to write and determine if an edit needs to be
// made (or does the FormattedChar match what was last written at this position?)
@@ -74,7 +74,7 @@ namespace InteractiveReadLine
for (int i = 0; i < writeText.Length; i++)
{
- if (i < _lastWrittenText.Length && writeText[i].Equals(_lastWrittenText[i]))
+ if (i < lastWrittenText.Length && writeText[i].Equals(lastWrittenText[i]))
{
if (edit != null)
{
@@ -113,30 +113,30 @@ namespace InteractiveReadLine
{
int left = this.ColOffset(e.Item1);
- int top = this.RowOffset(e.Item1) + _startingRow;
+ int top = this.RowOffset(e.Item1) + startingRow;
- if (left != _console.CursorLeft)
- _console.CursorLeft = left;
- if (top != _console.CursorTop)
- _console.CursorTop = top;
+ if (left != console.CursorLeft)
+ console.CursorLeft = left;
+ if (top != console.CursorTop)
+ console.CursorTop = top;
- _console.Write(e.Item2);
+ console.Write(e.Item2);
}
- _lastWrittenText = totalText;
+ lastWrittenText = totalText;
// Check if we shifted down the buffer. In certain cases, if we reach the end of the buffer
// height and we skip a line, the System.Console shifts everything up, and our starting row
// will effectively be less than where we started. It will never move down.
- var writtenRowOffset = this.RowOffset(_lastWrittenText.Length);
- if (writtenRowOffset + _startingRow >= _console.BufferHeight)
+ var writtenRowOffset = this.RowOffset(lastWrittenText.Length);
+ if (writtenRowOffset + startingRow >= console.BufferHeight)
{
- _startingRow = _console.BufferHeight - writtenRowOffset - 1;
+ startingRow = console.BufferHeight - writtenRowOffset - 1;
}
- _console.CursorTop = _startingRow + this.RowOffset(cursorPos);
- _console.CursorLeft = this.ColOffset(cursorPos);
- _lastWrittenCursor = cursorPos;
+ console.CursorTop = startingRow + this.RowOffset(cursorPos);
+ console.CursorLeft = this.ColOffset(cursorPos);
+ lastWrittenCursor = cursorPos;
}
/// <summary>
@@ -146,15 +146,15 @@ namespace InteractiveReadLine
/// <param name="text">The text to write to the console, a newline char will be added automatically</param>
public void InsertText(FormattedText text)
{
- var currentText = _lastWrittenText;
- var currentCursor = _lastWrittenCursor;
+ var currentText = lastWrittenText;
+ var currentCursor = lastWrittenCursor;
SetText(text, text.Length);
- _console.WriteLine("");
+ console.WriteLine("");
- _startingRow = _console.CursorTop;
- _lastWrittenText = "";
- _lastWrittenCursor = 0;
+ startingRow = console.CursorTop;
+ lastWrittenText = "";
+ lastWrittenCursor = 0;
SetText(currentText, currentCursor);
}
@@ -167,20 +167,20 @@ namespace InteractiveReadLine
private void Start()
{
// _console.WriteLine(string.Empty);
- _startingRow = _console.CursorTop;
- _startingCol = 1 + _console.CursorLeft; // AAB
+ startingRow = console.CursorTop;
+ startingCol = 1 + console.CursorLeft; // AAB
// _console.CursorLeft = 0;
- _lastWrittenText = string.Empty;
+ lastWrittenText = string.Empty;
}
private void Finish()
{
- _console.WriteLine(string.Empty);
+ console.WriteLine(string.Empty);
}
- private int ColOffset(int length) => _startingCol + length % _console.BufferWidth;
+ private int ColOffset(int length) => startingCol + length % console.BufferWidth;
- private int RowOffset(int length) => (length - this.ColOffset(length)) / _console.BufferWidth;
+ private int RowOffset(int length) => (length - this.ColOffset(length)) / console.BufferWidth;
/// <summary>
/// Provides a convenient static method of calling the ReadLine method on the System.Console
diff --git a/src/InteractiveReadLine/LineState.cs b/src/InteractiveReadLine/LineState.cs
index 5f33601..eeb95c5 100644
--- a/src/InteractiveReadLine/LineState.cs
+++ b/src/InteractiveReadLine/LineState.cs
@@ -7,30 +7,24 @@ namespace InteractiveReadLine
/// <summary>
/// Contains the text and cursor position data produced by a user's interaction with the IReadLineProvider.
/// </summary>
- public class LineState : IEquatable<LineState>
+ public class LineState(string text, int cursor) : IEquatable<LineState>
{
/// <summary>
/// A string can implicitly be converted to a LineState, however the cursor position will be 0 by default.
/// </summary>
- public static implicit operator LineState(string s) => new LineState(s, 0);
-
- public LineState(string text, int cursor)
- {
- Text = text;
- Cursor = cursor;
- }
+ public static implicit operator LineState(string s) => new(s, 0);
- /// <summary>
- /// Gets the cursor position as an integer offset from the first character
- /// </summary>
- public int Cursor { get; }
+ /// <summary>
+ /// Gets the cursor position as an integer offset from the first character
+ /// </summary>
+ public int Cursor { get; } = cursor;
- /// <summary>
- /// Gets the string contents of the line of text
- /// </summary>
- public string Text { get; }
+ /// <summary>
+ /// Gets the string contents of the line of text
+ /// </summary>
+ public string Text { get; } = text;
- public bool Equals(LineState other)
+ public bool Equals(LineState other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
diff --git a/src/InteractiveReadLine/ReadLineConfig.cs b/src/InteractiveReadLine/ReadLineConfig.cs
index d4b61c4..99eda1d 100644
--- a/src/InteractiveReadLine/ReadLineConfig.cs
+++ b/src/InteractiveReadLine/ReadLineConfig.cs
@@ -17,7 +17,7 @@ namespace InteractiveReadLine
{
private ReadLineConfig()
{
- this.KeyBehaviors = new Dictionary<KeyId, Action<IKeyBehaviorTarget>>();
+ this.KeyBehaviors = [];
}
/// <summary>
diff --git a/src/InteractiveReadLine/ReadLineHandler.cs b/src/InteractiveReadLine/ReadLineHandler.cs
index 2ed74b0..9888665 100644
--- a/src/InteractiveReadLine/ReadLineHandler.cs
+++ b/src/InteractiveReadLine/ReadLineHandler.cs
@@ -13,54 +13,39 @@ namespace InteractiveReadLine
/// provider, determines what the current line of text being edited should be and where the cursor should be positioned.
/// It pushes out the text to the provider, which also serves as the view.
/// </summary>
- public class ReadLineHandler : IKeyBehaviorTarget
+ public class ReadLineHandler(IReadLineProvider provider, ReadLineConfig config = null) : IKeyBehaviorTarget
{
- private readonly IReadLineProvider _provider;
- private readonly ReadLineConfig _config;
- private int _cursorPos;
- private int _autoCompleteIndex;
- private TokenizedLine _autoCompleteTokens;
- private bool _autoCompleteCalled = false;
- private string[] _autoCompleteSuggestions;
+ private readonly ReadLineConfig config = config ?? ReadLineConfig.Basic;
+ private int cursorPos = 0;
+ private int autoCompleteIndex = int.MinValue;
+ private TokenizedLine autoCompleteTokens;
+ private bool autoCompleteCalled = false;
+ private string[] autoCompleteSuggestions = null;
- private int _historyIndex;
- private LineState _preHistoryState;
+ private int historyIndex = config?.History?.Any() == true ? config.History.Count : 0;
+ private LineState preHistoryState;
- private bool _finishTrigger = false;
+ private bool finishTrigger = false;
- public ReadLineHandler(IReadLineProvider provider, ReadLineConfig config=null)
- {
- _config = config ?? ReadLineConfig.Basic;
- _provider = provider;
- TextBuffer = new StringBuilder();
- _cursorPos = 0;
-
- _autoCompleteIndex = int.MinValue;
- _autoCompleteSuggestions = null;
+ /// <summary>
+ /// Gets the current LineState representation of the text and the cursor position
+ /// </summary>
+ public LineState LineState => new(TextBuffer.ToString(), cursorPos);
- // The history index should start one element past the length of the current history
- _historyIndex = config?.History?.Any() == true ? config.History.Count : 0;
- }
+ /// <inheritdoc />
+ public StringBuilder TextBuffer { get; } = new StringBuilder();
- /// <summary>
- /// Gets the current LineState representation of the text and the cursor position
- /// </summary>
- public LineState LineState => new LineState(TextBuffer.ToString(), _cursorPos);
-
- /// <inheritdoc />
- public StringBuilder TextBuffer { get; }
-
- /// <inheritdoc />
- public int CursorPosition
+ /// <inheritdoc />
+ public int CursorPosition
{
- get => _cursorPos;
+ get => cursorPos;
set
{
- _cursorPos = value;
- if (_cursorPos > TextBuffer.Length)
- _cursorPos = TextBuffer.Length;
- if (_cursorPos < 0)
- _cursorPos = 0;
+ cursorPos = value;
+ if (cursorPos > TextBuffer.Length)
+ cursorPos = TextBuffer.Length;
+ if (cursorPos < 0)
+ cursorPos = 0;
}
}
@@ -70,12 +55,12 @@ namespace InteractiveReadLine
/// <inheritdoc />
public void AutoCompleteNext()
{
- if (_autoCompleteIndex >= 0)
+ if (autoCompleteIndex >= 0)
{
// Next index
- _autoCompleteIndex++;
- if (_autoCompleteIndex >= _autoCompleteSuggestions.Length)
- _autoCompleteIndex = 0;
+ autoCompleteIndex++;
+ if (autoCompleteIndex >= autoCompleteSuggestions.Length)
+ autoCompleteIndex = 0;
this.SetAutoCompleteText();
}
@@ -86,12 +71,12 @@ namespace InteractiveReadLine
/// <inheritdoc />
public void AutoCompletePrevious()
{
- if (_autoCompleteIndex >= 0)
+ if (autoCompleteIndex >= 0)
{
// Previous index
- _autoCompleteIndex--;
- if (_autoCompleteIndex < 0)
- _autoCompleteIndex = _autoCompleteSuggestions.Length - 1;
+ autoCompleteIndex--;
+ if (autoCompleteIndex < 0)
+ autoCompleteIndex = autoCompleteSuggestions.Length - 1;
this.SetAutoCompleteText();
}
@@ -103,37 +88,37 @@ namespace InteractiveReadLine
/// <inheritdoc />
public void InsertText(FormattedText text)
{
- _provider.InsertText(text);
+ provider.InsertText(text);
}
/// <inheritdoc />
public TokenizedLine GetTextTokens()
{
- return _config.Lexer?.Invoke(this.LineState);
+ return config.Lexer?.Invoke(this.LineState);
}
public void HistoryNext()
{
// If there is no history, we don't need to do anything
- if (_config.History?.Any() != true)
+ if (config.History?.Any() != true)
return;
// If we're at the end of the history (including the entered text) we do nothing
- if (_historyIndex == _config.History.Count)
+ if (historyIndex == config.History.Count)
return;
// Otherwise we increment the history index and set the current text buffer based
// on whether or not we still have another history element
- _historyIndex++;
+ historyIndex++;
this.TextBuffer.Clear();
- if (_historyIndex == _config.History.Count)
+ if (historyIndex == config.History.Count)
{
- this.TextBuffer.Append(_preHistoryState.Text);
- this.CursorPosition = _preHistoryState.Cursor;
+ this.TextBuffer.Append(preHistoryState.Text);
+ this.CursorPosition = preHistoryState.Cursor;
}
else
{
- this.TextBuffer.Append(_config.History[_historyIndex]);
+ this.TextBuffer.Append(config.History[historyIndex]);
this.CursorPosition = this.TextBuffer.Length;
}
}
@@ -141,22 +126,22 @@ namespace InteractiveReadLine
public void HistoryPrevious()
{
// If there is no history, we don't need to do anything
- if (_config.History?.Any() != true)
+ if (config.History?.Any() != true)
return;
- if (_historyIndex == 0)
+ if (historyIndex == 0)
return;
// Check if we're about to leave entered text to go backwards in the history. If so
// we want to store it first.
- if (_historyIndex == _config.History.Count)
+ if (historyIndex == config.History.Count)
{
- _preHistoryState = new LineState(this.LineState.Text, this.LineState.Cursor);
+ preHistoryState = new LineState(this.LineState.Text, this.LineState.Cursor);
}
- _historyIndex--;
+ historyIndex--;
this.TextBuffer.Clear();
- this.TextBuffer.Append(_config.History[_historyIndex]);
+ this.TextBuffer.Append(config.History[historyIndex]);
this.CursorPosition = this.TextBuffer.Length;
}
@@ -177,12 +162,12 @@ namespace InteractiveReadLine
// for the next key.
while (true)
{
- this.ReceivedKey = _provider.ReadKey();
+ this.ReceivedKey = provider.ReadKey();
// We will need to check if the line state (text & cursor position) is altered by the
// key behavior which will be run, so we store the current state now
var previousState = this.LineState;
- _autoCompleteCalled = false;
+ autoCompleteCalled = false;
// See if there's a specific behavior which should be mapped to this key,
// and if so, run it instead of checking the insert/enter behaviors
@@ -193,24 +178,24 @@ namespace InteractiveReadLine
}
else
{
- _config.DefaultKeyBehavior?.Invoke(this);
+ config.DefaultKeyBehavior?.Invoke(this);
}
// Check if the Finish behavior was called, indicating that we can exit this method
// and return the contents of the text buffer to the caller
- if (_finishTrigger)
+ if (finishTrigger)
break;
// If the text contents or the cursor have changed at all, and we weren't currently
// doing autocomplete, we need to invalidate the auto-completion information
- if ((!previousState.Equals(this.LineState)) && !_autoCompleteCalled)
+ if ((!previousState.Equals(this.LineState)) && !autoCompleteCalled)
this.InvalidateAutoComplete();
this.UpdateDisplay();
}
// If there is a delegate to update the history, invoke it now
- _config.UpdateHistory?.Invoke(TextBuffer.ToString());
+ config.UpdateHistory?.Invoke(TextBuffer.ToString());
return TextBuffer.ToString();
}
@@ -222,14 +207,14 @@ namespace InteractiveReadLine
private void UpdateDisplay()
{
// Finally, if we have an available formatter, we can get a display format from here
- var display = new LineDisplayState(string.Empty, TextBuffer.ToString(), string.Empty, _cursorPos);
+ var display = new LineDisplayState(string.Empty, TextBuffer.ToString(), string.Empty, cursorPos);
- if (_config.FormatterFromLine != null)
- display = _config.FormatterFromLine.Invoke(LineState);
- else if (_config.FormatterFromTokens != null && _config.Lexer != null)
- display = _config.FormatterFromTokens(GetTextTokens());
+ if (config.FormatterFromLine != null)
+ display = config.FormatterFromLine.Invoke(LineState);
+ else if (config.FormatterFromTokens != null && config.Lexer != null)
+ display = config.FormatterFromTokens(GetTextTokens());
- _provider.SetDisplay(display);
+ provider.SetDisplay(display);
}
/// <summary>
@@ -240,13 +225,13 @@ namespace InteractiveReadLine
private Action<IKeyBehaviorTarget> GetKeyAction(ConsoleKeyInfo info)
{
var charKey = new KeyId(info.KeyChar);
- if (_config.KeyBehaviors.ContainsKey(charKey))
- return _config.KeyBehaviors[charKey];
+ if (config.KeyBehaviors.TryGetValue(charKey, out Action<IKeyBehaviorTarget> value1))
+ return value1;
var key = new KeyId(info.Key, (info.Modifiers & ConsoleModifiers.Control) != 0,
(info.Modifiers & ConsoleModifiers.Alt) != 0, (info.Modifiers & ConsoleModifiers.Shift) != 0);
- if (_config.KeyBehaviors.ContainsKey(key))
- return _config.KeyBehaviors[key];
+ if (config.KeyBehaviors.TryGetValue(key, out Action<IKeyBehaviorTarget> value2))
+ return value2;
return null;
}
@@ -260,25 +245,25 @@ namespace InteractiveReadLine
/// </summary>
private void StartAutoComplete()
{
- if (!_config.CanAutoComplete)
+ if (!config.CanAutoComplete)
return;
- _autoCompleteTokens = _config.Lexer(new LineState(TextBuffer.ToString(), _cursorPos));
- if (_autoCompleteTokens.CursorToken == null)
+ autoCompleteTokens = config.Lexer(new LineState(TextBuffer.ToString(), cursorPos));
+ if (autoCompleteTokens.CursorToken == null)
return;
- _autoCompleteSuggestions = _config.AutoCompletion(_autoCompleteTokens) ?? Array.Empty<string>();
+ autoCompleteSuggestions = config.AutoCompletion(autoCompleteTokens) ?? [];
- if (_autoCompleteTokens.Text != TextBuffer.ToString())
+ if (autoCompleteTokens.Text != TextBuffer.ToString())
{
TextBuffer.Clear();
- TextBuffer.Append(_autoCompleteTokens.Text);
- CursorPosition = _autoCompleteTokens.Cursor;
+ TextBuffer.Append(autoCompleteTokens.Text);
+ CursorPosition = autoCompleteTokens.Cursor;
}
- if (_autoCompleteSuggestions.Any())
+ if (autoCompleteSuggestions.Length != 0)
{
- _autoCompleteIndex = 0;
+ autoCompleteIndex = 0;
SetAutoCompleteText();
}
@@ -291,9 +276,9 @@ namespace InteractiveReadLine
/// </summary>
private void InvalidateAutoComplete()
{
- _autoCompleteIndex = Int32.MinValue;
- _autoCompleteTokens = null;
- _autoCompleteSuggestions = null;
+ autoCompleteIndex = Int32.MinValue;
+ autoCompleteTokens = null;
+ autoCompleteSuggestions = null;
}
/// <summary>
@@ -302,22 +287,22 @@ namespace InteractiveReadLine
/// </summary>
private void SetAutoCompleteText()
{
- if (!_config.CanAutoComplete || _autoCompleteTokens == null || _autoCompleteIndex < 0)
+ if (!config.CanAutoComplete || autoCompleteTokens == null || autoCompleteIndex < 0)
return;
- _autoCompleteCalled = true;
- _autoCompleteTokens.CursorToken.Text = _autoCompleteSuggestions[_autoCompleteIndex];
- _autoCompleteTokens.CursorToken.Cursor = _autoCompleteTokens.CursorToken.Text.Length;
+ autoCompleteCalled = true;
+ autoCompleteTokens.CursorToken.Text = autoCompleteSuggestions[autoCompleteIndex];
+ autoCompleteTokens.CursorToken.Cursor = autoCompleteTokens.CursorToken.Text.Length;
TextBuffer.Clear();
- TextBuffer.Append(_autoCompleteTokens.Text);
- CursorPosition = _autoCompleteTokens.Cursor;
+ TextBuffer.Append(autoCompleteTokens.Text);
+ CursorPosition = autoCompleteTokens.Cursor;
}
/// <summary>
/// Causes the ReadLine handler to finish, returning the contents of the text buffer
/// </summary>
- public void Finish() => _finishTrigger = true;
+ public void Finish() => finishTrigger = true;
}
}
\ No newline at end of file
diff --git a/src/InteractiveReadLine/Tokenizing/CommonLexers.cs b/src/InteractiveReadLine/Tokenizing/CommonLexers.cs
index 9845554..b240cf9 100644
--- a/src/InteractiveReadLine/Tokenizing/CommonLexers.cs
+++ b/src/InteractiveReadLine/Tokenizing/CommonLexers.cs
@@ -1,8 +1,6 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
+
using System.Text;
-using System.Text.RegularExpressions;
+
using Lexer = System.Func<InteractiveReadLine.LineState, InteractiveReadLine.Tokenizing.TokenizedLine>;
using RegexList = System.Collections.Generic.List<InteractiveReadLine.Tokenizing.RegexTokenDef>;
@@ -10,7 +8,7 @@ namespace InteractiveReadLine.Tokenizing
{
public static class CommonLexers
{
- public static RegexList Regex => new RegexList();
+ public static RegexList Regex => new();
/// <summary>
/// Adds a new token type to the end of the regular expression token list. The token must begin with
@@ -48,7 +46,7 @@ namespace InteractiveReadLine.Tokenizing
/// <returns></returns>
public static Lexer ToLexer(this RegexList list)
{
- if (!list.Any())
+ if (list.Count == 0)
throw new ArgumentException("The RegexTokenDefs list must not be empty");
return lineState =>
diff --git a/src/InteractiveReadLine/Tokenizing/TokenizedLine.cs b/src/InteractiveReadLine/Tokenizing/TokenizedLine.cs
index 354c24d..f854c07 100644
--- a/src/InteractiveReadLine/Tokenizing/TokenizedLine.cs
+++ b/src/InteractiveReadLine/Tokenizing/TokenizedLine.cs
@@ -23,7 +23,7 @@ namespace InteractiveReadLine.Tokenizing
public TokenizedLine()
{
- _tokens = new List<Token>();
+ _tokens = [];
}
/// <summary>
@@ -108,7 +108,7 @@ namespace InteractiveReadLine.Tokenizing
{
var newToken = new Token(text, isHidden, this, typeCode);
- if (_tokens.Any())
+ if (_tokens.Count != 0)
{
_tokens.Last().Next = newToken;
newToken.Previous = _tokens.Last();
@@ -131,24 +131,13 @@ namespace InteractiveReadLine.Tokenizing
/// independent from a TokenizedLine, this class is hidden from consumers and exposed only through the
/// IToken interface.
/// </remarks>
- private class Token : IToken
+ private class Token(string text, bool isHidden, TokenizedLine parent, int typeCode) : IToken
{
- private readonly TokenizedLine _parent;
- private string _text;
+ public int TypeCode { get; } = typeCode;
- public Token(string text, bool isHidden, TokenizedLine parent, int typeCode)
+ public string Text
{
- _parent = parent;
- TypeCode = typeCode;
- _text = text;
- this.IsHidden = isHidden;
- }
-
- public int TypeCode { get; }
-
- public string Text
- {
- get => _text;
+ get => text;
set
{
int? cursorMove = null;
@@ -162,16 +151,16 @@ namespace InteractiveReadLine.Tokenizing
{
// Check if the cursor is after this token, and if so adjust it accordingly
var beforeLen = TextBefore().Length;
- if (beforeLen + _text.Length - 1 < _parent.Cursor)
+ if (beforeLen + text.Length - 1 < parent.Cursor)
{
- var delta = value.Length - _text.Length;
- cursorMove = _parent.Cursor + delta;
+ var delta = value.Length - text.Length;
+ cursorMove = parent.Cursor + delta;
}
}
- _text = value;
+ text = value;
if (cursorMove != null)
- _parent.Cursor = (int) cursorMove;
+ parent.Cursor = (int) cursorMove;
}
}
@@ -180,10 +169,10 @@ namespace InteractiveReadLine.Tokenizing
get
{
int lengthBefore = this.TextBefore().Length;
- if (_parent.Cursor < lengthBefore)
+ if (parent.Cursor < lengthBefore)
return null;
- int offset = _parent.Cursor - lengthBefore;
+ int offset = parent.Cursor - lengthBefore;
if (offset == Text.Length && Next?.IsHidden == false)
{
@@ -199,7 +188,7 @@ namespace InteractiveReadLine.Tokenizing
if (value == null || value < 0 || value > Text.Length)
return;
- _parent.Cursor = this.TextBefore().Length + (int) value;
+ parent.Cursor = this.TextBefore().Length + (int) value;
}
}
@@ -217,15 +206,14 @@ namespace InteractiveReadLine.Tokenizing
public Token FirstNonHidden => this.First.ThisOrNextIfHidden();
- public bool IsHidden { get; set; }
+ public bool IsHidden { get; set; } = isHidden;
- public int? DistanceTo(IToken other, bool ignoreHidden = false)
+ public int? DistanceTo(IToken other, bool ignoreHidden = false)
{
- var token = other as Token;
- if (token == null)
- return null;
+ if (other is not Token token)
+ return null;
- var gap = Next?.ForwardTo(token, new List<Token>());
+ var gap = Next?.ForwardTo(token, []);
if (ignoreHidden)
return gap?.Where(x => !x.IsHidden).Count();
else
@@ -256,7 +244,7 @@ namespace InteractiveReadLine.Tokenizing
pointer = pointer.Next;
}
- return tokens.ToArray();
+ return [.. tokens];
}
private string TextBefore()