Skip to content

Commit 171b240

Browse files
Merge pull request #283 from reduckted/feature/save-attributes-to-user-file
Allowed attributes to be stored in a project's user file
2 parents 4b75617 + 9aba3f9 commit 171b240

File tree

1 file changed

+45
-4
lines changed
  • src/toolkit/Community.VisualStudio.Toolkit.Shared/Solution

1 file changed

+45
-4
lines changed

src/toolkit/Community.VisualStudio.Toolkit.Shared/Solution/Project.cs

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,22 @@ public async Task SaveAsync()
102102
/// <summary>
103103
/// Tries to set an attribute in the project file for the item.
104104
/// </summary>
105-
public async Task<bool> TrySetAttributeAsync(string name, string value)
105+
public Task<bool> TrySetAttributeAsync(string name, string value)
106+
{
107+
return TrySetAttributeAsync(name, value, ProjectStorageType.ProjectFile);
108+
}
109+
110+
/// <summary>
111+
/// Tries to set an attribute in the project file for the item, storing the value in the specified storage type.
112+
/// </summary>
113+
public async Task<bool> TrySetAttributeAsync(string name, string value, ProjectStorageType storageType)
106114
{
107115
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
108116
GetItemInfo(out IVsHierarchy hierarchy, out _, out _);
109117

110118
if (hierarchy is IVsBuildPropertyStorage storage)
111119
{
112-
storage.SetPropertyValue(name, "", (uint)_PersistStorageType.PST_PROJECT_FILE, value);
120+
storage.SetPropertyValue(name, "", ToPersistStorageType(storageType), value);
113121
return true;
114122
}
115123

@@ -120,20 +128,38 @@ public async Task<bool> TrySetAttributeAsync(string name, string value)
120128
/// Tries to retrieve an attribute value from the project file for the item.
121129
/// </summary>
122130
/// <returns><see langword="null"/> if the attribute doesn't exist.</returns>
123-
public async Task<string?> GetAttributeAsync(string name)
131+
public Task<string?> GetAttributeAsync(string name)
132+
{
133+
return GetAttributeAsync(name, ProjectStorageType.ProjectFile);
134+
}
135+
136+
/// <summary>
137+
/// Tries to retrieve an attribute value from the project file for the item, getting the value from the specfied storage type.
138+
/// </summary>
139+
/// <returns><see langword="null"/> if the attribute doesn't exist.</returns>
140+
public async Task<string?> GetAttributeAsync(string name, ProjectStorageType storageType)
124141
{
125142
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
126143
GetItemInfo(out IVsHierarchy hierarchy, out _, out _);
127144

128145
if (hierarchy is IVsBuildPropertyStorage storage)
129146
{
130-
storage.GetPropertyValue(name, "", (uint)_PersistStorageType.PST_PROJECT_FILE, out string? value);
147+
storage.GetPropertyValue(name, "", ToPersistStorageType(storageType), out string? value);
131148
return value;
132149
}
133150

134151
return null;
135152
}
136153

154+
private static uint ToPersistStorageType(ProjectStorageType type)
155+
{
156+
return (uint)(type switch
157+
{
158+
ProjectStorageType.UserFile => _PersistStorageType.PST_USER_FILE,
159+
_ => _PersistStorageType.PST_PROJECT_FILE
160+
});
161+
}
162+
137163
/// <summary>
138164
/// Determines whether the project is loaded.
139165
/// </summary>
@@ -194,4 +220,19 @@ private async Task RefreshAsync(IVsSolution solution, Guid guid)
194220
Update(await hierarchy.ToHierarchyItemAsync(itemId));
195221
}
196222
}
223+
224+
/// <summary>
225+
/// Defines the type of file that project data is stored in.
226+
/// </summary>
227+
public enum ProjectStorageType
228+
{
229+
/// <summary>
230+
/// The <c>.csproj</c> file (or <c>.vbproj</c>, etc.)
231+
/// </summary>
232+
ProjectFile,
233+
/// <summary>
234+
/// The <c>.csproj.user</c> file (or <c>.vbproj.user</c>, etc.)
235+
/// </summary>
236+
UserFile
237+
}
197238
}

0 commit comments

Comments
 (0)