Skip to content

Commit

Permalink
style: Clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
s2quake committed Dec 25, 2024
1 parent e757e53 commit 8e92394
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/JSSoft.Commands/CommandCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public CommandCollection(IEnumerable<ICommand> commands)
Add(command);
}

_commandList = new List<ICommand>(commands);
_commandList = [.. commands];
}

public static CommandCollection Empty { get; } = new() { IsLocked = true };
Expand Down
2 changes: 1 addition & 1 deletion src/JSSoft.Commands/CommandMethodBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private static CommandCollection CreateCommands(CommandMethodBase obj)
var query = from methodDescriptor in methodDescriptors
select CreateCommand(obj, methodDescriptor);
var commands = query.ToArray();
return new CommandCollection(commands);
return [.. commands];

static ICommand CreateCommand(
CommandMethodBase obj, CommandMethodDescriptor methodDescriptor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@

namespace JSSoft.Commands.Exceptions;

internal sealed class CommandDeclaringTypeNullException : CommandDefinitionException
internal sealed class CommandDeclaringTypeNullException(CommandMemberInfo memberInfo)
: CommandDefinitionException($"{memberInfo}' does not have a declaring type.", memberInfo)
{
public CommandDeclaringTypeNullException(CommandMemberInfo memberInfo)
: base($"{memberInfo}' does not have a declaring type.", memberInfo)
{
}
}
13 changes: 5 additions & 8 deletions src/JSSoft.Commands/Exceptions/CommandInvalidNameException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@

namespace JSSoft.Commands.Exceptions;

internal sealed class CommandInvalidNameException : CommandDefinitionException
{
public CommandInvalidNameException(CommandMemberInfo memberInfo, string name)
: base(
message: $"'{name}' is not a name that matches the regular expression pattern " +
internal sealed class CommandInvalidNameException(CommandMemberInfo memberInfo, string name)
: CommandDefinitionException(
message: $"'{name}' is not a name that matches the regular expression pattern " +
$"'{CommandUtility.NamePattern}'.",
memberInfo: memberInfo)
{
}
memberInfo: memberInfo)
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@

namespace JSSoft.Commands.Exceptions;

internal sealed class CommandInvalidShortNameException : CommandDefinitionException
internal sealed class CommandInvalidShortNameException(CommandMemberInfo memberInfo, char value)
: CommandDefinitionException(
message: $"'{value}' is an invalid short name. Only alphabetic characters can be used.",
memberInfo: memberInfo)
{
public CommandInvalidShortNameException(CommandMemberInfo memberInfo, char value)
: base(
message: $"'{value}' is an invalid short name. Only alphabetic characters can be used.",
memberInfo: memberInfo)
{
}
}
14 changes: 8 additions & 6 deletions src/JSSoft.Terminals/SystemTerminalHost.KeyBindings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ public partial class SystemTerminalHost
new KeyBinding(TerminalKey.RightArrow, (t) => t.Right()),
];

public static TerminalKeyBindingCollection WindowsKeyBindings { get; } = new(CommonKeyBindings)
{
public static TerminalKeyBindingCollection WindowsKeyBindings { get; } =
[
.. CommonKeyBindings,
new KeyBinding(TerminalKey.Enter, InputEnter),
new KeyBinding(TerminalModifiers.Control, TerminalKey.C, (t) => t.CancelInput()),
new KeyBinding(TerminalKey.Escape, (t) => t.Command = string.Empty, (t) => !t.IsPassword),
Expand All @@ -38,10 +39,11 @@ public partial class SystemTerminalHost
new KeyBinding(TerminalModifiers.Shift, TerminalKey.Tab, (t) => t.PrevCompletion(), (t) => !t.IsPassword),
new KeyBinding(TerminalModifiers.Control, TerminalKey.LeftArrow, (t) => PrevWord(t), (t) => !t.IsPassword),
new KeyBinding(TerminalModifiers.Control, TerminalKey.RightArrow, (t) => NextWord(t), (t) => !t.IsPassword),
};
];

public static TerminalKeyBindingCollection LinuxKeyBindings { get; } = new(CommonKeyBindings)
{
public static TerminalKeyBindingCollection LinuxKeyBindings { get; } =
[
.. CommonKeyBindings,
new KeyBinding(TerminalKey.Enter, InputEnter),
new KeyBinding(TerminalModifiers.Control, TerminalKey.C, (t) => t.CancelInput()),
new KeyBinding(TerminalKey.Escape, (t) => {}),
Expand All @@ -54,7 +56,7 @@ public partial class SystemTerminalHost
new KeyBinding(TerminalModifiers.Control, TerminalKey.L, (t) => t.Clear(), (t) => !t.IsPassword),
new KeyBinding(TerminalKey.Tab, (t) => t.NextCompletion(), (t) => !t.IsPassword),
new KeyBinding(TerminalModifiers.Shift, TerminalKey.Tab, (t) => t.PrevCompletion(), (t) => !t.IsPassword),
};
];

private static TerminalKeyBindingCollection GetDefaultKeyBindings()
{
Expand Down
21 changes: 12 additions & 9 deletions src/JSSoft.Terminals/TerminalKeyBindings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,19 @@ public static TerminalKeyBindingCollection GetDefaultBindings()
new TerminalKeyBinding(TerminalKey.Delete, (t) => t.Delete()),
];

public static readonly TerminalKeyBindingCollection MacOS = new(Common)
{
public static readonly TerminalKeyBindingCollection MacOS =
[
.. Common,
new TerminalKeyBinding(TerminalModifiers.Control, TerminalKey.E, (t) => t.MoveToLast()),
new TerminalKeyBinding(TerminalModifiers.Control, TerminalKey.A, (t) => t.MoveToFirst()),

new TerminalKeyBinding(TerminalKey.PageUp, (g) => g.Scroll.PageUp()),
new TerminalKeyBinding(TerminalKey.PageDown, (g) => g.Scroll.PageDown()),
};
];

public static readonly TerminalKeyBindingCollection Windows = new(Common)
{
public static readonly TerminalKeyBindingCollection Windows =
[
.. Common,
new TerminalKeyBinding(TerminalKey.Home, (t) => t.MoveToFirst()),
new TerminalKeyBinding(TerminalKey.End, (t) => t.MoveToLast()),

Expand All @@ -68,10 +70,11 @@ public static TerminalKeyBindingCollection GetDefaultBindings()
new TerminalKeyBinding(TerminalModifiers.Control, TerminalKey.Home, (g) => g.Scroll.ScrollToTop()),
new TerminalKeyBinding(TerminalModifiers.Control, TerminalKey.End, (g) => g.Scroll.ScrollToBottom()),
new TerminalKeyBinding(TerminalModifiers.Control, TerminalKey.A, (g) => g.Selections.SelectAll()),
};
];

public static readonly TerminalKeyBindingCollection Linux = new(Common)
{
public static readonly TerminalKeyBindingCollection Linux =
[
.. Common,
new TerminalKeyBinding(TerminalModifiers.Control, TerminalKey.E, t => t.MoveToLast()),
new TerminalKeyBinding(TerminalModifiers.Control, TerminalKey.A, t => t.MoveToFirst()),
new TerminalKeyBinding(TerminalKey.Home, (t) => t.MoveToFirst()),
Expand All @@ -83,5 +86,5 @@ public static TerminalKeyBindingCollection GetDefaultBindings()
new TerminalKeyBinding(TerminalModifiers.Control | TerminalModifiers.Shift, TerminalKey.UpArrow, (g) => g.Scroll.LineUp()),
new TerminalKeyBinding(TerminalModifiers.Shift, TerminalKey.Home, (g) => g.Scroll.ScrollToTop()),
new TerminalKeyBinding(TerminalModifiers.Shift, TerminalKey.End, (g) => g.Scroll.ScrollToBottom()),
};
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public RunCommand()
itemList.AddRange(descriptors);
}

_descriptorByInstance = new(itemList);
_descriptorByInstance = itemList.ToDictionary(item => item.Key, item => item.Value);
_descriptors = new(GetType(), [.. _descriptorByInstance.Keys]);
}

Expand Down
2 changes: 1 addition & 1 deletion test/JSSoft.Tests.Shared/RandomUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public static List<T> List<T>(Func<T> generator)
items[i] = generator();
}

return new List<T>(items);
return [.. items];
}

public static ImmutableArray<T> ImmutableArray<T>(Func<T> generator)
Expand Down

0 comments on commit 8e92394

Please sign in to comment.