@@ -23,13 +23,13 @@ using Vala;
23
23
24
24
namespace Vls.CodeActions {
25
25
/**
26
- * Extracts a list of code actions for the given document and range.
26
+ * Extracts a list of code actions for the given document and range using the AST and the diagnostics .
27
27
*
28
28
* @param file the current document
29
29
* @param range the range to show code actions for
30
30
* @param uri the document URI
31
31
*/
32
- Collection<CodeAction > extract (Compilation compilation , TextDocument file , Range range , string uri ) {
32
+ Collection<CodeAction > generate_codeactions (Compilation compilation , TextDocument file , Range range , string uri , Reporter reporter ) {
33
33
// search for nodes containing the query range
34
34
var finder = new NodeSearch (file, range. start, true , range. end);
35
35
var code_actions = new ArrayList<CodeAction > ();
@@ -41,13 +41,14 @@ namespace Vls.CodeActions {
41
41
42
42
// add code actions
43
43
foreach (CodeNode code_node in finder. result) {
44
+ critical (" %s " , code_node. type_name);
44
45
if (code_node is IntegerLiteral ) {
45
- var lit = (IntegerLiteral )code_node;
46
+ var lit = (IntegerLiteral ) code_node;
46
47
var lit_range = new Range .from_sourceref (lit. source_reference);
47
48
if (lit_range. contains (range. start) && lit_range. contains (range. end))
48
49
code_actions. add (new BaseConverterAction (lit, document));
49
50
} else if (code_node is Class ) {
50
- var csym = (Class )code_node;
51
+ var csym = (Class ) code_node;
51
52
var clsdef_range = compute_class_def_range (csym, class_ranges);
52
53
var cls_range = new Range .from_sourceref (csym. source_reference);
53
54
if (cls_range. contains (range. start) && cls_range. contains (range. end)) {
@@ -57,6 +58,29 @@ namespace Vls.CodeActions {
57
58
code_actions. add (new ImplementMissingPrereqsAction (csym, missing. first, missing. second, clsdef_range. end, code_style, document));
58
59
}
59
60
}
61
+ } else if (code_node is ObjectCreationExpression ) {
62
+ var oce = (ObjectCreationExpression ) code_node;
63
+ foreach (var diag in reporter. messages) {
64
+ if (file. filename != diag. loc. file. filename)
65
+ continue ;
66
+ if (! (oce. source_reference. contains (diag. loc. begin) || oce. source_reference. contains (diag. loc. end)))
67
+ continue ;
68
+ if (diag. message. contains (" extra arguments for " )) {
69
+ var to_be_created = oce. type_reference. symbol;
70
+ if (! (to_be_created is Vala . Class )) {
71
+ continue ;
72
+ }
73
+ var constr = ((Vala . Class ) to_be_created). constructor;
74
+ if (constr != null ) {
75
+ continue ;
76
+ }
77
+ var target_file = to_be_created. source_reference. file;
78
+ // We can't just edit, e.g. some external vapi
79
+ if (! compilation. get_project_files (). contains (target_file))
80
+ continue ;
81
+ code_actions. add (new ImplementConstructorAction (oce, to_be_created));
82
+ }
83
+ }
60
84
}
61
85
}
62
86
@@ -72,11 +96,11 @@ namespace Vls.CodeActions {
72
96
// otherwise compute the result and cache it
73
97
// csym.source_reference must be non-null otherwise NodeSearch wouldn't have found csym
74
98
var pos = new Position .from_libvala (csym. source_reference. end);
75
- var offset = csym. source_reference. end. pos - (char * ) csym. source_reference. file. content;
99
+ var offset = csym. source_reference. end. pos - (char * ) csym. source_reference. file. content;
76
100
var dl = 0 ;
77
101
var dc = 0 ;
78
- while (offset < csym. source_reference. file. content. length && csym. source_reference. file. content[(long )offset] != ' {' ) {
79
- if (Util . is_newline (csym. source_reference. file. content[(long )offset])) {
102
+ while (offset < csym. source_reference. file. content. length && csym. source_reference. file. content[(long ) offset] != ' {' ) {
103
+ if (Util . is_newline (csym. source_reference. file. content[(long ) offset])) {
80
104
dl++ ;
81
105
dc = 0 ;
82
106
} else {
@@ -93,8 +117,8 @@ namespace Vls.CodeActions {
93
117
if (member. source_reference == null )
94
118
continue ;
95
119
range = range. union (new Range .from_sourceref (member. source_reference));
96
- if (member is Method && ((Method )member). body != null && ((Method )member). body. source_reference != null )
97
- range = range. union (new Range .from_sourceref (((Method )member). body. source_reference));
120
+ if (member is Method && ((Method ) member). body != null && ((Method ) member). body. source_reference != null )
121
+ range = range. union (new Range .from_sourceref (((Method ) member). body. source_reference));
98
122
}
99
123
class_ranges[csym] = range;
100
124
return range;
0 commit comments