Skip to content

Commit 10eaec0

Browse files
committed
First draft of integrating taplo. Make API reach base_db through syntax crate
1 parent 58de0b1 commit 10eaec0

File tree

6 files changed

+210
-3
lines changed

6 files changed

+210
-3
lines changed

Cargo.lock

+165-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/base-db/src/lib.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ pub mod fixture;
99
use std::panic;
1010

1111
use rustc_hash::FxHashSet;
12-
use syntax::{ast, Parse, SourceFile, TextRange, TextSize};
12+
use syntax::{
13+
ast::{self, TomlFile},
14+
Parse, SourceFile, TextRange, TextSize,
15+
};
1316
use triomphe::Arc;
1417

1518
pub use crate::{
@@ -78,6 +81,15 @@ pub trait SourceDatabase: FileLoader + std::fmt::Debug {
7881
/// The proc macros.
7982
#[salsa::input]
8083
fn proc_macros(&self) -> Arc<ProcMacros>;
84+
85+
#[salsa::invoke(parse_toml_query)]
86+
fn parse_toml(&self, file_id: FileId) -> Parse<ast::TomlFile>;
87+
}
88+
89+
fn parse_toml_query(db: &dyn SourceDatabase, file_id: FileId) -> Parse<ast::TomlFile> {
90+
let _p = profile::span("parse_toml_query").detail(|| format!("{file_id:?}"));
91+
let text = db.file_text(file_id);
92+
TomlFile::parse(&text)
8193
}
8294

8395
fn parse_query(db: &dyn SourceDatabase, file_id: FileId) -> Parse<ast::SourceFile> {

crates/syntax/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ itertools.workspace = true
1919
rowan = "0.15.11"
2020
rustc-hash = "1.1.0"
2121
once_cell = "1.17.0"
22+
taplo = "0.12.1"
2223
indexmap.workspace = true
2324
smol_str.workspace = true
2425
triomphe.workspace = true

crates/syntax/src/ast.rs

+5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ pub use self::{
3636
},
3737
};
3838

39+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
40+
pub struct TomlFile {
41+
pub(crate) syntax: taplo::syntax::SyntaxNode,
42+
}
43+
3944
/// The main trait to go from untyped `SyntaxNode` to a typed ast. The
4045
/// conversion itself has zero runtime cost: ast and syntax nodes have exactly
4146
/// the same representation: a pointer to the tree root and a pointer to the

crates/syntax/src/lib.rs

+20
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ pub mod hacks;
4646

4747
use std::marker::PhantomData;
4848

49+
pub use taplo;
50+
4951
use stdx::format_to;
52+
use taplo::parser::Parse as TomlParser;
5053
use text_edit::Indel;
5154
use triomphe::Arc;
5255

@@ -465,3 +468,20 @@ fn api_walkthrough() {
465468
}
466469
assert_eq!(exprs_cast, exprs_visit);
467470
}
471+
472+
pub use crate::ast::TomlFile;
473+
474+
impl TomlFile {
475+
pub fn parse(text: &str) -> Parse<TomlFile> {
476+
let TomlParser { green_node, errors } = taplo::parser::parse(text);
477+
let root = taplo::syntax::SyntaxNode::new_root(green_node.clone());
478+
479+
// FIXME : Express this in taplo terms.
480+
// parsed.errors.extend(validation::validate(&root));
481+
482+
assert_eq!(root.kind(), taplo::syntax::SyntaxKind::ROOT);
483+
let errs: Vec<SyntaxError> = errors.into_iter().map(SyntaxError::from).collect();
484+
485+
Parse { green: green_node, errors: errs.into(), _ty: PhantomData }
486+
}
487+
}

crates/syntax/src/syntax_error.rs

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ use crate::{TextRange, TextSize};
99
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1010
pub struct SyntaxError(String, TextRange);
1111

12+
impl From<taplo::parser::Error> for SyntaxError {
13+
fn from(value: taplo::parser::Error) -> Self {
14+
Self { 0: value.message, 1: value.range }
15+
}
16+
}
17+
1218
// FIXME: there was an unused SyntaxErrorKind previously (before this enum was removed)
1319
// It was introduced in this PR: https://github.com/rust-lang/rust-analyzer/pull/846/files#diff-827da9b03b8f9faa1bade5cdd44d5dafR95
1420
// but it was not removed by a mistake.

0 commit comments

Comments
 (0)