Skip to content

Commit

Permalink
Make Flowistry less annoying when it can't find a file
Browse files Browse the repository at this point in the history
  • Loading branch information
willcrichton committed Oct 15, 2022
1 parent 3de1426 commit b972b71
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 53 deletions.
12 changes: 8 additions & 4 deletions crates/flowistry_ide/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ fn postprocess<T: Serialize>(result: FlowistryResult<T>) -> RustcResult<()> {
FlowistryError::BuildError => {
return Err(rustc_errors::ErrorGuaranteed::unchecked_claim_error_was_emitted());
}
FlowistryError::AnalysisError(msg) => Err(msg),
e => Err(e),
},
};

Expand Down Expand Up @@ -234,13 +234,17 @@ fn run<A: FlowistryAnalysis, T: ToSpan>(
callbacks
.output
.unwrap()
.map_err(|e| FlowistryError::AnalysisError(e.to_string()))
.map_err(|e| FlowistryError::AnalysisError {
error: e.to_string(),
})
}

#[derive(Debug)]
#[derive(Debug, Serialize)]
#[serde(tag = "type")]
pub enum FlowistryError {
BuildError,
AnalysisError(String),
AnalysisError { error: String },
FileNotFound,
}

pub type FlowistryResult<T> = Result<T, FlowistryError>;
Expand Down
44 changes: 23 additions & 21 deletions crates/flowistry_ide/src/spans.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use flowistry::source_map::{find_bodies, Range};
use serde::Serialize;

use crate::plugin::FlowistryResult;
use crate::plugin::{FlowistryError, FlowistryResult};

#[derive(Serialize)]
pub struct SpansOutput {
Expand All @@ -10,7 +10,7 @@ pub struct SpansOutput {

struct Callbacks {
filename: String,
output: Option<SpansOutput>,
output: Option<FlowistryResult<SpansOutput>>,
}

impl rustc_driver::Callbacks for Callbacks {
Expand All @@ -22,25 +22,27 @@ impl rustc_driver::Callbacks for Callbacks {
queries.global_ctxt().unwrap().take().enter(|tcx| {
let spans = find_bodies(tcx).into_iter().map(|(span, _)| span);

let source_map = compiler.session().source_map();
let source_file = Range {
byte_start: 0,
byte_end: 0,
char_start: 0,
char_end: 0,
filename: self.filename.clone(),
}
.source_file(source_map)
.unwrap();
self.output = Some((|| {
let source_map = compiler.session().source_map();
let source_file = Range {
byte_start: 0,
byte_end: 0,
char_start: 0,
char_end: 0,
filename: self.filename.clone(),
}
.source_file(source_map)
.map_err(|_| FlowistryError::FileNotFound)?;

let spans = spans
.into_iter()
.filter(|span| {
source_map.lookup_source_file(span.lo()).name_hash == source_file.name_hash
})
.filter_map(|span| Range::from_span(span, source_map).ok())
.collect::<Vec<_>>();
self.output = Some(SpansOutput { spans });
let spans = spans
.into_iter()
.filter(|span| {
source_map.lookup_source_file(span.lo()).name_hash == source_file.name_hash
})
.filter_map(|span| Range::from_span(span, source_map).ok())
.collect::<Vec<_>>();
Ok(SpansOutput { spans })
})());
});
rustc_driver::Compilation::Stop
}
Expand All @@ -52,5 +54,5 @@ pub fn spans(args: &[String], filename: String) -> FlowistryResult<SpansOutput>
output: None,
};
crate::plugin::run_with_callbacks(args, &mut callbacks)?;
Ok(callbacks.output.unwrap())
callbacks.output.unwrap()
}
19 changes: 12 additions & 7 deletions ide/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ import { globals } from "./extension";
import { log, logs } from "./logging";

interface BuildError {
type: "build-error";
type: "BuildError";
error: string;
}
interface AnalysisError {
type: "analysis-error";
type: "AnalysisError";
error: string;
}
export type FlowistryError = BuildError | AnalysisError;
interface FileNotFound {
type: "FileNotFound"
}
export type FlowistryError = BuildError | AnalysisError | FileNotFound;
interface FlowistryOutput<T> {
type: "output";
value: T;
Expand Down Expand Up @@ -134,18 +137,20 @@ ${log_text}`,
}
};

export const show_error = async (error: BuildError | AnalysisError) => {
if (error.type === "build-error") {
export const show_error = async (error: FlowistryError) => {
if (error.type === "BuildError") {
await globals.error_pane.show(error.error);
} else {
} else if (error.type == "AnalysisError") {
await show_error_dialog(error.error);
} else if (error.type == "FileNotFound") {
// should not reach here
}
};

export let hide_error = () => globals.error_pane.hide();

export async function last_error(context: vscode.ExtensionContext) {
let error = context.workspaceState.get("err_log") as string;
let flowistry_err: BuildError = { type: "build-error", error };
let flowistry_err: BuildError = { type: "BuildError", error };
await show_error(flowistry_err);
}
22 changes: 16 additions & 6 deletions ide/src/focus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ let valid_document = (doc: vscode.TextDocument): boolean => {
*/
export class FocusMode {
mode: StatusBarState = "idle";
state: Map<string, FocusDocumentState> = new Map();
state: Map<string, FocusDocumentState | "notfound"> = new Map();

doc_save_callback?: vscode.Disposable;
doc_edit_callback?: vscode.Disposable;
Expand Down Expand Up @@ -313,7 +313,7 @@ export class FocusMode {
private get_doc_state = async (
editor: vscode.TextEditor
): Promise<FocusDocumentState | null> => {
if (this.mode !== "active") {
if (this.mode !== "active" && this.mode !== "notfound") {
return null;
}

Expand All @@ -326,13 +326,23 @@ export class FocusMode {
if (is_ok(doc_state_res)) {
this.state.set(filename, doc_state_res.value);
} else {
await show_error(doc_state_res);
this.set_mode("error");
return null;
if (doc_state_res.type == "FileNotFound") {
this.state.set(filename, "notfound");
} else {
await show_error(doc_state_res);
this.set_mode("error");
return null;
}
}
}

return this.state.get(filename)!;
let state = this.state.get(filename)!;
if (state == "notfound") {
this.set_mode("notfound");
return null;
} else {
return state;
}
};

private handle_analysis_result = async <T>(result: FlowistryResult<T>) => {
Expand Down
19 changes: 5 additions & 14 deletions ide/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Readable } from "stream";
import * as vscode from "vscode";

import { download } from "./download";
import { FlowistryResult } from "./errors";
import { FlowistryError, FlowistryResult } from "./errors";
import { globals } from "./extension";
import { log } from "./logging";

Expand All @@ -17,14 +17,8 @@ declare const TOOLCHAIN: {
components: string[];
};

// serde-compatible types
interface Ok<T> {
Ok: T;
}
interface Err {
Err: string;
}
type Result<T> = Ok<T> | Err;
// serde-compatible type
type Result<T> = { Ok: T } | { Err: FlowistryError };

/* eslint no-undef: "off" */
const LIBRARY_PATHS: Partial<Record<NodeJS.Platform, string>> = {
Expand Down Expand Up @@ -224,7 +218,7 @@ export async function setup(
context.workspaceState.update("err_log", e);

return {
type: "build-error",
type: "BuildError",
error: e,
};
}
Expand All @@ -238,10 +232,7 @@ export async function setup(

let output_typed: Result<T> = JSON.parse(output);
if ("Err" in output_typed) {
return {
type: "analysis-error",
error: output_typed.Err,
};
return output_typed.Err;
} else {
return {
type: "output",
Expand Down
12 changes: 11 additions & 1 deletion ide/src/status_bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ export type StatusBarState =
| "unsaved"
| "idle"
| "error"
| "loading";
| "loading"
| "notfound";

interface StatusBarConfig {
foreground: string;
background: string;
icon?: string;
command: string;
tooltip?: string;
}

const config_for_state: Record<StatusBarState, StatusBarConfig> = {
Expand Down Expand Up @@ -44,6 +46,13 @@ const config_for_state: Record<StatusBarState, StatusBarConfig> = {
icon: "sync~spin",
command: "flowistry.focus",
},
notfound: {
foreground: "statusBarItem.foreground",
background: "statusBarItem.background",
icon: "question",
command: "flowistry.focus",
tooltip: "Flowistry could not get Cargo to find this file (this is probably a Flowistry bug)"
},
};

export class StatusBar {
Expand All @@ -68,5 +77,6 @@ export class StatusBar {
this.bar_item.backgroundColor = new vscode.ThemeColor(config.background);
this.bar_item.text = `$(${config.icon}) flowistry`;
this.bar_item.command = config.command;
this.bar_item.tooltip = config.tooltip;
}
}

0 comments on commit b972b71

Please sign in to comment.