Skip to content

Commit

Permalink
Use preview window when hover content is multiline.
Browse files Browse the repository at this point in the history
Close #224.
  • Loading branch information
autozimu authored and Junfeng Li committed Mar 26, 2018
1 parent 8e5b198 commit 6b41182
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 19 deletions.
39 changes: 31 additions & 8 deletions src/languageclient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,14 @@ pub trait ILanguageClient: IVim {

self.goto_location(&None, &path, 0, 0)?;
let mut lines: Vec<String> = self.getbufline(&path)?;
let lines_len = lines.len();
lines = apply_TextEdits(&lines, &edits)?;
let fixendofline: u64 = self.eval("&fixendofline")?;
if fixendofline == 1 && lines[lines.len() - 1].is_empty() {
lines.pop();
}
self.notify(None, "setline", json!([1, lines]))?;
if lines.len() < lines_len {
self.command(&format!("{},{}d", lines.len() + 1, lines_len))?;
self.command("1,$d")?;
if self.call(None, "setline", json!([1, lines]))? != 0 {
bail!("Failed to set preview buffer content!");
}
debug!("End apply TextEdits");
Ok(())
Expand Down Expand Up @@ -552,6 +551,20 @@ pub trait ILanguageClient: IVim {
Ok(())
}

fn preview(&self, lines: &[String]) -> Result<()> {
let mut cmd = String::new();
cmd += "pedit! +setlocal\\ buftype=nofile\\ filetype=markdown\\ nobuflisted\\ noswapfile\\ nonumber LanguageClient ";
cmd += "| wincmd P ";
cmd += "| 1,$d ";
self.command(cmd)?;

if self.call(None, "setline", json!([1, lines]))? != 0 {
bail!("Failed to set preview buffer content!");
}

self.command("wincmd p")
}

/////// LSP ///////

fn initialize(&self, params: &Option<Params>) -> Result<Value> {
Expand Down Expand Up @@ -679,8 +692,11 @@ pub trait ILanguageClient: IVim {

let hover: Option<Hover> = serde_json::from_value(result.clone())?;
if let Some(hover) = hover {
let message = hover.to_string();
self.echo(&message)?;
if hover.len() <= 1 {
self.echo(hover.to_string())?;
} else {
self.preview(&hover.to_display())?;
}
}

info!("End {}", lsp::request::HoverRequest::METHOD);
Expand Down Expand Up @@ -1752,7 +1768,14 @@ pub trait ILanguageClient: IVim {

fn languageClient_handleTextChanged(&self, params: &Option<Params>) -> Result<()> {
info!("Begin {}", NOTIFICATION__HandleTextChanged);
let (filename,): (String,) = self.gather_args(&[VimVar::Filename], params)?;
let (buftype, filename): (String, String) =
self.gather_args(&[VimVar::Buftype, VimVar::Filename], params)?;
if !buftype.is_empty() {
info!(
"Skip handleTextChanged as buftype is non-empty: {}",
buftype
);
}
let skip_notification = self.get(|state| {
if let Some(metadata) = state.text_documents_metadata.get(&filename) {
if let Some(throttle) = state.change_throttle {
Expand All @@ -1764,7 +1787,7 @@ pub trait ILanguageClient: IVim {
Ok(false)
})?;
if skip_notification {
info!("Skip didChange notification due to throttling");
info!("Skip handleTextChanged due to throttling");
return Ok(());
}

Expand Down
53 changes: 45 additions & 8 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,17 +471,54 @@ impl ToString for lsp::MarkedString {

impl ToString for Hover {
fn to_string(&self) -> String {
let mut message = String::new();
match self.contents {
HoverContents::Scalar(ref ms) => ms.to_string(),
HoverContents::Array(ref vec) => vec.iter()
.map(|i| i.to_string())
.collect::<Vec<_>>()
.join("\n"),
HoverContents::Markup(ref mc) => mc.to_string(),
}
}
}

pub trait ToDisplay {
fn to_display(&self) -> Vec<String>;
}

impl ToDisplay for lsp::MarkedString {
fn to_display(&self) -> Vec<String> {
match *self {
MarkedString::String(ref s) => vec![s.clone()],
MarkedString::LanguageString(ref ls) => vec![
format!("```{}", ls.language),
ls.value.clone(),
"```".to_string(),
],
}
}
}

impl ToDisplay for Hover {
fn to_display(&self) -> Vec<String> {
match self.contents {
HoverContents::Scalar(ref ms) => message += &ms.to_string(),
HoverContents::Array(ref vec) => for item in vec {
message += &item.to_string();
},
HoverContents::Markup(ref mc) => message += &mc.to_string(),
};
HoverContents::Scalar(ref ms) => ms.to_display(),
HoverContents::Array(ref arr) => arr.iter().flat_map(|i| i.to_display()).collect(),
HoverContents::Markup(ref mc) => vec![mc.to_string()],
}
}
}

pub trait Len {
fn len(&self) -> usize;
}

message
impl Len for Hover {
fn len(&self) -> usize {
match self.contents {
HoverContents::Scalar(_) | HoverContents::Markup(_) => 1,
HoverContents::Array(ref arr) => arr.len(),
}
}
}

Expand Down
5 changes: 2 additions & 3 deletions tests/LanguageClient_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@ def test_textDocument_hover(nvim):
nvim.command("edit! {}".format(PATH_INDEXJS))
time.sleep(1)
nvim.funcs.cursor(13, 19)
nvim.command("redir => g:echo")
nvim.funcs.LanguageClient_textDocument_hover()
time.sleep(1)
nvim.command("redir END")
b = next(b for b in nvim.buffers if b.name.endswith('LanguageClient'))
expect = "function greet(): number"

assert expect in nvim.vars.get("echo")
assert expect in b


def test_textDocument_definition(nvim):
Expand Down

0 comments on commit 6b41182

Please sign in to comment.