Skip to content

Commit

Permalink
Extract getbufvar()
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchengxu committed Aug 10, 2023
1 parent 76f9ade commit cad5e2e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
10 changes: 2 additions & 8 deletions crates/maple_core/src/stdio_server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,7 @@ impl Client {
"generate-toc" => {
let curlnum = self.vim.line(".").await?;
let file = self.vim.current_buffer_path().await?;
let shiftwidth = self
.vim
.call("getbufvar", json!(["", "&shiftwidth"]))
.await?;
let shiftwidth = self.vim.getbufvar("", "&shiftwidth").await?;
let mut toc = plugin::generate_toc(file, curlnum, shiftwidth)?;
let prev_line = self.vim.curbufline(curlnum - 1).await?;
if !prev_line.map(|line| line.is_empty()).unwrap_or(false) {
Expand All @@ -267,10 +264,7 @@ impl Client {
let file = self.vim.current_buffer_path().await?;
let bufnr = self.vim.current_bufnr().await?;
if let Some((start, end)) = plugin::find_toc_range(&file)? {
let shiftwidth = self
.vim
.call("getbufvar", json!(["", "&shiftwidth"]))
.await?;
let shiftwidth = self.vim.getbufvar("", "&shiftwidth").await?;
// TODO: skip update if the new doc is the same as the old one.
let new_toc = plugin::generate_toc(file, start + 1, shiftwidth)?;
self.vim
Expand Down
8 changes: 8 additions & 0 deletions crates/maple_core/src/stdio_server/vim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,14 @@ impl Vim {
self.call("getpos", json![expr]).await
}

pub async fn getbufvar<R: DeserializeOwned>(
&self,
buf: impl Serialize,
varname: &str,
) -> Result<R> {
self.call("getbufvar", json!([buf, varname])).await
}

pub fn setbufvar(&self, bufnr: usize, varname: &str, val: impl Serialize) -> Result<()> {
self.exec("setbufvar", serde_json::json!([bufnr, varname, val]))
}
Expand Down

0 comments on commit cad5e2e

Please sign in to comment.