Skip to content

Commit

Permalink
associate code actions with diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
Prince781 committed Nov 24, 2022
1 parent 45790d7 commit 23b6397
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 28 deletions.
21 changes: 15 additions & 6 deletions src/codeaction/adddefaulttoswitchaction.vala
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ using Lsp;
using Gee;

class Vls.AddDefaultToSwitchAction : CodeAction {
public AddDefaultToSwitchAction (Vala.SwitchStatement sws,
public AddDefaultToSwitchAction (CodeActionContext context,
Vala.SwitchStatement sws,
VersionedTextDocumentIdentifier document,
CodeStyleAnalyzer code_style) {
this.title = "Add default case to switch-statement";
this.edit = new WorkspaceEdit ();

var sections = sws.get_sections ();
uint end_line, end_column;
string label_indent, inner_indent;
Expand Down Expand Up @@ -52,8 +56,6 @@ class Vls.AddDefaultToSwitchAction : CodeAction {
};
var insert_text = "%sdefault:\n%sassert_not_reached%*s();\n"
.printf (label_indent, inner_indent, code_style.average_spacing_before_parens, "");
this.title = "Add default case to switch-statement";
var workspace_edit = new WorkspaceEdit ();
var document_edit = new TextDocumentEdit (document);
var end_pos = new Position () {
line = end_line - 1,
Expand All @@ -64,8 +66,15 @@ class Vls.AddDefaultToSwitchAction : CodeAction {
end = end_pos
}, insert_text);
document_edit.edits.add (text_edit);
workspace_edit.documentChanges = new ArrayList<TextDocumentEdit> ();
workspace_edit.documentChanges.add (document_edit);
this.edit = workspace_edit;
this.edit.documentChanges = new ArrayList<TextDocumentEdit>.wrap ({document_edit});

// now, include all relevant diagnostics
foreach (var diag in context.diagnostics)
if (diag.message.contains ("does not handle"))
add_diagnostic (diag);
if (!diagnostics.is_empty)
this.kind = "quickfix";
else
this.kind = "refactor.rewrite";
}
}
20 changes: 14 additions & 6 deletions src/codeaction/addotherconstantstoswitchaction.vala
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ using Gee;
using Lsp;

class Vls.AddOtherConstantsToSwitchAction : CodeAction {
public AddOtherConstantsToSwitchAction (Vala.SwitchStatement sws,
public AddOtherConstantsToSwitchAction (CodeActionContext context,
Vala.SwitchStatement sws,
VersionedTextDocumentIdentifier document,
Vala.Enum e,
HashSet<string> missing,
CodeStyleAnalyzer code_style) {
this.title = "Add missing constants to switch";
this.edit = new WorkspaceEdit ();

var sections = sws.get_sections ();
uint end_line, end_column;
string label_indent, inner_indent;
Expand Down Expand Up @@ -64,8 +68,6 @@ class Vls.AddOtherConstantsToSwitchAction : CodeAction {
}
}
var insert_text = sb.str;
this.title = "Add missing constants to switch";
var workspace_edit = new WorkspaceEdit ();
var document_edit = new TextDocumentEdit (document);
var end_pos = new Position () {
line = end_line - 1,
Expand All @@ -76,8 +78,14 @@ class Vls.AddOtherConstantsToSwitchAction : CodeAction {
end = end_pos
}, insert_text);
document_edit.edits.add (text_edit);
workspace_edit.documentChanges = new ArrayList<TextDocumentEdit> ();
workspace_edit.documentChanges.add (document_edit);
this.edit = workspace_edit;
this.edit.documentChanges = new ArrayList<TextDocumentEdit>.wrap ({document_edit});
// now, include all relevant diagnostics
foreach (var diag in context.diagnostics)
if (/does not implement|some prerequisites .*are not met/.match (diag.message))
add_diagnostic (diag);
if (!diagnostics.is_empty)
this.kind = "quickfix";
else
this.kind = "refactor.rewrite";
}
}
9 changes: 8 additions & 1 deletion src/codeaction/implementmissingprereqsaction.vala
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ using Gee;
* Implement all missing prerequisites of a class type.
*/
class Vls.ImplementMissingPrereqsAction : CodeAction {
public ImplementMissingPrereqsAction (Vala.Class class_sym,
public ImplementMissingPrereqsAction (CodeActionContext context,
Vala.Class class_sym,
Vala.Collection<Vala.DataType> missing_prereqs,
Vala.Collection<Pair<Vala.DataType, Vala.Symbol>> missing_symbols,
Position classdef_end,
CodeStyleAnalyzer code_style,
VersionedTextDocumentIdentifier document) {
this.title = "Implement missing prerequisites for class";
this.kind = "quickfix";
this.edit = new WorkspaceEdit ();

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

this.edit.documentChanges = changes;

// now, include all relevant diagnostics
foreach (var diag in context.diagnostics)
if (/does not implement|some prerequisites .*are not met/.match (diag.message))
add_diagnostic (diag);
}
}
14 changes: 9 additions & 5 deletions src/codehelp/codeaction.vala
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace Vls.CodeActions {
* @param range the range to show code actions for
* @param uri the document URI
*/
Collection<CodeAction> extract (Compilation compilation, TextDocument file, Range range, string uri) {
Collection<CodeAction> extract (CodeActionContext context, Compilation compilation, TextDocument file, Range range, string uri) {
var code_actions = new ArrayList<CodeAction> ();

if (file.last_updated.compare (compilation.last_updated) > 0)
Expand Down Expand Up @@ -60,7 +60,9 @@ namespace Vls.CodeActions {
var missing = CodeHelp.gather_missing_prereqs_and_unimplemented_symbols (csym);
if (!missing.first.is_empty || !missing.second.is_empty) {
var code_style = compilation.get_analysis_for_file<CodeStyleAnalyzer> (file);
code_actions.add (new ImplementMissingPrereqsAction (csym, missing.first, missing.second, clsdef_range.end, code_style, document));
code_actions.add (new ImplementMissingPrereqsAction (context,
csym, missing.first, missing.second,
clsdef_range.end, code_style, document));
}
}
} else if (code_node is SwitchStatement) {
Expand Down Expand Up @@ -96,9 +98,11 @@ namespace Vls.CodeActions {
continue;
var code_style = compilation.get_analysis_for_file<CodeStyleAnalyzer> (file);
if (!found_default && sws.source_reference != null)
code_actions.add (new AddDefaultToSwitchAction (sws, document, code_style));
code_actions.add (new AddDefaultToSwitchAction (context, sws, document, code_style));
if (!consts_by_name.is_empty && sws.source_reference != null)
code_actions.add (new AddOtherConstantsToSwitchAction (sws, document, (Enum)e, consts_by_name, code_style));
code_actions.add (new AddOtherConstantsToSwitchAction (context,
sws, document,
(Enum)e, consts_by_name, code_style));
} else {
var found_default = false;
foreach (var l in labels) {
Expand All @@ -109,7 +113,7 @@ namespace Vls.CodeActions {
}
if (!found_default && sws.source_reference != null) {
var code_style = compilation.get_analysis_for_file<CodeStyleAnalyzer> (file);
code_actions.add (new AddDefaultToSwitchAction (sws, document, code_style));
code_actions.add (new AddDefaultToSwitchAction (context, sws, document, code_style));
}
}
}
Expand Down
20 changes: 11 additions & 9 deletions src/protocol.vala
Original file line number Diff line number Diff line change
Expand Up @@ -1005,25 +1005,27 @@ namespace Lsp {
class CodeAction : Object, Json.Serializable {
public string title { get; set; }
public string? kind { get; set; }
public Gee.List<Diagnostic>? diagnostics { get; set; }
public Gee.Collection<Diagnostic>? diagnostics { get; set; }
public bool isPreferred { get; set; }
public WorkspaceEdit? edit { get; set; }
public Command? command { get; set; }
public Object? data { get; set; }

protected void add_diagnostic (Diagnostic diag) {
if (diagnostics == null)
diagnostics = new Gee.ArrayList<Diagnostic> ();
diagnostics.add (diag);
}

public override Json.Node serialize_property (string property_name, GLib.Value value, GLib.ParamSpec pspec) {
if (property_name != "diagnostics")
return default_serialize_property (property_name, value, pspec);

var node = new Json.Node (Json.NodeType.ARRAY);
node.init_array (new Json.Array ());
if (diagnostics != null) {
var array = node.get_array ();
foreach (var text_edit in diagnostics) {
var array = new Json.Array ();
if (diagnostics != null)
foreach (var text_edit in diagnostics)
array.add_element (Json.gobject_serialize (text_edit));
}
}
return node;
return new Json.Node.alloc ().init_array (array);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/server.vala
Original file line number Diff line number Diff line change
Expand Up @@ -1664,7 +1664,8 @@ class Vls.Server : Jsonrpc.Server {
var json_array = new Json.Array ();

Vala.CodeContext.push (compilation.code_context);
var code_actions = CodeActions.extract (compilation, (TextDocument) source_file, p.range, Uri.unescape_string (p.textDocument.uri));
var code_actions = CodeActions.extract (p.context, compilation,
(TextDocument) source_file, p.range, Uri.unescape_string (p.textDocument.uri));
foreach (var action in code_actions)
json_array.add_element (Json.gobject_serialize (action));
Vala.CodeContext.pop ();
Expand Down

0 comments on commit 23b6397

Please sign in to comment.