From d6b1194b251ed40769c186b704dd532921c83ac0 Mon Sep 17 00:00:00 2001 From: Matt Parker <61717342+MattParkerDev@users.noreply.github.com> Date: Wed, 10 Dec 2025 20:51:08 +1000 Subject: [PATCH] get refactoring code actions from project references --- .../Features/Analysis/RoslynAnalysis.cs | 26 ++++++++++++++++--- .../Features/CodeEditor/SharpIdeCodeEdit.cs | 2 +- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/SharpIDE.Application/Features/Analysis/RoslynAnalysis.cs b/src/SharpIDE.Application/Features/Analysis/RoslynAnalysis.cs index eebfaff..b841632 100644 --- a/src/SharpIDE.Application/Features/Analysis/RoslynAnalysis.cs +++ b/src/SharpIDE.Application/Features/Analysis/RoslynAnalysis.cs @@ -604,9 +604,9 @@ public class RoslynAnalysis(ILogger logger, BuildService buildSe } // TODO: Pass in LinePositionSpan for refactorings that span multiple characters, e.g. extract method - public async Task> GetCodeFixesForDocumentAtPosition(SharpIdeFile fileModel, LinePosition linePosition, CancellationToken cancellationToken = default) + public async Task> GetCodeActionsForDocumentAtPosition(SharpIdeFile fileModel, LinePosition linePosition, CancellationToken cancellationToken = default) { - using var _ = SharpIdeOtel.Source.StartActivity($"{nameof(RoslynAnalysis)}.{nameof(GetCodeFixesForDocumentAtPosition)}"); + using var _ = SharpIdeOtel.Source.StartActivity($"{nameof(RoslynAnalysis)}.{nameof(GetCodeActionsForDocumentAtPosition)}"); await _solutionLoadedTcs.Task; var document = await GetDocumentForSharpIdeFile(fileModel, cancellationToken); Guard.Against.Null(document, nameof(document)); @@ -635,7 +635,10 @@ public class RoslynAnalysis(ILogger logger, BuildService buildSe var linePositionSpan = new LinePositionSpan(linePosition, new LinePosition(linePosition.Line, linePosition.Character + 1)); var selectedSpan = sourceText.Lines.GetTextSpan(linePositionSpan); - arrayBuilder.AddRange(await GetCodeRefactoringsAsync(document, selectedSpan, cancellationToken)); + + var codeRefactorings = await GetCodeRefactoringsAsync(document, selectedSpan, cancellationToken); + arrayBuilder.AddRange(codeRefactorings); + return arrayBuilder.ToImmutable(); } @@ -679,6 +682,23 @@ public class RoslynAnalysis(ILogger logger, BuildService buildSe } private static async Task> GetCodeRefactoringsAsync(Document document, TextSpan span, CancellationToken cancellationToken = default) + { + var refactorings = await _codeRefactoringService!.GetRefactoringsAsync( + document, + span, + cancellationToken); + + var codeActions = refactorings + .SelectMany(collection => collection.CodeActions) + .Where(s => s.action.NestedActions.Length is 0) // Currently, nested actions are not supported + .Select(s => s.action) + .ToImmutableArray(); + + return codeActions; + } + + [Obsolete] // ICodeRefactoringService seems to return refactorings from DefaultAssemblies! Unlike ICodeFixService. Leaving for reference + private static async Task> GetCodeRefactoringsFromDefaultAssembliesAsync(Document document, TextSpan span, CancellationToken cancellationToken = default) { var codeActions = new List(); var refactorContext = new CodeRefactoringContext( diff --git a/src/SharpIDE.Godot/Features/CodeEditor/SharpIdeCodeEdit.cs b/src/SharpIDE.Godot/Features/CodeEditor/SharpIdeCodeEdit.cs index 287cb22..9fbf99e 100644 --- a/src/SharpIDE.Godot/Features/CodeEditor/SharpIdeCodeEdit.cs +++ b/src/SharpIDE.Godot/Features/CodeEditor/SharpIdeCodeEdit.cs @@ -517,7 +517,7 @@ public partial class SharpIdeCodeEdit : CodeEdit _ = Task.GodotRun(async () => { var linePos = new LinePosition(caretLine, caretColumn); - var codeActions = await _roslynAnalysis.GetCodeFixesForDocumentAtPosition(_currentFile, linePos); + var codeActions = await _roslynAnalysis.GetCodeActionsForDocumentAtPosition(_currentFile, linePos); await this.InvokeAsync(() => { _popupMenu.Clear();