use super::*;
impl App {
pub(in crate::app) fn codeql_history_path() -> PathBuf {
croft_cache_dir().join("codeql-history.json")
}
pub(in crate::app) fn codeql_results_dir() -> PathBuf {
croft_cache_dir().join("codeql").join("results")
}
pub(in crate::app) fn refresh_codeql_history(&mut self) {
let history = crate::codeql_query::History::load(&Self::codeql_history_path());
self.codeql.history = history
.entries
.iter()
.map(crate::widgets::codeql::HistoryRow::new)
.collect();
self.codeql.history_sort = history.sort_by;
if let Some(out) = &self.codeql.expanded
&& !self.codeql.history.iter().any(|r| &r.output == out)
{
self.codeql.expanded = None;
}
}
pub(in crate::app) fn current_codeql_history(&mut self) -> Option<usize> {
let history = crate::codeql_query::History::load(&Self::codeql_history_path());
let current = self
.codeql
.selected_history()
.filter(|&i| i < history.entries.len())
.or_else(|| history.newest());
if current.is_none() {
self.status = String::from("There is no CodeQL query history yet");
}
current
}
pub(in crate::app) fn codeql_cached_results(output: &Path) -> Option<PathBuf> {
Self::cache_entry_holding(&Self::codeql_results_dir(), output.parent()?)
}
pub(in crate::app) fn confirm_remove_codeql_history(&mut self, index: usize) {
use crate::widgets::input_prompt::{InputPrompt, InputPurpose};
let history = crate::codeql_query::History::load(&Self::codeql_history_path());
let Some(entry) = history.entries.get(index) else {
return;
};
if entry.status == crate::codeql_query::RunStatus::Running {
self.status = String::from("That query is still running");
return;
}
let what = if Self::codeql_cached_results(&entry.output).is_some() {
"Its results are deleted."
} else {
"Its results stay on disk."
};
self.open_input_prompt(InputPrompt::confirm(
InputPurpose::CodeqlRemoveHistory {
output: entry.output.clone(),
},
format!(
"Remove query history entry '{}'? {what}",
entry.display_name()
),
"Enter or Y to remove · Esc or N to keep",
));
}
pub(in crate::app) fn perform_remove_codeql_history(&mut self, output: &Path) {
let path = Self::codeql_history_path();
let mut history = crate::codeql_query::History::load(&path);
let Some(i) = history.position(output) else {
self.status = String::from("That query history entry is no longer listed");
return;
};
let name = history.entries[i].display_name();
let cached = Self::codeql_cached_results(output);
history.remove(i);
if let Err(e) = history.save(&path) {
self.status = format!("Could not save the CodeQL query history: {e}");
return;
}
self.status = match cached.map(std::fs::remove_dir_all) {
Some(Err(e)) => {
format!("Removed {name} from the query history, but not its results: {e}")
}
_ => format!("Removed {name} from the query history"),
};
self.refresh_codeql_history();
let n = self.codeql.history.len();
if n > 0 {
self.codeql.select_history(i.min(n - 1));
}
if self.codeql.selected_hit().is_none() {
self.codeql.move_selection(false);
}
}
pub(in crate::app) fn prompt_rename_codeql_history(&mut self, index: usize) {
use crate::widgets::input_prompt::{InputPrompt, InputPurpose};
let history = crate::codeql_query::History::load(&Self::codeql_history_path());
let Some(entry) = history.entries.get(index) else {
return;
};
self.open_input_prompt(
InputPrompt::new(
InputPurpose::CodeqlRenameHistory {
output: entry.output.clone(),
},
String::from("Rename Query History Entry"),
"the label shown in the side bar",
)
.with_value(entry.display_name()),
);
}
pub(in crate::app) fn submit_rename_codeql_history(&mut self, output: &Path, value: &str) {
let path = Self::codeql_history_path();
let mut history = crate::codeql_query::History::load(&path);
let Some(i) = history.position(output) else {
self.status = String::from("That query history entry is no longer listed");
return;
};
if let Err(e) = history.rename(i, value) {
self.status = e;
return;
}
history.sort(history.sort_by);
self.status = match history.save(&path) {
Ok(()) => format!("Renamed query history entry to {}", value.trim()),
Err(e) => format!("Could not save the CodeQL query history: {e}"),
};
self.refresh_codeql_history();
if let Some(i) = history.position(output) {
self.codeql.select_history(i);
}
}
pub(in crate::app) fn sort_codeql_history(&mut self) {
let path = Self::codeql_history_path();
let mut history = crate::codeql_query::History::load(&path);
let selected = self
.codeql
.selected_history()
.and_then(|i| history.entries.get(i))
.map(|e| e.output.clone());
let by = history.sort_by.next();
history.sort(by);
self.status = match history.save(&path) {
Ok(()) => format!("CodeQL query history sorted by {}", by.label()),
Err(e) => format!("Could not save the CodeQL query history: {e}"),
};
self.refresh_codeql_history();
if let Some(i) = selected.and_then(|o| history.position(&o)) {
self.codeql.select_history(i);
}
}
pub(in crate::app) fn view_codeql_history_query(&mut self, index: usize) {
let history = crate::codeql_query::History::load(&Self::codeql_history_path());
let Some(query) = history.entries.get(index).map(|e| e.query.clone()) else {
return;
};
match self.editor.open(&query) {
Ok(()) => self.sync_open_file_poll_mtime(),
Err(e) => self.status = format!("{}: {e}", query.display()),
}
}
pub(in crate::app) fn view_codeql_history_query_text(&mut self, index: usize) {
let history = crate::codeql_query::History::load(&Self::codeql_history_path());
let Some(entry) = history.entries.get(index) else {
return;
};
let text = crate::codeql_query::query_text_path(&entry.output, &entry.query);
if !text.is_file() {
self.status = format!(
"{} ran before croft kept query text; v opens the query file",
entry.query_name()
);
return;
}
match self.editor.open(&text) {
Ok(()) => {
self.sync_open_file_poll_mtime();
self.status = format!("{} as it was when it ran", entry.query_name());
}
Err(e) => self.status = format!("{}: {e}", text.display()),
}
}
pub(in crate::app) fn codeql_history_log(&mut self, index: usize) -> Option<PathBuf> {
let history = crate::codeql_query::History::load(&Self::codeql_history_path());
let entry = history.entries.get(index)?;
if entry.status == crate::codeql_query::RunStatus::Running {
self.status = format!("{} is still running", entry.query_name());
return None;
}
let log = crate::codeql_query::evaluator_log(&entry.output);
if log.is_file() {
return Some(log);
}
self.status = format!("{} has no evaluator log", entry.query_name());
None
}
pub(in crate::app) fn open_codeql_results_directory(&mut self) {
let index = self
.codeql
.selected_history()
.or_else(|| crate::codeql_query::History::load(&Self::codeql_history_path()).newest());
match index {
Some(i) => self.open_codeql_history_results_dir(i),
None => self.status = String::from("No CodeQL query has run yet"),
}
}
pub(in crate::app) fn open_codeql_history_results_dir(&mut self, index: usize) {
let history = crate::codeql_query::History::load(&Self::codeql_history_path());
let Some(output) = history.entries.get(index).map(|e| e.output.clone()) else {
return;
};
let Some(dir) = output.parent().map(Path::to_path_buf) else {
return;
};
if !dir.is_dir() {
self.status = format!("{} does not exist (yet)", dir.display());
} else if self.tree.nodes.iter().any(|n| n.path == dir) {
self.reveal_in_explorer(dir);
} else if output.is_file() {
match self.editor.open(&output) {
Ok(()) => {
self.sync_open_file_poll_mtime();
self.status = format!("Results directory: {}", dir.display());
}
Err(e) => self.status = format!("{}: {e}", output.display()),
}
} else {
self.status = format!("Results directory: {}", dir.display());
}
}
}