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

Implement code completion #21323

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Parse --complete-at option
  • Loading branch information
sanxiyn committed Feb 10, 2015
commit c6414c5ec31814bbc472a8b5dfeb38711500fc5f
2 changes: 2 additions & 0 deletions src/librustc/session/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,8 @@ pub fn rustc_optgroups() -> Vec<RustcOptGroup> {
`everybody_loops` (all function bodies replaced with `loop {}`).",
"TYPE"),
opt::opt_u("", "show-span", "Show spans for compiler debugging", "expr|pat|ty"),
opt::opt_u("", "complete-at", "Give completions at the given position",
"FILENAME:BYTEPOS"),
]);
opts
}
Expand Down
3 changes: 3 additions & 0 deletions src/librustc/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ impl Session {
pub fn codemap<'a>(&'a self) -> &'a codemap::CodeMap {
&self.parse_sess.span_diagnostic.cm
}
pub fn set_complete_at(&self, complete_at: Option<(String, u32)>) {
*self.parse_sess.complete_at.borrow_mut() = complete_at;
}
// This exists to help with refactoring to eliminate impossible
// cases later on
pub fn impossible_case(&self, sp: Span, msg: &str) -> ! {
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ pub fn phase_1_parse_input(sess: &Session, cfg: ast::CrateConfig, input: &Input)
}
});

sess.set_complete_at(None);

if sess.opts.debugging_opts.ast_json_noexpand {
println!("{}", json::as_json(&krate));
}
Expand Down
13 changes: 13 additions & 0 deletions src/librustc_driver/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,22 @@ pub fn run_compiler<'a>(args: &[String],
};

let mut sess = build_session(sopts, input_file_path, descriptions);

if sess.unstable_options() {
sess.opts.show_span = matches.opt_str("show-span");

let complete_at = matches.opt_str("complete-at").and_then(|s| {
let parts: Vec<&str> = s.splitn(1, ':').collect();
if let [filename, bytepos] = &*parts {
if let Ok(bytepos) = bytepos.parse() {
return Some((filename.to_string(), bytepos));
}
}
None
});
sess.set_complete_at(complete_at);
}

let cfg = config::build_configuration(&sess);

do_or_return!(callbacks.late_callback(&matches, &sess, &input, &odir, &ofile));
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/save/span_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl<'a> SpanUtils<'a> {
let filemap = self.sess.codemap().new_filemap(String::from_str("<anon-dxr>"),
self.snippet(span));
let s = self.sess;
lexer::StringReader::new(s.diagnostic(), filemap)
lexer::StringReader::new(s.diagnostic(), filemap, None)
}

// Re-parses a path and returns the span for the last identifier in the path
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn highlight(src: &str, class: Option<&str>, id: Option<&str>) -> String {

let mut out = Vec::new();
doit(&sess,
lexer::StringReader::new(&sess.span_diagnostic, fm),
lexer::StringReader::new(&sess.span_diagnostic, fm, None),
class,
id,
&mut out).unwrap();
Expand Down
6 changes: 5 additions & 1 deletion src/libsyntax/parse/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub struct StringReader<'a> {
/// The last character to be read
pub curr: Option<char>,
pub filemap: Rc<codemap::FileMap>,
pub complete_at: Option<BytePos>,
/* cached: */
pub peek_tok: token::Token,
pub peek_span: Span,
Expand Down Expand Up @@ -150,6 +151,7 @@ impl<'a> StringReader<'a> {
col: CharPos(0),
curr: Some('\n'),
filemap: filemap,
complete_at: None,
/* dummy values; not read */
peek_tok: token::Eof,
peek_span: codemap::DUMMY_SP,
Expand All @@ -160,8 +162,10 @@ impl<'a> StringReader<'a> {
}

pub fn new<'b>(span_diagnostic: &'b SpanHandler,
filemap: Rc<codemap::FileMap>) -> StringReader<'b> {
filemap: Rc<codemap::FileMap>,
complete_at: Option<BytePos>) -> StringReader<'b> {
let mut sr = StringReader::new_raw(span_diagnostic, filemap);
sr.complete_at = complete_at;
sr.advance_token();
sr
}
Expand Down
14 changes: 12 additions & 2 deletions src/libsyntax/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//! The main parser interface

use ast;
use codemap::{Span, CodeMap, FileMap};
use codemap::{Span, CodeMap, FileMap, BytePos};
use diagnostic::{SpanHandler, mk_span_handler, default_handler, Auto};
use parse::attr::ParserAttr;
use parse::parser::Parser;
Expand All @@ -38,6 +38,7 @@ pub mod obsolete;
/// Info about a parsing session.
pub struct ParseSess {
pub span_diagnostic: SpanHandler, // better be the same as the one in the reader!
pub complete_at: RefCell<Option<(String, u32)>>,
/// Used to determine and report recursive mod inclusions
included_mod_stack: RefCell<Vec<Path>>,
pub node_id: Cell<ast::NodeId>,
Expand All @@ -46,6 +47,7 @@ pub struct ParseSess {
pub fn new_parse_sess() -> ParseSess {
ParseSess {
span_diagnostic: mk_span_handler(default_handler(Auto, None, true), CodeMap::new()),
complete_at: RefCell::new(None),
included_mod_stack: RefCell::new(Vec::new()),
node_id: Cell::new(1),
}
Expand All @@ -54,6 +56,7 @@ pub fn new_parse_sess() -> ParseSess {
pub fn new_parse_sess_special_handler(sh: SpanHandler) -> ParseSess {
ParseSess {
span_diagnostic: sh,
complete_at: RefCell::new(None),
included_mod_stack: RefCell::new(Vec::new()),
node_id: Cell::new(1),
}
Expand Down Expand Up @@ -285,7 +288,14 @@ pub fn filemap_to_tts(sess: &ParseSess, filemap: Rc<FileMap>)
// it appears to me that the cfg doesn't matter here... indeed,
// parsing tt's probably shouldn't require a parser at all.
let cfg = Vec::new();
let srdr = lexer::StringReader::new(&sess.span_diagnostic, filemap);
let complete_at = match *sess.complete_at.borrow() {
Some((ref filename, bytepos)) if filemap.name == *filename => {
Some(filemap.start_pos + BytePos(bytepos))
}
_ => None
};
let srdr = lexer::StringReader::new(&sess.span_diagnostic, filemap,
complete_at);
let mut p1 = Parser::new(sess, cfg, box srdr);
p1.parse_all_token_trees()
}
Expand Down