aboutsummaryrefslogtreecommitdiff
path: root/helix-view/src/document.rs
diff options
context:
space:
mode:
Diffstat (limited to 'helix-view/src/document.rs')
-rw-r--r--helix-view/src/document.rs116
1 files changed, 96 insertions, 20 deletions
diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs
index 51668ab1..0de0cd17 100644
--- a/helix-view/src/document.rs
+++ b/helix-view/src/document.rs
@@ -4,10 +4,12 @@ use arc_swap::ArcSwap;
use futures_util::future::BoxFuture;
use futures_util::FutureExt;
use helix_core::auto_pairs::AutoPairs;
+use helix_core::chars::char_is_word;
use helix_core::doc_formatter::TextFormat;
use helix_core::encoding::Encoding;
use helix_core::syntax::{Highlight, LanguageServerFeature};
use helix_core::text_annotations::{InlineAnnotation, TextAnnotations};
+use helix_lsp::util::lsp_pos_to_pos;
use helix_vcs::{DiffHandle, DiffProviderRegistry};
use ::parking_lot::Mutex;
@@ -1075,14 +1077,6 @@ impl Document {
};
}
- /// Set the programming language for the file if you know the name (scope) but don't have the
- /// [`syntax::LanguageConfiguration`] for it.
- pub fn set_language2(&mut self, scope: &str, config_loader: Arc<syntax::Loader>) {
- let language_config = config_loader.language_config_for_scope(scope);
-
- self.set_language(language_config, Some(config_loader));
- }
-
/// Set the programming language for the file if you know the language but don't have the
/// [`syntax::LanguageConfiguration`] for it.
pub fn set_language_by_language_id(
@@ -1714,29 +1708,107 @@ impl Document {
)
}
+ pub fn lsp_diagnostic_to_diagnostic(
+ text: &Rope,
+ language_config: Option<&LanguageConfiguration>,
+ diagnostic: &helix_lsp::lsp::Diagnostic,
+ language_server_id: usize,
+ offset_encoding: helix_lsp::OffsetEncoding,
+ ) -> Option<Diagnostic> {
+ use helix_core::diagnostic::{Range, Severity::*};
+
+ // TODO: convert inside server
+ let start =
+ if let Some(start) = lsp_pos_to_pos(text, diagnostic.range.start, offset_encoding) {
+ start
+ } else {
+ log::warn!("lsp position out of bounds - {:?}", diagnostic);
+ return None;
+ };
+
+ let end = if let Some(end) = lsp_pos_to_pos(text, diagnostic.range.end, offset_encoding) {
+ end
+ } else {
+ log::warn!("lsp position out of bounds - {:?}", diagnostic);
+ return None;
+ };
+
+ let severity = diagnostic.severity.map(|severity| match severity {
+ lsp::DiagnosticSeverity::ERROR => Error,
+ lsp::DiagnosticSeverity::WARNING => Warning,
+ lsp::DiagnosticSeverity::INFORMATION => Info,
+ lsp::DiagnosticSeverity::HINT => Hint,
+ severity => unreachable!("unrecognized diagnostic severity: {:?}", severity),
+ });
+
+ if let Some(lang_conf) = language_config {
+ if let Some(severity) = severity {
+ if severity < lang_conf.diagnostic_severity {
+ return None;
+ }
+ }
+ };
+ use helix_core::diagnostic::{DiagnosticTag, NumberOrString};
+
+ let code = match diagnostic.code.clone() {
+ Some(x) => match x {
+ lsp::NumberOrString::Number(x) => Some(NumberOrString::Number(x)),
+ lsp::NumberOrString::String(x) => Some(NumberOrString::String(x)),
+ },
+ None => None,
+ };
+
+ let tags = if let Some(tags) = &diagnostic.tags {
+ let new_tags = tags
+ .iter()
+ .filter_map(|tag| match *tag {
+ lsp::DiagnosticTag::DEPRECATED => Some(DiagnosticTag::Deprecated),
+ lsp::DiagnosticTag::UNNECESSARY => Some(DiagnosticTag::Unnecessary),
+ _ => None,
+ })
+ .collect();
+
+ new_tags
+ } else {
+ Vec::new()
+ };
+
+ let ends_at_word =
+ start != end && end != 0 && text.get_char(end - 1).map_or(false, char_is_word);
+ let starts_at_word = start != end && text.get_char(start).map_or(false, char_is_word);
+
+ Some(Diagnostic {
+ range: Range { start, end },
+ ends_at_word,
+ starts_at_word,
+ zero_width: start == end,
+ line: diagnostic.range.start.line as usize,
+ message: diagnostic.message.clone(),
+ severity,
+ code,
+ tags,
+ source: diagnostic.source.clone(),
+ data: diagnostic.data.clone(),
+ language_server_id,
+ })
+ }
+
#[inline]
pub fn diagnostics(&self) -> &[Diagnostic] {
&self.diagnostics
}
- pub fn shown_diagnostics(&self) -> impl Iterator<Item = &Diagnostic> + DoubleEndedIterator {
- self.diagnostics.iter().filter(|d| {
- self.language_servers_with_feature(LanguageServerFeature::Diagnostics)
- .any(|ls| ls.id() == d.language_server_id)
- })
- }
-
pub fn replace_diagnostics(
&mut self,
diagnostics: impl IntoIterator<Item = Diagnostic>,
unchanged_sources: &[String],
- language_server_id: usize,
+ language_server_id: Option<usize>,
) {
if unchanged_sources.is_empty() {
self.clear_diagnostics(language_server_id);
} else {
self.diagnostics.retain(|d| {
- if d.language_server_id != language_server_id {
+ if language_server_id.map_or(false, |id| id != d.language_server_id) {
return true;
}
@@ -1757,9 +1829,13 @@ impl Document {
});
}
- pub fn clear_diagnostics(&mut self, language_server_id: usize) {
- self.diagnostics
- .retain(|d| d.language_server_id != language_server_id);
+ /// clears diagnostics for a given language server id if set, otherwise all diagnostics are cleared
+ pub fn clear_diagnostics(&mut self, language_server_id: Option<usize>) {
+ if let Some(id) = language_server_id {
+ self.diagnostics.retain(|d| d.language_server_id != id);
+ } else {
+ self.diagnostics.clear();
+ }
}
/// Get the document's auto pairs. If the document has a recognized