Skip to content
This repository has been archived by the owner on Jan 25, 2024. It is now read-only.

Commit

Permalink
feat: format via alejandra when feature is set
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Volosatovs <rvolosatovs@riseup.net>
  • Loading branch information
rvolosatovs committed Jun 23, 2022
1 parent 2e49c1f commit 9189b50
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 11 deletions.
36 changes: 36 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ rnix = "0.10.2"
rowan = "0.12.6"
serde = "1.0.104"
serde_json = "1.0.44"
nixpkgs-fmt-rnix = "1.2.0"
nixpkgs-fmt-rnix = { version = "1.2.0", optional = true }
alejandra = { version = "1.5.0", optional = true }

[dev-dependencies]
stoppable_thread = "0.2"

[features]

# Set this to ["verbose"] when debugging
default = []
default = [ "nixpkgs-fmt-rnix" ]

# Enable showing internal errors via editor UI, such as via hover popups.
# This can be helpful for debugging, but we don't want the evaluator to
Expand Down
42 changes: 33 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,16 +205,40 @@ impl App {
let document_links = self.document_links(&params).unwrap_or_default();
self.reply(Response::new_ok(id, document_links));
} else if let Some((id, params)) = cast::<Formatting>(&mut req) {
let changes = if let Some((ast, code, _)) = self.files.get(&params.text_document.uri) {
let fmt = nixpkgs_fmt::reformat_node(&ast.node());
vec![TextEdit {
range: utils::range(&code, TextRange::up_to(ast.node().text().len())),
new_text: fmt.text().to_string(),
}]
if let Some((ast, code, _)) = self.files.get(&params.text_document.uri) {
let node = ast.node();
let range = utils::range(&code, TextRange::up_to(node.text().len()));
if cfg!(feature = "alejandra") {
#[cfg(feature = "alejandra")]
match alejandra::format::in_memory(
params.text_document.uri.to_string(),
code.to_string(),
) {
(alejandra::format::Status::Changed(true), new_text) => {
self.reply(Response::new_ok(id, vec![TextEdit { range, new_text }]))
}
(alejandra::format::Status::Changed(false), _) => {
self.reply(Response::new_ok(id, ()))
}
(alejandra::format::Status::Error(e), _) => {
self.reply(Response::new_err(id, ErrorCode::InternalError as i32, e))
}
}
} else if cfg!(feature = "nixpkgs-fmt-rnix") {
#[cfg(feature = "nixpkgs-fmt-rnix")]
self.reply(Response::new_ok(
id,
vec![TextEdit {
range,
new_text: nixpkgs_fmt::reformat_node(&node).text().to_string(),
}],
));
} else {
self.reply(Response::new_ok(id, ()))
}
} else {
Vec::new()
};
self.reply(Response::new_ok(id, changes));
self.reply(Response::new_ok(id, ()))
}
} else if let Some((id, params)) = cast::<HoverRequest>(&mut req) {
if let Some((range, markdown)) = self.hover(params) {
self.reply(Response::new_ok(
Expand Down

0 comments on commit 9189b50

Please sign in to comment.