From f29d1969cd0cf0abe7d2c892c70c40d49bbd1b1e Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Mon, 27 Feb 2023 03:34:31 -0600 Subject: [PATCH 1/3] fix: use alternative oxi binding to get filetype This solution works, but is quite hacky. It doesn't seem to break the api for end users though, and currently works on nightly. It likely works on 0.8 as well. --- src/lib.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5a17dac..bde2580 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,12 +36,14 @@ fn save_image(opts: Opts) -> Result<(), Error> { let code = get_lines(&opts)?; - let ft: oxi::String = Buffer::current().get_option("filetype")?; + // HACK: This allows us to avoid currently broken oxi APIs to get the filetype option. + // Instead we call into VimL and get the value that way -- super ghetto, but it works without + // any breaking changes from what I can tell. + let ft = oxi::api::exec("echo &filetype", true)?.unwrap(); let syntax = ps .find_syntax_by_token( - ft.as_str() - .map_err(|e| Error::Other(format!("utf error: {e}")))?, + &ft ) .ok_or_else(|| Error::Other("Could not find syntax for filetype.".to_owned()))?; @@ -260,4 +262,4 @@ fn silicon() -> oxi::Result { ("capture", Function::from_fn(save_image)), ("setup", Function::from_fn(setup)), ])) -} +} \ No newline at end of file From 2fb59077757acc4626aba11e8642e8c72def2b04 Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Mon, 27 Feb 2023 03:55:15 -0600 Subject: [PATCH 2/3] refactor: use more idiomatic error handling instead of unwrap Instead of causing an immediate panic when a filetype can't be found, instead boil an error up for user presentation stating a filetype isn't found. This ensures Neovim doesn't just up and die when a panic occurs. --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index bde2580..cce8567 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,7 +39,7 @@ fn save_image(opts: Opts) -> Result<(), Error> { // HACK: This allows us to avoid currently broken oxi APIs to get the filetype option. // Instead we call into VimL and get the value that way -- super ghetto, but it works without // any breaking changes from what I can tell. - let ft = oxi::api::exec("echo &filetype", true)?.unwrap(); + let ft = oxi::api::exec("echo &filetype", true)?.ok_or_else(|| Error::Other(String::from("Unable to determine filetype!")))?; let syntax = ps .find_syntax_by_token( From f6cfaa91b158c9f36285bdc903875307b59162ea Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Mon, 27 Feb 2023 03:57:19 -0600 Subject: [PATCH 3/3] refactor: remove unused import --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index cce8567..486954a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,7 +14,7 @@ use utils::{parse_str_color, IntoFont, IntoFontStyle}; use nvim_oxi as oxi; use oxi::{ - api::{self, opts::*, types::*, Buffer, Error}, + api::{self, opts::*, types::*, Error}, Dictionary, Function, }; use silicon::{