Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 20 additions & 2 deletions tooling/lsp/src/requests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::collections::BTreeMap;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::{collections::HashMap, future::Future};

use crate::{insert_all_files_for_workspace_into_file_manager, parse_diff, PackageCacheData};
Expand Down Expand Up @@ -271,14 +271,14 @@
signature_help_provider: Some(lsp_types::OneOf::Right(
lsp_types::SignatureHelpOptions {
trigger_characters: Some(vec!["(".to_string(), ",".to_string()]),
retrigger_characters: None,

Check warning on line 274 in tooling/lsp/src/requests/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (retrigger)
work_done_progress_options: WorkDoneProgressOptions {
work_done_progress: None,
},
},
)),
code_action_provider: Some(lsp_types::OneOf::Right(lsp_types::CodeActionOptions {
code_action_kinds: Some(vec![CodeActionKind::QUICKFIX]),

Check warning on line 281 in tooling/lsp/src/requests/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (QUICKFIX)
work_done_progress_options: WorkDoneProgressOptions {
work_done_progress: None,
},
Expand All @@ -301,6 +301,10 @@
state: &LspState,
params: lsp_types::DocumentFormattingParams,
) -> Result<Option<Vec<lsp_types::TextEdit>>, ResponseError> {
// The file_path might be Err/None if the action runs against an unsaved file
let file_path = params.text_document.uri.to_file_path().ok();
let directory_path = file_path.as_ref().and_then(|path| path.parent());

let path = params.text_document.uri.to_string();

if let Some(source) = state.input_files.get(&path) {
Expand All @@ -310,7 +314,8 @@
return Ok(None);
}

let new_text = nargo_fmt::format(source, module, &Config::default());
let config = read_format_config(directory_path);
let new_text = nargo_fmt::format(source, module, &config);

let start_position = Position { line: 0, character: 0 };
let end_position = Position {
Expand All @@ -327,6 +332,19 @@
}
}

fn read_format_config(file_path: Option<&Path>) -> Config {
match file_path {
Some(file_path) => match Config::read(file_path) {
Ok(config) => config,
Err(err) => {
eprintln!("{}", err);
Config::default()
}
},
None => Config::default(),
}
}

pub(crate) fn position_to_byte_index<'a, F>(
files: &'a F,
file_id: F::FileId,
Expand Down
4 changes: 2 additions & 2 deletions tooling/nargo_fmt/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn generate_formatter_tests(test_file: &mut File, test_data_dir: &Path) {

let (parsed_module, _errors) = noirc_frontend::parse_program(input);

let config = nargo_fmt::Config::of("{config}").unwrap();
let config = nargo_fmt::Config::of("{config}", &std::path::PathBuf::new()).unwrap();
let fmt_text = nargo_fmt::format(input, parsed_module, &config);

if std::env::var("UPDATE_EXPECT").is_ok() {{
Expand All @@ -84,7 +84,7 @@ fn generate_formatter_tests(test_file: &mut File, test_data_dir: &Path) {

let (parsed_module, _errors) = noirc_frontend::parse_program(expected_output);

let config = nargo_fmt::Config::of("{config}").unwrap();
let config = nargo_fmt::Config::of("{config}", &std::path::PathBuf::new()).unwrap();
let fmt_text = nargo_fmt::format(expected_output, parsed_module, &config);

similar_asserts::assert_eq!(fmt_text, expected_output);
Expand Down
29 changes: 19 additions & 10 deletions tooling/nargo_fmt/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,30 @@ config! {
}

impl Config {
pub fn read(path: &Path) -> Result<Self, ConfigError> {
let config_path = path.join("noirfmt.toml");
/// Reads a Config starting at the given path and going through the path parents
/// until a `noirfmt.toml` file is found in one of them or the root is reached.
pub fn read(mut path: &Path) -> Result<Self, ConfigError> {
loop {
let config_path = path.join("noirfmt.toml");
if config_path.exists() {
match std::fs::read_to_string(&config_path) {
Ok(input) => return Self::of(&input, &config_path),
Err(cause) => return Err(ConfigError::ReadFailed(config_path, cause)),
};
}

let input = match std::fs::read_to_string(&config_path) {
Ok(input) => input,
Err(cause) if cause.kind() == std::io::ErrorKind::NotFound => String::new(),
Err(cause) => return Err(ConfigError::ReadFailed(config_path, cause)),
};
let Some(parent_path) = path.parent() else {
return Ok(Config::default());
};

Self::of(&input)
path = parent_path;
}
}

pub fn of(s: &str) -> Result<Self, ConfigError> {
pub fn of(s: &str, path: &Path) -> Result<Self, ConfigError> {
let mut config = Self::default();
let toml = toml::from_str(s).map_err(ConfigError::MalformedFile)?;
let toml =
toml::from_str(s).map_err(|err| ConfigError::MalformedFile(path.to_path_buf(), err))?;
config.fill_from_toml(toml);
Ok(config)
}
Expand Down
4 changes: 2 additions & 2 deletions tooling/nargo_fmt/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ pub enum ConfigError {
#[error("Cannot read file {0} - {1}")]
ReadFailed(PathBuf, std::io::Error),

#[error("noirfmt.toml is badly formed, could not parse.\n\n {0}")]
MalformedFile(#[from] toml::de::Error),
#[error("{0} is badly formed, could not parse.\n\n {1}")]
MalformedFile(PathBuf, toml::de::Error),
}
Loading