mirror of
https://github.com/slint-ui/slint.git
synced 2025-11-17 18:57:10 +00:00
LSP: add "Repeat element" and "Make conditional" code actions
This commit is contained in:
parent
670fed7dbd
commit
0d366e0087
2 changed files with 103 additions and 19 deletions
|
|
@ -39,7 +39,7 @@ All notable changes to this project are documented in this file.
|
||||||
|
|
||||||
### LSP
|
### LSP
|
||||||
|
|
||||||
- Added "Wrap in element" and "Remove element" code actions
|
- Added "Wrap in element", "Remove element", "Repeat element", and "Make conditional" code actions
|
||||||
|
|
||||||
## [1.2.2] - 2023-10-02
|
## [1.2.2] - 2023-10-02
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -943,7 +943,47 @@ fn get_code_actions(
|
||||||
title: "Remove element".into(),
|
title: "Remove element".into(),
|
||||||
kind: Some(lsp_types::CodeActionKind::REFACTOR),
|
kind: Some(lsp_types::CodeActionKind::REFACTOR),
|
||||||
edit: Some(WorkspaceEdit {
|
edit: Some(WorkspaceEdit {
|
||||||
changes: Some(std::iter::once((uri, edits)).collect()),
|
changes: Some(std::iter::once((uri.clone(), edits)).collect()),
|
||||||
|
..Default::default()
|
||||||
|
}),
|
||||||
|
..Default::default()
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
// We have already checked that the node is a qualified name of an element.
|
||||||
|
// Check whether the element is a direct sub-element of another element
|
||||||
|
// meaning that it can be repeated or made conditional.
|
||||||
|
if node // QualifiedName
|
||||||
|
.parent() // Element
|
||||||
|
.unwrap()
|
||||||
|
.parent()
|
||||||
|
.filter(|n| n.kind() == SyntaxKind::SubElement)
|
||||||
|
.and_then(|p| p.parent())
|
||||||
|
.is_some_and(|n| n.kind() == SyntaxKind::Element)
|
||||||
|
{
|
||||||
|
let edits = vec![TextEdit::new(
|
||||||
|
lsp_types::Range::new(r.start, r.start),
|
||||||
|
"for ${1:name}[index] in ${0:model} : ".to_string(),
|
||||||
|
)];
|
||||||
|
result.push(CodeActionOrCommand::CodeAction(lsp_types::CodeAction {
|
||||||
|
title: "Repeat element".into(),
|
||||||
|
kind: Some(lsp_types::CodeActionKind::REFACTOR),
|
||||||
|
edit: Some(WorkspaceEdit {
|
||||||
|
changes: Some(std::iter::once((uri.clone(), edits)).collect()),
|
||||||
|
..Default::default()
|
||||||
|
}),
|
||||||
|
..Default::default()
|
||||||
|
}));
|
||||||
|
|
||||||
|
let edits = vec![TextEdit::new(
|
||||||
|
lsp_types::Range::new(r.start, r.start),
|
||||||
|
"if ${0:condition} : ".to_string(),
|
||||||
|
)];
|
||||||
|
result.push(CodeActionOrCommand::CodeAction(lsp_types::CodeAction {
|
||||||
|
title: "Make conditional".into(),
|
||||||
|
kind: Some(lsp_types::CodeActionKind::REFACTOR),
|
||||||
|
edit: Some(WorkspaceEdit {
|
||||||
|
changes: Some(std::iter::once((uri.clone(), edits)).collect()),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}),
|
}),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
|
|
@ -1522,30 +1562,74 @@ export component TestWindow inherits Window {
|
||||||
token,
|
token,
|
||||||
&capabilities
|
&capabilities
|
||||||
)),
|
)),
|
||||||
Some(vec![CodeActionOrCommand::CodeAction(lsp_types::CodeAction {
|
Some(vec![
|
||||||
title: "Wrap in element".into(),
|
CodeActionOrCommand::CodeAction(lsp_types::CodeAction {
|
||||||
kind: Some(lsp_types::CodeActionKind::REFACTOR),
|
title: "Wrap in element".into(),
|
||||||
edit: Some(WorkspaceEdit {
|
kind: Some(lsp_types::CodeActionKind::REFACTOR),
|
||||||
changes: Some(
|
edit: Some(WorkspaceEdit {
|
||||||
std::iter::once((
|
changes: Some(
|
||||||
url.clone(),
|
std::iter::once((
|
||||||
vec![TextEdit::new(
|
url.clone(),
|
||||||
text_element,
|
vec![TextEdit::new(
|
||||||
r#"${0:element} {
|
text_element,
|
||||||
|
r#"${0:element} {
|
||||||
Text {
|
Text {
|
||||||
text: "Hello World!";
|
text: "Hello World!";
|
||||||
font-size: 20px;
|
font-size: 20px;
|
||||||
}
|
}
|
||||||
}"#
|
}"#
|
||||||
.into()
|
.into()
|
||||||
)]
|
)]
|
||||||
))
|
))
|
||||||
.collect()
|
.collect()
|
||||||
),
|
),
|
||||||
|
..Default::default()
|
||||||
|
}),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}),
|
}),
|
||||||
..Default::default()
|
CodeActionOrCommand::CodeAction(lsp_types::CodeAction {
|
||||||
}),])
|
title: "Repeat element".into(),
|
||||||
|
kind: Some(lsp_types::CodeActionKind::REFACTOR),
|
||||||
|
edit: Some(WorkspaceEdit {
|
||||||
|
changes: Some(
|
||||||
|
std::iter::once((
|
||||||
|
url.clone(),
|
||||||
|
vec![TextEdit::new(
|
||||||
|
lsp_types::Range::new(
|
||||||
|
text_element.start,
|
||||||
|
text_element.start
|
||||||
|
),
|
||||||
|
r#"for ${1:name}[index] in ${0:model} : "#.into()
|
||||||
|
)]
|
||||||
|
))
|
||||||
|
.collect()
|
||||||
|
),
|
||||||
|
..Default::default()
|
||||||
|
}),
|
||||||
|
..Default::default()
|
||||||
|
}),
|
||||||
|
CodeActionOrCommand::CodeAction(lsp_types::CodeAction {
|
||||||
|
title: "Make conditional".into(),
|
||||||
|
kind: Some(lsp_types::CodeActionKind::REFACTOR),
|
||||||
|
edit: Some(WorkspaceEdit {
|
||||||
|
changes: Some(
|
||||||
|
std::iter::once((
|
||||||
|
url.clone(),
|
||||||
|
vec![TextEdit::new(
|
||||||
|
lsp_types::Range::new(
|
||||||
|
text_element.start,
|
||||||
|
text_element.start
|
||||||
|
),
|
||||||
|
r#"if ${0:condition} : "#.into()
|
||||||
|
)]
|
||||||
|
))
|
||||||
|
.collect()
|
||||||
|
),
|
||||||
|
..Default::default()
|
||||||
|
}),
|
||||||
|
..Default::default()
|
||||||
|
}),
|
||||||
|
])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue