Skip to content
This repository was archived by the owner on Oct 4, 2021. It is now read-only.

Commit fa14f8c

Browse files
committed
Make type discovery async
1 parent 8b6d235 commit fa14f8c

File tree

5 files changed

+38
-26
lines changed

5 files changed

+38
-26
lines changed

main/src/addins/MonoDevelop.AspNetCore/MonoDevelop.AspNetCore.Scaffolding/Fields/ComboField.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,20 @@
2424
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2525
// THE SOFTWARE.
2626

27-
using System.Linq;
27+
using System.Collections.Generic;
28+
using System.Threading.Tasks;
2829

2930
namespace MonoDevelop.AspNetCore.Scaffolding
3031
{
3132
class ComboField : ScaffolderField
3233
{
33-
public ComboField (string commandLineName, string displayName, string [] options, bool isEditable = false) : base (commandLineName, displayName)
34+
public ComboField (string commandLineName, string displayName, Task<IEnumerable<string>> options, bool isEditable = false) : base (commandLineName, displayName)
3435
{
3536
Options = options;
3637
IsEditable = isEditable;
3738
}
3839

3940
public bool IsEditable { get; }
40-
public string [] Options { get; }
41+
public Task<IEnumerable<string>> Options { get; }
4142
}
4243
}

main/src/addins/MonoDevelop.AspNetCore/MonoDevelop.AspNetCore.Scaffolding/ModelVisitor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public override void VisitNamedType (INamedTypeSymbol symbol)
9393
Visit (type);
9494
}
9595

96-
if(IncludeTypeInAddViewModelClassDropdown(symbol))
96+
if (IncludeTypeInAddViewModelClassDropdown (symbol))
9797
_types.Add (symbol);
9898
}
9999

@@ -111,7 +111,7 @@ public static bool IncludeTypeInAddViewModelClassDropdown (INamedTypeSymbol symb
111111

112112
static bool IsDerivedFromFilteredBaseClass (INamedTypeSymbol t)
113113
{
114-
if (_filteredBaseClasses.Any (baseClass => t.GetFullMetadataName().Equals (baseClass, StringComparison.Ordinal))) {
114+
if (_filteredBaseClasses.Any (baseClass => t.GetFullMetadataName ().Equals (baseClass, StringComparison.Ordinal))) {
115115
return true;
116116
}
117117
if (t.BaseType != null) {

main/src/addins/MonoDevelop.AspNetCore/MonoDevelop.AspNetCore.Scaffolding/ScaffolderTemplateConfigurePage.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2525
// THE SOFTWARE.
2626
using System.Linq;
27+
using System.Threading.Tasks;
28+
using MonoDevelop.Core;
2729
using Xwt;
2830

2931
namespace MonoDevelop.AspNetCore.Scaffolding
@@ -69,21 +71,30 @@ protected override Widget GetMainControl ()
6971
comboBox = new ComboBox ();
7072
}
7173

72-
foreach (var option in comboField.Options) {
73-
comboBox.Items.Add (option);
74-
}
74+
Task.Run (async () => {
75+
var options = await comboField.Options.ConfigureAwait (false);
76+
77+
await Runtime.RunInMainThread (() =>
78+
Xwt.Toolkit.NativeEngine.Invoke (() => {
79+
foreach (var option in options) {
80+
comboBox.Items.Add (option);
81+
}
82+
comboField.SelectedValue = options.FirstOrDefault ();
83+
if (comboBox.Items.Count > 0)
84+
comboBox.SelectedIndex = 0;
85+
}));
86+
});
87+
88+
7589

7690
label.Text = comboField.DisplayName;
7791

7892
table.Add (label, 0, rowIndex, hpos:WidgetPlacement.End);
7993
table.Add (comboBox, 1, rowIndex);
80-
comboField.SelectedValue = comboField.Options.FirstOrDefault ();
8194
comboBox.TextInput += (sender, args) => comboField.SelectedValue = comboBox.SelectedText;
8295

8396
comboBox.SelectionChanged += (sender, args) => comboField.SelectedValue = comboBox.SelectedText;
8497

85-
if (comboBox.Items.Count > 0)
86-
comboBox.SelectedIndex = 0;
8798
break;
8899
case BoolFieldList boolFieldList:
89100
label.Text = boolFieldList.DisplayName;

main/src/addins/MonoDevelop.AspNetCore/MonoDevelop.AspNetCore.Scaffolding/Scaffolders/RazorPageEntityFrameworkScaffolder.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2525
// THE SOFTWARE.
2626
using System.Collections.Generic;
27+
using System.Threading.Tasks;
2728

2829
namespace MonoDevelop.AspNetCore.Scaffolding
2930
{
@@ -49,11 +50,11 @@ IEnumerable<ScaffolderField> GetFields()
4950
LayoutPageField
5051
};
5152

52-
string [] viewTemplateOptions = new [] { "Empty", "Create", "Edit", "Delete", "Details", "List" };
53+
IEnumerable<string> viewTemplateOptions = new [] { "Empty", "Create", "Edit", "Delete", "Details", "List" };
5354

5455
fields = new ScaffolderField [] {
5556
NameField,
56-
new ComboField ("", "The template to use, supported view templates", viewTemplateOptions),
57+
new ComboField ("", "The template to use, supported view templates", Task.FromResult(viewTemplateOptions)),
5758
GetModelField(args.Project),
5859
GetDbContextField(args.Project),
5960
new BoolFieldList(options),

main/src/addins/MonoDevelop.AspNetCore/MonoDevelop.AspNetCore.Scaffolding/Scaffolders/ScaffolderBase.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2424
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2525
// THE SOFTWARE.
26+
using System;
2627
using System.Collections.Generic;
2728
using System.Linq;
29+
using System.Threading.Tasks;
2830
using Microsoft.CodeAnalysis.FindSymbols;
2931
using MonoDevelop.Ide;
3032
using MonoDevelop.Projects;
@@ -46,42 +48,39 @@ abstract class ScaffolderBase
4648

4749
protected ComboField GetDbContextField (DotNetProject project)
4850
{
49-
var dbContexts = GetDbContextClasses (project);
50-
return new ComboField ("--dataContext", "DbContext class to use", dbContexts.ToArray (), isEditable: true);
51+
return new ComboField ("--dataContext", "DbContext class to use", GetDbContextClassesAsync(project), isEditable: true);
5152
}
5253

5354
protected ComboField GetModelField (DotNetProject project)
5455
{
55-
var dbModels = GetModelClasses (project);
56-
return new ComboField ("--model", "Model class to use", dbModels.ToArray (), isEditable: true);
56+
return new ComboField ("--model", "Model class to use", GetModelClassesAsync(project), isEditable: true);
5757
}
5858

59-
IEnumerable<string> GetDbContextClasses (DotNetProject project)
59+
async Task<IEnumerable<string>> GetDbContextClassesAsync (DotNetProject project)
6060
{
61-
//TODO: make async
62-
var compilation = IdeApp.TypeSystemService.GetCompilationAsync (project).Result;
61+
var compilation = await IdeApp.TypeSystemService.GetCompilationAsync (project);
6362
if (compilation != null) {
6463
var dbContext = compilation.GetTypeByMetadataName (EFCDbContextTypeName)
6564
?? compilation.GetTypeByMetadataName (DbContextTypeName)
6665
?? compilation.GetTypeByMetadataName (EF7DbContextTypeName);
6766

6867
if (dbContext != null) {
69-
var result = SymbolFinder.FindDerivedClassesAsync (dbContext, IdeApp.TypeSystemService.Workspace.CurrentSolution).Result;
68+
var result = await SymbolFinder.FindDerivedClassesAsync (dbContext, IdeApp.TypeSystemService.Workspace.CurrentSolution);
7069

7170
return result.Where (ModelVisitor.IncludeTypeInAddViewModelClassDropdown).Select (c => c.MetadataName).Distinct().OrderBy (x => x);
7271
}
7372
}
73+
7474
return Enumerable.Empty<string> ();
7575
}
7676

77-
IEnumerable<string> GetModelClasses (DotNetProject project)
77+
async Task<IEnumerable<string>> GetModelClassesAsync (DotNetProject project)
7878
{
79-
//TODO: make async
80-
var compilation = IdeApp.TypeSystemService.GetCompilationAsync (project).Result;
79+
var compilation = await IdeApp.TypeSystemService.GetCompilationAsync (project);
8180
if (compilation != null) {
82-
8381
var modelTypes = ModelVisitor.FindModelTypes (compilation.Assembly);
84-
return modelTypes.Select (t => t.MetadataName).Distinct().OrderBy (x => x);
82+
var dbContextTypes = await GetDbContextClassesAsync (project);
83+
return modelTypes.Select (t => t.MetadataName).Except(dbContextTypes).Distinct().OrderBy (x => x);
8584
}
8685
return Enumerable.Empty<string> ();
8786
}

0 commit comments

Comments
 (0)