use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::Mutex as StdMutex;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc as std_mpsc;
use anyhow::Result;
use lsp_types::{
ClientCapabilities, CodeActionCapabilityResolveSupport, CodeActionClientCapabilities,
CodeActionKindLiteralSupport, CodeActionLiteralSupport, CodeActionOrCommand,
CodeActionProviderCapability, CodeActionResponse, CompletionClientCapabilities,
CompletionItemCapability, CompletionItemKind, CompletionItemKindCapability, CompletionResponse,
DeclarationCapability, DiagnosticServerCapabilities, DocumentChangeOperation, DocumentChanges,
DocumentSymbol, DocumentSymbolClientCapabilities, DocumentSymbolResponse,
GotoDefinitionResponse, HoverContents, HoverProviderCapability,
ImplementationProviderCapability, Location, MarkedString, MarkupKind, OneOf, Position,
PublishDiagnosticsClientCapabilities, SemanticTokenModifier, SemanticTokenType,
SemanticTokensClientCapabilities, SemanticTokensClientCapabilitiesRequests,
SemanticTokensFullOptions, SemanticTokensRangeResult, SemanticTokensResult,
SemanticTokensServerCapabilities, SemanticTokensWorkspaceClientCapabilities,
ServerCapabilities, SymbolKind, TextDocumentClientCapabilities, TextEdit, TokenFormat,
TypeDefinitionProviderCapability, Url, WindowClientCapabilities, WorkspaceClientCapabilities,
WorkspaceEdit,
};
use tokio::sync::{Mutex as TokioMutex, mpsc as tokio_mpsc, oneshot};
use crate::lsp::client::LspClient;
use crate::lsp::config::{Language, ServerConfig};
use crate::lsp::log_file;
use crate::lsp::registry::ServerRegistry;
use crate::lsp::runtime::LspRuntime;
use crate::text_edit::TextSpanEdit;
mod capabilities;
mod code_actions;
mod completion;
mod decorations;
mod diagnostics;
mod discovery;
mod documents;
mod formatting;
mod hover;
mod navigation;
mod rename;
mod semantic_tokens;
mod servers;
mod symbols;
mod text_ranges;
mod worker;
use capabilities::*;
#[cfg(test)]
pub use code_actions::CodeActionCommand;
pub use code_actions::CodeActionItem;
use code_actions::*;
pub use completion::CompletionItem;
pub use completion::CompletionResult;
pub use completion::CompletionTrigger;
pub use completion::InlineCompletionResult;
pub use completion::InlineItem;
pub use completion::SignatureInfo;
use completion::*;
pub use decorations::ColorItem;
pub use decorations::DocumentLinkItem;
pub use decorations::FoldRangeKind;
pub use decorations::FoldingRangeItem;
pub use decorations::InlayHintItem;
use decorations::*;
pub use diagnostics::Diagnostic;
pub use diagnostics::DiagnosticCode;
pub use diagnostics::DiagnosticOrigin;
pub use diagnostics::DiagnosticSeverity;
pub use diagnostics::DiagnosticTags;
pub use diagnostics::DiagnosticsUpdate;
pub use diagnostics::diagnostics_by_source;
pub use diagnostics::diagnostics_source;
pub use diagnostics::diagnostics_store_key;
pub use diagnostics::one_line;
use diagnostics::*;
pub(crate) use discovery::is_executable_file;
pub(crate) use discovery::is_on_path;
pub(crate) use discovery::path_to_language;
use discovery::*;
use documents::*;
pub use formatting::FormatResult;
pub use hover::HoverResult;
pub use hover::RustAnalyzerResult;
use hover::*;
pub use navigation::DeclarationResult;
pub use navigation::DefinitionResult;
pub use navigation::HierarchyKind;
pub use navigation::TypeDefinitionResult;
use navigation::*;
pub use rename::FileRenameOp;
pub use rename::PrepareRenameResult;
use rename::*;
use semantic_tokens::*;
use servers::*;
pub use symbols::OutlineKind;
pub use symbols::OutlineSymbol;
pub use symbols::WorkspaceSymbolItem;
use symbols::*;
#[cfg(test)]
pub use text_ranges::DocumentHighlightsResult;
pub use text_ranges::LinkedEditingResult;
pub use text_ranges::OccurrenceItem;
use text_ranges::*;
use worker::*;
#[derive(Debug)]
pub struct ServerUpdate {
pub server: String,
pub event: ServerEvent,
}
#[derive(Debug)]
pub enum ServerEvent {
Progress(Option<String>),
Message {
#[cfg_attr(not(test), allow(dead_code))]
level: lsp_types::MessageType,
text: String,
},
Question {
text: String,
actions: Vec<lsp_types::MessageActionItem>,
reply: oneshot::Sender<Option<lsp_types::MessageActionItem>>,
},
ShowDocument {
uri: lsp_types::Url,
external: bool,
take_focus: bool,
selection: Option<lsp_types::Range>,
reply: oneshot::Sender<bool>,
},
}
pub fn message_is_shown(level: lsp_types::MessageType, text: &str) -> bool {
use lsp_types::MessageType as MT;
level == MT::ERROR
|| level == MT::WARNING
|| (level == MT::INFO && (text.contains("https://") || text.contains("http://")))
}
enum Cmd {
Shutdown {
done: oneshot::Sender<()>,
},
OpenDoc {
path: PathBuf,
text: String,
},
RestartServersFor {
path: PathBuf,
},
ReplaceRegistry {
registry: ServerRegistry,
},
PrewarmWorkspace,
SetExtraRoots {
roots: Vec<PathBuf>,
},
RequestWorkspaceDiagnostics,
RequestSemanticTokens {
path: PathBuf,
seq: u64,
generation: u64,
},
RequestSemanticTokensRange {
path: PathBuf,
start_line: u32,
end: Position,
seq: u64,
generation: u64,
},
RequestInlayHints {
path: PathBuf,
end: Position,
seq: u64,
},
RequestDocumentLinks {
path: PathBuf,
seq: u64,
},
RequestCodeLens {
path: PathBuf,
seq: u64,
},
RequestFoldingRanges {
path: PathBuf,
seq: u64,
},
RequestDocumentColors {
path: PathBuf,
seq: u64,
},
RequestColorPresentations {
request_id: u64,
path: PathBuf,
color: (f32, f32, f32, f32),
start: (u32, u32),
end: (u32, u32),
},
ChangeDoc {
path: PathBuf,
text: String,
},
SaveDoc {
path: PathBuf,
text: String,
},
CloseDoc {
path: PathBuf,
},
RequestCompletion {
request_id: u64,
path: PathBuf,
line: u32,
character: u32,
trigger: CompletionTrigger,
},
ResolveCompletion {
request_id: u64,
path: PathBuf,
item: Arc<lsp_types::CompletionItem>,
},
RequestSignatureHelp {
request_id: u64,
path: PathBuf,
line: u32,
character: u32,
},
RequestHover {
request_id: u64,
path: PathBuf,
line: u32,
character: u32,
},
RustAnalyzer {
request_id: u64,
path: PathBuf,
request: crate::lsp::rust_analyzer::RaRequest,
line: u32,
character: u32,
},
RequestDefinition {
request_id: u64,
path: PathBuf,
line: u32,
character: u32,
},
RequestDocumentSymbols {
path: PathBuf,
seq: u64,
},
RequestDeclaration {
request_id: u64,
path: PathBuf,
line: u32,
character: u32,
},
RequestTypeDefinition {
request_id: u64,
path: PathBuf,
line: u32,
character: u32,
},
RequestImplementation {
request_id: u64,
path: PathBuf,
line: u32,
character: u32,
},
RequestWorkspaceSymbols {
request_id: u64,
query: String,
},
RequestReferences {
request_id: u64,
path: PathBuf,
line: u32,
character: u32,
},
RequestDocumentHighlights {
request_id: u64,
path: PathBuf,
line: u32,
character: u32,
},
RequestLinkedEditing {
request_id: u64,
path: PathBuf,
line: u32,
character: u32,
},
RequestInlineCompletion {
request_id: u64,
path: PathBuf,
line: u32,
character: u32,
},
RequestSelectionRanges {
request_id: u64,
path: PathBuf,
positions: Vec<(u32, u32)>,
},
RequestCallHierarchy {
request_id: u64,
path: PathBuf,
line: u32,
character: u32,
incoming: bool,
},
RequestTypeHierarchy {
request_id: u64,
path: PathBuf,
line: u32,
character: u32,
supertypes: bool,
},
RequestRename {
request_id: u64,
path: PathBuf,
line: u32,
character: u32,
new_name: String,
},
RequestPrepareRename {
request_id: u64,
path: PathBuf,
line: u32,
character: u32,
},
RequestWillRenameFiles {
request_id: u64,
renames: Vec<FileRenameOp>,
},
DidRenameFiles {
renames: Vec<FileRenameOp>,
},
DidChangeWatchedFiles {
changes: Vec<(PathBuf, lsp_types::FileChangeType)>,
},
RequestFormatting {
request_id: u64,
path: PathBuf,
tab_size: u32,
insert_spaces: bool,
},
RequestOnTypeFormatting {
request_id: u64,
path: PathBuf,
line: u32,
character: u32,
ch: char,
tab_size: u32,
insert_spaces: bool,
},
RequestRangeFormatting {
request_id: u64,
path: PathBuf,
start: (u32, u32),
end: (u32, u32),
tab_size: u32,
insert_spaces: bool,
},
RequestCodeAction {
request_id: u64,
path: PathBuf,
start_line: u32,
start_char: u32,
end_line: u32,
end_char: u32,
diagnostics: Vec<Diagnostic>,
},
ResolveCodeAction {
request_id: u64,
path: PathBuf,
server: String,
action: serde_json::Value,
},
ExecuteCommand {
path: PathBuf,
command: String,
arguments: Vec<serde_json::Value>,
},
}
#[derive(Default)]
struct LangCapabilitySupport {
declaration: HashMap<Language, bool>,
type_definition: HashMap<Language, bool>,
implementation: HashMap<Language, bool>,
references: HashMap<Language, bool>,
formatting: HashMap<Language, bool>,
code_action: HashMap<Language, bool>,
call_hierarchy: HashMap<Language, bool>,
inline_completion: HashSet<ClientKey>,
will_rename: HashMap<Language, bool>,
on_type_triggers: HashMap<Language, String>,
completion_triggers: HashMap<ClientKey, String>,
document_roots: HashMap<PathBuf, PathBuf>,
document_servers: HashMap<PathBuf, Vec<String>>,
document_clients: HashMap<PathBuf, Vec<String>>,
}
type CapabilitySupport = Arc<StdMutex<LangCapabilitySupport>>;
pub struct LspManager {
registered_servers: HashMap<Language, Vec<&'static str>>,
semantic_generation: std::sync::atomic::AtomicU64,
cmd_tx: tokio_mpsc::UnboundedSender<Cmd>,
replies: Replies,
#[cfg(test)]
reply_test_tx: ResultSenders,
#[cfg(test)]
synced_texts: std::sync::Mutex<HashMap<PathBuf, String>>,
capability_support: CapabilitySupport,
semantic_refresh: Arc<AtomicBool>,
inlay_refresh: Arc<AtomicBool>,
diagnostic_refresh: Arc<AtomicBool>,
servers_restarted: Arc<AtomicBool>,
next_request_id: u64,
#[cfg_attr(not(test), allow(dead_code))]
workspace_root: PathBuf,
_runtime: LspRuntime,
}
impl Drop for LspManager {
fn drop(&mut self) {
let (done_tx, done_rx) = oneshot::channel();
if self.cmd_tx.send(Cmd::Shutdown { done: done_tx }).is_ok() {
let _ = self._runtime.handle().block_on(async {
tokio::time::timeout(std::time::Duration::from_secs(3), done_rx).await
});
}
}
}
pub fn document_end(text: &str) -> Position {
let line = text.matches('\n').count() as u32;
let last = text.rsplit('\n').next().unwrap_or("");
Position::new(line, last.encode_utf16().count() as u32)
}
pub fn rows_end(text: &str, end_row: u32) -> Position {
let end = document_end(text);
if end_row <= end.line {
Position::new(end_row, 0)
} else {
end
}
}
impl LspManager {
pub fn new(workspace_root: PathBuf) -> Result<Self> {
let runtime = LspRuntime::new()?;
let (cmd_tx, cmd_rx) = tokio_mpsc::unbounded_channel();
let (senders, replies) = reply_channels();
#[cfg(test)]
let reply_test_tx = senders.clone();
let capability_support: CapabilitySupport =
Arc::new(StdMutex::new(LangCapabilitySupport::default()));
let semantic_refresh = Arc::new(AtomicBool::new(false));
let inlay_refresh = Arc::new(AtomicBool::new(false));
let diagnostic_refresh = Arc::new(AtomicBool::new(false));
let servers_restarted = Arc::new(AtomicBool::new(false));
let root = workspace_root.clone();
let user_sources = crate::lsp::manifest::read_extension_sources(
&crate::lsp::manifest::user_extensions_dir(),
);
let user_refs: Vec<&str> = user_sources.iter().map(String::as_str).collect();
crate::lsp::languages::reload_with_user_sources(&user_refs);
let disabled = crate::prefs::Prefs::load_or_default().disabled_extensions;
let registry = ServerRegistry::with_user_extensions_filtered(&user_refs, &disabled);
let registered_servers = registered_servers_of(®istry);
runtime.handle().spawn(worker_loop(
root,
registry,
cmd_rx,
senders,
capability_support.clone(),
semantic_refresh.clone(),
inlay_refresh.clone(),
diagnostic_refresh.clone(),
servers_restarted.clone(),
));
Ok(Self {
registered_servers,
semantic_generation: std::sync::atomic::AtomicU64::new(0),
cmd_tx,
replies,
#[cfg(test)]
reply_test_tx,
#[cfg(test)]
synced_texts: Default::default(),
capability_support,
semantic_refresh,
inlay_refresh,
diagnostic_refresh,
servers_restarted,
next_request_id: 1,
workspace_root,
_runtime: runtime,
})
}
#[cfg_attr(not(test), allow(dead_code))]
pub fn workspace_root(&self) -> &Path {
&self.workspace_root
}
fn send_request(&mut self, make: impl FnOnce(u64) -> Cmd) -> u64 {
let id = self.next_request_id;
self.next_request_id += 1;
let _ = self.cmd_tx.send(make(id));
id
}
fn language_supports(
&self,
lang: Language,
pick: fn(&LangCapabilitySupport) -> &HashMap<Language, bool>,
) -> Option<bool> {
pick(&*self.capability_support.lock().ok()?)
.get(&lang)
.copied()
}
}
struct ManagedClient {
name: String,
client: Arc<TokioMutex<LspClient>>,
supports_completion: bool,
completion_triggers: String,
supports_signature_help: bool,
supports_workspace_diagnostics: bool,
diagnostic_identifier: Option<String>,
diagnostic_result_ids: Arc<TokioMutex<DiagnosticResultIds>>,
diagnostic_pull_stalled: Arc<AtomicBool>,
save_notify: SaveNotify,
supports_document_diagnostics: bool,
document_pull_stalled: Arc<AtomicBool>,
prepare_rename_stalled: Arc<AtomicBool>,
document_pull_generations: Arc<StdMutex<HashMap<PathBuf, u64>>>,
supports_hover: bool,
supports_definition: bool,
supports_document_symbol: bool,
supports_declaration: bool,
supports_type_definition: bool,
supports_implementation: bool,
supports_references: bool,
supports_document_highlight: bool,
supports_linked_editing: bool,
supports_inline_completion: bool,
supports_selection_range: bool,
supports_call_hierarchy: bool,
supports_type_hierarchy: bool,
supports_workspace_symbols: bool,
supports_rename: bool,
supports_prepare_rename: bool,
supports_formatting: bool,
on_type_triggers: Option<String>,
supports_range_formatting: bool,
supports_code_action: bool,
semantic_legend: Option<Arc<Vec<String>>>,
semantic_supports_range: bool,
supports_inlay_hints: bool,
supports_document_link: bool,
supports_code_lens: bool,
code_lens_resolve: bool,
server_commands: Arc<Vec<String>>,
supports_folding_range: bool,
supports_document_color: bool,
will_rename: Option<Arc<crate::lsp::file_ops::FileOpFilters>>,
did_rename: Option<Arc<crate::lsp::file_ops::FileOpFilters>>,
}
impl ManagedClient {
fn new(name: String, client: LspClient, support: ServerSupport) -> Self {
let ServerSupport {
supports_completion,
completion_triggers,
supports_signature_help,
supports_workspace_diagnostics,
diagnostic_identifier,
save_notify,
supports_document_diagnostics,
supports_hover,
supports_definition,
supports_document_symbol,
supports_declaration,
supports_type_definition,
supports_implementation,
supports_references,
supports_document_highlight,
supports_linked_editing,
supports_inline_completion,
supports_selection_range,
supports_call_hierarchy,
supports_type_hierarchy,
supports_workspace_symbols,
supports_rename,
supports_prepare_rename,
supports_formatting,
on_type_triggers,
supports_range_formatting,
supports_code_action,
semantic_legend,
semantic_supports_range,
supports_inlay_hints,
supports_document_link,
supports_code_lens,
code_lens_resolve,
server_commands,
supports_folding_range,
supports_document_color,
will_rename,
did_rename,
} = support;
Self {
name,
client: Arc::new(TokioMutex::new(client)),
supports_completion,
completion_triggers,
supports_signature_help,
supports_workspace_diagnostics,
diagnostic_identifier,
diagnostic_result_ids: Arc::new(TokioMutex::new(DiagnosticResultIds::default())),
diagnostic_pull_stalled: Arc::new(AtomicBool::new(false)),
save_notify,
supports_document_diagnostics,
document_pull_stalled: Arc::new(AtomicBool::new(false)),
prepare_rename_stalled: Arc::new(AtomicBool::new(false)),
document_pull_generations: Arc::new(StdMutex::new(HashMap::new())),
supports_hover,
supports_definition,
supports_document_symbol,
supports_declaration,
supports_type_definition,
supports_implementation,
supports_references,
supports_document_highlight,
supports_linked_editing,
supports_inline_completion,
supports_selection_range,
supports_call_hierarchy,
supports_type_hierarchy,
supports_workspace_symbols,
supports_rename,
supports_prepare_rename,
supports_formatting,
on_type_triggers,
supports_range_formatting,
supports_code_action,
semantic_legend,
semantic_supports_range,
supports_inlay_hints,
supports_document_link,
supports_code_lens,
code_lens_resolve,
server_commands,
supports_folding_range,
supports_document_color,
will_rename,
did_rename,
}
}
}
type ClientKey = (Language, PathBuf);
enum NoServer {
Unknown,
Unsupported,
}
struct WorkerState {
workspace_root: PathBuf,
extra_roots: Vec<PathBuf>,
registry: ServerRegistry,
clients: HashMap<ClientKey, Vec<ManagedClient>>,
docs: HashMap<PathBuf, DocState>,
capability_support: CapabilitySupport,
semantic_refresh: Arc<AtomicBool>,
inlay_refresh: Arc<AtomicBool>,
diagnostic_refresh: Arc<AtomicBool>,
diagnostics_tx: std_mpsc::Sender<DiagnosticsUpdate>,
progress_tx: std_mpsc::Sender<ServerUpdate>,
restarts: ServerRestarts,
}
#[derive(Default)]
struct ServerRestarts {
attempts: HashMap<ClientKey, Vec<std::time::Instant>>,
gave_up: std::collections::HashSet<ClientKey>,
incomplete: HashMap<ClientKey, Vec<String>>,
respawn: Option<Vec<String>>,
restarted: Arc<AtomicBool>,
}
struct DocState {
language: Language,
project_root: PathBuf,
version: i32,
}
#[cfg_attr(test, derive(Clone))]
struct ResultSenders {
completion: std_mpsc::Sender<CompletionResult>,
signature_help: std_mpsc::Sender<SignatureHelpResult>,
hover: std_mpsc::Sender<HoverResult>,
rust_analyzer: std_mpsc::Sender<RustAnalyzerResult>,
definition: std_mpsc::Sender<DefinitionResult>,
document_symbols: std_mpsc::Sender<DocumentSymbolsResult>,
declaration: std_mpsc::Sender<DeclarationResult>,
type_definition: std_mpsc::Sender<TypeDefinitionResult>,
implementation: std_mpsc::Sender<ImplementationResult>,
references: std_mpsc::Sender<ReferencesResult>,
document_highlights: std_mpsc::Sender<DocumentHighlightsResult>,
linked_editing: std_mpsc::Sender<LinkedEditingResult>,
inline_completion: std_mpsc::Sender<InlineCompletionResult>,
selection_ranges: std_mpsc::Sender<SelectionRangesResult>,
call_hierarchy: std_mpsc::Sender<CallHierarchyResult>,
workspace_symbols: std_mpsc::Sender<WorkspaceSymbolsResult>,
rename: std_mpsc::Sender<RenameResult>,
prepare_rename: std_mpsc::Sender<PrepareRenameResult>,
will_rename_files: std_mpsc::Sender<WillRenameFilesResult>,
formatting: std_mpsc::Sender<FormatResult>,
on_type_formatting: std_mpsc::Sender<FormatResult>,
code_action: std_mpsc::Sender<CodeActionResult>,
semantic_tokens: std_mpsc::Sender<SemanticTokensUpdate>,
inlay_hints: std_mpsc::Sender<InlayHintsUpdate>,
document_links: std_mpsc::Sender<DocumentLinksUpdate>,
code_lens: std_mpsc::Sender<CodeLensUpdate>,
folding_ranges: std_mpsc::Sender<FoldingRangesUpdate>,
document_colors: std_mpsc::Sender<DocumentColorsUpdate>,
color_presentations: std_mpsc::Sender<ColorPresentationsResult>,
diagnostics: std_mpsc::Sender<DiagnosticsUpdate>,
progress: std_mpsc::Sender<ServerUpdate>,
}
struct Replies {
completion: std_mpsc::Receiver<CompletionResult>,
signature_help: std_mpsc::Receiver<SignatureHelpResult>,
hover: std_mpsc::Receiver<HoverResult>,
rust_analyzer: std_mpsc::Receiver<RustAnalyzerResult>,
definition: std_mpsc::Receiver<DefinitionResult>,
document_symbols: std_mpsc::Receiver<DocumentSymbolsResult>,
declaration: std_mpsc::Receiver<DeclarationResult>,
type_definition: std_mpsc::Receiver<TypeDefinitionResult>,
implementation: std_mpsc::Receiver<ImplementationResult>,
references: std_mpsc::Receiver<ReferencesResult>,
document_highlights: std_mpsc::Receiver<DocumentHighlightsResult>,
linked_editing: std_mpsc::Receiver<LinkedEditingResult>,
inline_completion: std_mpsc::Receiver<InlineCompletionResult>,
selection_ranges: std_mpsc::Receiver<SelectionRangesResult>,
call_hierarchy: std_mpsc::Receiver<CallHierarchyResult>,
workspace_symbols: std_mpsc::Receiver<WorkspaceSymbolsResult>,
rename: std_mpsc::Receiver<RenameResult>,
prepare_rename: std_mpsc::Receiver<PrepareRenameResult>,
will_rename_files: std_mpsc::Receiver<WillRenameFilesResult>,
formatting: std_mpsc::Receiver<FormatResult>,
on_type_formatting: std_mpsc::Receiver<FormatResult>,
code_action: std_mpsc::Receiver<CodeActionResult>,
semantic_tokens: std_mpsc::Receiver<SemanticTokensUpdate>,
inlay_hints: std_mpsc::Receiver<InlayHintsUpdate>,
document_links: std_mpsc::Receiver<DocumentLinksUpdate>,
code_lens: std_mpsc::Receiver<CodeLensUpdate>,
folding_ranges: std_mpsc::Receiver<FoldingRangesUpdate>,
document_colors: std_mpsc::Receiver<DocumentColorsUpdate>,
color_presentations: std_mpsc::Receiver<ColorPresentationsResult>,
diagnostics: std_mpsc::Receiver<DiagnosticsUpdate>,
progress: std_mpsc::Receiver<ServerUpdate>,
}
fn reply_channels() -> (ResultSenders, Replies) {
let (completion, completion_rx) = std_mpsc::channel();
let (signature_help, signature_help_rx) = std_mpsc::channel();
let (hover, hover_rx) = std_mpsc::channel();
let (rust_analyzer, rust_analyzer_rx) = std_mpsc::channel();
let (definition, definition_rx) = std_mpsc::channel();
let (document_symbols, document_symbols_rx) = std_mpsc::channel();
let (declaration, declaration_rx) = std_mpsc::channel();
let (type_definition, type_definition_rx) = std_mpsc::channel();
let (implementation, implementation_rx) = std_mpsc::channel();
let (references, references_rx) = std_mpsc::channel();
let (document_highlights, document_highlights_rx) = std_mpsc::channel();
let (linked_editing, linked_editing_rx) = std_mpsc::channel();
let (inline_completion, inline_completion_rx) = std_mpsc::channel();
let (selection_ranges, selection_ranges_rx) = std_mpsc::channel();
let (call_hierarchy, call_hierarchy_rx) = std_mpsc::channel();
let (workspace_symbols, workspace_symbols_rx) = std_mpsc::channel();
let (rename, rename_rx) = std_mpsc::channel();
let (prepare_rename, prepare_rename_rx) = std_mpsc::channel();
let (will_rename_files, will_rename_files_rx) = std_mpsc::channel();
let (formatting, formatting_rx) = std_mpsc::channel();
let (on_type_formatting, on_type_formatting_rx) = std_mpsc::channel();
let (code_action, code_action_rx) = std_mpsc::channel();
let (semantic_tokens, semantic_tokens_rx) = std_mpsc::channel();
let (inlay_hints, inlay_hints_rx) = std_mpsc::channel();
let (document_links, document_links_rx) = std_mpsc::channel();
let (code_lens, code_lens_rx) = std_mpsc::channel();
let (folding_ranges, folding_ranges_rx) = std_mpsc::channel();
let (document_colors, document_colors_rx) = std_mpsc::channel();
let (color_presentations, color_presentations_rx) = std_mpsc::channel();
let (diagnostics, diagnostics_rx) = std_mpsc::channel();
let (progress, progress_rx) = std_mpsc::channel();
(
ResultSenders {
completion,
signature_help,
hover,
rust_analyzer,
definition,
document_symbols,
declaration,
type_definition,
implementation,
references,
document_highlights,
linked_editing,
inline_completion,
selection_ranges,
call_hierarchy,
workspace_symbols,
rename,
prepare_rename,
will_rename_files,
formatting,
on_type_formatting,
code_action,
semantic_tokens,
inlay_hints,
document_links,
code_lens,
folding_ranges,
document_colors,
color_presentations,
diagnostics,
progress,
},
Replies {
completion: completion_rx,
signature_help: signature_help_rx,
hover: hover_rx,
rust_analyzer: rust_analyzer_rx,
definition: definition_rx,
document_symbols: document_symbols_rx,
declaration: declaration_rx,
type_definition: type_definition_rx,
implementation: implementation_rx,
references: references_rx,
document_highlights: document_highlights_rx,
linked_editing: linked_editing_rx,
inline_completion: inline_completion_rx,
selection_ranges: selection_ranges_rx,
call_hierarchy: call_hierarchy_rx,
workspace_symbols: workspace_symbols_rx,
rename: rename_rx,
prepare_rename: prepare_rename_rx,
will_rename_files: will_rename_files_rx,
formatting: formatting_rx,
on_type_formatting: on_type_formatting_rx,
code_action: code_action_rx,
semantic_tokens: semantic_tokens_rx,
inlay_hints: inlay_hints_rx,
document_links: document_links_rx,
code_lens: code_lens_rx,
folding_ranges: folding_ranges_rx,
document_colors: document_colors_rx,
color_presentations: color_presentations_rx,
diagnostics: diagnostics_rx,
progress: progress_rx,
},
)
}
fn workspace_edits(edit: &WorkspaceEdit) -> Vec<(PathBuf, Vec<TextSpanEdit>)> {
let mut out: Vec<(PathBuf, Vec<TextSpanEdit>)> = Vec::new();
if let Some(changes) = edit.changes.as_ref() {
for (uri, edits) in changes {
if let Ok(path) = uri.to_file_path() {
out.push((path, edits.iter().map(text_edit_to_span).collect()));
}
}
}
if let Some(doc_changes) = edit.document_changes.as_ref() {
match doc_changes {
DocumentChanges::Edits(edits) => {
for tde in edits {
if let Ok(path) = tde.text_document.uri.to_file_path() {
let spans = tde
.edits
.iter()
.map(|e| match e {
OneOf::Left(te) => text_edit_to_span(te),
OneOf::Right(ate) => text_edit_to_span(&ate.text_edit),
})
.collect();
out.push((path, spans));
}
}
}
DocumentChanges::Operations(ops) => {
for op in ops {
if let DocumentChangeOperation::Edit(tde) = op
&& let Ok(path) = tde.text_document.uri.to_file_path()
{
let spans = tde
.edits
.iter()
.map(|e| match e {
OneOf::Left(te) => text_edit_to_span(te),
OneOf::Right(ate) => text_edit_to_span(&ate.text_edit),
})
.collect();
out.push((path, spans));
}
}
}
}
}
out
}
fn text_edit_to_span(te: &TextEdit) -> TextSpanEdit {
TextSpanEdit {
start: (
te.range.start.line as usize,
te.range.start.character as usize,
),
end: (te.range.end.line as usize, te.range.end.character as usize),
new_text: te.new_text.clone(),
utf16: true,
}
}
#[derive(Default)]
pub struct DiagnosticResultIds {
by_uri: std::collections::HashMap<String, String>,
}
struct PrepareRenameAsk {
request_id: u64,
server_name: String,
client: Arc<TokioMutex<LspClient>>,
uri: Url,
line: u32,
character: u32,
stalled: Arc<AtomicBool>,
}
struct WorkspacePull {
name: String,
client: Arc<TokioMutex<LspClient>>,
identifier: Option<String>,
result_ids: Arc<TokioMutex<DiagnosticResultIds>>,
stalled: Arc<AtomicBool>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
struct RustupLack {
why: String,
fix: String,
}
#[cfg(test)]
mod tests;