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