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

注释优化 #298

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
41 changes: 36 additions & 5 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ parking_lot = "0.12"
derivative = "2.2"
console = "0.15"
anstyle = "1.0"
lasso = {version = "0.7", features = ["multi-threaded"]}

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = "0.2"
Expand Down
2 changes: 1 addition & 1 deletion src/ast/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,7 @@ impl<'a, 'ctx> Ctx<'a> {
if let Some(docs) = docs {
for doc in docs {
if let NodeEnum::Comment(c) = *doc {
string.push_str(&c.comment);
string.push_str(&c.get_comment());
string.push('\n');
}
}
Expand Down
62 changes: 52 additions & 10 deletions src/ast/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ impl FmtBuilder {
pub fn parse_struct_def_node(&mut self, node: &StructDefNode) {
for c in node.pre_comments.iter() {
c.format(self);
self.enter();
}
self.prefix();
if let Some((modi, _)) = node.modifier {
Expand Down Expand Up @@ -263,6 +264,11 @@ impl FmtBuilder {
}
pub fn parse_array_init_node(&mut self, node: &ArrayInitNode) {
self.l_bracket();
for com in &node.comments[0] {
com.format(self);
self.enter();
self.prefix();
}
for (i, exp) in node.exps.iter().enumerate() {
exp.format(self);
if i != node.exps.len() - 1 {
Expand All @@ -274,7 +280,6 @@ impl FmtBuilder {
}
pub fn parse_generic_param_node(&mut self, node: &GenericParamNode) {
self.l_angle_bracket();

for (i, generic) in node.generics.iter().enumerate() {
match generic {
Some(n) => n.format(self),
Expand All @@ -289,6 +294,7 @@ impl FmtBuilder {
pub fn parse_def_node(&mut self, node: &DefNode) {
self.token("let");
self.space();
self.parse_comments_with_tab(&node.comments[0]);
node.var.format(self);
if let Some(tp) = &node.tp {
self.colon();
Expand Down Expand Up @@ -320,37 +326,65 @@ impl FmtBuilder {
self.prefix();
statement.format(self);
match &**statement {
NodeEnum::For(_) | NodeEnum::While(_) | NodeEnum::If(_) | NodeEnum::Comment(_) => {}
NodeEnum::For(_)
| NodeEnum::While(_)
| NodeEnum::If(_)
| NodeEnum::Comment(_)
| NodeEnum::Ret(_) => {}
_ => {
self.semicolon();
}
}
match &**statement {
NodeEnum::Comment(_) => {}
_ => {
self.enter();
}
}
self.enter();
}
}
fn parse_comments_with_tab(&mut self, comments: &Vec<Box<NodeEnum>>) {
for com in comments {
com.format(self);
self.enter();
self.add_tab();
self.prefix();
self.sub_tab();
}
}
pub fn parse_ret_node(&mut self, node: &RetNode) {
if let Some(value) = &node.value {
self.token("return");
self.space();
self.parse_comments_with_tab(&node.comments[0]);
value.format(self);
} else {
self.token("return");
self.parse_comments_with_tab(&node.comments[0]);
}
self.semicolon();
for com in &node.comments[1] {
com.format(self);
}
}
pub fn parse_primary_node(&mut self, node: &PrimaryNode) {
for com in &node.comments[0] {
com.format(self);
self.enter();
self.prefix();
}
node.value.format(self);
for com in &node.comments[1] {
self.prefix();
com.format(self);
self.enter();
}
}

pub fn parse_array_element_node(&mut self, node: &ArrayElementNode) {
// arr[1] // 123
// // 456
// .foo();
node.arr.format(self);
self.l_bracket();
node.index.format(self);
self.r_bracket();
self.parse_comments_with_tab(&node.comments[0]);
}
pub fn parse_parantheses_node(&mut self, node: &ParanthesesNode) {
self.l_paren();
Expand All @@ -377,10 +411,14 @@ impl FmtBuilder {
node.right.format(self);
}
pub fn parse_take_op_node(&mut self, node: &TakeOpNode) {
// head.id //123
// //456
// .foo();
node.head.format(self);
for id in &node.field {
self.dot();
id.format(self);
self.parse_comments_with_tab(&node.comments[0]);
}
}
pub fn parse_impl_node(&mut self, node: &ImplNode) {
Expand Down Expand Up @@ -422,6 +460,9 @@ impl FmtBuilder {
self.enter();
}
pub fn parse_func_call_node(&mut self, node: &FuncCallNode) {
// foo(1, 2, 3) // 456
// // 123
// .bar();
node.callee.format(self);
if let Some(generic_params) = &node.generic_params {
generic_params.format(self);
Expand All @@ -439,13 +480,15 @@ impl FmtBuilder {
}
}
self.r_paren();
self.parse_comments_with_tab(&node.comments[0]);
}
pub fn parse_func_def_node(&mut self, node: &FuncDefNode) {
let paralist = &node.paralist;
let params_print = print_params(paralist);
for c in node.pre_comments.iter() {
self.prefix();
c.format(self);
self.enter();
}
self.prefix();
if let Some((modi, _)) = node.modifier {
Expand Down Expand Up @@ -582,8 +625,7 @@ impl FmtBuilder {
} else {
self.token("//");
}
self.token(&node.comment);
self.enter();
self.token(&node.get_comment());
}
pub fn parse_continue_node(&mut self, _node: &ContinueNode) {
self.token("continue");
Expand Down
13 changes: 11 additions & 2 deletions src/ast/node/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,27 @@ use super::*;
use crate::ast::ctx::Ctx;
use internal_macro::node;
use lsp_types::SemanticTokenType;
lazy_static::lazy_static! {
pub static ref RODEO: lasso::ThreadedRodeo = lasso::ThreadedRodeo::default();
}

#[node]
pub struct CommentNode {
pub comment: String,
pub comment_key: lasso::Spur,
pub is_doc: bool, // use "///" (is_doc:true)
}

impl CommentNode {
pub fn get_comment(&self) -> String {
RODEO.resolve(&self.comment_key).to_string()
}
}

impl PrintTrait for CommentNode {
fn print(&self, tabs: usize, end: bool, mut line: Vec<bool>) {
deal_line(tabs, &mut line, end);
tab(tabs, line.clone(), end);
println!("CommentNode: {}", self.comment);
println!("CommentNode: {}", self.get_comment());
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/ast/node/ret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ impl Node for RetNode {
builder: &'b BuilderEnum<'a, 'ctx>,
) -> NodeResult {
let ret_pltype = ctx.rettp.as_ref().unwrap().clone();
ctx.emit_comment_highlight(&self.comments[0]);
if let Some(ret_node) = &mut self.value {
// let (value, value_pltype, _) = ctx.emit_with_expectation(ret_node, Some(ret_pltype.clone()), ret_pltype.borrow().get_range().unwrap_or_default(), builder)?;
let v = ret_node.emit(ctx, builder)?.get_value().unwrap();
ctx.emit_comment_highlight(&self.comments[0]);
ctx.emit_comment_highlight(&self.comments[1]);
let value_pltype = v.get_ty();
let mut value = ctx.try_load2var(self.range, v.get_value(), builder)?;
let eqres = ctx.eq(ret_pltype.clone(), value_pltype.clone());
Expand Down
1 change: 1 addition & 0 deletions src/ast/node/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl Node for DefNode {
ctx: &'b mut Ctx<'a>,
builder: &'b BuilderEnum<'a, 'ctx>,
) -> NodeResult {
ctx.emit_comment_highlight(&self.comments[0]);
let range = self.range();
ctx.push_semantic_token(self.var.range, SemanticTokenType::VARIABLE, 0);
if self.exp.is_none() && self.tp.is_none() {
Expand Down
3 changes: 2 additions & 1 deletion src/ast/node/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ impl Node for StructInitNode {
}
}

#[node]
#[node(comment)]
pub struct ArrayInitNode {
// pub tp: Box<TypeNameNode>,
pub exps: Vec<Box<NodeEnum>>,
Expand All @@ -737,6 +737,7 @@ impl Node for ArrayInitNode {
ctx: &'b mut Ctx<'a>,
builder: &'b BuilderEnum<'a, 'ctx>,
) -> NodeResult {
ctx.emit_comment_highlight(&self.comments[0]);
let mut exps = Vec::new();
let mut tp0 = None;

Expand Down
20 changes: 18 additions & 2 deletions src/nomparser/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,31 @@ pub fn array_init(input: Span) -> IResult<Span, Box<NodeEnum>> {
map_res(
tuple((
tag_token_symbol(TokenType::LBRACKET),
many0(del_newline_or_space!(comment)),
separated_list0(
tag_token_symbol(TokenType::COMMA),
tuple((
many0(del_newline_or_space!(comment)),
del_newline_or_space!(general_exp),
many0(del_newline_or_space!(comment))
)),
),
tag_token_symbol(TokenType::RBRACKET),
)),
|((_, lb), exps, (_, rb))| {
|((_, lb), coms0, exps, (_, rb))| {
let range = lb.start.to(rb.end);
res_enum(ArrayInitNode { exps, range }.into())
let mut coms = vec![coms0];
let mut exs = vec![];
for (_, (comsl, exp, comsr)) in exps.iter().enumerate() {
coms.push(comsl.clone());
exs.push(exp.clone());
coms.push(comsr.clone());
}
res_enum(ArrayInitNode {
exps:exs,
range,
comments: coms,
}.into())
},
)(input)
}
Expand Down
Loading