diff --git a/src/codeaction/adddefaulttoswitchaction.vala b/src/codeaction/adddefaulttoswitchaction.vala index bcb9541e..f3c4ca97 100644 --- a/src/codeaction/adddefaulttoswitchaction.vala +++ b/src/codeaction/adddefaulttoswitchaction.vala @@ -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; @@ -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, @@ -64,8 +66,15 @@ class Vls.AddDefaultToSwitchAction : CodeAction { end = end_pos }, insert_text); document_edit.edits.add (text_edit); - workspace_edit.documentChanges = new ArrayList (); - workspace_edit.documentChanges.add (document_edit); - this.edit = workspace_edit; + this.edit.documentChanges = new ArrayList.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"; } } diff --git a/src/codeaction/addotherconstantstoswitchaction.vala b/src/codeaction/addotherconstantstoswitchaction.vala index 0406d399..7e7b7e56 100644 --- a/src/codeaction/addotherconstantstoswitchaction.vala +++ b/src/codeaction/addotherconstantstoswitchaction.vala @@ -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 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; @@ -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, @@ -76,8 +78,14 @@ class Vls.AddOtherConstantsToSwitchAction : CodeAction { end = end_pos }, insert_text); document_edit.edits.add (text_edit); - workspace_edit.documentChanges = new ArrayList (); - workspace_edit.documentChanges.add (document_edit); - this.edit = workspace_edit; + this.edit.documentChanges = new ArrayList.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"; } } diff --git a/src/codeaction/implementmissingprereqsaction.vala b/src/codeaction/implementmissingprereqsaction.vala index c6570c29..d421d8af 100644 --- a/src/codeaction/implementmissingprereqsaction.vala +++ b/src/codeaction/implementmissingprereqsaction.vala @@ -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 missing_prereqs, Vala.Collection> 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 (); @@ -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); } } diff --git a/src/codehelp/codeaction.vala b/src/codehelp/codeaction.vala index fb887d6f..15a61a06 100644 --- a/src/codehelp/codeaction.vala +++ b/src/codehelp/codeaction.vala @@ -29,7 +29,7 @@ namespace Vls.CodeActions { * @param range the range to show code actions for * @param uri the document URI */ - Collection extract (Compilation compilation, TextDocument file, Range range, string uri) { + Collection extract (CodeActionContext context, Compilation compilation, TextDocument file, Range range, string uri) { var code_actions = new ArrayList (); if (file.last_updated.compare (compilation.last_updated) > 0) @@ -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 (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) { @@ -96,9 +98,11 @@ namespace Vls.CodeActions { continue; var code_style = compilation.get_analysis_for_file (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) { @@ -109,7 +113,7 @@ namespace Vls.CodeActions { } if (!found_default && sws.source_reference != null) { var code_style = compilation.get_analysis_for_file (file); - code_actions.add (new AddDefaultToSwitchAction (sws, document, code_style)); + code_actions.add (new AddDefaultToSwitchAction (context, sws, document, code_style)); } } } diff --git a/src/protocol.vala b/src/protocol.vala index a371deb7..a8dce48e 100644 --- a/src/protocol.vala +++ b/src/protocol.vala @@ -1005,25 +1005,27 @@ namespace Lsp { class CodeAction : Object, Json.Serializable { public string title { get; set; } public string? kind { get; set; } - public Gee.List? diagnostics { get; set; } + public Gee.Collection? 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 (); + 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); } } diff --git a/src/server.vala b/src/server.vala index 418817d6..c931ac01 100644 --- a/src/server.vala +++ b/src/server.vala @@ -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 ();