refactor: upgrade to deno_ast 0.15 (#14680)

This commit is contained in:
David Sherret 2022-05-20 16:40:55 -04:00 committed by GitHub
parent e7c894e8f5
commit 1fcecb6789
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
34 changed files with 365 additions and 314 deletions

View file

@ -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> {