Skip to content
This repository was archived by the owner on Mar 21, 2025. It is now read-only.

Commit d9da56b

Browse files
committed
Add a SourceTextProvider abstraction and use it for workspaces.
Also remove PhysicalWorkspace as it no longer serves a purpose.
1 parent e21aa05 commit d9da56b

File tree

11 files changed

+67
-46
lines changed

11 files changed

+67
-46
lines changed

src/driver/Verbs/RunVerb.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ protected override async ValueTask<int> RunAsync(CancellationToken cancellationT
2424

2525
if (workspace.GetEntryPointDocument() is not { } doc)
2626
throw new DriverException(
27-
$"No entry point document named '{PhysicalWorkspace.EntryPointDocumentName}' found in the workspace.");
27+
$"No entry point document named '{WorkspaceDocument.EntryPointPath}' found in the workspace.");
2828

2929
// TODO: Run the program.
3030

src/driver/Verbs/Verb.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ protected static async ValueTask<Workspace> OpenWorkspaceAsync(
5050
{
5151
try
5252
{
53-
workspace = await ProjectWorkspace.OpenAsync(directory, disableAnalysis, cancellationToken);
53+
workspace = await ProjectWorkspace.OpenAsync(
54+
directory, PhysicalSourceTextProvider.Instance, disableAnalysis, cancellationToken);
5455
}
5556
catch (PathTooLongException)
5657
{
@@ -82,7 +83,7 @@ protected static async ValueTask<Workspace> OpenWorkspaceAsync(
8283
catch (FileNotFoundException)
8384
{
8485
// No configuration file, so try a simple workspace.
85-
workspace = SimpleWorkspace.Open(directory, disableAnalysis);
86+
workspace = SimpleWorkspace.Open(directory, PhysicalSourceTextProvider.Instance, disableAnalysis);
8687
}
8788

8889
PhysicalWorkspaceWatcher.Populate(workspace);

src/driver/Workspaces/ScriptWorkspace.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
namespace Vezel.Celerity.Driver.Workspaces;
22

3-
internal sealed class ScriptWorkspace : PhysicalWorkspace
3+
internal sealed class ScriptWorkspace : Workspace
44
{
55
public ScriptWorkspace(string file)
6-
: base(System.IO.Path.GetDirectoryName(file)!)
6+
: base(System.IO.Path.GetDirectoryName(file)!, PhysicalSourceTextProvider.Instance)
77
{
88
new ManualWorkspaceWatcher(this).AddDocument(System.IO.Path.GetFileName(file));
99
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace Vezel.Celerity.Language.Tooling.Workspaces;
2+
3+
public sealed class PhysicalSourceTextProvider : SourceTextProvider
4+
{
5+
public static PhysicalSourceTextProvider Instance { get; } = new();
6+
7+
private PhysicalSourceTextProvider()
8+
{
9+
}
10+
11+
protected internal override ValueTask<SourceText> GetTextAsync(
12+
Workspace workspace, string path, CancellationToken cancellationToken)
13+
{
14+
Check.Null(workspace);
15+
Check.Argument(WorkspaceWatcher.IsValidPath(path), path);
16+
17+
return GetTextAsync();
18+
19+
[AsyncMethodBuilder(typeof(PoolingAsyncValueTaskMethodBuilder<>))]
20+
async ValueTask<SourceText> GetTextAsync()
21+
{
22+
return new StringSourceText(
23+
path, await File.ReadAllTextAsync(Path.Join(workspace.Path, path), cancellationToken)
24+
.ConfigureAwait(false));
25+
}
26+
}
27+
}

src/language/tooling/Workspaces/PhysicalWorkspace.cs

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/language/tooling/Workspaces/ProjectWorkspace.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Vezel.Celerity.Language.Tooling.Workspaces;
44

5-
public sealed class ProjectWorkspace : PhysicalWorkspace
5+
public sealed class ProjectWorkspace : Workspace
66
{
77
public const string ConfigurationFileName = "celerity.json";
88

@@ -12,23 +12,26 @@ public sealed class ProjectWorkspace : PhysicalWorkspace
1212

1313
private readonly bool _disableAnalysis;
1414

15-
private ProjectWorkspace(string path, ProjectConfiguration configuration, bool disableAnalysis)
16-
: base(path)
15+
private ProjectWorkspace(
16+
string path, SourceTextProvider textProvider, ProjectConfiguration configuration, bool disableAnalysis)
17+
: base(path, textProvider)
1718
{
1819
Configuration = configuration;
1920
_sourceDirectory = new(System.IO.Path.Join(path, configuration.SourcePath));
2021
_disableAnalysis = disableAnalysis;
2122
}
2223

23-
public static ValueTask<ProjectWorkspace> OpenAsync(string path, CancellationToken cancellationToken = default)
24+
public static ValueTask<ProjectWorkspace> OpenAsync(
25+
string path, SourceTextProvider textProvider, CancellationToken cancellationToken = default)
2426
{
25-
return OpenAsync(path, false, cancellationToken);
27+
return OpenAsync(path, textProvider, false, cancellationToken);
2628
}
2729

2830
public static ValueTask<ProjectWorkspace> OpenAsync(
29-
string path, bool disableAnalysis, CancellationToken cancellationToken = default)
31+
string path, SourceTextProvider textProvider, bool disableAnalysis, CancellationToken cancellationToken = default)
3032
{
3133
Check.Null(path);
34+
Check.Null(textProvider);
3235

3336
return OpenAsync();
3437

@@ -43,7 +46,7 @@ async ValueTask<ProjectWorkspace> OpenAsync()
4346
await using (stream.ConfigureAwait(false))
4447
cfg = await ProjectConfiguration.LoadAsync(stream, cancellationToken).ConfigureAwait(false);
4548

46-
return new(path, cfg, disableAnalysis);
49+
return new(path, textProvider, cfg, disableAnalysis);
4750
}
4851
}
4952

@@ -59,7 +62,7 @@ protected override WorkspaceDocumentAttributes GetDocumentAttributes(string path
5962
? WorkspaceDocumentAttributes.DisableAnalyzers | WorkspaceDocumentAttributes.SuppressDiagnostics
6063
: WorkspaceDocumentAttributes.None;
6164

62-
if ((file.DirectoryName, file.Name) == (_sourceDirectory.FullName, EntryPointDocumentName))
65+
if ((file.DirectoryName, file.Name) == (_sourceDirectory.FullName, WorkspaceDocument.EntryPointPath))
6366
return WorkspaceDocumentAttributes.EntryPoint | srcAttrs;
6467

6568
var current = file.Directory;

src/language/tooling/Workspaces/SimpleWorkspace.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace Vezel.Celerity.Language.Tooling.Workspaces;
22

3-
public sealed class SimpleWorkspace : PhysicalWorkspace
3+
public sealed class SimpleWorkspace : Workspace
44
{
55
private static readonly IEnumerable<DiagnosticAnalyzer> _diagnosticAnalyzers = new[]
66
{
@@ -9,15 +9,15 @@ public sealed class SimpleWorkspace : PhysicalWorkspace
99

1010
private readonly bool _disableAnalysis;
1111

12-
private SimpleWorkspace(string path, bool disableAnalysis)
13-
: base(path)
12+
private SimpleWorkspace(string path, SourceTextProvider textProvider, bool disableAnalysis)
13+
: base(path, textProvider)
1414
{
1515
_disableAnalysis = disableAnalysis;
1616
}
1717

18-
public static SimpleWorkspace Open(string path, bool disableAnalysis = false)
18+
public static SimpleWorkspace Open(string path, SourceTextProvider textProvider, bool disableAnalysis = false)
1919
{
20-
return new(path, disableAnalysis);
20+
return new(path, textProvider, disableAnalysis);
2121
}
2222

2323
protected internal override IEnumerable<DiagnosticAnalyzer> GetDiagnosticAnalyzers()
@@ -27,7 +27,7 @@ protected internal override IEnumerable<DiagnosticAnalyzer> GetDiagnosticAnalyze
2727

2828
protected override WorkspaceDocumentAttributes GetDocumentAttributes(string path)
2929
{
30-
var attrs = path == EntryPointDocumentName
30+
var attrs = path == WorkspaceDocument.EntryPointPath
3131
? WorkspaceDocumentAttributes.EntryPoint
3232
: WorkspaceDocumentAttributes.None;
3333

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Vezel.Celerity.Language.Tooling.Workspaces;
2+
3+
public abstract class SourceTextProvider
4+
{
5+
protected internal abstract ValueTask<SourceText> GetTextAsync(
6+
Workspace workspace, string path, CancellationToken cancellationToken);
7+
}

src/language/tooling/Workspaces/Workspace.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public event Action<WorkspaceDocument>? DocumentRemoved
2828

2929
public string Path { get; }
3030

31+
public SourceTextProvider TextProvider { get; }
32+
3133
public ImmutableDictionary<string, WorkspaceDocument> Documents { get; private set; } =
3234
ImmutableDictionary<string, WorkspaceDocument>.Empty;
3335

@@ -39,11 +41,13 @@ public event Action<WorkspaceDocument>? DocumentRemoved
3941

4042
private readonly Event<WorkspaceDocument> _documentRemoved = new();
4143

42-
protected Workspace(string path)
44+
protected Workspace(string path, SourceTextProvider textProvider)
4345
{
4446
Check.Null(path);
47+
Check.Null(textProvider);
4548

4649
Path = path;
50+
TextProvider = textProvider;
4751
}
4852

4953
public WorkspaceDocument? GetEntryPointDocument()
@@ -52,8 +56,6 @@ protected Workspace(string path)
5256
static doc => doc.Attributes.HasFlag(WorkspaceDocumentAttributes.EntryPoint));
5357
}
5458

55-
protected internal abstract ValueTask<SourceText> LoadTextAsync(string path, CancellationToken cancellationToken);
56-
5759
protected internal abstract IEnumerable<DiagnosticAnalyzer> GetDiagnosticAnalyzers();
5860

5961
protected abstract WorkspaceDocumentAttributes GetDocumentAttributes(string path);

src/language/tooling/Workspaces/WorkspaceDocument.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ namespace Vezel.Celerity.Language.Tooling.Workspaces;
44

55
public sealed class WorkspaceDocument
66
{
7+
public const string EntryPointPath = "main.cel";
8+
79
public Workspace Workspace { get; }
810

911
public WorkspaceDocumentAttributes Attributes { get; }
@@ -63,8 +65,8 @@ async ValueTask<SourceText> GetTextAsync()
6365
case SemanticTree semantics:
6466
return semantics.Syntax.GetText();
6567
case var path:
66-
var state = await Workspace.LoadTextAsync(Unsafe.As<string>(path), cancellationToken)
67-
.ConfigureAwait(false);
68+
var state = await Workspace.TextProvider.GetTextAsync(
69+
Workspace, Unsafe.As<string>(path), cancellationToken).ConfigureAwait(false);
6870

6971
Check.Operation(state != null);
7072

0 commit comments

Comments
 (0)