Skip to content

Commit 094df68

Browse files
author
Junfeng Li
committed
Fix case when one content is multi-line.
1 parent 6af61f7 commit 094df68

File tree

2 files changed

+51
-32
lines changed

2 files changed

+51
-32
lines changed

src/languageclient.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -568,10 +568,8 @@ pub trait ILanguageClient: IVim {
568568
if self.get(|state| Ok(state.is_nvim))? {
569569
let bufnr: u64 = serde_json::from_value(self.call(None, "bufnr", bufname)?)?;
570570
self.notify(None, "nvim_buf_set_lines", json!([bufnr, 0, -1, 0, lines]))?;
571-
} else {
572-
if self.call(None, "setbufline", json!([bufname, 1, lines]))? != 0 {
573-
bail!("Failed to set preview buffer content!");
574-
}
571+
} else if self.call(None, "setbufline", json!([bufname, 1, lines]))? != 0 {
572+
bail!("Failed to set preview buffer content!");
575573
// TODO: removing existing bottom lines.
576574
}
577575

@@ -705,7 +703,7 @@ pub trait ILanguageClient: IVim {
705703

706704
let hover: Option<Hover> = serde_json::from_value(result.clone())?;
707705
if let Some(hover) = hover {
708-
if hover.len() <= 1 {
706+
if hover.lines_len() <= 1 {
709707
self.echo(hover.to_string())?;
710708
} else {
711709
self.preview(&hover.to_display())?;

src/types.rs

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -465,12 +465,17 @@ impl ToString for lsp::MarkedString {
465465
fn to_string(&self) -> String {
466466
match *self {
467467
MarkedString::String(ref s) => s.clone(),
468-
// TODO: display language content properly.
469468
MarkedString::LanguageString(ref ls) => ls.value.clone(),
470469
}
471470
}
472471
}
473472

473+
impl ToString for lsp::MarkupContent {
474+
fn to_string(&self) -> String {
475+
self.value.clone()
476+
}
477+
}
478+
474479
impl ToString for Hover {
475480
fn to_string(&self) -> String {
476481
match self.contents {
@@ -484,23 +489,47 @@ impl ToString for Hover {
484489
}
485490
}
486491

492+
impl ToString for lsp::Documentation {
493+
fn to_string(&self) -> String {
494+
match *self {
495+
lsp::Documentation::String(ref s) => s.to_owned(),
496+
lsp::Documentation::MarkupContent(ref mc) => mc.to_string(),
497+
}
498+
}
499+
}
500+
501+
impl ToString for NumberOrString {
502+
fn to_string(&self) -> String {
503+
match *self {
504+
NumberOrString::Number(n) => format!("{}", n),
505+
NumberOrString::String(ref s) => s.clone(),
506+
}
507+
}
508+
}
509+
487510
pub trait ToDisplay {
488511
fn to_display(&self) -> Vec<String>;
489512
}
490513

491514
impl ToDisplay for lsp::MarkedString {
492515
fn to_display(&self) -> Vec<String> {
493516
match *self {
494-
MarkedString::String(ref s) => vec![s.clone()],
517+
MarkedString::String(ref s) => s.split('\n').map(|i| i.to_string()).collect(),
495518
MarkedString::LanguageString(ref ls) => vec![
496519
format!("```{}", ls.language),
497-
ls.value.clone(),
520+
ls.value.split('\n').map(|i| i.to_string()).collect(),
498521
"```".to_string(),
499522
],
500523
}
501524
}
502525
}
503526

527+
impl ToDisplay for MarkupContent {
528+
fn to_display(&self) -> Vec<String> {
529+
self.value.split('\n').map(|i| i.to_string()).collect()
530+
}
531+
}
532+
504533
impl ToDisplay for Hover {
505534
fn to_display(&self) -> Vec<String> {
506535
match self.contents {
@@ -511,39 +540,31 @@ impl ToDisplay for Hover {
511540
}
512541
}
513542

514-
pub trait Len {
515-
fn len(&self) -> usize;
543+
pub trait LinesLen {
544+
fn lines_len(&self) -> usize;
516545
}
517546

518-
impl Len for Hover {
519-
fn len(&self) -> usize {
520-
match self.contents {
521-
HoverContents::Scalar(_) | HoverContents::Markup(_) => 1,
522-
HoverContents::Array(ref arr) => arr.len(),
547+
impl LinesLen for lsp::MarkedString {
548+
fn lines_len(&self) -> usize {
549+
match *self {
550+
MarkedString::String(ref s) => s.split('\n').count(),
551+
MarkedString::LanguageString(ref ls) => ls.value.split('\n').count(),
523552
}
524553
}
525554
}
526555

527-
impl ToString for lsp::MarkupContent {
528-
fn to_string(&self) -> String {
529-
self.value.clone()
530-
}
531-
}
532-
533-
impl ToString for lsp::Documentation {
534-
fn to_string(&self) -> String {
535-
match *self {
536-
lsp::Documentation::String(ref s) => s.to_owned(),
537-
lsp::Documentation::MarkupContent(ref mc) => mc.to_string(),
538-
}
556+
impl LinesLen for MarkupContent {
557+
fn lines_len(&self) -> usize {
558+
self.value.split('\n').count()
539559
}
540560
}
541561

542-
impl ToString for NumberOrString {
543-
fn to_string(&self) -> String {
544-
match *self {
545-
NumberOrString::Number(n) => format!("{}", n),
546-
NumberOrString::String(ref s) => s.clone(),
562+
impl LinesLen for Hover {
563+
fn lines_len(&self) -> usize {
564+
match self.contents {
565+
HoverContents::Scalar(ref c) => c.lines_len(),
566+
HoverContents::Array(ref arr) => arr.iter().map(|i| i.lines_len()).sum(),
567+
HoverContents::Markup(ref c) => c.lines_len(),
547568
}
548569
}
549570
}

0 commit comments

Comments
 (0)