Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions Apollo.Components/Editor/ApolloCodeEditor.razor
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
return new StandaloneEditorConstructionOptions
{
AutomaticLayout = true,
Language = "csharp",
Language = SolutionsState.ActiveFile?.GetMonacoLanguage() ?? "csharp",
Value = SolutionsState.ActiveFile.Data,
FontSize = editorSettings.FontSize,
LineHeight = editorSettings.LineHeight,
Expand Down Expand Up @@ -143,23 +143,30 @@

if (file != null)
{
var model = await _editor.GetModel();
await BlazorMonaco.Editor.Global.SetModelLanguage(JsRuntime, model, file.GetMonacoLanguage());

await SetCodeAsync(file.Data);
await CodeAnalysisState.SetCurrentDocumentAsync(file.Uri);
await CodeAnalysisState.UpdateDocumentAsync(file.Uri, file.Data);

await GetDiagnosticsAsync(new ModelContentChangedEvent());

_diagnosticsCancellation = new CancellationTokenSource();
var token = _diagnosticsCancellation.Token;
if (file.IsCSharp())
{
await CodeAnalysisState.SetCurrentDocumentAsync(file.Uri);
await CodeAnalysisState.UpdateDocumentAsync(file.Uri, file.Data);

_ = Task.Delay(debounceMilliseconds, token)
.ContinueWith(async _ =>
{
if (!token.IsCancellationRequested)
await GetDiagnosticsAsync(new ModelContentChangedEvent());

_diagnosticsCancellation = new CancellationTokenSource();
var token = _diagnosticsCancellation.Token;

_ = Task.Delay(debounceMilliseconds, token)
.ContinueWith(async _ =>
{
await GetDiagnosticsAsync(new ModelContentChangedEvent());
}
}, token);
if (!token.IsCancellationRequested)
{
await GetDiagnosticsAsync(new ModelContentChangedEvent());
}
}, token);
}
}
else
{
Expand Down Expand Up @@ -337,6 +344,9 @@
{
await ClearMarkers();

if (SolutionsState.ActiveFile == null || !SolutionsState.ActiveFile.IsCSharp())
return;

if (_diagnosticsCancellation != null)
{
try
Expand All @@ -355,7 +365,7 @@

try
{
if (SolutionsState.ActiveFile != null && evt.Changes != null && evt.Changes.Any())
if (evt.Changes != null && evt.Changes.Any())
{
var textChanges = ConvertMonacoChangesToTextChanges(evt.Changes);
if (textChanges.Count > 0)
Expand Down Expand Up @@ -420,6 +430,7 @@
private async Task GetDiagnosticsAsync(ModelContentChangedEvent evt)
{
if (!SolutionsState.HasActiveSolution) return;
if (SolutionsState.ActiveFile == null || !SolutionsState.ActiveFile.IsCSharp()) return;

try
{
Expand Down
30 changes: 30 additions & 0 deletions Apollo.Components/Solutions/SolutionFileExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace Apollo.Components.Solutions;

public static class SolutionFileExtensions
{
public static bool IsCSharp(this SolutionFile file)
=> file.Extension.Equals(".cs", StringComparison.OrdinalIgnoreCase);

public static bool IsHtml(this SolutionFile file)
=> file.Extension.Equals(".html", StringComparison.OrdinalIgnoreCase);

public static string GetMonacoLanguage(this SolutionFile file)
=> file.Extension.ToLowerInvariant() switch
{
".cs" => "csharp",
".html" => "html",
".htm" => "html",
".css" => "css",
".js" => "javascript",
".ts" => "typescript",
".json" => "json",
".xml" => "xml",
".md" => "markdown",
".razor" => "razor",
".sql" => "sql",
".yaml" => "yaml",
".yml" => "yaml",
_ => "plaintext"
};
}

Loading