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
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
+243 −82
Diff settings

Always

Just for now

Copy path View file

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -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" }
Copy path View file
@@ -30,6 +30,7 @@
#![feature(rustc_private)]
#![feature(set_stdio)]
#![feature(staged_api)]
#![feature(rustc_highlight)]

extern crate arena;
extern crate flate;
@@ -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]
@@ -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;
@@ -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) {
@@ -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));
@@ -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.
Oops, something went wrong.
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.