Skip to content

Commit e6c87dd

Browse files
refactor: revamp configuration file discovering and custom language registration
fix ast-grep#1553
1 parent 42c407d commit e6c87dd

File tree

5 files changed

+17
-19
lines changed

5 files changed

+17
-19
lines changed

crates/cli/src/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ pub fn find_rules(
9797
read_directory_yaml(&project_config, global_rules, rule_overwrite)
9898
}
9999

100-
pub fn register_custom_language(config_path: Option<PathBuf>) -> Result<()> {
100+
pub fn register_custom_language(project_config: Option<ProjectConfig>) -> Result<()> {
101101
// do not report error if no sgconfig.yml is found
102-
let Some(project_config) = ProjectConfig::by_config_path(config_path)? else {
102+
let Some(project_config) = project_config else {
103103
return Ok(());
104104
};
105105
let ProjectConfig {

crates/cli/src/lsp.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::config::{find_config_path_with_default, find_rules, register_custom_language};
1+
use crate::config::{find_rules, register_custom_language, ProjectConfig};
22
use crate::utils::ErrorContext as EC;
33
use anyhow::{Context, Result};
44
use ast_grep_lsp::{Backend, LspService, Server};
@@ -12,18 +12,15 @@ pub struct LspArg {
1212
config: Option<PathBuf>,
1313
}
1414

15-
fn find_config_base(config: Option<PathBuf>) -> Result<PathBuf> {
16-
let mut config_path = find_config_path_with_default(config, None)?;
17-
config_path.pop();
18-
Ok(config_path)
19-
}
20-
2115
async fn run_language_server_impl(arg: LspArg) -> Result<()> {
2216
// env_logger::init();
23-
register_custom_language(arg.config.clone())?;
17+
let project_config = ProjectConfig::by_config_path(arg.config.clone())?;
18+
// TODO: move this error to client
19+
let project_config = project_config.ok_or_else(|| anyhow::anyhow!(EC::ProjectNotExist))?;
20+
let config_base = project_config.project_dir.clone();
21+
register_custom_language(Some(project_config))?;
2422
let stdin = tokio::io::stdin();
2523
let stdout = tokio::io::stdout();
26-
let config_base = find_config_base(arg.config.clone())?;
2724
let config_result = find_rules(arg.config, Default::default());
2825
let config_result_std: std::result::Result<_, String> = config_result
2926
.map_err(|e| {

crates/cli/src/new.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::config::{
2-
find_config_path_with_default, read_config_from_dir, register_custom_language, AstGrepConfig,
3-
ProjectConfig, TestConfig,
2+
read_config_from_dir, register_custom_language, AstGrepConfig, ProjectConfig, TestConfig,
43
};
54
use crate::lang::SgLang;
65
use crate::utils::ErrorContext as EC;
@@ -147,8 +146,8 @@ impl Display for Entity {
147146
}
148147

149148
pub fn run_create_new(mut arg: NewArg) -> Result<()> {
150-
let config_path = find_config_path_with_default(None, Some(arg.base_dir.as_path()))?;
151-
register_custom_language(Some(config_path))?;
149+
let config_path = ProjectConfig::by_project_dir(&arg.base_dir)?;
150+
register_custom_language(config_path)?;
152151
if let Some(entity) = arg.entity.take() {
153152
run_create_entity(entity, arg)
154153
} else {

crates/cli/src/scan.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use ast_grep_core::{NodeMatch, StrDoc};
99
use clap::Args;
1010
use ignore::WalkParallel;
1111

12-
use crate::config::{find_rules, read_rule_file, register_custom_language};
12+
use crate::config::{find_rules, read_rule_file, register_custom_language, ProjectConfig};
1313
use crate::lang::SgLang;
1414
use crate::print::{
1515
CloudPrinter, ColoredPrinter, Diff, InteractivePrinter, JSONPrinter, Platform, Printer,
@@ -68,7 +68,8 @@ pub struct ScanArg {
6868
}
6969

7070
pub fn run_with_config(arg: ScanArg) -> Result<()> {
71-
register_custom_language(arg.config.clone())?;
71+
let project_config = ProjectConfig::by_config_path(arg.config.clone())?;
72+
register_custom_language(project_config)?;
7273
let context = arg.context.get();
7374
if let Some(_format) = &arg.format {
7475
let printer = CloudPrinter::stdout();

crates/cli/src/verify.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ mod reporter;
44
mod snapshot;
55
mod test_case;
66

7-
use crate::config::{find_rules, register_custom_language};
7+
use crate::config::{find_rules, register_custom_language, ProjectConfig};
88
use crate::lang::SgLang;
99
use crate::utils::ErrorContext;
1010
use anyhow::{anyhow, Result};
@@ -183,7 +183,8 @@ pub struct TestArg {
183183
}
184184

185185
pub fn run_test_rule(arg: TestArg) -> Result<()> {
186-
register_custom_language(arg.config.clone())?;
186+
let project_config = ProjectConfig::by_config_path(arg.config.clone())?;
187+
register_custom_language(project_config)?;
187188
if arg.interactive {
188189
let reporter = InteractiveReporter {
189190
output: std::io::stdout(),

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy