Skip to content

Commit

Permalink
fix: use alternative oxi binding to get filetype (#28)
Browse files Browse the repository at this point in the history
* 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.

* 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.

* refactor: remove unused import
  • Loading branch information
PriceHiller authored Feb 27, 2023
1 parent 30a5181 commit 51aa0c4
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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)?.ok_or_else(|| Error::Other(String::from("Unable to determine filetype!")))?;

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()))?;

Expand Down Expand Up @@ -260,4 +262,4 @@ fn silicon() -> oxi::Result<Dictionary> {
("capture", Function::from_fn(save_image)),
("setup", Function::from_fn(setup)),
]))
}
}

0 comments on commit 51aa0c4

Please sign in to comment.