-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLspCodeActionsCommand.cs
More file actions
126 lines (103 loc) · 4.56 KB
/
LspCodeActionsCommand.cs
File metadata and controls
126 lines (103 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using Cast.Tool.Core;
using Spectre.Console;
using Spectre.Console.Cli;
using System.ComponentModel;
using Newtonsoft.Json.Linq;
namespace Cast.Tool.Commands;
public class LspCodeActionsCommand : Command<LspCodeActionsCommand.Settings>
{
public class Settings : LspSettings
{
[CommandOption("--end-line")]
[Description("End line number (0-based) for range selection")]
public int? EndLineNumber { get; init; }
[CommandOption("--end-column")]
[Description("End column number (0-based) for range selection")]
public int? EndColumnNumber { get; init; }
}
public override int Execute(CommandContext context, Settings settings)
{
return ExecuteAsync(context, settings).GetAwaiter().GetResult();
}
public async Task<int> ExecuteAsync(CommandContext context, Settings settings)
{
var helper = new LspHelper();
try
{
helper.ValidateInputs(settings);
using var client = await helper.CreateLspClientAsync(settings);
if (client == null) return 1;
var documentUri = helper.CreateDocumentUri(settings.FilePath);
var languageId = helper.GetLanguageId(settings.FilePath);
var fileContent = await File.ReadAllTextAsync(settings.FilePath);
// Open the document
await client.DidOpenAsync(documentUri, languageId, fileContent);
// Give the server time to analyze the document
await Task.Delay(1000);
// Create range for code actions
var startPosition = helper.CreatePosition(settings);
var endPosition = settings.EndLineNumber.HasValue || settings.EndColumnNumber.HasValue
? new LspPosition(settings.EndLineNumber ?? settings.LineNumber, settings.EndColumnNumber ?? settings.ColumnNumber)
: startPosition;
var range = new LspRange(startPosition, endPosition);
// Request code actions
var result = await client.GetCodeActionsAsync(documentUri, range);
if (result == null)
{
AnsiConsole.WriteLine("[yellow]No code actions available for the specified range.[/]");
return 0;
}
if (result is JArray actionsArray)
{
AnsiConsole.WriteLine($"[green]Available code actions ({actionsArray.Count}):[/]");
var actionIndex = 1;
foreach (var action in actionsArray)
{
var title = action["title"]?.ToString();
var kind = action["kind"]?.ToString();
var command = action["command"];
var edit = action["edit"];
var diagnostics = action["diagnostics"] as JArray;
if (!string.IsNullOrEmpty(title))
{
AnsiConsole.WriteLine($"{actionIndex}. [cyan]{title}[/]");
if (!string.IsNullOrEmpty(kind))
{
AnsiConsole.WriteLine($" Kind: {kind}");
}
if (diagnostics != null && diagnostics.Count > 0)
{
AnsiConsole.WriteLine($" Fixes {diagnostics.Count} diagnostic(s)");
}
if (edit?["changes"] != null)
{
var changes = edit["changes"] as JObject;
if (changes != null)
{
AnsiConsole.WriteLine($" Affects {changes.Count} file(s)");
}
}
if (command != null)
{
var commandName = command["command"]?.ToString();
if (!string.IsNullOrEmpty(commandName))
{
AnsiConsole.WriteLine($" Command: {commandName}");
}
}
}
actionIndex++;
AnsiConsole.WriteLine();
}
}
// Close the document
await client.DidCloseAsync(documentUri);
return 0;
}
catch (Exception ex)
{
AnsiConsole.WriteLine($"[red]Error: {ex.Message}[/]");
return 1;
}
}
}