Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public IDictionaryEnumerator GetMetadataEnumerator()
_mergedMetadata = metaData;
}

return _mergedMetadata.GetEnumerator();
return _mergedMetadata?.GetEnumerator();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you enable nullability on this file or is it a big change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its a big change I think. I'd rather have it done in another PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@halgab / @gpetrou did either of you want to tackle null annotating ResourceCodeDomSerializer.SerializationResourceManager.cs?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was waiting for #9099 to be merged to tackle CodeDomSerializer and then derived classes, but I can take care of this one file after this PR merges for sure

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it is: #9457

}

/// <summary>
Expand Down Expand Up @@ -392,8 +392,7 @@ private Dictionary<string, object> GetResourceSet(CultureInfo culture)
{
Debug.Assert(culture is not null, "null parameter");
Dictionary<string, object> resourceSet = null;
object objRs = ResourceTable[culture];
if (objRs is null)
if (!ResourceTable.TryGetValue(culture, out Dictionary<string, object> objRs))
{
IResourceService resSvc = (IResourceService)_manager.GetService(typeof(IResourceService));
TraceIf(TraceLevel.Error, resSvc is null, "IResourceService is not available. We will not be able to load resources.");
Expand Down Expand Up @@ -423,7 +422,7 @@ private Dictionary<string, object> GetResourceSet(CultureInfo culture)
}
else
{
resourceSet = objRs as Dictionary<string, object>;
resourceSet = objRs;
if (resourceSet is null)
{
// the resourceSets hash table may contain our "this" pointer as a sentinel value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ public void Insert(int index, Glyph value)
/// </summary>
public void Remove(Glyph value)
{
List.Remove(value);
// Its possible that the value doesn't exist in the list in case of an `Undo` operation
if (List.Contains(value))
Copy link
Contributor Author

@elachlan elachlan Jul 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was because remove was throwing exceptions. But the exceptions were caught further up. Up to the team if they are happy with the exception.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@elachlan, Did you find a scenario where we are trying to call remove for a glyph that wasn't added to collection?

This change should be good.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it would have been when deleting and then performing an "undo" (I think). Do you want a debug.assert for when the value isn't in the list?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it is a valid scenario, i would put a comment about it. Debug assert will be annoying for a valid scenario.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added comment.

{
List.Remove(value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,21 @@ internal SplitterPanel? Selected
{
if (_selectedPanel is not null)
{
var panelDesigner1 = (SplitterPanelDesigner)_designerHost!.GetDesigner(_selectedPanel)!;
panelDesigner1.Selected = false;
if (_designerHost!.GetDesigner(_selectedPanel) is SplitterPanelDesigner panelDesigner1)
{
panelDesigner1.Selected = false;
}

_selectedPanel = null;
}

if (value is not null)
{
var panelDesigner = (SplitterPanelDesigner)_designerHost!.GetDesigner(value)!;
_selectedPanel = value;
panelDesigner.Selected = true;
if (_designerHost!.GetDesigner(value) is SplitterPanelDesigner panelDesigner)
{
panelDesigner.Selected = true;
}
}
}
}
Expand All @@ -126,7 +131,7 @@ public override ICollection AssociatedComponents
List<Control> components = new();
foreach (SplitterPanel panel in _splitContainer!.Controls)
{
components.AddRange((IEnumerable<Control>)panel.Controls);
components.AddRange(panel.Controls.Cast<Control>());
}

return components;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System.ComponentModel;
using System.ComponentModel.Design;

Expand Down Expand Up @@ -36,7 +34,7 @@ public StringCollectionForm(CollectionEditor editor)
HookEvents();
}

private void Edit1_keyDown(object sender, KeyEventArgs e)
private void Edit1_keyDown(object? sender, KeyEventArgs e)
{
if (e.KeyCode != Keys.Escape)
{
Expand All @@ -47,13 +45,13 @@ private void Edit1_keyDown(object sender, KeyEventArgs e)
e.Handled = true;
}

private void StringCollectionEditor_HelpButtonClicked(object sender, CancelEventArgs e)
private void StringCollectionEditor_HelpButtonClicked(object? sender, CancelEventArgs e)
{
e.Cancel = true;
_editor.ShowHelp();
}

private void Form_HelpRequested(object sender, HelpEventArgs e)
private void Form_HelpRequested(object? sender, HelpEventArgs e)
{
_editor.ShowHelp();
}
Expand All @@ -69,6 +67,11 @@ private void HookEvents()
/// NOTE: The following code is required by the form designer.
/// It can be modified using the form editor. Do not modify it using the code editor.
/// </summary>
[MemberNotNull(nameof(_instruction))]
[MemberNotNull(nameof(_textEntry))]
[MemberNotNull(nameof(_okButton))]
[MemberNotNull(nameof(_cancelButton))]
[MemberNotNull(nameof(_overarchingLayoutPanel))]
private void InitializeComponent()
{
ComponentResourceManager resources = new ComponentResourceManager(typeof(StringCollectionEditor));
Expand Down Expand Up @@ -135,79 +138,53 @@ private void InitializeComponent()
/// <summary>
/// Commits the changes to the editor.
/// </summary>
private void OKButton_click(object sender, EventArgs e)
private void OKButton_click(object? sender, EventArgs e)
{
char[] delims = new char[] { '\n' };
char[] trims = new char[] { '\r' };
// Split the text into array of lines.
string[] lines = _textEntry.Text.Split('\n');

string[] strings = _textEntry.Text.Split(delims);
object[] curItems = Items;
// Remove trailing carriage return characters.
for (int i = 0; i < lines.Length; i++)
{
lines[i] = lines[i].TrimEnd('\r');
}

int nItems = strings.Length;
for (int i = 0; i < nItems; i++)
// Check if the content has changed.
if (lines.Length != Items.Length)
{
strings[i] = strings[i].Trim(trims);
UpdateItems(lines);
return;
}

bool dirty = true;
if (nItems == curItems.Length)
for (int i = 0; i < lines.Length; ++i)
{
int i;
for (i = 0; i < nItems; ++i)
if (!lines[i].Equals(Items[i]?.ToString()))
{
if (!strings[i].Equals((string)curItems[i]))
{
break;
}
UpdateItems(lines);
return;
}

if (i == nItems)
dirty = false;
}

if (!dirty)
{
DialogResult = DialogResult.Cancel;
return;
}
DialogResult = DialogResult.Cancel;

// If the final line is blank, we don't want to create an item from it
if (strings.Length > 0 && strings[strings.Length - 1].Length == 0)
void UpdateItems(string[] newLines)
{
nItems--;
}
// If the last line is empty, we don't want to create an item from it.
if (newLines[^1].Length == 0)
{
Array.Resize(ref newLines, newLines.Length - 1);
}

object[] values = new object[nItems];
for (int i = 0; i < nItems; i++)
{
values[i] = strings[i];
// Assign newLines to Items.
Items = newLines;
}

Items = values;
}

/// <summary>
/// This is called when the value property in the CollectionForm has changed.
/// In it you should update your user interface to reflect the current value.
/// </summary>
protected override void OnEditValueChanged()
{
object[] items = Items;
string text = string.Empty;

for (int i = 0; i < items.Length; i++)
{
if (items[i] is string)
{
text += (string)items[i];
if (i != items.Length - 1)
{
text += "\r\n";
}
}
}

_textEntry.Text = text;
}
=> _textEntry.Text = string.Join(Environment.NewLine, Items);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public Entry(object item)
}

public object Item { get; set; }

public override string? ToString()
{
return Item.ToString();
Copy link
Contributor

@RussKie RussKie Jul 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❗ This can NRE

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be Item.ToString()!?

Copy link
Contributor Author

@elachlan elachlan Jul 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait, Item.ToString() ?? string.Empty;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has nullable types enabled and object is marked as non-nullable. So it shouldn't NRE?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has nullable types enabled and object is marked as non-nullable. So it shouldn't NRE?

Item = null! 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we know for sure Item can't be a null then I retract my comment :)

}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<PackageLicenseExpression>CPOL</PackageLicenseExpression>
<PackageProjectUrl>https://www.codeproject.com/Articles/24385/Have-a-Great-DesignTime-Experience-with-a-Powerful</PackageProjectUrl>
<SuppressLicenseValidation>true</SuppressLicenseValidation>
<EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,29 +371,30 @@ private void InitServices()
public void DoAction(string command)
{
if (string.IsNullOrEmpty(command))
{
return;
}

IMenuCommandService ims = GetService(typeof(IMenuCommandService)) as IMenuCommandService;

try
{
switch (command.ToUpper())
CommandID commandId = command.ToUpper() switch
{
"CUT" => StandardCommands.Cut,
"COPY" => StandardCommands.Copy,
"PASTE" => StandardCommands.Paste,
"DELETE" => StandardCommands.Delete,
_ => null,
};

if (commandId is not null)
{
case "CUT":
ims.GlobalInvoke(StandardCommands.Cut);
break;
case "COPY":
ims.GlobalInvoke(StandardCommands.Copy);
break;
case "PASTE":
ims.GlobalInvoke(StandardCommands.Paste);
break;
case "DELETE":
ims.GlobalInvoke(StandardCommands.Delete);
break;
default:
// do nothing;
break;
MenuCommand menuCommand = ims?.FindCommand(commandId);
if (menuCommand?.Enabled is true)
{
menuCommand.Invoke();
}
}
}
catch (Exception ex)
Expand Down