Skip to content

Commit c6414c5

Browse files
committed
Parse --complete-at option
1 parent 94c06a1 commit c6414c5

File tree

8 files changed

+39
-5
lines changed

8 files changed

+39
-5
lines changed

src/librustc/session/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,8 @@ pub fn rustc_optgroups() -> Vec<RustcOptGroup> {
807807
`everybody_loops` (all function bodies replaced with `loop {}`).",
808808
"TYPE"),
809809
opt::opt_u("", "show-span", "Show spans for compiler debugging", "expr|pat|ty"),
810+
opt::opt_u("", "complete-at", "Give completions at the given position",
811+
"FILENAME:BYTEPOS"),
810812
]);
811813
opts
812814
}

src/librustc/session/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ impl Session {
182182
pub fn codemap<'a>(&'a self) -> &'a codemap::CodeMap {
183183
&self.parse_sess.span_diagnostic.cm
184184
}
185+
pub fn set_complete_at(&self, complete_at: Option<(String, u32)>) {
186+
*self.parse_sess.complete_at.borrow_mut() = complete_at;
187+
}
185188
// This exists to help with refactoring to eliminate impossible
186189
// cases later on
187190
pub fn impossible_case(&self, sp: Span, msg: &str) -> ! {

src/librustc_driver/driver.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,8 @@ pub fn phase_1_parse_input(sess: &Session, cfg: ast::CrateConfig, input: &Input)
346346
}
347347
});
348348

349+
sess.set_complete_at(None);
350+
349351
if sess.opts.debugging_opts.ast_json_noexpand {
350352
println!("{}", json::as_json(&krate));
351353
}

src/librustc_driver/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,22 @@ pub fn run_compiler<'a>(args: &[String],
134134
};
135135

136136
let mut sess = build_session(sopts, input_file_path, descriptions);
137+
137138
if sess.unstable_options() {
138139
sess.opts.show_span = matches.opt_str("show-span");
140+
141+
let complete_at = matches.opt_str("complete-at").and_then(|s| {
142+
let parts: Vec<&str> = s.splitn(1, ':').collect();
143+
if let [filename, bytepos] = &*parts {
144+
if let Ok(bytepos) = bytepos.parse() {
145+
return Some((filename.to_string(), bytepos));
146+
}
147+
}
148+
None
149+
});
150+
sess.set_complete_at(complete_at);
139151
}
152+
140153
let cfg = config::build_configuration(&sess);
141154

142155
do_or_return!(callbacks.late_callback(&matches, &sess, &input, &odir, &ofile));

src/librustc_trans/save/span_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl<'a> SpanUtils<'a> {
8686
let filemap = self.sess.codemap().new_filemap(String::from_str("<anon-dxr>"),
8787
self.snippet(span));
8888
let s = self.sess;
89-
lexer::StringReader::new(s.diagnostic(), filemap)
89+
lexer::StringReader::new(s.diagnostic(), filemap, None)
9090
}
9191

9292
// Re-parses a path and returns the span for the last identifier in the path

src/librustdoc/html/highlight.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub fn highlight(src: &str, class: Option<&str>, id: Option<&str>) -> String {
3030

3131
let mut out = Vec::new();
3232
doit(&sess,
33-
lexer::StringReader::new(&sess.span_diagnostic, fm),
33+
lexer::StringReader::new(&sess.span_diagnostic, fm, None),
3434
class,
3535
id,
3636
&mut out).unwrap();

src/libsyntax/parse/lexer/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ pub struct StringReader<'a> {
6969
/// The last character to be read
7070
pub curr: Option<char>,
7171
pub filemap: Rc<codemap::FileMap>,
72+
pub complete_at: Option<BytePos>,
7273
/* cached: */
7374
pub peek_tok: token::Token,
7475
pub peek_span: Span,
@@ -150,6 +151,7 @@ impl<'a> StringReader<'a> {
150151
col: CharPos(0),
151152
curr: Some('\n'),
152153
filemap: filemap,
154+
complete_at: None,
153155
/* dummy values; not read */
154156
peek_tok: token::Eof,
155157
peek_span: codemap::DUMMY_SP,
@@ -160,8 +162,10 @@ impl<'a> StringReader<'a> {
160162
}
161163

162164
pub fn new<'b>(span_diagnostic: &'b SpanHandler,
163-
filemap: Rc<codemap::FileMap>) -> StringReader<'b> {
165+
filemap: Rc<codemap::FileMap>,
166+
complete_at: Option<BytePos>) -> StringReader<'b> {
164167
let mut sr = StringReader::new_raw(span_diagnostic, filemap);
168+
sr.complete_at = complete_at;
165169
sr.advance_token();
166170
sr
167171
}

src/libsyntax/parse/mod.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//! The main parser interface
1212
1313
use ast;
14-
use codemap::{Span, CodeMap, FileMap};
14+
use codemap::{Span, CodeMap, FileMap, BytePos};
1515
use diagnostic::{SpanHandler, mk_span_handler, default_handler, Auto};
1616
use parse::attr::ParserAttr;
1717
use parse::parser::Parser;
@@ -38,6 +38,7 @@ pub mod obsolete;
3838
/// Info about a parsing session.
3939
pub struct ParseSess {
4040
pub span_diagnostic: SpanHandler, // better be the same as the one in the reader!
41+
pub complete_at: RefCell<Option<(String, u32)>>,
4142
/// Used to determine and report recursive mod inclusions
4243
included_mod_stack: RefCell<Vec<Path>>,
4344
pub node_id: Cell<ast::NodeId>,
@@ -46,6 +47,7 @@ pub struct ParseSess {
4647
pub fn new_parse_sess() -> ParseSess {
4748
ParseSess {
4849
span_diagnostic: mk_span_handler(default_handler(Auto, None, true), CodeMap::new()),
50+
complete_at: RefCell::new(None),
4951
included_mod_stack: RefCell::new(Vec::new()),
5052
node_id: Cell::new(1),
5153
}
@@ -54,6 +56,7 @@ pub fn new_parse_sess() -> ParseSess {
5456
pub fn new_parse_sess_special_handler(sh: SpanHandler) -> ParseSess {
5557
ParseSess {
5658
span_diagnostic: sh,
59+
complete_at: RefCell::new(None),
5760
included_mod_stack: RefCell::new(Vec::new()),
5861
node_id: Cell::new(1),
5962
}
@@ -285,7 +288,14 @@ pub fn filemap_to_tts(sess: &ParseSess, filemap: Rc<FileMap>)
285288
// it appears to me that the cfg doesn't matter here... indeed,
286289
// parsing tt's probably shouldn't require a parser at all.
287290
let cfg = Vec::new();
288-
let srdr = lexer::StringReader::new(&sess.span_diagnostic, filemap);
291+
let complete_at = match *sess.complete_at.borrow() {
292+
Some((ref filename, bytepos)) if filemap.name == *filename => {
293+
Some(filemap.start_pos + BytePos(bytepos))
294+
}
295+
_ => None
296+
};
297+
let srdr = lexer::StringReader::new(&sess.span_diagnostic, filemap,
298+
complete_at);
289299
let mut p1 = Parser::new(sess, cfg, box srdr);
290300
p1.parse_all_token_trees()
291301
}

0 commit comments

Comments
 (0)