2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2021-12-15 13:23:43 -05:00
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use deno_ast::LineAndColumnIndex;
|
|
|
|
use deno_ast::ModuleSpecifier;
|
|
|
|
use deno_ast::SourceTextInfo;
|
|
|
|
use deno_core::anyhow::anyhow;
|
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::serde_json;
|
2022-04-03 00:17:30 -04:00
|
|
|
use tower_lsp::lsp_types::ClientCapabilities;
|
|
|
|
use tower_lsp::lsp_types::ClientInfo;
|
|
|
|
use tower_lsp::lsp_types::CompletionContext;
|
|
|
|
use tower_lsp::lsp_types::CompletionParams;
|
|
|
|
use tower_lsp::lsp_types::CompletionResponse;
|
|
|
|
use tower_lsp::lsp_types::CompletionTextEdit;
|
|
|
|
use tower_lsp::lsp_types::CompletionTriggerKind;
|
|
|
|
use tower_lsp::lsp_types::DidChangeTextDocumentParams;
|
|
|
|
use tower_lsp::lsp_types::DidCloseTextDocumentParams;
|
|
|
|
use tower_lsp::lsp_types::DidOpenTextDocumentParams;
|
|
|
|
use tower_lsp::lsp_types::InitializeParams;
|
|
|
|
use tower_lsp::lsp_types::InitializedParams;
|
|
|
|
use tower_lsp::lsp_types::PartialResultParams;
|
|
|
|
use tower_lsp::lsp_types::Position;
|
|
|
|
use tower_lsp::lsp_types::Range;
|
|
|
|
use tower_lsp::lsp_types::TextDocumentContentChangeEvent;
|
|
|
|
use tower_lsp::lsp_types::TextDocumentIdentifier;
|
|
|
|
use tower_lsp::lsp_types::TextDocumentItem;
|
|
|
|
use tower_lsp::lsp_types::TextDocumentPositionParams;
|
|
|
|
use tower_lsp::lsp_types::VersionedTextDocumentIdentifier;
|
|
|
|
use tower_lsp::lsp_types::WorkDoneProgressParams;
|
|
|
|
use tower_lsp::LanguageServer;
|
2021-12-15 13:23:43 -05:00
|
|
|
|
|
|
|
use super::client::Client;
|
|
|
|
use super::config::CompletionSettings;
|
|
|
|
use super::config::ImportCompletionSettings;
|
2022-03-29 18:59:27 -04:00
|
|
|
use super::config::TestingSettings;
|
2021-12-15 13:23:43 -05:00
|
|
|
use super::config::WorkspaceSettings;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct ReplCompletionItem {
|
|
|
|
pub new_text: String,
|
2022-05-20 16:40:55 -04:00
|
|
|
pub range: std::ops::Range<usize>,
|
2021-12-15 13:23:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ReplLanguageServer {
|
|
|
|
language_server: super::language_server::LanguageServer,
|
|
|
|
document_version: i32,
|
|
|
|
document_text: String,
|
|
|
|
pending_text: String,
|
|
|
|
cwd_uri: ModuleSpecifier,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ReplLanguageServer {
|
|
|
|
pub async fn new_initialized() -> Result<ReplLanguageServer, AnyError> {
|
2023-03-30 10:43:16 -04:00
|
|
|
// downgrade info and warn lsp logging to debug
|
2021-12-15 13:23:43 -05:00
|
|
|
super::logging::set_lsp_log_level(log::Level::Debug);
|
2023-03-30 10:43:16 -04:00
|
|
|
super::logging::set_lsp_warn_level(log::Level::Debug);
|
|
|
|
|
2021-12-15 13:23:43 -05:00
|
|
|
let language_server =
|
|
|
|
super::language_server::LanguageServer::new(Client::new_for_repl());
|
|
|
|
|
|
|
|
let cwd_uri = get_cwd_uri()?;
|
|
|
|
|
|
|
|
#[allow(deprecated)]
|
|
|
|
language_server
|
|
|
|
.initialize(InitializeParams {
|
|
|
|
process_id: None,
|
|
|
|
root_path: None,
|
|
|
|
root_uri: Some(cwd_uri.clone()),
|
|
|
|
initialization_options: Some(
|
|
|
|
serde_json::to_value(get_repl_workspace_settings()).unwrap(),
|
|
|
|
),
|
|
|
|
capabilities: ClientCapabilities {
|
|
|
|
workspace: None,
|
|
|
|
text_document: None,
|
|
|
|
window: None,
|
|
|
|
general: None,
|
|
|
|
experimental: None,
|
2022-10-14 08:04:38 -04:00
|
|
|
offset_encoding: None,
|
2021-12-15 13:23:43 -05:00
|
|
|
},
|
|
|
|
trace: None,
|
|
|
|
workspace_folders: None,
|
|
|
|
client_info: Some(ClientInfo {
|
|
|
|
name: "Deno REPL".to_string(),
|
|
|
|
version: None,
|
|
|
|
}),
|
|
|
|
locale: None,
|
|
|
|
})
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
language_server.initialized(InitializedParams {}).await;
|
|
|
|
|
|
|
|
let server = ReplLanguageServer {
|
|
|
|
language_server,
|
|
|
|
document_version: 0,
|
|
|
|
document_text: String::new(),
|
|
|
|
pending_text: String::new(),
|
|
|
|
cwd_uri,
|
|
|
|
};
|
|
|
|
server.open_current_document().await;
|
|
|
|
|
|
|
|
Ok(server)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn commit_text(&mut self, line_text: &str) {
|
|
|
|
self.did_change(line_text).await;
|
|
|
|
self.document_text.push_str(&self.pending_text);
|
|
|
|
self.pending_text = String::new();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn completions(
|
|
|
|
&mut self,
|
|
|
|
line_text: &str,
|
|
|
|
position: usize,
|
|
|
|
) -> Vec<ReplCompletionItem> {
|
|
|
|
self.did_change(line_text).await;
|
|
|
|
let text_info = deno_ast::SourceTextInfo::from_string(format!(
|
|
|
|
"{}{}",
|
|
|
|
self.document_text, self.pending_text
|
|
|
|
));
|
2022-05-20 16:40:55 -04:00
|
|
|
let before_line_len = self.document_text.len();
|
|
|
|
let position = text_info.range().start + before_line_len + position;
|
2021-12-15 13:23:43 -05:00
|
|
|
let line_and_column = text_info.line_and_column_index(position);
|
|
|
|
let response = self
|
|
|
|
.language_server
|
|
|
|
.completion(CompletionParams {
|
|
|
|
text_document_position: TextDocumentPositionParams {
|
|
|
|
text_document: TextDocumentIdentifier {
|
|
|
|
uri: self.get_document_specifier(),
|
|
|
|
},
|
|
|
|
position: Position {
|
|
|
|
line: line_and_column.line_index as u32,
|
|
|
|
character: line_and_column.column_index as u32,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
work_done_progress_params: WorkDoneProgressParams {
|
|
|
|
work_done_token: None,
|
|
|
|
},
|
|
|
|
partial_result_params: PartialResultParams {
|
|
|
|
partial_result_token: None,
|
|
|
|
},
|
|
|
|
context: Some(CompletionContext {
|
|
|
|
trigger_kind: CompletionTriggerKind::INVOKED,
|
|
|
|
trigger_character: None,
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.ok()
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
2022-07-12 22:21:56 -04:00
|
|
|
let mut items = match response {
|
2021-12-15 13:23:43 -05:00
|
|
|
Some(CompletionResponse::Array(items)) => items,
|
|
|
|
Some(CompletionResponse::List(list)) => list.items,
|
|
|
|
None => Vec::new(),
|
|
|
|
};
|
2022-07-12 22:21:56 -04:00
|
|
|
items.sort_by_key(|item| {
|
|
|
|
if let Some(sort_text) = &item.sort_text {
|
|
|
|
sort_text.clone()
|
|
|
|
} else {
|
|
|
|
item.label.clone()
|
|
|
|
}
|
|
|
|
});
|
2021-12-15 13:23:43 -05:00
|
|
|
items
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|item| {
|
|
|
|
item.text_edit.and_then(|edit| match edit {
|
|
|
|
CompletionTextEdit::Edit(edit) => Some(ReplCompletionItem {
|
|
|
|
new_text: edit.new_text,
|
2022-05-20 16:40:55 -04:00
|
|
|
range: lsp_range_to_std_range(&text_info, &edit.range),
|
2021-12-15 13:23:43 -05:00
|
|
|
}),
|
|
|
|
CompletionTextEdit::InsertAndReplace(_) => None,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.filter(|item| {
|
|
|
|
// filter the results to only exact matches
|
2022-05-20 16:40:55 -04:00
|
|
|
let text = &text_info.text_str()[item.range.clone()];
|
2021-12-15 13:23:43 -05:00
|
|
|
item.new_text.starts_with(text)
|
|
|
|
})
|
|
|
|
.map(|mut item| {
|
|
|
|
// convert back to a line position
|
2022-05-20 16:40:55 -04:00
|
|
|
item.range.start -= before_line_len;
|
|
|
|
item.range.end -= before_line_len;
|
2021-12-15 13:23:43 -05:00
|
|
|
item
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn did_change(&mut self, new_text: &str) {
|
|
|
|
self.check_cwd_change().await;
|
|
|
|
let new_text = if new_text.ends_with('\n') {
|
|
|
|
new_text.to_string()
|
|
|
|
} else {
|
2023-01-27 10:43:16 -05:00
|
|
|
format!("{new_text}\n")
|
2021-12-15 13:23:43 -05:00
|
|
|
};
|
|
|
|
self.document_version += 1;
|
|
|
|
let current_line_count =
|
|
|
|
self.document_text.chars().filter(|c| *c == '\n').count() as u32;
|
|
|
|
let pending_line_count =
|
|
|
|
self.pending_text.chars().filter(|c| *c == '\n').count() as u32;
|
|
|
|
self
|
|
|
|
.language_server
|
|
|
|
.did_change(DidChangeTextDocumentParams {
|
|
|
|
text_document: VersionedTextDocumentIdentifier {
|
|
|
|
uri: self.get_document_specifier(),
|
|
|
|
version: self.document_version,
|
|
|
|
},
|
|
|
|
content_changes: vec![TextDocumentContentChangeEvent {
|
|
|
|
range: Some(Range {
|
|
|
|
start: Position::new(current_line_count, 0),
|
|
|
|
end: Position::new(current_line_count + pending_line_count, 0),
|
|
|
|
}),
|
|
|
|
range_length: None,
|
|
|
|
text: new_text.to_string(),
|
|
|
|
}],
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
self.pending_text = new_text;
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn check_cwd_change(&mut self) {
|
|
|
|
// handle if the cwd changes, if the cwd is deleted in the case of
|
|
|
|
// get_cwd_uri() erroring, then keep using it as the base
|
|
|
|
let cwd_uri = get_cwd_uri().unwrap_or_else(|_| self.cwd_uri.clone());
|
|
|
|
if self.cwd_uri != cwd_uri {
|
|
|
|
self
|
|
|
|
.language_server
|
|
|
|
.did_close(DidCloseTextDocumentParams {
|
|
|
|
text_document: TextDocumentIdentifier {
|
|
|
|
uri: self.get_document_specifier(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
self.cwd_uri = cwd_uri;
|
|
|
|
self.document_version = 0;
|
|
|
|
self.open_current_document().await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn open_current_document(&self) {
|
|
|
|
self
|
|
|
|
.language_server
|
|
|
|
.did_open(DidOpenTextDocumentParams {
|
|
|
|
text_document: TextDocumentItem {
|
|
|
|
uri: self.get_document_specifier(),
|
|
|
|
language_id: "typescript".to_string(),
|
|
|
|
version: self.document_version,
|
|
|
|
text: format!("{}{}", self.document_text, self.pending_text),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_document_specifier(&self) -> ModuleSpecifier {
|
|
|
|
self.cwd_uri.join("$deno$repl.ts").unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-20 16:40:55 -04:00
|
|
|
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 {
|
2021-12-15 13:23:43 -05:00
|
|
|
line_index: range.start.line as usize,
|
|
|
|
column_index: range.start.character as usize,
|
2022-05-20 16:40:55 -04:00
|
|
|
})
|
|
|
|
.as_byte_index(text_info.range().start);
|
|
|
|
let end_index = text_info
|
|
|
|
.loc_to_source_pos(LineAndColumnIndex {
|
2021-12-15 13:23:43 -05:00
|
|
|
line_index: range.end.line as usize,
|
|
|
|
column_index: range.end.character as usize,
|
2022-05-20 16:40:55 -04:00
|
|
|
})
|
|
|
|
.as_byte_index(text_info.range().start);
|
|
|
|
|
|
|
|
start_index..end_index
|
2021-12-15 13:23:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_cwd_uri() -> Result<ModuleSpecifier, AnyError> {
|
|
|
|
let cwd = std::env::current_dir()?;
|
|
|
|
ModuleSpecifier::from_directory_path(&cwd)
|
|
|
|
.map_err(|_| anyhow!("Could not get URI from {}", cwd.display()))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_repl_workspace_settings() -> WorkspaceSettings {
|
|
|
|
WorkspaceSettings {
|
|
|
|
enable: true,
|
2022-03-20 21:33:37 -04:00
|
|
|
enable_paths: Vec::new(),
|
2021-12-15 13:23:43 -05:00
|
|
|
config: None,
|
2022-01-23 19:27:52 -05:00
|
|
|
certificate_stores: None,
|
2021-12-15 13:23:43 -05:00
|
|
|
cache: None,
|
|
|
|
import_map: None,
|
|
|
|
code_lens: Default::default(),
|
2022-10-15 22:39:43 -04:00
|
|
|
inlay_hints: Default::default(),
|
2021-12-15 13:23:43 -05:00
|
|
|
internal_debug: false,
|
|
|
|
lint: false,
|
2023-05-11 17:17:14 -04:00
|
|
|
document_preload_limit: 0, // don't pre-load any modules as it's expensive and not useful for the repl
|
2022-01-23 19:27:52 -05:00
|
|
|
tls_certificate: None,
|
|
|
|
unsafely_ignore_certificate_errors: None,
|
2021-12-15 13:23:43 -05:00
|
|
|
unstable: false,
|
|
|
|
suggest: CompletionSettings {
|
|
|
|
complete_function_calls: false,
|
|
|
|
names: false,
|
|
|
|
paths: false,
|
|
|
|
auto_imports: false,
|
|
|
|
imports: ImportCompletionSettings {
|
|
|
|
auto_discover: false,
|
|
|
|
hosts: HashMap::from([("https://deno.land".to_string(), true)]),
|
|
|
|
},
|
|
|
|
},
|
2022-03-29 18:59:27 -04:00
|
|
|
testing: TestingSettings {
|
|
|
|
args: vec![],
|
|
|
|
enable: false,
|
|
|
|
},
|
2021-12-15 13:23:43 -05:00
|
|
|
}
|
|
|
|
}
|