Skip to content

Commit

Permalink
First draft of integrating taplo. Make API reach base_db through synt…
Browse files Browse the repository at this point in the history
…ax crate
  • Loading branch information
alibektas committed Nov 16, 2023
1 parent 57ef70c commit e1f8e3f
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 3 deletions.
167 changes: 165 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion crates/base-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ pub mod fixture;
use std::panic;

use rustc_hash::FxHashSet;
use syntax::{ast, Parse, SourceFile, TextRange, TextSize};
use syntax::{
ast::{self, TomlFile},
Parse, SourceFile, TextRange, TextSize,
};
use triomphe::Arc;

pub use crate::{
Expand Down Expand Up @@ -78,6 +81,15 @@ pub trait SourceDatabase: FileLoader + std::fmt::Debug {
/// The proc macros.
#[salsa::input]
fn proc_macros(&self) -> Arc<ProcMacros>;

#[salsa::invoke(parse_toml_query)]
fn parse_toml(&self, file_id: FileId) -> Parse<ast::TomlFile>;
}

fn parse_toml_query(db: &dyn SourceDatabase, file_id: FileId) -> Parse<ast::TomlFile> {
let _p = profile::span("parse_toml_query").detail(|| format!("{file_id:?}"));
let text = db.file_text(file_id);
TomlFile::parse(&text)
}

fn parse_query(db: &dyn SourceDatabase, file_id: FileId) -> Parse<ast::SourceFile> {
Expand Down
1 change: 1 addition & 0 deletions crates/syntax/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ rowan = "0.15.11"
rustc-hash = "1.1.0"
once_cell = "1.17.0"
indexmap = "2.0.0"
taplo = "0.12.1"
smol_str.workspace = true
triomphe.workspace = true

Expand Down
5 changes: 5 additions & 0 deletions crates/syntax/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ pub use self::{
},
};

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TomlFile {
pub(crate) syntax: taplo::syntax::SyntaxNode,
}

/// The main trait to go from untyped `SyntaxNode` to a typed ast. The
/// conversion itself has zero runtime cost: ast and syntax nodes have exactly
/// the same representation: a pointer to the tree root and a pointer to the
Expand Down
20 changes: 20 additions & 0 deletions crates/syntax/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ pub mod hacks;

use std::marker::PhantomData;

pub use taplo;

use stdx::format_to;
use taplo::parser::Parse as TomlParser;
use text_edit::Indel;
use triomphe::Arc;

Expand Down Expand Up @@ -465,3 +468,20 @@ fn api_walkthrough() {
}
assert_eq!(exprs_cast, exprs_visit);
}

pub use crate::ast::TomlFile;

impl TomlFile {
pub fn parse(text: &str) -> Parse<TomlFile> {
let TomlParser { green_node, errors } = taplo::parser::parse(text);
let root = taplo::syntax::SyntaxNode::new_root(green_node.clone());

// FIXME : Express this in taplo terms.
// parsed.errors.extend(validation::validate(&root));

assert_eq!(root.kind(), taplo::syntax::SyntaxKind::ROOT);
let errs: Vec<SyntaxError> = errors.into_iter().map(SyntaxError::from).collect();

Parse { green: green_node, errors: errs.into(), _ty: PhantomData }
}
}
6 changes: 6 additions & 0 deletions crates/syntax/src/syntax_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ use crate::{TextRange, TextSize};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct SyntaxError(String, TextRange);

impl From<taplo::parser::Error> for SyntaxError {
fn from(value: taplo::parser::Error) -> Self {
Self { 0: value.message, 1: value.range }
}
}

// FIXME: there was an unused SyntaxErrorKind previously (before this enum was removed)
// It was introduced in this PR: https://github.com/rust-lang/rust-analyzer/pull/846/files#diff-827da9b03b8f9faa1bade5cdd44d5dafR95
// but it was not removed by a mistake.
Expand Down

0 comments on commit e1f8e3f

Please sign in to comment.