Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion crates/rune/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ emit = ["codespan-reporting"]
bench = []
workspace = ["toml", "semver", "relative-path", "serde-hashkey"]
doc = ["rust-embed", "handlebars", "pulldown-cmark", "syntect"]
cli = ["doc", "bincode", "atty", "tracing-subscriber", "anyhow/std", "clap", "webbrowser", "capture-io", "disable-io", "languageserver"]
cli = ["doc", "bincode", "atty", "tracing-subscriber", "anyhow/std", "clap", "webbrowser", "capture-io", "disable-io", "languageserver", "fmt"]
languageserver = ["lsp", "ropey", "percent-encoding", "url", "serde_json", "tokio", "workspace"]
capture-io = ["parking_lot"]
disable-io = []
fmt = []

[dependencies]
rune-macros = { version = "=0.12.3", path = "../rune-macros" }
Expand Down
15 changes: 8 additions & 7 deletions crates/rune/src/cli/format.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use anyhow::{Context, Result};
use clap::Parser;
use codespan_reporting::term::termcolor::WriteColor;
use std::io::Write;
use std::path::PathBuf;

use crate::cli::{ExitCode, Io, SharedFlags};
use crate::termcolor::WriteColor;
use crate::Source;

#[derive(Parser, Debug, Clone)]
Expand All @@ -21,18 +21,19 @@ pub(super) struct Flags {
}

pub(super) fn run(io: &mut Io<'_>, paths: &[PathBuf], flags: &Flags) -> Result<ExitCode> {
let mut red = codespan_reporting::term::termcolor::ColorSpec::new();
red.set_fg(Some(codespan_reporting::term::termcolor::Color::Red));
let mut red = crate::termcolor::ColorSpec::new();
red.set_fg(Some(crate::termcolor::Color::Red));

let mut green = codespan_reporting::term::termcolor::ColorSpec::new();
green.set_fg(Some(codespan_reporting::term::termcolor::Color::Green));
let mut green = crate::termcolor::ColorSpec::new();
green.set_fg(Some(crate::termcolor::Color::Green));

let mut yellow = codespan_reporting::term::termcolor::ColorSpec::new();
yellow.set_fg(Some(codespan_reporting::term::termcolor::Color::Yellow));
let mut yellow = crate::termcolor::ColorSpec::new();
yellow.set_fg(Some(crate::termcolor::Color::Yellow));

let mut succeeded = 0;
let mut failed = 0;
let mut unchanged = 0;

for path in paths {
let source =
Source::from_path(path).with_context(|| format!("reading file: {}", path.display()))?;
Expand Down
16 changes: 7 additions & 9 deletions crates/rune/src/fmt/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#[cfg(test)]
mod tests;

use std::iter::Peekable;
use std::str::CharIndices;

use crate::ast::Span;

Expand All @@ -25,7 +25,7 @@ pub(super) struct Comment {
pub(super) fn parse_comments(input: &str) -> Result<Vec<Comment>, FormattingError> {
let mut comments = Vec::new();

let mut chars = input.char_indices().peekable();
let mut chars = input.char_indices();

let mut in_string = false;
let mut in_char = false;
Expand All @@ -34,7 +34,7 @@ pub(super) fn parse_comments(input: &str) -> Result<Vec<Comment>, FormattingErro

while let Some((idx, c)) = chars.next() {
match c {
'/' if !in_string && !in_char && !in_template => match chars.peek() {
'/' if !in_string && !in_char && !in_template => match chars.clone().next() {
Some((_, '/')) => {
let end = parse_line_comment(&mut chars);

Expand Down Expand Up @@ -81,7 +81,6 @@ pub(super) fn parse_comments(input: &str) -> Result<Vec<Comment>, FormattingErro
on_new_line = true;
}
c if c.is_whitespace() => {}

_ => {
on_new_line = false;
}
Expand All @@ -91,7 +90,7 @@ pub(super) fn parse_comments(input: &str) -> Result<Vec<Comment>, FormattingErro
Ok(comments)
}

fn parse_line_comment(chars: &mut Peekable<impl Iterator<Item = (usize, char)>>) -> usize {
fn parse_line_comment(chars: &mut CharIndices<'_>) -> usize {
let mut last_i = 0;

for (i, c) in chars.by_ref() {
Expand All @@ -106,12 +105,11 @@ fn parse_line_comment(chars: &mut Peekable<impl Iterator<Item = (usize, char)>>)
last_i + 1
}

fn parse_block_comment(chars: &mut Peekable<impl Iterator<Item = (usize, char)>>) -> Option<usize> {
fn parse_block_comment(chars: &mut CharIndices<'_>) -> Option<usize> {
while let Some((_, c)) = chars.next() {
if c == '*' {
if let Some((_, '/')) = chars.peek() {
let (offset, _) = chars.next()?;
return Some(offset);
if let Some((_, '/')) = chars.clone().next() {
return Some(chars.next()?.0);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/rune/src/fmt/comments/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ use super::*;
#[test]
fn test_parse_line_comment() {
let input = "// this is a comment\n";
let mut chars = input.char_indices().peekable();
let mut chars = input.char_indices();
let end = parse_line_comment(&mut chars);
assert_eq!(end, input.len() - 1);
}

#[test]
fn test_parse_block_comment() {
let input = "/* this is a comment */";
let mut chars = input.char_indices().peekable();
let mut chars = input.char_indices();
let end = parse_block_comment(&mut chars).unwrap();
assert_eq!(end, input.len() - 1);
}
Expand Down
1 change: 1 addition & 0 deletions crates/rune/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ mod exported_macros;
#[macro_use]
pub mod ast;

#[cfg(feature = "fmt")]
pub mod fmt;

cfg_emit! {
Expand Down