summaryrefslogtreecommitdiff
path: root/cli/lsp
diff options
context:
space:
mode:
authorDavid Sherret <dsherret@users.noreply.github.com>2024-10-08 12:48:29 +0100
committerGitHub <noreply@github.com>2024-10-08 11:48:29 +0000
commit3fc9e49613547bbedcad1b02361db40c7f513b1f (patch)
tree34355427cb877be34286394ef34d583a40af4d60 /cli/lsp
parentff4e682ff95af37b52c404d694ffcfcfa57bb127 (diff)
refactor: remove usage of full_range (#26065)
For https://github.com/denoland/deno_graph/pull/538/files This was removed because the deserialization was not backwards compatible.
Diffstat (limited to 'cli/lsp')
-rw-r--r--cli/lsp/analysis.rs38
1 files changed, 34 insertions, 4 deletions
diff --git a/cli/lsp/analysis.rs b/cli/lsp/analysis.rs
index 4890221d9..8c2e8bb1d 100644
--- a/cli/lsp/analysis.rs
+++ b/cli/lsp/analysis.rs
@@ -44,6 +44,7 @@ use std::cmp::Ordering;
use std::collections::HashMap;
use std::collections::HashSet;
use std::path::Path;
+use text_lines::LineAndColumnIndex;
use tower_lsp::lsp_types as lsp;
use tower_lsp::lsp_types::Position;
use tower_lsp::lsp_types::Range;
@@ -1187,6 +1188,34 @@ impl CodeActionCollection {
range: &lsp::Range,
language_server: &language_server::Inner,
) {
+ fn import_start_from_specifier(
+ document: &Document,
+ import: &deno_graph::Import,
+ ) -> Option<LineAndColumnIndex> {
+ // find the top level statement that contains the specifier
+ let parsed_source = document.maybe_parsed_source()?.as_ref().ok()?;
+ let text_info = parsed_source.text_info_lazy();
+ let specifier_range = SourceRange::new(
+ text_info.loc_to_source_pos(LineAndColumnIndex {
+ line_index: import.specifier_range.start.line,
+ column_index: import.specifier_range.start.character,
+ }),
+ text_info.loc_to_source_pos(LineAndColumnIndex {
+ line_index: import.specifier_range.end.line,
+ column_index: import.specifier_range.end.character,
+ }),
+ );
+
+ match parsed_source.program_ref() {
+ deno_ast::swc::ast::Program::Module(module) => module
+ .body
+ .iter()
+ .find(|i| i.range().contains(&specifier_range))
+ .map(|i| text_info.line_and_column_index(i.range().start)),
+ deno_ast::swc::ast::Program::Script(_) => None,
+ }
+ }
+
async fn deno_types_for_npm_action(
document: &Document,
range: &lsp::Range,
@@ -1207,14 +1236,15 @@ impl CodeActionCollection {
range.end.line as usize,
range.end.character as usize,
);
- let import_range = dependency.imports.iter().find_map(|i| {
+ let import_start = dependency.imports.iter().find_map(|i| {
if json!(i.kind) != json!("es") && json!(i.kind) != json!("tsType") {
return None;
}
if !i.specifier_range.includes(&position) {
return None;
}
- i.full_range.as_ref()
+
+ import_start_from_specifier(document, i)
})?;
let referrer = document.specifier();
let file_referrer = document.file_referrer();
@@ -1301,8 +1331,8 @@ impl CodeActionCollection {
.specifier_to_uri(referrer, file_referrer)
.ok()?;
let position = lsp::Position {
- line: import_range.start.line as u32,
- character: import_range.start.character as u32,
+ line: import_start.line_index as u32,
+ character: import_start.column_index as u32,
};
let new_text = format!(
"{}// @deno-types=\"{}\"\n",