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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use crate::{
binder::SymbolTablesCtx, helpers, refactorings::*, syntax_tree::SyntaxTreeCtx, uri::UrisCtx,
LanguageService,
};
use lsp_types::{CodeActionKind, CodeActionOrCommand, CodeActionParams};
use wat_syntax::{SyntaxElement, SyntaxKind, SyntaxNode};
impl LanguageService {
/// Handler for `textDocument/codeAction` request.
pub fn code_action(&self, params: CodeActionParams) -> Option<Vec<CodeActionOrCommand>> {
let uri = self.uri(params.text_document.uri.clone());
let line_index = self.line_index(uri);
let root = SyntaxNode::new_root(self.root(uri));
let symbol_table = self.symbol_table(uri);
let mut quickfix = params.context.only.is_none();
let mut rewrite = params.context.only.is_none();
let mut inline = params.context.only.is_none();
params
.context
.only
.iter()
.flatten()
.cloned()
.for_each(|kind| {
if kind == CodeActionKind::QUICKFIX {
quickfix = true;
} else if kind == CodeActionKind::REFACTOR_REWRITE {
rewrite = true;
} else if kind == CodeActionKind::REFACTOR_INLINE {
inline = true;
}
});
let mut actions = vec![];
let range = helpers::lsp_range_to_rowan_range(&line_index, params.range)?;
let mut node = root.clone();
while let Some(SyntaxElement::Node(it)) = node.child_or_token_at_range(range) {
match it.kind() {
SyntaxKind::MODULE_FIELD_FUNC => {
if rewrite {
if let Some(action) = func_header_join::act(
self,
uri,
&line_index,
&it,
SyntaxKind::LOCAL,
range,
) {
actions.push(CodeActionOrCommand::CodeAction(action));
}
}
}
SyntaxKind::PLAIN_INSTR => {
if quickfix {
if let Some(action) =
fix_invalid_mem_arg::act(self, uri, &line_index, &it, ¶ms.context)
{
actions.push(CodeActionOrCommand::CodeAction(action));
}
}
if rewrite {
if let Some(action) = br_if_to_if_br::act(self, uri, &line_index, &it) {
actions.push(CodeActionOrCommand::CodeAction(action));
}
}
}
SyntaxKind::PARAM => {
if rewrite {
if let Some(action) =
func_header_split::act(self, uri, &line_index, &it, SyntaxKind::PARAM)
{
actions.push(CodeActionOrCommand::CodeAction(action));
}
}
}
SyntaxKind::RESULT => {
if rewrite {
if let Some(action) =
func_header_split::act(self, uri, &line_index, &it, SyntaxKind::RESULT)
{
actions.push(CodeActionOrCommand::CodeAction(action));
}
}
}
SyntaxKind::LOCAL => {
if rewrite {
if let Some(action) =
func_header_split::act(self, uri, &line_index, &it, SyntaxKind::LOCAL)
{
actions.push(CodeActionOrCommand::CodeAction(action));
}
}
}
SyntaxKind::TYPE_USE | SyntaxKind::FUNC_TYPE => {
if rewrite {
if let Some(action) = func_header_join::act(
self,
uri,
&line_index,
&it,
SyntaxKind::PARAM,
range,
) {
actions.push(CodeActionOrCommand::CodeAction(action));
}
if let Some(action) = func_header_join::act(
self,
uri,
&line_index,
&it,
SyntaxKind::RESULT,
range,
) {
actions.push(CodeActionOrCommand::CodeAction(action));
}
}
if inline {
if let Some(action) =
inline_func_type::act(self, uri, &line_index, &root, &symbol_table, &it)
{
actions.push(CodeActionOrCommand::CodeAction(action));
}
}
}
SyntaxKind::BLOCK_IF => {
if rewrite {
if let Some(action) = if_br_to_br_if::act(self, uri, &line_index, &it) {
actions.push(CodeActionOrCommand::CodeAction(action));
}
}
}
SyntaxKind::GLOBAL_TYPE => {
if quickfix {
if let Some(action) =
remove_mut::act(self, uri, &line_index, &it, ¶ms.context)
{
actions.push(CodeActionOrCommand::CodeAction(action));
}
}
}
_ => {}
}
node = it;
}
if actions.is_empty() {
None
} else {
Some(actions)
}
}
}