diff --git a/Apollo.Components/Editor/ApolloCodeEditor.razor b/Apollo.Components/Editor/ApolloCodeEditor.razor index cc7afae..5f95645 100644 --- a/Apollo.Components/Editor/ApolloCodeEditor.razor +++ b/Apollo.Components/Editor/ApolloCodeEditor.razor @@ -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, @@ -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 { @@ -337,6 +344,9 @@ { await ClearMarkers(); + if (SolutionsState.ActiveFile == null || !SolutionsState.ActiveFile.IsCSharp()) + return; + if (_diagnosticsCancellation != null) { try @@ -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) @@ -420,6 +430,7 @@ private async Task GetDiagnosticsAsync(ModelContentChangedEvent evt) { if (!SolutionsState.HasActiveSolution) return; + if (SolutionsState.ActiveFile == null || !SolutionsState.ActiveFile.IsCSharp()) return; try { diff --git a/Apollo.Components/Solutions/SolutionFileExtensions.cs b/Apollo.Components/Solutions/SolutionFileExtensions.cs new file mode 100644 index 0000000..8a7a11b --- /dev/null +++ b/Apollo.Components/Solutions/SolutionFileExtensions.cs @@ -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" + }; +} +