Skip to content

Commit

Permalink
Combine GoContextProvider after rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
mrnugget committed May 22, 2024
1 parent 71ec940 commit 6ab7614
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 141 deletions.
213 changes: 78 additions & 135 deletions crates/languages/src/go.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,128 +402,6 @@ impl super::LspAdapter for GoLspAdapter {
}
}

pub(crate) struct GoContextProvider;

const GO_TEST_DIRECTORY_TASK_VARIABLE: VariableName =
VariableName::Custom(Cow::Borrowed("GO_TEST_DIRECTORY"));
const GO_SUBTEST_NAME_TASK_VARIABLE: VariableName =
VariableName::Custom(Cow::Borrowed("GO_SUBTEST_NAME"));

impl ContextProvider for GoContextProvider {
fn build_context(
&self,
_: Option<&Path>,
location: &Location,
cx: &mut gpui::AppContext,
) -> Result<TaskVariables> {
let local_abs_path = location
.buffer
.read(cx)
.file()
.and_then(|file| Some(file.as_local()?.abs_path(cx)));

let test_folder = local_abs_path
.as_deref()
.and_then(|local_abs_path| local_abs_path.parent())
.and_then(|parent| parent.to_string_lossy().into());

let snapshot = location.buffer.read(cx).snapshot();
let point_range = location.range.to_point(&snapshot);
let end = Point::new(point_range.start.row + 1, 0);
let line = snapshot.text_for_range(point_range.start..end).peek();

let subtest_name = extract_subtest_name(line.unwrap_or(""));

Ok(match (subtest_name, test_folder) {
(Some(subtest), Some(folder)) => TaskVariables::from_iter([
(GO_TEST_DIRECTORY_TASK_VARIABLE.clone(), folder.to_string()),
(GO_SUBTEST_NAME_TASK_VARIABLE.clone(), subtest),
]),
(None, Some(folder)) => TaskVariables::from_iter(Some((
GO_TEST_DIRECTORY_TASK_VARIABLE.clone(),
folder.to_string(),
))),
_ => TaskVariables::default(),
})
}

fn associated_tasks(&self) -> Option<TaskTemplates> {
Some(TaskTemplates(vec![
TaskTemplate {
label: format!("test {}", VariableName::Symbol.template_value()),
command: "go".into(),
args: vec![
"test".into(),
GO_TEST_DIRECTORY_TASK_VARIABLE.template_value(),
"-v".into(),
"-run".into(),
format!("^{}$", VariableName::Symbol.template_value()),
],
tags: vec!["go-test".to_owned()],
..TaskTemplate::default()
},
TaskTemplate {
label: format!(
"test {}/{}",
VariableName::Symbol.template_value(),
GO_SUBTEST_NAME_TASK_VARIABLE.template_value(),
),
command: "go".into(),
args: vec![
"test".into(),
GO_TEST_DIRECTORY_TASK_VARIABLE.template_value(),
"-v".into(),
"-run".into(),
format!(
"^{}$/^{}$",
VariableName::Symbol.template_value(),
GO_SUBTEST_NAME_TASK_VARIABLE.template_value(),
),
],
tags: vec!["go-subtest".to_owned()],
..TaskTemplate::default()
},
TaskTemplate {
label: format!("bench {}", VariableName::Symbol.template_value()),
command: "go".into(),
args: vec![
"test".into(),
GO_TEST_DIRECTORY_TASK_VARIABLE.template_value(),
"-benchmem".into(),
"-run=^$".into(),
"-bench".into(),
format!("^{}$", VariableName::Symbol.template_value()),
],
tags: vec!["go-benchmark".to_owned()],
..TaskTemplate::default()
},
TaskTemplate {
label: format!("run {}", VariableName::Symbol.template_value()),
command: "go".into(),
args: vec!["run".into(), VariableName::File.template_value()],
tags: vec!["go-run".to_owned()],
..TaskTemplate::default()
},
]))
}
}

fn extract_subtest_name(input: &str) -> Option<String> {
GO_EXTRACT_SUBTEST_NAME_REGEX
.captures(input)
.map(|captures| {
let subtest_name = captures
.get(1)
.map(|matched| matched.as_str().replace(' ', "_"))
.unwrap_or_default();
GO_ESCAPE_SUBTEST_NAME_REGEX
.replace_all(&subtest_name, |caps: &regex::Captures| {
format!("\\{}", &caps[0])
})
.to_string()
})
}

async fn get_cached_server_binary(container_dir: PathBuf) -> Option<LanguageServerBinary> {
maybe!(async {
let mut last_binary_path = None;
Expand Down Expand Up @@ -568,6 +446,8 @@ fn adjust_runs(
pub(crate) struct GoContextProvider;

const GO_PACKAGE_TASK_VARIABLE: VariableName = VariableName::Custom(Cow::Borrowed("GO_PACKAGE"));
const GO_SUBTEST_NAME_TASK_VARIABLE: VariableName =
VariableName::Custom(Cow::Borrowed("GO_SUBTEST_NAME"));

impl ContextProvider for GoContextProvider {
fn build_context(
Expand All @@ -582,11 +462,10 @@ impl ContextProvider for GoContextProvider {
.file()
.and_then(|file| Some(file.as_local()?.abs_path(cx)));

Ok(
if let Some(buffer_dir) = local_abs_path
.as_deref()
.and_then(|local_abs_path| local_abs_path.parent())
{
let go_package_variable = local_abs_path
.as_deref()
.and_then(|local_abs_path| local_abs_path.parent())
.map(|buffer_dir| {
// Prefer the relative form `./my-nested-package/is-here` over
// absolute path, because it's more readable in the modal, but
// the absolute path also works.
Expand All @@ -601,14 +480,22 @@ impl ContextProvider for GoContextProvider {
})
.unwrap_or_else(|| format!("{}", buffer_dir.to_string_lossy()));

TaskVariables::from_iter(Some((
GO_PACKAGE_TASK_VARIABLE.clone(),
package_name.to_string(),
)))
} else {
TaskVariables::default()
},
)
(GO_PACKAGE_TASK_VARIABLE.clone(), package_name.to_string())
});

let snapshot = location.buffer.read(cx).snapshot();
let point_range = location.range.to_point(&snapshot);
let end = Point::new(point_range.start.row + 1, 0);
let line = snapshot.text_for_range(point_range.start..end).peek();

let go_subtest_variable = extract_subtest_name(line.unwrap_or(""))
.map(|subtest_name| (GO_SUBTEST_NAME_TASK_VARIABLE.clone(), subtest_name));

Ok(TaskVariables::from_iter(
[go_package_variable, go_subtest_variable]
.into_iter()
.flat_map(|var| var),
))
}

fn associated_tasks(&self) -> Option<TaskTemplates> {
Expand Down Expand Up @@ -641,6 +528,46 @@ impl ContextProvider for GoContextProvider {
args: vec!["test".into(), "./...".into()],
..TaskTemplate::default()
},
TaskTemplate {
label: format!(
"go test {} -run {} {}",
GO_PACKAGE_TASK_VARIABLE.template_value(),
VariableName::Symbol.template_value(),
GO_SUBTEST_NAME_TASK_VARIABLE.template_value(),
),
command: "go".into(),
args: vec![
"test".into(),
GO_PACKAGE_TASK_VARIABLE.template_value(),
"-v".into(),
"-run".into(),
format!(
"^{}$/^{}$",
VariableName::Symbol.template_value(),
GO_SUBTEST_NAME_TASK_VARIABLE.template_value(),
),
],
tags: vec!["go-subtest".to_owned()],
..TaskTemplate::default()
},
TaskTemplate {
label: format!(
"go test {} -bench {}",
GO_PACKAGE_TASK_VARIABLE.template_value(),
VariableName::Symbol.template_value()
),
command: "go".into(),
args: vec![
"test".into(),
GO_PACKAGE_TASK_VARIABLE.template_value(),
"-benchmem".into(),
"-run=^$".into(),
"-bench".into(),
format!("^{}$", VariableName::Symbol.template_value()),
],
tags: vec!["go-benchmark".to_owned()],
..TaskTemplate::default()
},
TaskTemplate {
label: format!("go run {}", GO_PACKAGE_TASK_VARIABLE.template_value(),),
command: "go".into(),
Expand All @@ -652,6 +579,22 @@ impl ContextProvider for GoContextProvider {
}
}

fn extract_subtest_name(input: &str) -> Option<String> {
GO_EXTRACT_SUBTEST_NAME_REGEX
.captures(input)
.map(|captures| {
let subtest_name = captures
.get(1)
.map(|matched| matched.as_str().replace(' ', "_"))
.unwrap_or_default();
GO_ESCAPE_SUBTEST_NAME_REGEX
.replace_all(&subtest_name, |caps: &regex::Captures| {
format!("\\{}", &caps[0])
})
.to_string()
})
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
6 changes: 0 additions & 6 deletions crates/languages/src/go/runnables.scm
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@
(#match? @_name "^Benchmark.+"))
) @go-benchmark

; `t.Run`
(
(call_expression function: (_) @run
(#match? @run "^t.Run.*"))
) @go-subtest

; go run
(
(function_declaration name: (_) @run @_name
Expand Down
23 changes: 23 additions & 0 deletions crates/task/src/task_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,4 +676,27 @@ mod tests {
expected.sort_by_key(|var| var.to_string());
assert_eq!(resolved_variables, expected)
}

#[test]
fn substitute_funky_labels() {
let faulty_go_test = TaskTemplate {
label: format!(
"go test {}/{}",
VariableName::Symbol.template_value(),
VariableName::Symbol.template_value(),
),
command: "go".into(),
args: vec![format!(
"^{}$/^{}$",
VariableName::Symbol.template_value(),
VariableName::Symbol.template_value()
)],
..TaskTemplate::default()
};
let mut context = TaskContext::default();
context
.task_variables
.insert(VariableName::Symbol, "my-symbol".to_string());
assert!(faulty_go_test.resolve_task("base", &context).is_some());
}
}

0 comments on commit 6ab7614

Please sign in to comment.