mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +00:00
Proper span representation with syntax context
This commit is contained in:
parent
890eb17b4e
commit
e36b3f7b8c
16 changed files with 414 additions and 470 deletions
|
@ -2,20 +2,30 @@ use arbitrary::{Arbitrary, Unstructured};
|
|||
use expect_test::{expect, Expect};
|
||||
use mbe::syntax_node_to_token_tree;
|
||||
use syntax::{ast, AstNode};
|
||||
use tt::Span;
|
||||
use tt::{SpanAnchor, SyntaxContext};
|
||||
|
||||
use crate::{CfgAtom, CfgExpr, CfgOptions, DnfExpr};
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
struct DummyFile;
|
||||
impl Span for DummyFile {
|
||||
impl SpanAnchor for DummyFile {
|
||||
const DUMMY: Self = DummyFile;
|
||||
}
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
struct DummyCtx;
|
||||
impl SyntaxContext for DummyCtx {
|
||||
const DUMMY: Self = DummyCtx;
|
||||
}
|
||||
|
||||
fn assert_parse_result(input: &str, expected: CfgExpr) {
|
||||
let source_file = ast::SourceFile::parse(input).ok().unwrap();
|
||||
let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
|
||||
let tt = syntax_node_to_token_tree(tt.syntax(), DummyFile, 0.into(), &Default::default());
|
||||
let tt = syntax_node_to_token_tree::<_, DummyCtx>(
|
||||
tt.syntax(),
|
||||
DummyFile,
|
||||
0.into(),
|
||||
&Default::default(),
|
||||
);
|
||||
let cfg = CfgExpr::parse(&tt);
|
||||
assert_eq!(cfg, expected);
|
||||
}
|
||||
|
@ -23,7 +33,12 @@ fn assert_parse_result(input: &str, expected: CfgExpr) {
|
|||
fn check_dnf(input: &str, expect: Expect) {
|
||||
let source_file = ast::SourceFile::parse(input).ok().unwrap();
|
||||
let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
|
||||
let tt = syntax_node_to_token_tree(tt.syntax(), DummyFile, 0.into(), &Default::default());
|
||||
let tt = syntax_node_to_token_tree::<_, DummyCtx>(
|
||||
tt.syntax(),
|
||||
DummyFile,
|
||||
0.into(),
|
||||
&Default::default(),
|
||||
);
|
||||
let cfg = CfgExpr::parse(&tt);
|
||||
let actual = format!("#![cfg({})]", DnfExpr::new(cfg));
|
||||
expect.assert_eq(&actual);
|
||||
|
@ -32,7 +47,12 @@ fn check_dnf(input: &str, expect: Expect) {
|
|||
fn check_why_inactive(input: &str, opts: &CfgOptions, expect: Expect) {
|
||||
let source_file = ast::SourceFile::parse(input).ok().unwrap();
|
||||
let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
|
||||
let tt = syntax_node_to_token_tree(tt.syntax(), DummyFile, 0.into(), &Default::default());
|
||||
let tt = syntax_node_to_token_tree::<_, DummyCtx>(
|
||||
tt.syntax(),
|
||||
DummyFile,
|
||||
0.into(),
|
||||
&Default::default(),
|
||||
);
|
||||
let cfg = CfgExpr::parse(&tt);
|
||||
let dnf = DnfExpr::new(cfg);
|
||||
let why_inactive = dnf.why_inactive(opts).unwrap().to_string();
|
||||
|
@ -43,7 +63,12 @@ fn check_why_inactive(input: &str, opts: &CfgOptions, expect: Expect) {
|
|||
fn check_enable_hints(input: &str, opts: &CfgOptions, expected_hints: &[&str]) {
|
||||
let source_file = ast::SourceFile::parse(input).ok().unwrap();
|
||||
let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
|
||||
let tt = syntax_node_to_token_tree(tt.syntax(), DummyFile, 0.into(), &Default::default());
|
||||
let tt = syntax_node_to_token_tree::<_, DummyCtx>(
|
||||
tt.syntax(),
|
||||
DummyFile,
|
||||
0.into(),
|
||||
&Default::default(),
|
||||
);
|
||||
let cfg = CfgExpr::parse(&tt);
|
||||
let dnf = DnfExpr::new(cfg);
|
||||
let hints = dnf.compute_enable_hints(opts).map(|diff| diff.to_string()).collect::<Vec<_>>();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue