Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tools/lsp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1403,7 +1403,7 @@ impl LanguageServer for Backend {
let mut actions = Vec::new();
let uri = params.text_document.uri;

// Quick Fix 1: Organize Imports Action
// Code Action 1: Organize Imports
let mut organize_edits = Vec::new();
organize_edits.push(TextEdit {
range: Range {
Expand All @@ -1427,7 +1427,7 @@ impl LanguageServer for Backend {
..Default::default()
}));

// Quick Fix 2: Convert mutable "make" to immutable "const"
// Code Action 2: Convert mutable "make" to immutable "const"
for diagnostic in params.context.diagnostics {
if let Some(ref code) = diagnostic.code {
if let NumberOrString::String(ref s) = code {
Expand Down
74 changes: 74 additions & 0 deletions tools/lsp/tests/lsp_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,77 @@ async fn test_lsp_formatting() {
let new_text = &res[0].new_text;
assert!(new_text.contains(" say 42"));
}

#[tokio::test]
async fn test_lsp_code_action_make_to_const() {
let (service, _) = LspService::new(Backend::new);

let doc_uri = Url::parse("file:///main.txs").unwrap();
service
.inner()
.did_open(DidOpenTextDocumentParams {
text_document: TextDocumentItem {
uri: doc_uri.clone(),
language_id: "techscript".to_string(),
version: 1,
text: "build main() {\n make x = 10\n}".to_string(),
},
})
.await;

let res = service
.inner()
.code_action(CodeActionParams {
text_document: TextDocumentIdentifier {
uri: doc_uri.clone(),
},
range: Range::default(),
context: CodeActionContext {
diagnostics: vec![Diagnostic {
range: Range {
start: Position::new(1, 4),
end: Position::new(1, 8),
},
severity: Some(DiagnosticSeverity::WARNING),
code: Some(NumberOrString::String("warning".to_string())),
code_description: None,
source: Some("techscript".to_string()),
message: "Variable is never mutated. Consider using `const`".to_string(),
related_information: None,
tags: None,
data: None,
}],
only: None,
trigger_kind: None,
},
work_done_progress_params: Default::default(),
partial_result_params: Default::default(),
})
.await
.unwrap();

println!("{:#?}", res);

let actions = res.unwrap();

// Find the quick fix action
let mut found = false;
for action in actions {
if let CodeActionOrCommand::CodeAction(a) = action {
if a.title.contains("immutable 'const'") {
found = true;

let edits = a.edit.unwrap().changes.unwrap();
let edits_for_uri = edits.get(&doc_uri).unwrap();

assert_eq!(edits_for_uri.len(), 1);
assert_eq!(edits_for_uri[0].new_text, "const ");
assert_eq!(edits_for_uri[0].range.start.line, 1);
assert_eq!(edits_for_uri[0].range.start.character, 4);
assert_eq!(edits_for_uri[0].range.end.character, 8);
}
}
}

assert!(found, "Did not find the 'Change to const' quick fix");
}
Loading