Skip to content

Commit 111c437

Browse files
committed
fix(lsp): align organize imports output with tsgo
1 parent 1ea5ff0 commit 111c437

File tree

5 files changed

+161
-303
lines changed

5 files changed

+161
-303
lines changed

cli/lsp/code_lens.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use std::cell::RefCell;
44
use std::collections::HashSet;
55
use std::rc::Rc;
6-
use std::sync::Arc;
76

87
use deno_ast::ParsedSource;
98
use deno_ast::SourceRange;
@@ -399,7 +398,7 @@ pub fn collect_test(
399398
pub fn collect_tsc(
400399
uri: &Uri,
401400
code_lens_settings: &CodeLensSettings,
402-
line_index: Arc<LineIndex>,
401+
line_index: &LineIndex,
403402
navigation_tree: &NavigationTree,
404403
token: &CancellationToken,
405404
) -> Result<Vec<lsp::CodeLens>, AnyError> {
@@ -412,15 +411,15 @@ pub fn collect_tsc(
412411
let source = CodeLensSource::Implementations;
413412
match i.kind {
414413
tsc::ScriptElementKind::InterfaceElement => {
415-
code_lenses.push(i.to_code_lens(line_index.clone(), uri, source));
414+
code_lenses.push(i.to_code_lens(line_index, uri, source));
416415
}
417416
tsc::ScriptElementKind::ClassElement
418417
| tsc::ScriptElementKind::MemberFunctionElement
419418
| tsc::ScriptElementKind::MemberVariableElement
420419
| tsc::ScriptElementKind::MemberGetAccessorElement
421420
| tsc::ScriptElementKind::MemberSetAccessorElement => {
422421
if ABSTRACT_MODIFIER.is_match(&i.kind_modifiers) {
423-
code_lenses.push(i.to_code_lens(line_index.clone(), uri, source));
422+
code_lenses.push(i.to_code_lens(line_index, uri, source));
424423
}
425424
}
426425
_ => (),
@@ -433,30 +432,30 @@ pub fn collect_tsc(
433432
if let Some(parent) = &mp
434433
&& parent.kind == tsc::ScriptElementKind::EnumElement
435434
{
436-
code_lenses.push(i.to_code_lens(line_index.clone(), uri, source));
435+
code_lenses.push(i.to_code_lens(line_index, uri, source));
437436
}
438437
match i.kind {
439438
tsc::ScriptElementKind::FunctionElement => {
440439
if code_lens_settings.references_all_functions {
441-
code_lenses.push(i.to_code_lens(line_index.clone(), uri, source));
440+
code_lenses.push(i.to_code_lens(line_index, uri, source));
442441
}
443442
}
444443
tsc::ScriptElementKind::ConstElement
445444
| tsc::ScriptElementKind::LetElement
446445
| tsc::ScriptElementKind::VariableElement => {
447446
if EXPORT_MODIFIER.is_match(&i.kind_modifiers) {
448-
code_lenses.push(i.to_code_lens(line_index.clone(), uri, source));
447+
code_lenses.push(i.to_code_lens(line_index, uri, source));
449448
}
450449
}
451450
tsc::ScriptElementKind::ClassElement => {
452451
if i.text != "<class>" {
453-
code_lenses.push(i.to_code_lens(line_index.clone(), uri, source));
452+
code_lenses.push(i.to_code_lens(line_index, uri, source));
454453
}
455454
}
456455
tsc::ScriptElementKind::InterfaceElement
457456
| tsc::ScriptElementKind::TypeElement
458457
| tsc::ScriptElementKind::EnumElement => {
459-
code_lenses.push(i.to_code_lens(line_index.clone(), uri, source));
458+
code_lenses.push(i.to_code_lens(line_index, uri, source));
460459
}
461460
tsc::ScriptElementKind::LocalFunctionElement
462461
| tsc::ScriptElementKind::MemberFunctionElement
@@ -471,11 +470,7 @@ pub fn collect_tsc(
471470
tsc::ScriptElementKind::ClassElement
472471
| tsc::ScriptElementKind::InterfaceElement
473472
| tsc::ScriptElementKind::TypeElement => {
474-
code_lenses.push(i.to_code_lens(
475-
line_index.clone(),
476-
uri,
477-
source,
478-
));
473+
code_lenses.push(i.to_code_lens(line_index, uri, source));
479474
}
480475
_ => (),
481476
}

cli/lsp/ts_server.rs

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use std::sync::Arc;
66
use deno_core::anyhow::anyhow;
77
use deno_core::error::AnyError;
88
use deno_core::futures::future::Shared;
9-
use deno_core::serde_json::json;
109
use deno_resolver::deno_json::CompilerOptionsKey;
1110
use indexmap::IndexMap;
1211
use indexmap::IndexSet;
@@ -269,7 +268,7 @@ impl TsServer {
269268
let code_lenses = crate::lsp::code_lens::collect_tsc(
270269
&module.uri,
271270
settings,
272-
module.line_index.clone(),
271+
&module.line_index,
273272
&navigation_tree,
274273
token,
275274
)?;
@@ -310,7 +309,7 @@ impl TsServer {
310309
return Err(anyhow!("request cancelled"));
311310
}
312311
item.collect_document_symbols(
313-
module.line_index.clone(),
312+
&module.line_index,
314313
&mut document_symbols,
315314
);
316315
}
@@ -492,40 +491,35 @@ impl TsServer {
492491
o.contains(&lsp::CodeActionKind::SOURCE_ORGANIZE_IMPORTS)
493492
})
494493
{
495-
let document_has_errors = context.diagnostics.iter().any(|d| {
496-
// Assume diagnostics without a severity are errors
497-
d.severity
498-
.is_none_or(|s| s == lsp::DiagnosticSeverity::ERROR)
499-
});
500494
let organize_imports_edit = ts_server
501-
.organize_imports(
502-
snapshot.clone(),
503-
module,
504-
document_has_errors,
505-
token,
506-
)
495+
.organize_imports(snapshot.clone(), module, token)
507496
.await
508497
.map_err(|err| {
509498
anyhow!(
510499
"Unable to get organize imports edit from TypeScript: {:#}",
511500
err
512501
)
513502
})?;
514-
if !organize_imports_edit.is_empty() {
515-
let changes_with_modules = organize_imports_edit
503+
let text_edits = organize_imports_edit.first().map(|c| {
504+
c.text_changes
516505
.iter()
517-
.map(|c| (c, module))
518-
.collect::<IndexMap<_, _>>();
506+
.map(|c| c.as_text_edit(&module.line_index))
507+
.collect::<Vec<_>>()
508+
});
509+
if let Some(text_edits) = text_edits
510+
&& !text_edits.is_empty()
511+
{
519512
actions.push(lsp::CodeActionOrCommand::CodeAction(
520513
lsp::CodeAction {
521-
title: "Organize imports".to_string(),
514+
title: "Organize Imports".to_string(),
522515
kind: Some(lsp::CodeActionKind::SOURCE_ORGANIZE_IMPORTS),
523-
edit: file_text_changes_to_workspace_edit(
524-
changes_with_modules,
525-
&snapshot,
526-
token,
527-
)?,
528-
data: Some(json!({ "uri": &module.uri})),
516+
edit: Some(lsp::WorkspaceEdit {
517+
changes: Some(
518+
std::iter::once((module.uri.as_ref().clone(), text_edits))
519+
.collect(),
520+
),
521+
..Default::default()
522+
}),
529523
..Default::default()
530524
},
531525
));
@@ -564,11 +558,9 @@ impl TsServer {
564558
highlights
565559
.into_iter()
566560
.map(|dh| {
567-
dh.to_highlight(module.line_index.clone(), token).map_err(
568-
|err| {
569-
anyhow!("Unable to convert document highlights: {:#}", err)
570-
},
571-
)
561+
dh.to_highlight(&module.line_index, token).map_err(|err| {
562+
anyhow!("Unable to convert document highlights: {:#}", err)
563+
})
572564
})
573565
.collect::<Result<Vec<_>, _>>()
574566
.map(|s| s.into_iter().flatten().collect())
@@ -691,7 +683,7 @@ impl TsServer {
691683
.map(|completion_info| {
692684
completion_info
693685
.as_completion_response(
694-
module.line_index.clone(),
686+
&module.line_index,
695687
&snapshot
696688
.config
697689
.language_settings_for_specifier(&module.specifier)
@@ -860,7 +852,7 @@ impl TsServer {
860852
return Err(anyhow!("request cancelled"));
861853
}
862854
Ok(span.to_folding_range(
863-
module.line_index.clone(),
855+
&module.line_index,
864856
module.text.as_bytes(),
865857
snapshot.config.line_folding_only_capable(),
866858
))
@@ -1112,9 +1104,8 @@ impl TsServer {
11121104
token,
11131105
)
11141106
.await?;
1115-
selection_ranges.push(
1116-
selection_range.to_selection_range(module.line_index.clone()),
1117-
);
1107+
selection_ranges
1108+
.push(selection_range.to_selection_range(&module.line_index));
11181109
}
11191110
Ok(Some(selection_ranges))
11201111
}
@@ -1144,7 +1135,7 @@ impl TsServer {
11441135
token,
11451136
)
11461137
.await?
1147-
.to_semantic_tokens(module.line_index.clone(), token),
1138+
.to_semantic_tokens(&module.line_index, token),
11481139
// TODO(nayeemrmn): Fix when tsgo supports semantic tokens.
11491140
Self::Go(_) => Ok(Default::default()),
11501141
}
@@ -1184,7 +1175,7 @@ impl TsServer {
11841175
token,
11851176
)
11861177
.await?
1187-
.to_semantic_tokens(module.line_index.clone(), token)?,
1178+
.to_semantic_tokens(&module.line_index, token)?,
11881179
// TODO(nayeemrmn): Fix when tsgo supports semantic tokens.
11891180
Self::Go(_) => Default::default(),
11901181
};
@@ -1325,12 +1316,10 @@ impl TsServer {
13251316
) -> Result<Option<Vec<lsp::InlayHint>>, AnyError> {
13261317
match self {
13271318
Self::Js(ts_server) => {
1328-
let text_span =
1329-
tsc::TextSpan::from_range(range, module.line_index.clone()).map_err(
1330-
|err| {
1331-
anyhow!("Failed to convert range to tsc text span: {:#}", err)
1332-
},
1333-
)?;
1319+
let text_span = tsc::TextSpan::from_range(range, &module.line_index)
1320+
.map_err(|err| {
1321+
anyhow!("Failed to convert range to tsc text span: {:#}", err)
1322+
})?;
13341323
let mut inlay_hints = ts_server
13351324
.provide_inlay_hints(snapshot.clone(), module, text_span, token)
13361325
.await;

0 commit comments

Comments
 (0)