mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-12 21:28:51 +00:00
subscriptions
This commit is contained in:
parent
7570d85869
commit
9fcebbc512
6 changed files with 85 additions and 61 deletions
|
@ -39,11 +39,11 @@ impl AnalysisHostImpl {
|
|||
|
||||
pub fn analysis(
|
||||
&self,
|
||||
file_resolver: impl FileResolver,
|
||||
file_resolver: Arc<dyn FileResolver>,
|
||||
) -> AnalysisImpl {
|
||||
AnalysisImpl {
|
||||
needs_reindex: AtomicBool::new(false),
|
||||
file_resolver: Arc::new(file_resolver),
|
||||
file_resolver,
|
||||
data: self.data.clone(),
|
||||
}
|
||||
}
|
||||
|
@ -78,7 +78,7 @@ impl AnalysisHostImpl {
|
|||
|
||||
pub(crate) struct AnalysisImpl {
|
||||
needs_reindex: AtomicBool,
|
||||
file_resolver: Arc<FileResolver>,
|
||||
file_resolver: Arc<dyn FileResolver>,
|
||||
data: Arc<WorldData>,
|
||||
}
|
||||
|
||||
|
@ -236,15 +236,13 @@ impl AnalysisImpl {
|
|||
("add `#[derive]`", libeditor::add_derive(&file, offset).map(|f| f())),
|
||||
("add impl", libeditor::add_impl(&file, offset).map(|f| f())),
|
||||
];
|
||||
let mut res = Vec::new();
|
||||
for (name, local_edit) in actions {
|
||||
if let Some(local_edit) = local_edit {
|
||||
res.push(SourceChange::from_local_edit(
|
||||
file_id, name, local_edit
|
||||
actions.into_iter()
|
||||
.filter_map(|(name, local_edit)| {
|
||||
Some(SourceChange::from_local_edit(
|
||||
file_id, name, local_edit?,
|
||||
))
|
||||
}
|
||||
}
|
||||
res
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn index_resolve(&self, name_ref: ast::NameRef) -> Vec<(FileId, FileSymbol)> {
|
||||
|
|
|
@ -12,6 +12,8 @@ mod symbol_index;
|
|||
mod module_map;
|
||||
mod imp;
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use relative_path::{RelativePath, RelativePathBuf};
|
||||
use libsyntax2::{File, TextRange, TextUnit, AtomEdit};
|
||||
use imp::{AnalysisImpl, AnalysisHostImpl};
|
||||
|
@ -31,7 +33,7 @@ pub trait FileResolver: Send + Sync + 'static {
|
|||
|
||||
#[derive(Debug)]
|
||||
pub struct AnalysisHost {
|
||||
pub(crate) imp: AnalysisHostImpl
|
||||
imp: AnalysisHostImpl
|
||||
}
|
||||
|
||||
impl AnalysisHost {
|
||||
|
@ -39,7 +41,7 @@ impl AnalysisHost {
|
|||
AnalysisHost { imp: AnalysisHostImpl::new() }
|
||||
}
|
||||
pub fn analysis(&self, file_resolver: impl FileResolver) -> Analysis {
|
||||
Analysis { imp: self.imp.analysis(file_resolver) }
|
||||
Analysis { imp: self.imp.analysis(Arc::new(file_resolver)) }
|
||||
}
|
||||
pub fn change_file(&mut self, file_id: FileId, text: Option<String>) {
|
||||
self.change_files(::std::iter::once((file_id, text)));
|
||||
|
@ -121,7 +123,7 @@ impl Query {
|
|||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Analysis {
|
||||
pub(crate) imp: AnalysisImpl
|
||||
imp: AnalysisImpl
|
||||
}
|
||||
|
||||
impl Analysis {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
use languageserver_types::{
|
||||
Diagnostic, DiagnosticSeverity, Url, DocumentSymbol,
|
||||
Diagnostic, DiagnosticSeverity, DocumentSymbol,
|
||||
Command, TextDocumentIdentifier,
|
||||
SymbolInformation, Position, Location, TextEdit,
|
||||
CompletionItem, InsertTextFormat, CompletionItemKind,
|
||||
|
@ -325,9 +325,9 @@ pub fn handle_code_action(
|
|||
|
||||
pub fn publish_diagnostics(
|
||||
world: ServerWorld,
|
||||
uri: Url
|
||||
file_id: FileId,
|
||||
) -> Result<req::PublishDiagnosticsParams> {
|
||||
let file_id = world.uri_to_file_id(&uri)?;
|
||||
let uri = world.file_id_to_uri(file_id)?;
|
||||
let line_index = world.analysis().file_line_index(file_id);
|
||||
let diagnostics = world.analysis().diagnostics(file_id)
|
||||
.into_iter()
|
||||
|
@ -344,9 +344,9 @@ pub fn publish_diagnostics(
|
|||
|
||||
pub fn publish_decorations(
|
||||
world: ServerWorld,
|
||||
uri: Url
|
||||
file_id: FileId,
|
||||
) -> Result<req::PublishDecorationsParams> {
|
||||
let file_id = world.uri_to_file_id(&uri)?;
|
||||
let uri = world.file_id_to_uri(file_id)?;
|
||||
Ok(req::PublishDecorationsParams {
|
||||
uri,
|
||||
decorations: highlight(&world, file_id),
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
mod handlers;
|
||||
mod subscriptions;
|
||||
|
||||
use std::{
|
||||
collections::{HashSet},
|
||||
|
@ -6,7 +7,7 @@ use std::{
|
|||
|
||||
use threadpool::ThreadPool;
|
||||
use crossbeam_channel::{Sender, Receiver};
|
||||
use languageserver_types::Url;
|
||||
use libanalysis::FileId;
|
||||
|
||||
use {
|
||||
req, dispatch,
|
||||
|
@ -14,6 +15,7 @@ use {
|
|||
io::{Io, RawMsg, RawRequest, RawNotification},
|
||||
vfs::FileEvent,
|
||||
server_world::{ServerWorldState, ServerWorld},
|
||||
main_loop::subscriptions::{Subscriptions},
|
||||
};
|
||||
|
||||
pub(super) fn main_loop(
|
||||
|
@ -28,6 +30,7 @@ pub(super) fn main_loop(
|
|||
|
||||
let mut pending_requests: HashSet<u64> = HashSet::new();
|
||||
let mut fs_events_receiver = Some(&fs_events_receiver);
|
||||
let mut subs = Subscriptions::new();
|
||||
loop {
|
||||
enum Event {
|
||||
Msg(RawMsg),
|
||||
|
@ -47,7 +50,7 @@ pub(super) fn main_loop(
|
|||
None => Event::FsWatcherDead,
|
||||
}
|
||||
};
|
||||
|
||||
let mut state_changed = false;
|
||||
match event {
|
||||
Event::ReceiverDead => {
|
||||
io.cleanup_receiver()?;
|
||||
|
@ -70,6 +73,7 @@ pub(super) fn main_loop(
|
|||
Event::Fs(events) => {
|
||||
trace!("fs change, {} events", events.len());
|
||||
state.apply_fs_changes(events);
|
||||
state_changed = true;
|
||||
}
|
||||
Event::Msg(msg) => {
|
||||
match msg {
|
||||
|
@ -79,7 +83,8 @@ pub(super) fn main_loop(
|
|||
}
|
||||
}
|
||||
RawMsg::Notification(not) => {
|
||||
on_notification(io, &mut state, pool, &task_sender, not)?
|
||||
on_notification(io, &mut state, &mut subs, not)?;
|
||||
state_changed = true;
|
||||
}
|
||||
RawMsg::Response(resp) => {
|
||||
if !pending_requests.remove(&resp.id) {
|
||||
|
@ -89,6 +94,15 @@ pub(super) fn main_loop(
|
|||
}
|
||||
}
|
||||
};
|
||||
|
||||
if state_changed {
|
||||
update_file_notifications_on_threadpool(
|
||||
pool,
|
||||
state.snapshot(),
|
||||
task_sender.clone(),
|
||||
subs.subscriptions(),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -140,8 +154,7 @@ fn on_request(
|
|||
fn on_notification(
|
||||
io: &mut Io,
|
||||
state: &mut ServerWorldState,
|
||||
pool: &ThreadPool,
|
||||
sender: &Sender<Task>,
|
||||
subs: &mut Subscriptions,
|
||||
not: RawNotification,
|
||||
) -> Result<()> {
|
||||
let mut not = Some(not);
|
||||
|
@ -149,13 +162,8 @@ fn on_notification(
|
|||
let uri = params.text_document.uri;
|
||||
let path = uri.to_file_path()
|
||||
.map_err(|()| format_err!("invalid uri: {}", uri))?;
|
||||
state.add_mem_file(path, params.text_document.text);
|
||||
update_file_notifications_on_threadpool(
|
||||
pool,
|
||||
state.snapshot(),
|
||||
sender.clone(),
|
||||
uri,
|
||||
);
|
||||
let file_id = state.add_mem_file(path, params.text_document.text);
|
||||
subs.add_sub(file_id);
|
||||
Ok(())
|
||||
})?;
|
||||
dispatch::handle_notification::<req::DidChangeTextDocument, _>(&mut not, |mut params| {
|
||||
|
@ -166,23 +174,15 @@ fn on_notification(
|
|||
.ok_or_else(|| format_err!("empty changes"))?
|
||||
.text;
|
||||
state.change_mem_file(path.as_path(), text)?;
|
||||
update_file_notifications_on_threadpool(
|
||||
pool,
|
||||
state.snapshot(),
|
||||
sender.clone(),
|
||||
uri,
|
||||
);
|
||||
Ok(())
|
||||
})?;
|
||||
dispatch::handle_notification::<req::DidCloseTextDocument, _>(&mut not, |params| {
|
||||
let uri = params.text_document.uri;
|
||||
let path = uri.to_file_path()
|
||||
.map_err(|()| format_err!("invalid uri: {}", uri))?;
|
||||
state.remove_mem_file(path.as_path())?;
|
||||
let not = req::PublishDiagnosticsParams {
|
||||
uri,
|
||||
diagnostics: Vec::new(),
|
||||
};
|
||||
let file_id = state.remove_mem_file(path.as_path())?;
|
||||
subs.remove_sub(file_id);
|
||||
let not = req::PublishDiagnosticsParams { uri, diagnostics: Vec::new() };
|
||||
let not = dispatch::send_notification::<req::PublishDiagnostics>(not);
|
||||
io.send(RawMsg::Notification(not));
|
||||
Ok(())
|
||||
|
@ -227,25 +227,27 @@ fn update_file_notifications_on_threadpool(
|
|||
pool: &ThreadPool,
|
||||
world: ServerWorld,
|
||||
sender: Sender<Task>,
|
||||
uri: Url,
|
||||
subscriptions: Vec<FileId>,
|
||||
) {
|
||||
pool.execute(move || {
|
||||
match handlers::publish_diagnostics(world.clone(), uri.clone()) {
|
||||
Err(e) => {
|
||||
error!("failed to compute diagnostics: {:?}", e)
|
||||
for file_id in subscriptions {
|
||||
match handlers::publish_diagnostics(world.clone(), file_id) {
|
||||
Err(e) => {
|
||||
error!("failed to compute diagnostics: {:?}", e)
|
||||
}
|
||||
Ok(params) => {
|
||||
let not = dispatch::send_notification::<req::PublishDiagnostics>(params);
|
||||
sender.send(Task::Notify(not));
|
||||
}
|
||||
}
|
||||
Ok(params) => {
|
||||
let not = dispatch::send_notification::<req::PublishDiagnostics>(params);
|
||||
sender.send(Task::Notify(not));
|
||||
}
|
||||
}
|
||||
match handlers::publish_decorations(world, uri) {
|
||||
Err(e) => {
|
||||
error!("failed to compute decorations: {:?}", e)
|
||||
}
|
||||
Ok(params) => {
|
||||
let not = dispatch::send_notification::<req::PublishDecorations>(params);
|
||||
sender.send(Task::Notify(not))
|
||||
match handlers::publish_decorations(world.clone(), file_id) {
|
||||
Err(e) => {
|
||||
error!("failed to compute decorations: {:?}", e)
|
||||
}
|
||||
Ok(params) => {
|
||||
let not = dispatch::send_notification::<req::PublishDecorations>(params);
|
||||
sender.send(Task::Notify(not))
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
21
crates/server/src/main_loop/subscriptions.rs
Normal file
21
crates/server/src/main_loop/subscriptions.rs
Normal file
|
@ -0,0 +1,21 @@
|
|||
use std::collections::HashSet;
|
||||
use libanalysis::FileId;
|
||||
|
||||
pub struct Subscriptions {
|
||||
subs: HashSet<FileId>,
|
||||
}
|
||||
|
||||
impl Subscriptions {
|
||||
pub fn new() -> Subscriptions {
|
||||
Subscriptions { subs: HashSet::new() }
|
||||
}
|
||||
pub fn add_sub(&mut self, file_id: FileId) {
|
||||
self.subs.insert(file_id);
|
||||
}
|
||||
pub fn remove_sub(&mut self, file_id: FileId) {
|
||||
self.subs.remove(&file_id);
|
||||
}
|
||||
pub fn subscriptions(&self) -> Vec<FileId> {
|
||||
self.subs.iter().cloned().collect()
|
||||
}
|
||||
}
|
|
@ -61,10 +61,11 @@ impl ServerWorldState {
|
|||
self.analysis_host.change_files(changes);
|
||||
}
|
||||
|
||||
pub fn add_mem_file(&mut self, path: PathBuf, text: String) {
|
||||
pub fn add_mem_file(&mut self, path: PathBuf, text: String) -> FileId {
|
||||
let file_id = self.path_map.get_or_insert(path);
|
||||
self.mem_map.insert(file_id, None);
|
||||
self.analysis_host.change_file(file_id, Some(text));
|
||||
file_id
|
||||
}
|
||||
|
||||
pub fn change_mem_file(&mut self, path: &Path, text: String) -> Result<()> {
|
||||
|
@ -75,7 +76,7 @@ impl ServerWorldState {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub fn remove_mem_file(&mut self, path: &Path) -> Result<()> {
|
||||
pub fn remove_mem_file(&mut self, path: &Path) -> Result<FileId> {
|
||||
let file_id = self.path_map.get_id(path).ok_or_else(|| {
|
||||
format_err!("change to unknown file: {}", path.display())
|
||||
})?;
|
||||
|
@ -86,7 +87,7 @@ impl ServerWorldState {
|
|||
// Do this via file watcher ideally.
|
||||
let text = fs::read_to_string(path).ok();
|
||||
self.analysis_host.change_file(file_id, text);
|
||||
Ok(())
|
||||
Ok(file_id)
|
||||
}
|
||||
|
||||
pub fn snapshot(&self) -> ServerWorld {
|
||||
|
|
Loading…
Reference in a new issue