diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2022-05-20 16:40:55 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-20 16:40:55 -0400 |
commit | 1fcecb6789c3f111bc1554766ba9347afcfd02dc (patch) | |
tree | 19cd1b121412b992994b2fe4bea0463793d3986e /cli/lsp/repl.rs | |
parent | e7c894e8f54ebd2d9fd61c97a265906ac54e2068 (diff) |
refactor: upgrade to deno_ast 0.15 (#14680)
Diffstat (limited to 'cli/lsp/repl.rs')
-rw-r--r-- | cli/lsp/repl.rs | 42 |
1 files changed, 21 insertions, 21 deletions
diff --git a/cli/lsp/repl.rs b/cli/lsp/repl.rs index d937b0273..5cb747204 100644 --- a/cli/lsp/repl.rs +++ b/cli/lsp/repl.rs @@ -2,8 +2,6 @@ use std::collections::HashMap; -use deno_ast::swc::common::BytePos; -use deno_ast::swc::common::Span; use deno_ast::LineAndColumnIndex; use deno_ast::ModuleSpecifier; use deno_ast::SourceTextInfo; @@ -42,7 +40,7 @@ use super::config::WorkspaceSettings; #[derive(Debug)] pub struct ReplCompletionItem { pub new_text: String, - pub span: Span, + pub range: std::ops::Range<usize>, } pub struct ReplLanguageServer { @@ -113,12 +111,12 @@ impl ReplLanguageServer { position: usize, ) -> Vec<ReplCompletionItem> { self.did_change(line_text).await; - let before_line_len = BytePos(self.document_text.len() as u32); - let position = before_line_len + BytePos(position as u32); let text_info = deno_ast::SourceTextInfo::from_string(format!( "{}{}", self.document_text, self.pending_text )); + let before_line_len = self.document_text.len(); + let position = text_info.range().start + before_line_len + position; let line_and_column = text_info.line_and_column_index(position); let response = self .language_server @@ -158,24 +156,20 @@ impl ReplLanguageServer { item.text_edit.and_then(|edit| match edit { CompletionTextEdit::Edit(edit) => Some(ReplCompletionItem { new_text: edit.new_text, - span: lsp_range_to_span(&text_info, &edit.range), + range: lsp_range_to_std_range(&text_info, &edit.range), }), CompletionTextEdit::InsertAndReplace(_) => None, }) }) .filter(|item| { // filter the results to only exact matches - let text = &text_info.text_str() - [item.span.lo.0 as usize..item.span.hi.0 as usize]; + let text = &text_info.text_str()[item.range.clone()]; item.new_text.starts_with(text) }) .map(|mut item| { // convert back to a line position - item.span = Span::new( - item.span.lo - before_line_len, - item.span.hi - before_line_len, - Default::default(), - ); + item.range.start -= before_line_len; + item.range.end -= before_line_len; item }) .collect() @@ -251,18 +245,24 @@ impl ReplLanguageServer { } } -fn lsp_range_to_span(text_info: &SourceTextInfo, range: &Range) -> Span { - Span::new( - text_info.byte_index(LineAndColumnIndex { +fn lsp_range_to_std_range( + text_info: &SourceTextInfo, + range: &Range, +) -> std::ops::Range<usize> { + let start_index = text_info + .loc_to_source_pos(LineAndColumnIndex { line_index: range.start.line as usize, column_index: range.start.character as usize, - }), - text_info.byte_index(LineAndColumnIndex { + }) + .as_byte_index(text_info.range().start); + let end_index = text_info + .loc_to_source_pos(LineAndColumnIndex { line_index: range.end.line as usize, column_index: range.end.character as usize, - }), - Default::default(), - ) + }) + .as_byte_index(text_info.range().start); + + start_index..end_index } fn get_cwd_uri() -> Result<ModuleSpecifier, AnyError> { |