Skip to content

Commit

Permalink
refactor(service): respect diagnostics in code action
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Dec 24, 2024
1 parent 7be16f9 commit 72525e9
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 10 deletions.
23 changes: 15 additions & 8 deletions crates/service/src/features/code_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ impl LanguageService {

let mut quickfix = params.context.only.is_none();
let mut rewrite = params.context.only.is_none();
params.context.only.into_iter().flatten().for_each(|kind| {
if kind == CodeActionKind::QUICKFIX {
quickfix = true;
} else if kind == CodeActionKind::REFACTOR_REWRITE {
rewrite = true;
}
});
params
.context
.only
.iter()
.flatten()
.cloned()
.for_each(|kind| {
if kind == CodeActionKind::QUICKFIX {
quickfix = true;
} else if kind == CodeActionKind::REFACTOR_REWRITE {
rewrite = true;
}
});

let mut actions = vec![];
let range = helpers::lsp_range_to_rowan_range(&line_index, params.range)?;
Expand All @@ -25,7 +31,8 @@ impl LanguageService {
match it.kind() {
SyntaxKind::PLAIN_INSTR => {
if quickfix {
if let Some(action) = fix_invalid_mem_arg::act(self, uri, &line_index, &it)
if let Some(action) =
fix_invalid_mem_arg::act(self, uri, &line_index, &it, &params.context)
{
actions.push(CodeActionOrCommand::CodeAction(action));
}
Expand Down
19 changes: 18 additions & 1 deletion crates/service/src/refactorings/fix_invalid_mem_arg.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::{files::FilesCtx, helpers, InternUri, LanguageService};
use line_index::LineIndex;
use lsp_types::{CodeAction, CodeActionKind, TextEdit, WorkspaceEdit};
use lsp_types::{
CodeAction, CodeActionContext, CodeActionKind, NumberOrString, TextEdit, WorkspaceEdit,
};
use rowan::{ast::AstNode, SyntaxElementChildren, TextRange};
use std::collections::HashMap;
use wat_syntax::{ast::Operand, SyntaxElement, SyntaxKind, SyntaxNode, WatLanguage};
Expand All @@ -10,6 +12,7 @@ pub fn act(
uri: InternUri,
line_index: &LineIndex,
node: &SyntaxNode,
context: &CodeActionContext,
) -> Option<CodeAction> {
let mut text_edits = vec![];

Expand Down Expand Up @@ -86,6 +89,20 @@ pub fn act(
..Default::default()
}),
is_preferred: Some(true),
diagnostics: Some(
context
.diagnostics
.iter()
.filter(|diagnostic| {
if let Some(NumberOrString::String(s)) = &diagnostic.code {
s.starts_with("syntax/")
} else {
false
}
})
.cloned()
.collect(),
),
..Default::default()
})
}
Expand Down
25 changes: 24 additions & 1 deletion crates/service/tests/code_action/fix_invalid_mem_arg.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;
use insta::assert_json_snapshot;
use lsp_types::{Position, Range, Uri};
use lsp_types::{Diagnostic, NumberOrString, Position, Range, Uri};
use wat_service::LanguageService;

#[test]
Expand Down Expand Up @@ -53,3 +53,26 @@ fn around_whitespaces() {
));
assert_json_snapshot!(response);
}

#[test]
fn diagnostics() {
let uri = "untitled:test".parse::<Uri>().unwrap();
let source = "
(module
(func (i32.load align =1))
)
";
let mut service = LanguageService::default();
service.commit(uri.clone(), source.into());
let mut params = create_params(uri, Range::new(Position::new(2, 25), Position::new(2, 25)));
params.context = CodeActionContext {
diagnostics: vec![Diagnostic {
message: "syntax error: expected operand".into(),
code: Some(NumberOrString::String("syntax/operand".into())),
..Default::default()
}],
..Default::default()
};
let response = service.code_action(params);
assert_json_snapshot!(response);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ expression: response
{
"title": "Fix invalid memory argument",
"kind": "quickfix",
"diagnostics": [],
"edit": {
"changes": {
"untitled:test": [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
source: crates/service/tests/code_action/fix_invalid_mem_arg.rs
expression: response
---
[
{
"title": "Fix invalid memory argument",
"kind": "quickfix",
"diagnostics": [
{
"range": {
"start": {
"line": 0,
"character": 0
},
"end": {
"line": 0,
"character": 0
}
},
"code": "syntax/operand",
"message": "syntax error: expected operand"
}
],
"edit": {
"changes": {
"untitled:test": [
{
"range": {
"start": {
"line": 2,
"character": 25
},
"end": {
"line": 2,
"character": 26
}
},
"newText": ""
}
]
}
},
"isPreferred": true
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ expression: response
{
"title": "Fix invalid memory argument",
"kind": "quickfix",
"diagnostics": [],
"edit": {
"changes": {
"untitled:test": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ expression: response
{
"title": "Fix invalid memory argument",
"kind": "quickfix",
"diagnostics": [],
"edit": {
"changes": {
"untitled:test": [
Expand Down

0 comments on commit 72525e9

Please sign in to comment.