Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Highlight code in rustc --explain (WIP) #39300

Closed
wants to merge 3 commits into from
Closed
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
11 changes: 11 additions & 0 deletions src/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/librustc_driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ rustc_resolve = { path = "../librustc_resolve" }
rustc_save_analysis = { path = "../librustc_save_analysis" }
rustc_trans = { path = "../librustc_trans" }
rustc_typeck = { path = "../librustc_typeck" }
rustc_highlight = { path = "../librustc_highlight" }
serialize = { path = "../libserialize" }
syntax = { path = "../libsyntax" }
syntax_ext = { path = "../libsyntax_ext" }
Expand Down
49 changes: 41 additions & 8 deletions src/librustc_driver/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#![feature(rustc_private)]
#![feature(set_stdio)]
#![feature(staged_api)]
#![feature(rustc_highlight)]

extern crate arena;
extern crate flate;
Expand All @@ -53,6 +54,7 @@ extern crate rustc_resolve;
extern crate rustc_save_analysis;
extern crate rustc_trans;
extern crate rustc_typeck;
extern crate rustc_highlight;
extern crate serialize;
extern crate rustc_llvm as llvm;
#[macro_use]
Expand All @@ -78,6 +80,7 @@ use rustc::lint::Lint;
use rustc::lint;
use rustc_metadata::locator;
use rustc_metadata::cstore::CStore;
use rustc_highlight::highlight::render_to_stdout_with_highlighting;
use rustc::util::common::time;

use serialize::json::ToJson;
Expand Down Expand Up @@ -349,6 +352,43 @@ pub trait CompilerCalls<'a> {
#[derive(Copy, Clone)]
pub struct RustcDefaultCalls;

fn print_msg(text: &str) {
let mut code = String::new();
let mut in_code = false;
let mut code_is_rust = false;
for (i, line) in text.split('\n').enumerate() {
// Slice off the leading newline and print.
if i == 0 && line.len() == 0 {
continue;
}
if line.starts_with("```") {
if in_code {
if code_is_rust {
println!("rust");
render_to_stdout_with_highlighting(code);
} else {
render_to_stdout_with_highlighting(code);
}
code = String::new();
code_is_rust = false;
} else {
if line.starts_with("```rust") {
code_is_rust = true;
}
}
in_code = !in_code;
println!("```");
} else {
if in_code {
code.push_str(&line);
code.push_str("\n");
} else {
println!("{}", line);
}
}
}
}

fn handle_explain(code: &str,
descriptions: &errors::registry::Registry,
output: ErrorOutputType) {
Expand All @@ -359,14 +399,7 @@ fn handle_explain(code: &str,
};
match descriptions.find_description(&normalised) {
Some(ref description) => {
// Slice off the leading newline and print.
print!("{}", &(&description[1..]).split("\n").map(|x| {
format!("{}\n", if x.starts_with("```") {
"```"
} else {
x
})
}).collect::<String>());
print_msg(description);
}
None => {
early_error(output, &format!("no extended information for {}", code));
Expand Down
14 changes: 14 additions & 0 deletions src/librustc_highlight/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
authors = ["The Rust Project Developers"]
name = "rustc_highlight"
version = "0.0.0"

[lib]
name = "rustc_highlight"
path = "lib.rs"
crate-type = ["dylib"]

[dependencies]
syntax = { path = "../libsyntax" }
syntax_pos = { path = "../libsyntax_pos" }
log = { path = "../liblog" }
File renamed without changes.
Loading