GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [gh-261] Initial work to allow SourcePath in language-syntax-explanat… · vs0533/SharpLab@b3faf1a

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • .editorconfig
  • .gitmodules
  • source
    • #external
      • SourcePath
    • Directory.Build.props
    • Server.Azure
      • Server.Azure.csproj
    • Server
      • Explanation
        • Explainer.cs
        • Internal
          • ExternalSyntaxExplanationProvider.cs
          • ISyntaxExplanationProvider.cs
        • SyntaxExplanation.cs
      • Server.csproj
      • ServerModule.cs
    • SharpLab.sln
    • Tests
      • ExplanationTests.cs
      • Tests.csproj
    • WebApp
      • WebApp.csproj
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎.editorconfig‎

Copy file name to clipboardExpand all lines: .editorconfig
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ insert_final_newline = false
1111
[*.cs]
1212
csharp_new_line_before_open_brace = none
1313

14-
[*.{html,config}]
14+
[*.{html,config,targets,props}]
1515
indent_size = 2
Collapse file

‎.gitmodules‎

Copy file name to clipboard
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
[submodule "source/Tests/TestData/language-syntax-explanations"]
22
path = source/Tests/TestData/language-syntax-explanations
33
url = https://github.com/ashmind/language-syntax-explanations.git
4+
[submodule "source/#external/SourcePath"]
5+
path = "source/#external/SourcePath"
6+
url = https://github.com/ashmind/SourcePath.git
Collapse file

‎source/#external/SourcePath‎

Copy file name to clipboard
Submodule SourcePath added at aef9558
Collapse file

‎source/Directory.Build.props‎

Copy file name to clipboard
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<Project>
22
<PropertyGroup>
33
<SolutionDir Condition="'$(SolutionDir)'==''">$(MSBuildThisFileDirectory)</SolutionDir>
4+
<SourcePathSharpLabTargetFrameworksFix>true</SourcePathSharpLabTargetFrameworksFix>
45
</PropertyGroup>
56
</Project>
Collapse file

‎source/Server.Azure/Server.Azure.csproj‎

Copy file name to clipboardExpand all lines: source/Server.Azure/Server.Azure.csproj
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFramework>net46</TargetFramework>
3+
<TargetFramework>net47</TargetFramework>
44
<AssemblyName>SharpLab.Server.Azure</AssemblyName>
55
<RootNamespace>SharpLab.Server.Azure</RootNamespace>
66
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
Collapse file

‎source/Server/Explanation/Explainer.cs‎

Copy file name to clipboardExpand all lines: source/Server/Explanation/Explainer.cs
+10-7Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Linq;
45
using System.Threading;
56
using System.Threading.Tasks;
67
using Microsoft.CodeAnalysis;
78
using Microsoft.CodeAnalysis.CSharp;
89
using Microsoft.CodeAnalysis.CSharp.Syntax;
910
using MirrorSharp.Advanced;
11+
using SourcePath.Roslyn;
1012
using SharpLab.Server.Explanation.Internal;
1113

1214
namespace SharpLab.Server.Explanation {
1315
public class Explainer : IExplainer {
1416
private readonly ISyntaxExplanationProvider _explanationProvider;
17+
private readonly IRoslynCSharpSyntaxQueryExecutor _syntaxPathExecutor;
1518

16-
public Explainer(ISyntaxExplanationProvider explanationProvider) {
19+
public Explainer(ISyntaxExplanationProvider explanationProvider, IRoslynCSharpSyntaxQueryExecutor syntaxPathExecutor) {
1720
_explanationProvider = explanationProvider;
21+
_syntaxPathExecutor = syntaxPathExecutor;
1822
}
1923

2024
public async Task<ExplanationResult> ExplainAsync(object ast, IWorkSession session, CancellationToken cancellationToken) {
@@ -25,14 +29,13 @@ public async Task<ExplanationResult> ExplainAsync(object ast, IWorkSession sessi
2529
return new ExplanationResult(MapExplanations(ast, explanations));
2630
}
2731

28-
private IEnumerable<(SyntaxNodeOrToken, SyntaxExplanation)> MapExplanations(object ast, IReadOnlyDictionary<SyntaxKind, SyntaxExplanation> explanations) {
29-
var seen = new HashSet<SyntaxExplanation>();
32+
private IEnumerable<(SyntaxNodeOrToken, SyntaxExplanation)> MapExplanations(object ast, IReadOnlyCollection<SyntaxExplanation> explanations) {
3033
var tree = (CSharpSyntaxNode)ast;
3134
var results = new List<(SyntaxNode, SyntaxExplanation)>();
32-
foreach (var descendant in tree.DescendantNodesAndTokensAndSelf()) {
33-
if (!explanations.TryGetValue(descendant.Kind(), out var explanation) || !seen.Add(explanation))
34-
continue;
35-
yield return (explanation.GetBestFragment(descendant), explanation);
35+
foreach (var explanation in explanations) {
36+
var match = _syntaxPathExecutor.QueryAll(tree, explanation.Path).FirstOrDefault();
37+
if (match != default(SyntaxNodeOrToken))
38+
yield return (match, explanation);
3639
}
3740
}
3841

Collapse file

‎source/Server/Explanation/Internal/ExternalSyntaxExplanationProvider.cs‎

Copy file name to clipboardExpand all lines: source/Server/Explanation/Internal/ExternalSyntaxExplanationProvider.cs
+28-14Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
using System.Net.Http;
44
using System.Threading;
55
using System.Threading.Tasks;
6-
using Microsoft.CodeAnalysis.CSharp;
7-
using SharpLab.Server.Monitoring;
86
using SharpYaml.Serialization;
7+
using SourcePath.CSharp;
8+
using SharpLab.Server.Monitoring;
99

1010
namespace SharpLab.Server.Explanation.Internal {
1111
public class ExternalSyntaxExplanationProvider : ISyntaxExplanationProvider, IDisposable {
1212
private readonly Func<HttpClient> _httpClientFactory;
1313
private readonly Uri _sourceUrl;
1414

15-
private IReadOnlyDictionary<SyntaxKind, SyntaxExplanation> _explanations;
15+
private IReadOnlyCollection<SyntaxExplanation> _explanations;
1616
private readonly SemaphoreSlim _explanationsLock = new SemaphoreSlim(1);
1717

1818
private Task _updateTask;
@@ -23,15 +23,23 @@ public class ExternalSyntaxExplanationProvider : ISyntaxExplanationProvider, IDi
2323
NamingConvention = new FlatNamingConvention()
2424
});
2525
private readonly IMonitor _monitor;
26+
private readonly ISyntaxPathParser _syntaxPathParser;
2627

27-
public ExternalSyntaxExplanationProvider(Func<HttpClient> httpClientFactory, Uri sourceUrl, TimeSpan updatePeriod, IMonitor monitor) {
28+
public ExternalSyntaxExplanationProvider(
29+
Func<HttpClient> httpClientFactory,
30+
Uri sourceUrl,
31+
TimeSpan updatePeriod,
32+
IMonitor monitor,
33+
ISyntaxPathParser syntaxPathParser
34+
) {
2835
_httpClientFactory = httpClientFactory;
2936
_sourceUrl = sourceUrl;
3037
_updatePeriod = updatePeriod;
3138
_monitor = monitor;
39+
_syntaxPathParser = syntaxPathParser;
3240
}
3341

34-
public async ValueTask<IReadOnlyDictionary<SyntaxKind, SyntaxExplanation>> GetExplanationsAsync(CancellationToken cancellationToken) {
42+
public async ValueTask<IReadOnlyCollection<SyntaxExplanation>> GetExplanationsAsync(CancellationToken cancellationToken) {
3543
if (_explanations == null) {
3644
try {
3745
await _explanationsLock.WaitAsync(cancellationToken).ConfigureAwait(false);
@@ -49,8 +57,8 @@ public async ValueTask<IReadOnlyDictionary<SyntaxKind, SyntaxExplanation>> GetEx
4957
return _explanations;
5058
}
5159

52-
private async Task<IReadOnlyDictionary<SyntaxKind, SyntaxExplanation>> LoadExplanationsSlowAsync(CancellationToken cancellationToken) {
53-
var explanations = new Dictionary<SyntaxKind, SyntaxExplanation>();
60+
private async Task<IReadOnlyCollection<SyntaxExplanation>> LoadExplanationsSlowAsync(CancellationToken cancellationToken) {
61+
var explanations = new List<SyntaxExplanation>();
5462
var serializer = new Serializer();
5563
using (var client = _httpClientFactory()) {
5664
var response = await client.GetAsync(_sourceUrl, cancellationToken).ConfigureAwait(false);
@@ -59,15 +67,23 @@ private async Task<IReadOnlyDictionary<SyntaxKind, SyntaxExplanation>> LoadExpla
5967
var yamlString = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
6068
var yaml = _serilializer.Deserialize<IEnumerable<YamlExplanation>>(yamlString);
6169
foreach (var item in yaml) {
62-
var explanation = new SyntaxExplanation(item.Name, item.Text, item.Link, item.Scope);
63-
foreach (var kind in item.Match) {
64-
explanations.Add(kind, explanation);
65-
}
70+
explanations.Add(ParseExplanation(item));
6671
}
6772
}
6873
return explanations;
6974
}
7075

76+
private SyntaxExplanation ParseExplanation(YamlExplanation item) {
77+
SyntaxPath path;
78+
try {
79+
path = _syntaxPathParser.Parse(item.Path, SyntaxPathAxis.DescendantOrSelf);
80+
}
81+
catch (Exception ex) {
82+
throw new Exception($"Failed to parse path for '{item.Name}': {ex.Message}.", ex);
83+
}
84+
return new SyntaxExplanation(path, item.Name, item.Text, item.Link);
85+
}
86+
7187
private async Task UpdateLoopAsync() {
7288
while (!_updateCancellationSource.IsCancellationRequested) {
7389
try {
@@ -103,9 +119,7 @@ private class YamlExplanation {
103119
public string Name { get; set; }
104120
public string Text { get; set; }
105121
public string Link { get; set; }
106-
107-
public SyntaxKind[] Match { get; set; }
108-
public SyntaxFragmentScope Scope { get; set; } = SyntaxFragmentScope.Self;
122+
public string Path { get; set; }
109123
}
110124
}
111125
}
Collapse file

‎source/Server/Explanation/Internal/ISyntaxExplanationProvider.cs‎

Copy file name to clipboard
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
using System.Collections.Generic;
22
using System.Threading;
33
using System.Threading.Tasks;
4-
using Microsoft.CodeAnalysis.CSharp;
54

65
namespace SharpLab.Server.Explanation.Internal {
76
public interface ISyntaxExplanationProvider {
8-
ValueTask<IReadOnlyDictionary<SyntaxKind, SyntaxExplanation>> GetExplanationsAsync(CancellationToken cancellationToken);
7+
ValueTask<IReadOnlyCollection<SyntaxExplanation>> GetExplanationsAsync(CancellationToken cancellationToken);
98
}
109
}
Collapse file

‎source/Server/Explanation/SyntaxExplanation.cs‎

Copy file name to clipboardExpand all lines: source/Server/Explanation/SyntaxExplanation.cs
+4-14Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
1-
using System;
2-
using Microsoft.CodeAnalysis;
1+
using SourcePath.CSharp;
32

43
namespace SharpLab.Server.Explanation {
54
public class SyntaxExplanation {
6-
private readonly SyntaxFragmentScope _fragmentScope;
7-
8-
public SyntaxExplanation(string name, string text, string link, SyntaxFragmentScope fragmentScope) {
5+
public SyntaxExplanation(SyntaxPath path, string name, string text, string link) {
6+
Path = Argument.NotNull(nameof(path), path);
97
Name = Argument.NotNullOrEmpty(nameof(name), name);
108
Text = Argument.NotNullOrEmpty(nameof(text), text);
119
Link = link;
12-
_fragmentScope = fragmentScope;
13-
}
14-
15-
public SyntaxNodeOrToken GetBestFragment(SyntaxNodeOrToken match) {
16-
switch (_fragmentScope) {
17-
case SyntaxFragmentScope.Self: return match;
18-
case SyntaxFragmentScope.Parent: return match.Parent;
19-
default: throw new ArgumentOutOfRangeException($"Unknown fragment scope: '{_fragmentScope}'.");
20-
}
2110
}
2211

12+
public SyntaxPath Path { get; }
2313
public string Name { get; }
2414
public string Text { get; }
2515
public string Link { get; }
Collapse file

‎source/Server/Server.csproj‎

Copy file name to clipboardExpand all lines: source/Server/Server.csproj
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net46</TargetFramework>
4+
<TargetFramework>net47</TargetFramework>
55
<AssemblyName>SharpLab.Server</AssemblyName>
66
<RootNamespace>SharpLab.Server</RootNamespace>
77
<Company>Andrey Shchekin</Company>
@@ -27,7 +27,7 @@
2727

2828
<ItemGroup>
2929
<PackageReference Include="AppDomainToolkit" Version="1.0.4.3" />
30-
<PackageReference Include="Argument" Version="1.0.3" />
30+
<PackageReference Include="Argument" Version="1.1.0" />
3131
<PackageReference Include="AshMind.Extensions" Version="2.0.0-pre-20160719" />
3232
<PackageReference Include="Autofac" Version="3.5.2" />
3333
<PackageReference Include="Autofac.Extras.FileSystemRegistration" Version="0.1.3" />
@@ -57,6 +57,8 @@
5757
</ItemGroup>
5858

5959
<ItemGroup>
60+
<ProjectReference Include="..\#external\SourcePath\CSharp\CSharp.csproj" />
61+
<ProjectReference Include="..\#external\SourcePath\Roslyn\Roslyn.csproj" />
6062
<ProjectReference Include="..\Runtime\Runtime.csproj" />
6163
</ItemGroup>
6264

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.