Skip to content

Commit 490f95c

Browse files
Unification of ParseOptions usage.
1 parent 09748ae commit 490f95c

File tree

4 files changed

+19
-13
lines changed

4 files changed

+19
-13
lines changed

CSharpRepl.Services/Disassembly/Disassembler.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ internal class Disassembler
2727
{
2828
private readonly AssemblyReferenceService referenceService;
2929
private readonly (string name, CompileDelegate compile)[] compilers;
30+
private readonly CSharpParseOptions parseOptions;
3031
private readonly CSharpCompilationOptions compilationOptions;
3132

32-
public Disassembler(CSharpCompilationOptions compilationOptions, AssemblyReferenceService referenceService, ScriptRunner scriptRunner)
33+
public Disassembler(CSharpParseOptions parseOptions, CSharpCompilationOptions compilationOptions, AssemblyReferenceService referenceService, ScriptRunner scriptRunner)
3334
{
35+
this.parseOptions = parseOptions.WithKind(SourceCodeKind.Regular);
3436
this.compilationOptions = compilationOptions;
3537
this.referenceService = referenceService;
3638

@@ -45,7 +47,7 @@ public Disassembler(CSharpCompilationOptions compilationOptions, AssemblyReferen
4547
compile: (code, optimizationLevel) => Compile(code, optimizationLevel, OutputKind.DynamicallyLinkedLibrary)),
4648
// Compiling as a script will work for most other cases, but it's quite verbose so we use it as a last resort.
4749
(name: "Scripting session (will be overly verbose)",
48-
compile: (code, optimizationLevel) => scriptRunner.CompileTransient(code, optimizationLevel))
50+
compile: scriptRunner.CompileTransient)
4951
];
5052
}
5153

@@ -151,7 +153,7 @@ static PlainTextOutput DisassembleAll(PEFile file, PlainTextOutput ilCodeOutput)
151153

152154
private Compilation Compile(string code, OptimizationLevel optimizationLevel, OutputKind outputKind)
153155
{
154-
var ast = CSharpSyntaxTree.ParseText(code, new CSharpParseOptions(LanguageVersion.Latest));
156+
var ast = CSharpSyntaxTree.ParseText(code, parseOptions);
155157
var compilation = CSharpCompilation.Create("CompilationForDisassembly",
156158
[ast],
157159
referenceService.LoadedReferenceAssemblies,

CSharpRepl.Services/Roslyn/References/AssemblyReferenceService.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,16 @@ internal sealed class AssemblyReferenceService
3434
private readonly HashSet<string> implementationAssemblyPaths;
3535
private readonly HashSet<string> sharedFrameworkImplementationAssemblyPaths;
3636
private readonly HashSet<UsingDirectiveSyntax> usings;
37+
private readonly CSharpParseOptions parseOptions;
3738

3839
public IReadOnlySet<string> ImplementationAssemblyPaths => implementationAssemblyPaths;
3940
public IReadOnlySet<MetadataReference> LoadedImplementationAssemblies => loadedImplementationAssemblies;
4041
public IReadOnlySet<MetadataReference> LoadedReferenceAssemblies => loadedReferenceAssemblies;
4142
public IReadOnlyCollection<UsingDirectiveSyntax> Usings => usings;
4243

43-
public AssemblyReferenceService(Configuration config, ITraceLogger logger)
44+
public AssemblyReferenceService(Configuration config, CSharpParseOptions parseOptions, ITraceLogger logger)
4445
{
46+
this.parseOptions = parseOptions;
4547
this.dotnetInstallationLocator = new DotNetInstallationLocator(logger);
4648
this.referenceAssemblyPaths = [];
4749
this.implementationAssemblyPaths = [];
@@ -201,7 +203,7 @@ public void LoadSharedFrameworkConfiguration(SharedFramework[] sharedFrameworks)
201203
}
202204

203205
internal IReadOnlyCollection<UsingDirectiveSyntax> GetUsings(string code) =>
204-
CSharpSyntaxTree.ParseText(code)
206+
CSharpSyntaxTree.ParseText(code, parseOptions)
205207
.GetRoot()
206208
.DescendantNodes()
207209
.OfType<UsingDirectiveSyntax>()
@@ -210,7 +212,7 @@ internal IReadOnlyCollection<UsingDirectiveSyntax> GetUsings(string code) =>
210212
internal void TrackUsings(IReadOnlyCollection<UsingDirectiveSyntax> usingsToAdd) =>
211213
usings.UnionWith(usingsToAdd);
212214

213-
private IReadOnlyCollection<MetadataReference> CreateDefaultReferences(string assemblyPath, IReadOnlyCollection<string> assemblies)
215+
private List<PortableExecutableReference> CreateDefaultReferences(string assemblyPath, IReadOnlyCollection<string> assemblies)
214216
{
215217
return assemblies
216218
.AsParallel()

CSharpRepl.Services/Roslyn/RoslynServices.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public sealed partial class RoslynServices
4747
private readonly SemaphoreSlim semaphore = new(1);
4848
private readonly IPromptCallbacks defaultPromptCallbacks = new PromptCallbacks();
4949
private readonly ThreadLocal<OverloadItemGenerator> overloadItemGenerator;
50+
private readonly CSharpParseOptions parseOptions = CSharpParseOptions.Default.WithKind(SourceCodeKind.Script).WithLanguageVersion(LanguageVersion.Latest);
5051
private ScriptRunner? scriptRunner;
5152
private WorkspaceManager? workspaceManager;
5253
private Disassembler? disassembler;
@@ -78,7 +79,7 @@ public RoslynServices(IConsoleEx console, Configuration config, ITraceLogger log
7879
{
7980
logger.Log("Starting background initialization");
8081

81-
this.referenceService = new AssemblyReferenceService(config, logger);
82+
this.referenceService = new AssemblyReferenceService(config, parseOptions, logger);
8283

8384
this.compilationOptions = new CSharpCompilationOptions(
8485
OutputKind.DynamicallyLinkedLibrary,
@@ -91,9 +92,9 @@ public RoslynServices(IConsoleEx console, Configuration config, ITraceLogger log
9192
// is updated alongside. The workspace is a datamodel used in "editor services" like
9293
// syntax highlighting, autocompletion, and roslyn symbol queries.
9394
this.workspaceManager = new WorkspaceManager(compilationOptions, referenceService, logger);
94-
this.scriptRunner = new ScriptRunner(workspaceManager, compilationOptions, referenceService, console, config);
95+
this.scriptRunner = new ScriptRunner(workspaceManager, parseOptions, compilationOptions, referenceService, console, config);
9596

96-
this.disassembler = new Disassembler(compilationOptions, referenceService, scriptRunner);
97+
this.disassembler = new Disassembler(parseOptions, compilationOptions, referenceService, scriptRunner);
9798
this.prettyPrinter = new PrettyPrinter(console, highlighter, config);
9899
this.symbolExplorer = new SymbolExplorer(referenceService, scriptRunner);
99100
this.autocompleteService = new AutoCompleteService(highlighter, cache, config);

CSharpRepl.Services/Roslyn/Scripting/ScriptRunner.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,22 @@ internal sealed class ScriptRunner
2929
private readonly CompositeAlternativeReferenceResolver alternativeReferenceResolver;
3030
private readonly MetadataReferenceResolver metadataResolver;
3131
private readonly WorkspaceManager workspaceManager;
32+
private readonly CSharpParseOptions parseOptions;
3233
private readonly AssemblyReferenceService referenceAssemblyService;
3334
private ScriptOptions scriptOptions;
3435
private ScriptState<object>? state;
3536

3637
public ScriptRunner(
3738
WorkspaceManager workspaceManager,
39+
CSharpParseOptions parseOptions,
3840
CSharpCompilationOptions compilationOptions,
3941
AssemblyReferenceService referenceAssemblyService,
4042
IConsoleEx console,
4143
Configuration configuration)
4244
{
4345
this.console = console;
4446
this.workspaceManager = workspaceManager;
47+
this.parseOptions = parseOptions;
4548
this.referenceAssemblyService = referenceAssemblyService;
4649
this.assemblyLoader = new InteractiveAssemblyLoader(new MetadataShadowCopyProvider());
4750

@@ -110,7 +113,7 @@ public Compilation CompileTransient(string code, OptimizationLevel optimizationL
110113
{
111114
return CSharpCompilation.CreateScriptCompilation(
112115
"CompilationTransient",
113-
CSharpSyntaxTree.ParseText(code, CSharpParseOptions.Default.WithKind(SourceCodeKind.Script).WithLanguageVersion(LanguageVersion.Latest)),
116+
CSharpSyntaxTree.ParseText(code, parseOptions),
114117
scriptOptions.MetadataReferences,
115118
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, usings: scriptOptions.Imports, optimizationLevel: optimizationLevel, allowUnsafe: scriptOptions.AllowUnsafe, metadataReferenceResolver: metadataResolver),
116119
previousScriptCompilation: state?.Script.GetCompilation() is CSharpCompilation previous ? previous : null,
@@ -165,7 +168,5 @@ await tree.GetRootAsync(cancellationToken).ConfigureAwait(false) is CompilationU
165168
}
166169

167170
private ScriptGlobals CreateGlobalsObject(string[]? args)
168-
{
169-
return new ScriptGlobals(console, args ?? []);
170-
}
171+
=> new(console, args ?? []);
171172
}

0 commit comments

Comments
 (0)