Skip to content

Commit 23b6397

Browse files
committed
associate code actions with diagnostics
1 parent 45790d7 commit 23b6397

File tree

6 files changed

+59
-28
lines changed

6 files changed

+59
-28
lines changed

src/codeaction/adddefaulttoswitchaction.vala

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@ using Lsp;
2222
using Gee;
2323

2424
class Vls.AddDefaultToSwitchAction : CodeAction {
25-
public AddDefaultToSwitchAction (Vala.SwitchStatement sws,
25+
public AddDefaultToSwitchAction (CodeActionContext context,
26+
Vala.SwitchStatement sws,
2627
VersionedTextDocumentIdentifier document,
2728
CodeStyleAnalyzer code_style) {
29+
this.title = "Add default case to switch-statement";
30+
this.edit = new WorkspaceEdit ();
31+
2832
var sections = sws.get_sections ();
2933
uint end_line, end_column;
3034
string label_indent, inner_indent;
@@ -52,8 +56,6 @@ class Vls.AddDefaultToSwitchAction : CodeAction {
5256
};
5357
var insert_text = "%sdefault:\n%sassert_not_reached%*s();\n"
5458
.printf (label_indent, inner_indent, code_style.average_spacing_before_parens, "");
55-
this.title = "Add default case to switch-statement";
56-
var workspace_edit = new WorkspaceEdit ();
5759
var document_edit = new TextDocumentEdit (document);
5860
var end_pos = new Position () {
5961
line = end_line - 1,
@@ -64,8 +66,15 @@ class Vls.AddDefaultToSwitchAction : CodeAction {
6466
end = end_pos
6567
}, insert_text);
6668
document_edit.edits.add (text_edit);
67-
workspace_edit.documentChanges = new ArrayList<TextDocumentEdit> ();
68-
workspace_edit.documentChanges.add (document_edit);
69-
this.edit = workspace_edit;
69+
this.edit.documentChanges = new ArrayList<TextDocumentEdit>.wrap ({document_edit});
70+
71+
// now, include all relevant diagnostics
72+
foreach (var diag in context.diagnostics)
73+
if (diag.message.contains ("does not handle"))
74+
add_diagnostic (diag);
75+
if (!diagnostics.is_empty)
76+
this.kind = "quickfix";
77+
else
78+
this.kind = "refactor.rewrite";
7079
}
7180
}

src/codeaction/addotherconstantstoswitchaction.vala

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@ using Gee;
2222
using Lsp;
2323

2424
class Vls.AddOtherConstantsToSwitchAction : CodeAction {
25-
public AddOtherConstantsToSwitchAction (Vala.SwitchStatement sws,
25+
public AddOtherConstantsToSwitchAction (CodeActionContext context,
26+
Vala.SwitchStatement sws,
2627
VersionedTextDocumentIdentifier document,
2728
Vala.Enum e,
2829
HashSet<string> missing,
2930
CodeStyleAnalyzer code_style) {
31+
this.title = "Add missing constants to switch";
32+
this.edit = new WorkspaceEdit ();
33+
3034
var sections = sws.get_sections ();
3135
uint end_line, end_column;
3236
string label_indent, inner_indent;
@@ -64,8 +68,6 @@ class Vls.AddOtherConstantsToSwitchAction : CodeAction {
6468
}
6569
}
6670
var insert_text = sb.str;
67-
this.title = "Add missing constants to switch";
68-
var workspace_edit = new WorkspaceEdit ();
6971
var document_edit = new TextDocumentEdit (document);
7072
var end_pos = new Position () {
7173
line = end_line - 1,
@@ -76,8 +78,14 @@ class Vls.AddOtherConstantsToSwitchAction : CodeAction {
7678
end = end_pos
7779
}, insert_text);
7880
document_edit.edits.add (text_edit);
79-
workspace_edit.documentChanges = new ArrayList<TextDocumentEdit> ();
80-
workspace_edit.documentChanges.add (document_edit);
81-
this.edit = workspace_edit;
81+
this.edit.documentChanges = new ArrayList<TextDocumentEdit>.wrap ({document_edit});
82+
// now, include all relevant diagnostics
83+
foreach (var diag in context.diagnostics)
84+
if (/does not implement|some prerequisites .*are not met/.match (diag.message))
85+
add_diagnostic (diag);
86+
if (!diagnostics.is_empty)
87+
this.kind = "quickfix";
88+
else
89+
this.kind = "refactor.rewrite";
8290
}
8391
}

src/codeaction/implementmissingprereqsaction.vala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ using Gee;
2525
* Implement all missing prerequisites of a class type.
2626
*/
2727
class Vls.ImplementMissingPrereqsAction : CodeAction {
28-
public ImplementMissingPrereqsAction (Vala.Class class_sym,
28+
public ImplementMissingPrereqsAction (CodeActionContext context,
29+
Vala.Class class_sym,
2930
Vala.Collection<Vala.DataType> missing_prereqs,
3031
Vala.Collection<Pair<Vala.DataType, Vala.Symbol>> missing_symbols,
3132
Position classdef_end,
3233
CodeStyleAnalyzer code_style,
3334
VersionedTextDocumentIdentifier document) {
3435
this.title = "Implement missing prerequisites for class";
36+
this.kind = "quickfix";
3537
this.edit = new WorkspaceEdit ();
3638

3739
var changes = new ArrayList<TextDocumentEdit> ();
@@ -159,5 +161,10 @@ class Vls.ImplementMissingPrereqsAction : CodeAction {
159161
}, symbols_insert_text.str));
160162

161163
this.edit.documentChanges = changes;
164+
165+
// now, include all relevant diagnostics
166+
foreach (var diag in context.diagnostics)
167+
if (/does not implement|some prerequisites .*are not met/.match (diag.message))
168+
add_diagnostic (diag);
162169
}
163170
}

src/codehelp/codeaction.vala

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace Vls.CodeActions {
2929
* @param range the range to show code actions for
3030
* @param uri the document URI
3131
*/
32-
Collection<CodeAction> extract (Compilation compilation, TextDocument file, Range range, string uri) {
32+
Collection<CodeAction> extract (CodeActionContext context, Compilation compilation, TextDocument file, Range range, string uri) {
3333
var code_actions = new ArrayList<CodeAction> ();
3434

3535
if (file.last_updated.compare (compilation.last_updated) > 0)
@@ -60,7 +60,9 @@ namespace Vls.CodeActions {
6060
var missing = CodeHelp.gather_missing_prereqs_and_unimplemented_symbols (csym);
6161
if (!missing.first.is_empty || !missing.second.is_empty) {
6262
var code_style = compilation.get_analysis_for_file<CodeStyleAnalyzer> (file);
63-
code_actions.add (new ImplementMissingPrereqsAction (csym, missing.first, missing.second, clsdef_range.end, code_style, document));
63+
code_actions.add (new ImplementMissingPrereqsAction (context,
64+
csym, missing.first, missing.second,
65+
clsdef_range.end, code_style, document));
6466
}
6567
}
6668
} else if (code_node is SwitchStatement) {
@@ -96,9 +98,11 @@ namespace Vls.CodeActions {
9698
continue;
9799
var code_style = compilation.get_analysis_for_file<CodeStyleAnalyzer> (file);
98100
if (!found_default && sws.source_reference != null)
99-
code_actions.add (new AddDefaultToSwitchAction (sws, document, code_style));
101+
code_actions.add (new AddDefaultToSwitchAction (context, sws, document, code_style));
100102
if (!consts_by_name.is_empty && sws.source_reference != null)
101-
code_actions.add (new AddOtherConstantsToSwitchAction (sws, document, (Enum)e, consts_by_name, code_style));
103+
code_actions.add (new AddOtherConstantsToSwitchAction (context,
104+
sws, document,
105+
(Enum)e, consts_by_name, code_style));
102106
} else {
103107
var found_default = false;
104108
foreach (var l in labels) {
@@ -109,7 +113,7 @@ namespace Vls.CodeActions {
109113
}
110114
if (!found_default && sws.source_reference != null) {
111115
var code_style = compilation.get_analysis_for_file<CodeStyleAnalyzer> (file);
112-
code_actions.add (new AddDefaultToSwitchAction (sws, document, code_style));
116+
code_actions.add (new AddDefaultToSwitchAction (context, sws, document, code_style));
113117
}
114118
}
115119
}

src/protocol.vala

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,25 +1005,27 @@ namespace Lsp {
10051005
class CodeAction : Object, Json.Serializable {
10061006
public string title { get; set; }
10071007
public string? kind { get; set; }
1008-
public Gee.List<Diagnostic>? diagnostics { get; set; }
1008+
public Gee.Collection<Diagnostic>? diagnostics { get; set; }
10091009
public bool isPreferred { get; set; }
10101010
public WorkspaceEdit? edit { get; set; }
10111011
public Command? command { get; set; }
10121012
public Object? data { get; set; }
10131013

1014+
protected void add_diagnostic (Diagnostic diag) {
1015+
if (diagnostics == null)
1016+
diagnostics = new Gee.ArrayList<Diagnostic> ();
1017+
diagnostics.add (diag);
1018+
}
1019+
10141020
public override Json.Node serialize_property (string property_name, GLib.Value value, GLib.ParamSpec pspec) {
10151021
if (property_name != "diagnostics")
10161022
return default_serialize_property (property_name, value, pspec);
10171023

1018-
var node = new Json.Node (Json.NodeType.ARRAY);
1019-
node.init_array (new Json.Array ());
1020-
if (diagnostics != null) {
1021-
var array = node.get_array ();
1022-
foreach (var text_edit in diagnostics) {
1024+
var array = new Json.Array ();
1025+
if (diagnostics != null)
1026+
foreach (var text_edit in diagnostics)
10231027
array.add_element (Json.gobject_serialize (text_edit));
1024-
}
1025-
}
1026-
return node;
1028+
return new Json.Node.alloc ().init_array (array);
10271029
}
10281030
}
10291031

src/server.vala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1664,7 +1664,8 @@ class Vls.Server : Jsonrpc.Server {
16641664
var json_array = new Json.Array ();
16651665

16661666
Vala.CodeContext.push (compilation.code_context);
1667-
var code_actions = CodeActions.extract (compilation, (TextDocument) source_file, p.range, Uri.unescape_string (p.textDocument.uri));
1667+
var code_actions = CodeActions.extract (p.context, compilation,
1668+
(TextDocument) source_file, p.range, Uri.unescape_string (p.textDocument.uri));
16681669
foreach (var action in code_actions)
16691670
json_array.add_element (Json.gobject_serialize (action));
16701671
Vala.CodeContext.pop ();

0 commit comments

Comments
 (0)