Skip to content

Commit

Permalink
fix: fallback to clients not supporting pull-diags
Browse files Browse the repository at this point in the history
  • Loading branch information
Desdaemon committed Dec 12, 2023
1 parent 2acc76b commit 33dc4e6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
18 changes: 17 additions & 1 deletion src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub struct TextDocumentItem {
pub language: Option<Language>,
pub rope: Option<Rope>,
pub old_rope: Option<Rope>,
pub open: bool,
}

pub enum Text {
Expand All @@ -64,17 +65,26 @@ impl Backend {
pub const LIMIT: usize = 80;
/// Maximum number of descendants to show in docstring.
const INHERITS_LIMIT: usize = 3;
pub const LINE_LIMIT: usize = 1500;

pub async fn on_change(&self, params: TextDocumentItem) -> miette::Result<()> {
let split_uri = params.uri.path().rsplit_once('.');
let mut diagnostics = vec![];
let rope = match (params.rope, &params.text) {
(Some(rope), _) => rope,
(None, Text::Full(full)) => ropey::Rope::from_str(full),
(None, Text::Delta(_)) => Err(diagnostic!("No rope and got delta"))?,
};
match (split_uri, params.language) {
(Some((_, "py")), _) | (_, Some(Language::Python)) => {
self.on_change_python(&params.text, &params.uri, rope, params.old_rope)?;
self.on_change_python(
&params.text,
&params.uri,
rope,
params.old_rope,
&mut diagnostics,
params.open,
)?;
}
(Some((_, "xml")), _) | (_, Some(Language::Xml)) => {
self.on_change_xml(&params.text, &params.uri, rope).await?;
Expand All @@ -85,6 +95,12 @@ impl Backend {
other => return Err(diagnostic!("Unhandled language: {other:?}")).into_diagnostic(),
}

if !diagnostics.is_empty() {
self.client
.publish_diagnostics(params.uri, diagnostics, Some(params.version))
.await;
}

Ok(())
}
pub fn update_ast(
Expand Down
8 changes: 8 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ impl LanguageServer for Backend {
language: Some(language),
rope: Some(rope),
old_rope: None,
open: true,
})
.await
.report(|| format_loc!("did_open failed"))
Expand All @@ -289,6 +290,7 @@ impl LanguageServer for Backend {
language: None,
rope: None,
old_rope: None,
open: false,
})
.await
.report(|| format_loc!("did_change failed"));
Expand Down Expand Up @@ -324,6 +326,7 @@ impl LanguageServer for Backend {
language: None,
rope: Some(rope.value().clone()),
old_rope: Some(old_rope),
open: false,
})
.await
.report(|| format_loc!("did_change failed"));
Expand All @@ -337,6 +340,11 @@ impl LanguageServer for Backend {
self.update_models(Text::Full(text.into_owned()), &uri, rope.value().clone())
.await
.report(|| format_loc!("update_models"));
if !self.capabilities.pull_diagnostics.load(Relaxed) {
let mut diagnostics = vec![];
self.diagnose_python(&uri, rope.value().clone(), &mut diagnostics);
self.client.publish_diagnostics(uri, diagnostics, None).await;
}
}
}
async fn goto_definition(&self, params: GotoDefinitionParams) -> Result<Option<GotoDefinitionResponse>> {
Expand Down
14 changes: 13 additions & 1 deletion src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{Backend, Text};

use std::borrow::Cow;
use std::path::Path;
use std::sync::atomic::Ordering::Relaxed;

use lasso::Key;
use log::{debug, error, warn};
Expand Down Expand Up @@ -125,12 +126,23 @@ struct Mapped<'text> {
}

impl Backend {
pub fn on_change_python(&self, text: &Text, uri: &Url, rope: Rope, old_rope: Option<Rope>) -> miette::Result<()> {
pub fn on_change_python(
&self,
text: &Text,
uri: &Url,
rope: Rope,
old_rope: Option<Rope>,
diagnostics: &mut Vec<Diagnostic>,
open: bool,
) -> miette::Result<()> {
let mut parser = Parser::new();
parser
.set_language(tree_sitter_python::language())
.expect("bug: failed to init python parser");
self.update_ast(text, uri, rope.clone(), old_rope, parser)?;
if !self.capabilities.pull_diagnostics.load(Relaxed) && (open || rope.len_lines() < Self::LINE_LIMIT) {
self.diagnose_python(uri, rope.clone(), diagnostics);
}
Ok(())
}
pub async fn update_models(&self, text: Text, uri: &Url, rope: Rope) -> miette::Result<()> {
Expand Down

0 comments on commit 33dc4e6

Please sign in to comment.