Skip to content

Commit

Permalink
Merge pull request #4 from Cysharp/hotfix/FixSave
Browse files Browse the repository at this point in the history
Fix the problem of not saving when adding an item.
  • Loading branch information
mayuki authored May 28, 2021
2 parents cb4e002 + b958e1a commit 3c43c96
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
Expand All @@ -18,40 +19,56 @@ public class AddAnalyzerReferenceFeature : CsprojModifierFeatureBase
public override void Initialize()
{
var settings = CsprojModifierSettings.Instance;
_reorderableListAdditionalAddAnalyzerProjects = new ReorderableList(settings.AddAnalyzerReferencesAdditionalProjects, typeof(string), draggable: true, displayHeader: false, displayAddButton: true, displayRemoveButton: true)
// WORKAROUND: https://issuetracker.unity3d.com/issues/missingmethodexception-when-adding-elements-to-reorderablelist-with-string-type
var addAnalyzerReferencesAdditionalProjects = new List<ValueTuple<string>>(settings.AddAnalyzerReferencesAdditionalProjects.Select(x => ValueTuple.Create(x)));
_reorderableListAdditionalAddAnalyzerProjects = new ReorderableList(addAnalyzerReferencesAdditionalProjects, typeof(ValueTuple<string>), draggable: true, displayHeader: false, displayAddButton: true, displayRemoveButton: true)
{
drawNoneElementCallback = rect => EditorGUI.LabelField(rect, "Assembly-CSharp.csproj and Assembly-CSharp-Editor.csproj are always targeted."),
drawElementCallback = ((rect, index, active, focused) =>
{
var selectedItem = settings.AddAnalyzerReferencesAdditionalProjects[index];
using (var editScope = new EditorGUI.ChangeCheckScope())
{
var selectedItem = addAnalyzerReferencesAdditionalProjects[index];

rect.height -= 4;
rect.y += 2;
rect.height -= 4;
rect.y += 2;

const int buttonBrowseWidth = 32;
const int controlGap = 4;
const int buttonBrowseWidth = 32;
const int controlGap = 4;

rect.width -= controlGap + buttonBrowseWidth;
selectedItem = EditorGUI.TextField(rect, selectedItem);
rect.width -= controlGap + buttonBrowseWidth;
selectedItem = ValueTuple.Create(EditorGUI.TextField(rect, selectedItem.Item1));

rect.x += rect.width + controlGap;
rect.width = buttonBrowseWidth;
if (GUI.Button(rect, "..."))
{
var selectedFilePath = EditorUtility.OpenFilePanelWithFilters(
"Add Additional Project",
Path.GetDirectoryName(Application.dataPath),
new[] { "C# Project File (*.csproj)", "csproj", "All files", "*" }
);
if (!string.IsNullOrWhiteSpace(selectedFilePath))
rect.x += rect.width + controlGap;
rect.width = buttonBrowseWidth;
if (GUI.Button(rect, "..."))
{
var selectedFilePath = EditorUtility.OpenFilePanelWithFilters(
"Add Additional Project",
Path.GetDirectoryName(Application.dataPath),
new[] { "C# Project File (*.csproj)", "csproj", "All files", "*" }
);
if (!string.IsNullOrWhiteSpace(selectedFilePath))
{
selectedItem = ValueTuple.Create(PathEx.MakeRelative(Application.dataPath, selectedFilePath));
GUI.changed = true;
}
}

if (editScope.changed)
{
selectedItem = PathEx.MakeRelative(Application.dataPath, selectedFilePath);
addAnalyzerReferencesAdditionalProjects[index] = selectedItem;
settings.AddAnalyzerReferencesAdditionalProjects = addAnalyzerReferencesAdditionalProjects.Select(x => x.Item1).ToList();
settings.Save();
}
}

settings.AddAnalyzerReferencesAdditionalProjects[index] = selectedItem;
}),
onChangedCallback = (list) => settings.Save(),
onChangedCallback = (list) =>
{
settings.AddAnalyzerReferencesAdditionalProjects = addAnalyzerReferencesAdditionalProjects.Select(x => x.Item1).ToList();
settings.Save();
},
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
Expand All @@ -24,75 +25,101 @@ public override void Initialize()
{
drawElementCallback = ((rect, index, active, focused) =>
{
var selectedItem = settings.AdditionalImports[index];
using (var editScope = new EditorGUI.ChangeCheckScope())
{
var selectedItem = settings.AdditionalImports[index];

rect.height -= 4;
rect.y += 2;
rect.height -= 4;
rect.y += 2;

const int buttonBrowseWidth = 32;
const int buttonPositionWidth = 96;
const int controlGap = 4;
const int buttonBrowseWidth = 32;
const int buttonPositionWidth = 96;
const int controlGap = 4;

rect.width -= controlGap + buttonBrowseWidth + controlGap + buttonPositionWidth;
selectedItem.Path = EditorGUI.TextField(rect, selectedItem.Path);
rect.width -= controlGap + buttonBrowseWidth + controlGap + buttonPositionWidth;
selectedItem.Path = EditorGUI.TextField(rect, selectedItem.Path);

rect.x += rect.width + controlGap;
rect.width = buttonBrowseWidth;
if (GUI.Button(rect, "..."))
{
var selectedFilePath = EditorUtility.OpenFilePanelWithFilters(
"Add Additional Project",
Path.GetDirectoryName(Application.dataPath),
new[] { "MSBuild Project File (*.props;*.target)", "props,target", "All files", "*" }
);
if (!string.IsNullOrWhiteSpace(selectedFilePath))
rect.x += rect.width + controlGap;
rect.width = buttonBrowseWidth;
if (GUI.Button(rect, "..."))
{
selectedItem.Path = PathEx.MakeRelative(Application.dataPath, selectedFilePath);
var selectedFilePath = EditorUtility.OpenFilePanelWithFilters(
"Add Additional Project",
Path.GetDirectoryName(Application.dataPath),
new[] { "MSBuild Project File (*.props;*.target)", "props,target", "All files", "*" }
);
if (!string.IsNullOrWhiteSpace(selectedFilePath))
{
selectedItem.Path = PathEx.MakeRelative(Application.dataPath, selectedFilePath);
}
}
}

rect.x += rect.width + controlGap;
rect.width = buttonPositionWidth;
selectedItem.Position = (ImportProjectPosition)EditorGUI.EnumPopup(rect, selectedItem.Position);
rect.x += rect.width + controlGap;
rect.width = buttonPositionWidth;
selectedItem.Position = (ImportProjectPosition)EditorGUI.EnumPopup(rect, selectedItem.Position);

if (editScope.changed)
{
settings.Save();
}
}
}),
onChangedCallback = (list) => settings.Save(),
onChangedCallback = (list) =>
{
settings.Save();
},
};


_reorderableListAdditionalImportsAdditionalProjects = new ReorderableList(settings.AdditionalImportsAdditionalProjects, typeof(string), draggable: true, displayHeader: false, displayAddButton: true, displayRemoveButton: true)
// WORKAROUND: https://issuetracker.unity3d.com/issues/missingmethodexception-when-adding-elements-to-reorderablelist-with-string-type
var additionalImportsAdditionalProjects = new List<ValueTuple<string>>(settings.AdditionalImportsAdditionalProjects.Select(x => ValueTuple.Create(x)));
_reorderableListAdditionalImportsAdditionalProjects = new ReorderableList(additionalImportsAdditionalProjects, typeof(ValueTuple<string>), draggable: true, displayHeader: false, displayAddButton: true, displayRemoveButton: true)
{
drawNoneElementCallback = rect => EditorGUI.LabelField(rect, "Assembly-CSharp.csproj and Assembly-CSharp-Editor.csproj are always targeted."),
drawElementCallback = ((rect, index, active, focused) =>
{
var selectedItem = settings.AdditionalImportsAdditionalProjects[index];
using (var editScope = new EditorGUI.ChangeCheckScope())
{
var selectedItem = additionalImportsAdditionalProjects[index];

rect.height -= 4;
rect.y += 2;
rect.height -= 4;
rect.y += 2;

const int buttonBrowseWidth = 32;
const int controlGap = 4;
const int buttonBrowseWidth = 32;
const int controlGap = 4;

rect.width -= controlGap + buttonBrowseWidth;
selectedItem = EditorGUI.TextField(rect, selectedItem);
rect.width -= controlGap + buttonBrowseWidth;
selectedItem = ValueTuple.Create(EditorGUI.TextField(rect, selectedItem.Item1));

rect.x += rect.width + controlGap;
rect.width = buttonBrowseWidth;
if (GUI.Button(rect, "..."))
{
var selectedFilePath = EditorUtility.OpenFilePanelWithFilters(
"Add Additional Project",
Path.GetDirectoryName(Application.dataPath),
new[] { "C# Project File (*.csproj)", "csproj", "All files", "*" }
);
if (!string.IsNullOrWhiteSpace(selectedFilePath))
rect.x += rect.width + controlGap;
rect.width = buttonBrowseWidth;
if (GUI.Button(rect, "..."))
{
selectedItem = PathEx.MakeRelative(Application.dataPath, selectedFilePath);
var selectedFilePath = EditorUtility.OpenFilePanelWithFilters(
"Add Additional Project",
Path.GetDirectoryName(Application.dataPath),
new[] { "C# Project File (*.csproj)", "csproj", "All files", "*" }
);
if (!string.IsNullOrWhiteSpace(selectedFilePath))
{
selectedItem = ValueTuple.Create(PathEx.MakeRelative(Application.dataPath, selectedFilePath));
GUI.changed = true;
}
}

if (editScope.changed)
{
additionalImportsAdditionalProjects[index] = selectedItem;
settings.AdditionalImportsAdditionalProjects = additionalImportsAdditionalProjects.Select(x => x.Item1).ToList();
settings.Save();
}
}

settings.AdditionalImportsAdditionalProjects[index] = selectedItem;
}),
onChangedCallback = (list) => settings.Save(),
onChangedCallback = (list) =>
{
settings.AdditionalImportsAdditionalProjects = additionalImportsAdditionalProjects.Select(x => x.Item1).ToList();
settings.Save();
},
};
}

Expand Down

0 comments on commit 3c43c96

Please sign in to comment.