mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-28 14:24:45 +00:00
expr working, debugging def
This commit is contained in:
parent
bc50280c0e
commit
c6066cc629
8 changed files with 306 additions and 277 deletions
|
@ -19,7 +19,7 @@ pub fn new_comma_mn(expr_id: ExprId, parent_id_opt: Option<MarkNodeId>) -> Marku
|
||||||
MarkupNode::Text {
|
MarkupNode::Text {
|
||||||
content: nodes::COMMA.to_owned(),
|
content: nodes::COMMA.to_owned(),
|
||||||
ast_node_id: ASTNodeId::AExprId(expr_id),
|
ast_node_id: ASTNodeId::AExprId(expr_id),
|
||||||
syn_high_style: HighlightStyle::Blank,
|
syn_high_style: HighlightStyle::Comma,
|
||||||
attributes: Attributes::default(),
|
attributes: Attributes::default(),
|
||||||
parent_id_opt,
|
parent_id_opt,
|
||||||
newlines_at_end: 0,
|
newlines_at_end: 0,
|
||||||
|
|
|
@ -6,6 +6,7 @@ use crate::colors::{self, from_hsb, RgbaTup};
|
||||||
#[derive(Hash, Eq, PartialEq, Copy, Clone, Debug, Deserialize, Serialize)]
|
#[derive(Hash, Eq, PartialEq, Copy, Clone, Debug, Deserialize, Serialize)]
|
||||||
pub enum HighlightStyle {
|
pub enum HighlightStyle {
|
||||||
Operator, // =+-<>...
|
Operator, // =+-<>...
|
||||||
|
Comma,
|
||||||
String,
|
String,
|
||||||
FunctionName,
|
FunctionName,
|
||||||
Type,
|
Type,
|
||||||
|
@ -25,6 +26,7 @@ pub fn default_highlight_map() -> HashMap<HighlightStyle, RgbaTup> {
|
||||||
let mut highlight_map = HashMap::new();
|
let mut highlight_map = HashMap::new();
|
||||||
[
|
[
|
||||||
(Operator, colors::WHITE),
|
(Operator, colors::WHITE),
|
||||||
|
(Comma, from_hsb(258, 50, 90)),
|
||||||
(String, from_hsb(346, 65, 97)),
|
(String, from_hsb(346, 65, 97)),
|
||||||
(FunctionName, colors::WHITE),
|
(FunctionName, colors::WHITE),
|
||||||
(Type, colors::WHITE),
|
(Type, colors::WHITE),
|
||||||
|
|
123
docs/src/def.rs
123
docs/src/def.rs
|
@ -1,12 +1,119 @@
|
||||||
use crate::html::ToHtml;
|
use roc_ast::{ast_error::ASTResult, lang::{self, core::def::def_to_def2::def_to_def2}, mem_pool::pool::Pool};
|
||||||
|
use roc_code_markup::{markup::nodes::def2_to_markup, slow_pool::SlowPool};
|
||||||
|
use roc_module::symbol::{IdentIds, Interns, ModuleId, ModuleIds};
|
||||||
|
use roc_region::all::Region;
|
||||||
|
use bumpalo::{Bump, collections::String as BumpString};
|
||||||
|
use roc_types::subs::VarStore;
|
||||||
|
|
||||||
struct EqualSign;
|
use crate::html::mark_node_to_html;
|
||||||
|
|
||||||
impl<'a> ToHtml<'a> for EqualSign {
|
// html is written to buf
|
||||||
fn css_class(&self) -> Option<&'a str> {
|
pub fn defs_to_html<'a>(buf: &mut BumpString<'a>, defs: Vec<roc_parse::ast::Def<'a>>, env_module_id: ModuleId, env_module_ids: &'a ModuleIds, interns: &Interns) {
|
||||||
Some("operator")
|
|
||||||
}
|
let mut env_pool = Pool::with_capacity(1024);
|
||||||
fn html_body(&self, buf: &mut bumpalo::collections::String<'a>) {
|
let mut env_arena = Bump::new();
|
||||||
buf.push_str(" = ")
|
|
||||||
|
let mut var_store = VarStore::default();
|
||||||
|
let dep_idents = IdentIds::exposed_builtins(8);
|
||||||
|
let exposed_ident_ids = IdentIds::default();
|
||||||
|
|
||||||
|
let def_arena = Bump::new();
|
||||||
|
|
||||||
|
let mut env = lang::env::Env::new(
|
||||||
|
env_module_id,
|
||||||
|
&mut env_arena,
|
||||||
|
&mut env_pool,
|
||||||
|
&mut var_store,
|
||||||
|
dep_idents,
|
||||||
|
env_module_ids,
|
||||||
|
exposed_ident_ids,
|
||||||
|
);
|
||||||
|
|
||||||
|
let mut scope = lang::scope::Scope::new(env.home, env.pool, env.var_store);
|
||||||
|
let region = Region::new(0, 0, 0, 0);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
for def in defs.iter() {
|
||||||
|
// TODO remove unwrap
|
||||||
|
write_def_to_bump_str_html(
|
||||||
|
&def_arena,
|
||||||
|
&mut env,
|
||||||
|
&mut scope,
|
||||||
|
region,
|
||||||
|
&def,
|
||||||
|
interns,
|
||||||
|
buf
|
||||||
|
).unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// html is written to buf
|
||||||
|
pub fn def_to_html<'a>(buf: &mut BumpString<'a>, def: roc_parse::ast::Def<'a>, env_module_id: ModuleId, env_module_ids: &'a ModuleIds, interns: &Interns) {
|
||||||
|
|
||||||
|
let mut env_pool = Pool::with_capacity(1024);
|
||||||
|
let mut env_arena = Bump::new();
|
||||||
|
|
||||||
|
let mut var_store = VarStore::default();
|
||||||
|
let dep_idents = IdentIds::exposed_builtins(8);
|
||||||
|
let exposed_ident_ids = IdentIds::default();
|
||||||
|
|
||||||
|
let def_arena = Bump::new();
|
||||||
|
|
||||||
|
let mut env = lang::env::Env::new(
|
||||||
|
env_module_id,
|
||||||
|
&mut env_arena,
|
||||||
|
&mut env_pool,
|
||||||
|
&mut var_store,
|
||||||
|
dep_idents,
|
||||||
|
env_module_ids,
|
||||||
|
exposed_ident_ids,
|
||||||
|
);
|
||||||
|
|
||||||
|
let mut scope = lang::scope::Scope::new(env.home, env.pool, env.var_store);
|
||||||
|
let region = Region::new(0, 0, 0, 0);
|
||||||
|
|
||||||
|
|
||||||
|
// TODO remove unwrap
|
||||||
|
write_def_to_bump_str_html(
|
||||||
|
&def_arena,
|
||||||
|
&mut env,
|
||||||
|
&mut scope,
|
||||||
|
region,
|
||||||
|
&def,
|
||||||
|
interns,
|
||||||
|
buf
|
||||||
|
).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_def_to_bump_str_html<'a, 'b>(
|
||||||
|
arena: &'a Bump,
|
||||||
|
env: &mut lang::env::Env<'a>,
|
||||||
|
scope: &mut lang::scope::Scope,
|
||||||
|
region: Region,
|
||||||
|
def: &'a roc_parse::ast::Def<'a>,
|
||||||
|
interns: &Interns,
|
||||||
|
buf: &mut BumpString<'b>
|
||||||
|
) -> ASTResult<()> {
|
||||||
|
|
||||||
|
let def2 = def_to_def2(arena, env, scope, def, region);
|
||||||
|
|
||||||
|
let mut def2_pool = Pool::with_capacity(1024);
|
||||||
|
let def2_id = def2_pool.add(def2);
|
||||||
|
|
||||||
|
let mut mark_node_pool = SlowPool::default();
|
||||||
|
|
||||||
|
let def2_markup_id = def2_to_markup(
|
||||||
|
env,
|
||||||
|
&def2_pool.get(def2_id),
|
||||||
|
def2_id,
|
||||||
|
&mut mark_node_pool,
|
||||||
|
interns,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let def2_markup_node = mark_node_pool.get(def2_markup_id);
|
||||||
|
|
||||||
|
mark_node_to_html(def2_markup_node, &mark_node_pool, buf);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
192
docs/src/expr.rs
192
docs/src/expr.rs
|
@ -1,173 +1,53 @@
|
||||||
use crate::html::ToHtml;
|
use crate::html::{mark_node_to_html};
|
||||||
use roc_ast::{ast_error::ASTResult, lang::{self, core::expr::expr_to_expr2::expr_to_expr2}, mem_pool::pool::Pool};
|
use roc_ast::{ast_error::ASTResult, lang::{self, core::expr::expr_to_expr2::expr_to_expr2}, mem_pool::pool::Pool};
|
||||||
use roc_code_markup::{markup::nodes::expr2_to_markup, slow_pool::SlowPool};
|
use roc_code_markup::{markup::nodes::expr2_to_markup, slow_pool::SlowPool};
|
||||||
use roc_module::symbol::Interns;
|
use roc_module::symbol::{IdentIds, Interns, ModuleId, ModuleIds};
|
||||||
use roc_parse::ast::{Expr, StrLiteral};
|
use roc_parse::ast::{Expr};
|
||||||
use roc_region::all::{Region};
|
use roc_region::all::{Region};
|
||||||
use roc_code_markup::{markup::nodes::{MarkupNode}};
|
use bumpalo::{Bump, collections::String as BumpString};
|
||||||
|
use roc_types::subs::VarStore;
|
||||||
|
|
||||||
impl<'a> ToHtml<'a> for Expr<'a> {
|
// html is written to buf
|
||||||
fn css_class(&self) -> Option<&'a str> {
|
pub fn expr_to_html<'a>(buf: &mut BumpString<'a>, expr: Expr<'a>, env_module_id: ModuleId, env_module_ids: &'a ModuleIds, interns: &Interns) {
|
||||||
match self {
|
|
||||||
Expr::Float(_) | Expr::Num(_) | Expr::NonBase10Int { .. } => Some("num"),
|
|
||||||
Expr::Str(_) => Some("str"),
|
|
||||||
// Expr::Access(_, _) => {}
|
|
||||||
// Expr::AccessorFunction(_) => {}
|
|
||||||
// Expr::List { .. } => {}
|
|
||||||
// Expr::RecordUpdate { .. } => {}
|
|
||||||
// Expr::Record { .. } => {}
|
|
||||||
Expr::Var { .. } => None,
|
|
||||||
// Expr::Underscore(_) => {}
|
|
||||||
// Expr::GlobalTag(_) => {}
|
|
||||||
// Expr::PrivateTag(_) => {}
|
|
||||||
Expr::Closure(_, _) => None,
|
|
||||||
|
|
||||||
// Expr::Defs(_, _) => {}
|
let mut env_pool = Pool::with_capacity(1024);
|
||||||
// Expr::Backpassing(_, _, _) => {}
|
let mut env_arena = Bump::new();
|
||||||
// Expr::Expect(_, _) => {}
|
|
||||||
// Expr::Apply(_, _, _) => {}
|
|
||||||
// Expr::BinOps(_, _) => {}
|
|
||||||
// Expr::UnaryOp(_, _) => {}
|
|
||||||
// Expr::If(_, _) => {}
|
|
||||||
// Expr::When(_, _) => {}
|
|
||||||
Expr::SpaceBefore(_, _) => None,
|
|
||||||
Expr::SpaceAfter(_, _) => None,
|
|
||||||
// Expr::ParensAround(_) => {}
|
|
||||||
// Expr::MalformedIdent(_, _) => {}
|
|
||||||
// Expr::MalformedClosure => {}
|
|
||||||
// Expr::PrecedenceConflict(_) => {}
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fn html_body(&self, buf: &mut bumpalo::collections::String<'a>) {
|
|
||||||
match self {
|
|
||||||
Expr::Float(str) => {
|
|
||||||
buf.push_str(str);
|
|
||||||
}
|
|
||||||
Expr::Num(str) => {
|
|
||||||
buf.push_str(str);
|
|
||||||
}
|
|
||||||
Expr::NonBase10Int { string, .. } => {
|
|
||||||
buf.push_str(string);
|
|
||||||
}
|
|
||||||
Expr::Str(str_literal) => match str_literal {
|
|
||||||
StrLiteral::PlainLine(str) => {
|
|
||||||
buf.push('"');
|
|
||||||
buf.push_str(str);
|
|
||||||
buf.push('"');
|
|
||||||
}
|
|
||||||
StrLiteral::Line(line) => {
|
|
||||||
panic!("TODO str segments");
|
|
||||||
}
|
|
||||||
StrLiteral::Block(str) => {
|
|
||||||
panic!("TODO str segments");
|
|
||||||
}
|
|
||||||
},
|
|
||||||
// Expr::Access(_, _) => {}
|
|
||||||
// Expr::AccessorFunction(_) => {}
|
|
||||||
// Expr::List { .. } => {}
|
|
||||||
// Expr::RecordUpdate { .. } => {}
|
|
||||||
// Expr::Record { .. } => {}
|
|
||||||
Expr::Var { ident, module_name } => {
|
|
||||||
if !module_name.is_empty() {
|
|
||||||
buf.push_str(module_name);
|
|
||||||
buf.push('.');
|
|
||||||
}
|
|
||||||
buf.push_str(ident);
|
|
||||||
}
|
|
||||||
// Expr::Underscore(_) => {}
|
|
||||||
// Expr::GlobalTag(_) => {}
|
|
||||||
// Expr::PrivateTag(_) => {}
|
|
||||||
Expr::Closure(patterns, loc_sub_expr) => {
|
|
||||||
ClosureDash.html(buf);
|
|
||||||
|
|
||||||
let mut patterns_iter = patterns.iter().peekable();
|
let mut var_store = VarStore::default();
|
||||||
|
let dep_idents = IdentIds::exposed_builtins(8);
|
||||||
|
let exposed_ident_ids = IdentIds::default();
|
||||||
|
|
||||||
while let Some(pattern) = patterns_iter.next() {
|
let mut env = lang::env::Env::new(
|
||||||
pattern.value.html(buf);
|
env_module_id,
|
||||||
if let Some(_) = patterns_iter.peek() {
|
&mut env_arena,
|
||||||
ParamComma.html(buf);
|
&mut env_pool,
|
||||||
}
|
&mut var_store,
|
||||||
}
|
dep_idents,
|
||||||
|
env_module_ids,
|
||||||
|
exposed_ident_ids,
|
||||||
|
);
|
||||||
|
|
||||||
ClosureArrow.html(buf);
|
let mut scope = lang::scope::Scope::new(env.home, env.pool, env.var_store);
|
||||||
loc_sub_expr.html(buf);
|
let region = Region::new(0, 0, 0, 0);
|
||||||
}
|
|
||||||
Expr::Defs(defs, sub_expr) => {
|
// TODO remove unwrap
|
||||||
for def_loc in defs.iter() {
|
write_expr_to_bump_str_html(
|
||||||
unimplemented!();
|
&mut env,
|
||||||
//def_loc.html(buf);
|
&mut scope,
|
||||||
}
|
region,
|
||||||
sub_expr.html(buf)
|
&expr,
|
||||||
}
|
interns,
|
||||||
// Expr::Backpassing(_, _, _) => {}
|
buf
|
||||||
// Expr::Expect(_, _) => {}
|
).unwrap();
|
||||||
// Expr::Apply(_, _, _) => {}
|
|
||||||
// Expr::BinOps(_, _) => {}
|
|
||||||
// Expr::UnaryOp(_, _) => {}
|
|
||||||
// Expr::If(_, _) => {}
|
|
||||||
// Expr::When(_, _) => {}
|
|
||||||
Expr::SpaceBefore(sub_expr, spaces) => {
|
|
||||||
for space in spaces.iter() {
|
|
||||||
space.html(buf);
|
|
||||||
}
|
|
||||||
sub_expr.html(buf);
|
|
||||||
}
|
|
||||||
Expr::SpaceAfter(sub_expr, spaces) => {
|
|
||||||
sub_expr.html(buf);
|
|
||||||
for space in spaces.iter() {
|
|
||||||
space.html(buf);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Expr::ParensAround(_) => {}
|
|
||||||
// Expr::MalformedIdent(_, _) => {}
|
|
||||||
// Expr::MalformedClosure => {}
|
|
||||||
// Expr::PrecedenceConflict(_) => {}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ClosureDash;
|
fn write_expr_to_bump_str_html<'a, 'b>(
|
||||||
|
|
||||||
impl<'a> ToHtml<'a> for ClosureDash {
|
|
||||||
fn css_class(&self) -> Option<&'a str> {
|
|
||||||
Some("closure-dash")
|
|
||||||
}
|
|
||||||
fn html_body(&self, buf: &mut bumpalo::collections::String<'a>) {
|
|
||||||
buf.push('\\')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct ClosureArrow;
|
|
||||||
|
|
||||||
impl<'a> ToHtml<'a> for ClosureArrow {
|
|
||||||
fn css_class(&self) -> Option<&'a str> {
|
|
||||||
Some("closure-arrow")
|
|
||||||
}
|
|
||||||
fn html_body(&self, buf: &mut bumpalo::collections::String<'a>) {
|
|
||||||
buf.push_str(" ->")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct ParamComma;
|
|
||||||
|
|
||||||
impl<'a> ToHtml<'a> for ParamComma {
|
|
||||||
fn css_class(&self) -> Option<&'a str> {
|
|
||||||
Some("param-comma")
|
|
||||||
}
|
|
||||||
fn html_body(&self, buf: &mut bumpalo::collections::String<'a>) {
|
|
||||||
buf.push_str(", ")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write_expr_to_bump_str_html<'a, 'b>(
|
|
||||||
env: &mut lang::env::Env<'a>,
|
env: &mut lang::env::Env<'a>,
|
||||||
scope: &mut lang::scope::Scope,
|
scope: &mut lang::scope::Scope,
|
||||||
region: Region,
|
region: Region,
|
||||||
expr: &'a Expr,
|
expr: &'a Expr,
|
||||||
interns: &Interns,
|
interns: &Interns,
|
||||||
bump_str: &mut bumpalo::collections::String<'b>
|
buf: &mut BumpString<'b>
|
||||||
) -> ASTResult<()> {
|
) -> ASTResult<()> {
|
||||||
let (expr2, _) = expr_to_expr2(env, scope, expr, region);
|
let (expr2, _) = expr_to_expr2(env, scope, expr, region);
|
||||||
|
|
||||||
|
@ -186,7 +66,7 @@ pub fn write_expr_to_bump_str_html<'a, 'b>(
|
||||||
|
|
||||||
let expr2_markup_node = mark_node_pool.get(expr2_markup_id);
|
let expr2_markup_node = mark_node_pool.get(expr2_markup_id);
|
||||||
|
|
||||||
expr2_markup_node.html(bump_str);
|
mark_node_to_html(expr2_markup_node, &mark_node_pool, buf);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,26 +48,27 @@ where
|
||||||
|
|
||||||
|
|
||||||
// determine appropriate css class for MarkupNode
|
// determine appropriate css class for MarkupNode
|
||||||
fn mark_node_html<'a>(mark_node: &MarkupNode, mark_node_pool: &SlowPool, buf: &mut bumpalo::collections::String<'a>) {
|
pub fn mark_node_to_html<'a>(mark_node: &MarkupNode, mark_node_pool: &SlowPool, buf: &mut BumpString<'a>) {
|
||||||
|
let additional_newlines:usize;
|
||||||
|
|
||||||
match mark_node {
|
match mark_node {
|
||||||
MarkupNode::Nested { children_ids, newlines_at_end, .. } => {
|
MarkupNode::Nested { children_ids, newlines_at_end, .. } => {
|
||||||
for &child_id in children_ids {
|
for &child_id in children_ids {
|
||||||
mark_node_html(
|
mark_node_to_html(
|
||||||
mark_node_pool.get(child_id),
|
mark_node_pool.get(child_id),
|
||||||
mark_node_pool,
|
mark_node_pool,
|
||||||
buf
|
buf
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _ in 0..*newlines_at_end {
|
additional_newlines = *newlines_at_end;
|
||||||
buf.push('\n')
|
|
||||||
}
|
}
|
||||||
}
|
MarkupNode::Text { content, syn_high_style, newlines_at_end, .. } => {
|
||||||
MarkupNode::Text { syn_high_style, newlines_at_end, .. } => {
|
use roc_code_markup::syntax_highlight::HighlightStyle::*;
|
||||||
use HighlightStyle::*;
|
|
||||||
|
|
||||||
let css_class = match syn_high_style {
|
let css_class = match syn_high_style {
|
||||||
Operator => "operator",
|
Operator => "operator",
|
||||||
|
Comma => "comma",
|
||||||
String => "string",
|
String => "string",
|
||||||
FunctionName => "function_name",
|
FunctionName => "function_name",
|
||||||
Type => "type",
|
Type => "type",
|
||||||
|
@ -80,27 +81,37 @@ fn mark_node_html<'a>(mark_node: &MarkupNode, mark_node_pool: &SlowPool, buf: &m
|
||||||
Provides => "provides",
|
Provides => "provides",
|
||||||
Blank => "blank",
|
Blank => "blank",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
write_html_to_buf(content, css_class, buf);
|
||||||
|
|
||||||
|
additional_newlines = *newlines_at_end;
|
||||||
}
|
}
|
||||||
MarkupNode::Blank { syn_high_style, newlines_at_end, .. } => {
|
MarkupNode::Blank { newlines_at_end, .. } => {
|
||||||
let mut content_str = " ".to_string();
|
let mut content_str = " ".to_string();
|
||||||
|
|
||||||
for _ in 0..*newlines_at_end {
|
for _ in 0..*newlines_at_end {
|
||||||
content_str.push('\n');
|
content_str.push('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
write_html_to_buf(content_str, "blank", buf)
|
write_html_to_buf(&content_str, "blank", buf);
|
||||||
|
|
||||||
|
additional_newlines = *newlines_at_end;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _ in 0..additional_newlines {
|
||||||
|
buf.push('\n')
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_html_to_buf<'a>(content: String, css_class: &'static str, buf: &mut BumpString<'a>) {
|
fn write_html_to_buf<'a>(content: &str, css_class: &'static str, buf: &mut BumpString<'a>) {
|
||||||
|
|
||||||
let opening_tag: String = ["<span class=\"syntax-", css_class, "\">"].concat();
|
let opening_tag: String = ["<span class=\"syntax-", css_class, "\">"].concat();
|
||||||
|
|
||||||
buf.push_str(opening_tag.as_str());
|
buf.push_str(opening_tag.as_str());
|
||||||
|
|
||||||
buf.push_str(&content);
|
buf.push_str(content);
|
||||||
|
|
||||||
buf.push_str("</span>");
|
buf.push_str("</span>");
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
extern crate pulldown_cmark;
|
extern crate pulldown_cmark;
|
||||||
extern crate roc_load;
|
extern crate roc_load;
|
||||||
use bumpalo::{collections::String as BumpString, collections::Vec as BumpVec, Bump};
|
use bumpalo::{collections::String as BumpString, collections::Vec as BumpVec, Bump};
|
||||||
use expr::write_expr_to_bump_str_html;
|
use def::defs_to_html;
|
||||||
use roc_ast::lang;
|
use expr::{expr_to_html};
|
||||||
use roc_ast::mem_pool::pool::Pool;
|
|
||||||
use roc_builtins::std::StdLib;
|
use roc_builtins::std::StdLib;
|
||||||
use roc_can::builtins::builtin_defs_map;
|
use roc_can::builtins::builtin_defs_map;
|
||||||
use roc_can::scope::Scope;
|
use roc_can::scope::Scope;
|
||||||
|
@ -13,11 +12,9 @@ use roc_load::docs::{DocEntry, TypeAnnotation};
|
||||||
use roc_load::docs::{ModuleDocumentation, RecordField};
|
use roc_load::docs::{ModuleDocumentation, RecordField};
|
||||||
use roc_load::file::{LoadedModule, LoadingProblem};
|
use roc_load::file::{LoadedModule, LoadingProblem};
|
||||||
use roc_module::symbol::{IdentIds, Interns, ModuleId, ModuleIds};
|
use roc_module::symbol::{IdentIds, Interns, ModuleId, ModuleIds};
|
||||||
use roc_parse::ast::Expr;
|
|
||||||
use roc_parse::ident::{parse_ident, Ident};
|
use roc_parse::ident::{parse_ident, Ident};
|
||||||
use roc_parse::parser::{State, SyntaxError};
|
use roc_parse::parser::{State, SyntaxError};
|
||||||
use roc_region::all::Region;
|
use roc_region::all::Region;
|
||||||
use roc_types::subs::VarStore;
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
|
@ -130,42 +127,8 @@ pub fn generate_docs_html(filenames: Vec<PathBuf>, std_lib: StdLib, build_dir: &
|
||||||
println!("🎉 Docs generated in {}", build_dir.display());
|
println!("🎉 Docs generated in {}", build_dir.display());
|
||||||
}
|
}
|
||||||
|
|
||||||
// html is written to buf
|
// converts plain-text code to highlighted html
|
||||||
fn expr_to_html<'a>(buf: &mut BumpString<'a>, expr: Expr<'a>, env_module_id: ModuleId, env_module_ids: &'a ModuleIds, interns: &Interns) {
|
pub fn syntax_highlight_expr<'a>(
|
||||||
|
|
||||||
let mut env_pool = Pool::with_capacity(1024);
|
|
||||||
let mut env_arena = Bump::new();
|
|
||||||
|
|
||||||
let mut var_store = VarStore::default();
|
|
||||||
let dep_idents = IdentIds::exposed_builtins(8);
|
|
||||||
let exposed_ident_ids = IdentIds::default();
|
|
||||||
|
|
||||||
let mut env = lang::env::Env::new(
|
|
||||||
env_module_id,
|
|
||||||
&mut env_arena,
|
|
||||||
&mut env_pool,
|
|
||||||
&mut var_store,
|
|
||||||
dep_idents,
|
|
||||||
env_module_ids,
|
|
||||||
exposed_ident_ids,
|
|
||||||
);
|
|
||||||
|
|
||||||
let mut scope = lang::scope::Scope::new(env.home, env.pool, env.var_store);
|
|
||||||
let region = Region::new(0, 0, 0, 0);
|
|
||||||
|
|
||||||
// TODO remove unwrap
|
|
||||||
write_expr_to_bump_str_html(
|
|
||||||
&mut env,
|
|
||||||
&mut scope,
|
|
||||||
region,
|
|
||||||
&expr,
|
|
||||||
interns,
|
|
||||||
buf
|
|
||||||
).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
// converts plain text code to highlighted html
|
|
||||||
pub fn syntax_highlight_code<'a>(
|
|
||||||
arena: &'a Bump,
|
arena: &'a Bump,
|
||||||
buf: &mut BumpString<'a>,
|
buf: &mut BumpString<'a>,
|
||||||
code_str: &'a str,
|
code_str: &'a str,
|
||||||
|
@ -184,11 +147,30 @@ pub fn syntax_highlight_code<'a>(
|
||||||
}
|
}
|
||||||
Err(fail) => Err(SyntaxError::Expr(fail)),
|
Err(fail) => Err(SyntaxError::Expr(fail)),
|
||||||
}
|
}
|
||||||
// roc_parse::test_helpers::parse_expr_with(&arena, trimmed_code_str).map(|expr| {
|
}
|
||||||
// expr.html(buf);
|
|
||||||
//
|
// converts plain-text code to highlighted html
|
||||||
// buf.to_string()
|
pub fn syntax_highlight_top_level_defs<'a>(
|
||||||
// })
|
arena: &'a Bump,
|
||||||
|
buf: &mut BumpString<'a>,
|
||||||
|
code_str: &'a str,
|
||||||
|
env_module_id: ModuleId,
|
||||||
|
env_module_ids: &'a ModuleIds,
|
||||||
|
interns: &Interns
|
||||||
|
) -> Result<String, SyntaxError<'a>> {
|
||||||
|
let trimmed_code_str = code_str.trim_end().trim();
|
||||||
|
|
||||||
|
match roc_parse::test_helpers::parse_defs_with(arena, trimmed_code_str) {
|
||||||
|
Ok(vec_loc_def) => {
|
||||||
|
let vec_def =
|
||||||
|
vec_loc_def.iter().map(|loc| loc.value).collect();
|
||||||
|
|
||||||
|
defs_to_html(buf, vec_def, env_module_id, env_module_ids, interns);
|
||||||
|
|
||||||
|
Ok(buf.to_string())
|
||||||
|
}
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO improve name, what is main content?
|
// TODO improve name, what is main content?
|
||||||
|
@ -997,7 +979,7 @@ fn markdown_to_html(
|
||||||
let code_block_arena = Bump::new();
|
let code_block_arena = Bump::new();
|
||||||
|
|
||||||
let mut code_block_buf = BumpString::new_in(&code_block_arena);
|
let mut code_block_buf = BumpString::new_in(&code_block_arena);
|
||||||
match syntax_highlight_code(
|
match syntax_highlight_expr(
|
||||||
&code_block_arena,
|
&code_block_arena,
|
||||||
&mut code_block_buf,
|
&mut code_block_buf,
|
||||||
code_str,
|
code_str,
|
||||||
|
|
|
@ -7,7 +7,6 @@
|
||||||
--body-bg-color: #fdfdfd;
|
--body-bg-color: #fdfdfd;
|
||||||
--border-color: #e9e9e9;
|
--border-color: #e9e9e9;
|
||||||
--faded-color: #4c4c4c;
|
--faded-color: #4c4c4c;
|
||||||
--monospace-font;
|
|
||||||
--font-sans: -apple-system, BlinkMacSystemFont, Roboto, Helvetica, Arial, sans-serif;
|
--font-sans: -apple-system, BlinkMacSystemFont, Roboto, Helvetica, Arial, sans-serif;
|
||||||
--font-mono: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace;
|
--font-mono: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace;
|
||||||
--top-header-height: 67px;
|
--top-header-height: 67px;
|
||||||
|
@ -310,22 +309,25 @@ pre {
|
||||||
display: none !important;
|
display: none !important;
|
||||||
}
|
}
|
||||||
|
|
||||||
.syntax-num {
|
.syntax-number {
|
||||||
color: #8080ff;
|
color: #60B7BF;
|
||||||
|
}
|
||||||
|
.syntax-string {
|
||||||
|
color:#F7577C;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.syntax-bracket {
|
||||||
|
color:#FF335F;
|
||||||
|
}
|
||||||
.syntax-closure-dash,
|
.syntax-closure-dash,
|
||||||
.syntax-closure-arrow,
|
.syntax-closure-arrow,
|
||||||
.syntax-operator ,
|
.syntax-operator
|
||||||
.syntax-param-comma
|
|
||||||
{
|
{
|
||||||
color: #ff0000;
|
color: #ffffff;
|
||||||
}
|
}
|
||||||
|
.syntax-comma {
|
||||||
.syntax-str {
|
color: #9573E6;
|
||||||
color:#ffff00;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.syntax-comment {
|
.syntax-comment {
|
||||||
color: #ff0000;
|
color: #ff0000;
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,11 +7,11 @@ mod insert_doc_syntax_highlighting {
|
||||||
use roc_can::env::Env;
|
use roc_can::env::Env;
|
||||||
use roc_can::scope::Scope;
|
use roc_can::scope::Scope;
|
||||||
use roc_collections::all::MutMap;
|
use roc_collections::all::MutMap;
|
||||||
use roc_docs::syntax_highlight_code;
|
use roc_docs::{syntax_highlight_expr, syntax_highlight_top_level_defs};
|
||||||
use roc_module::symbol::{IdentIds, Interns, ModuleIds};
|
use roc_module::symbol::{IdentIds, Interns, ModuleIds};
|
||||||
use roc_types::subs::VarStore;
|
use roc_types::subs::VarStore;
|
||||||
|
|
||||||
fn expect_html(code_str: &str, want: &str) {
|
fn expect_html(code_str: &str, want: &str, use_expr: bool) {
|
||||||
let code_block_arena = Bump::new();
|
let code_block_arena = Bump::new();
|
||||||
|
|
||||||
|
|
||||||
|
@ -24,7 +24,24 @@ mod insert_doc_syntax_highlighting {
|
||||||
};
|
};
|
||||||
let mut code_block_buf = BumpString::new_in(&code_block_arena);
|
let mut code_block_buf = BumpString::new_in(&code_block_arena);
|
||||||
|
|
||||||
match syntax_highlight_code(
|
if use_expr {
|
||||||
|
match syntax_highlight_expr(
|
||||||
|
&code_block_arena,
|
||||||
|
&mut code_block_buf,
|
||||||
|
code_str,
|
||||||
|
mod_id,
|
||||||
|
&module_ids,
|
||||||
|
&interns
|
||||||
|
) {
|
||||||
|
Ok(highlighted_code_str) => {
|
||||||
|
assert_eq!(highlighted_code_str, want);
|
||||||
|
}
|
||||||
|
Err(syntax_error) => {
|
||||||
|
panic!("Unexpected parse failure when parsing this for rendering in docs:\n\n{}\n\nParse error was:\n\n{:?}\n\n", code_str, syntax_error)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
match syntax_highlight_top_level_defs(
|
||||||
&code_block_arena,
|
&code_block_arena,
|
||||||
&mut code_block_buf,
|
&mut code_block_buf,
|
||||||
code_str,
|
code_str,
|
||||||
|
@ -41,35 +58,63 @@ mod insert_doc_syntax_highlighting {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/*#[test]
|
}
|
||||||
fn simple_code_block() {
|
|
||||||
expect_html(
|
|
||||||
r#"
|
|
||||||
f = \x, y ->
|
|
||||||
b = 6
|
|
||||||
c
|
|
||||||
|
|
||||||
"string"
|
fn expect_html_expr(code_str: &str, want: &str) {
|
||||||
"#,
|
expect_html(code_str, want, true)
|
||||||
r#"
|
}
|
||||||
<div class="syntax-var">x</div> <div class="syntax-operator">:</div> <div class="syntax-type">Nat</div>
|
|
||||||
<div class="syntax-var">x</div> <div class="syntax-operator">=</div>
|
|
||||||
<div class="syntax-number">4</div>
|
|
||||||
|
|
||||||
<div class="syntax-number">2</div>
|
fn expect_html_def(code_str: &str, want: &str) {
|
||||||
"#,
|
expect_html(code_str, want, false)
|
||||||
);
|
}
|
||||||
}*/
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn simple_code_block() {
|
fn number_expr() {
|
||||||
expect_html(
|
expect_html_expr(
|
||||||
r#"
|
"2",
|
||||||
2
|
r#"<span class="syntax-number">2</span>"#,
|
||||||
"#,
|
);
|
||||||
r#"
|
}
|
||||||
<div class="syntax-number">2</div>
|
|
||||||
"#,
|
#[test]
|
||||||
|
fn string_expr() {
|
||||||
|
expect_html_expr(
|
||||||
|
r#""abc""#,
|
||||||
|
r#"<span class="syntax-string">"abc"</span>"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn empty_list_expr() {
|
||||||
|
expect_html_expr(
|
||||||
|
r#"[]"#,
|
||||||
|
r#"<span class="syntax-bracket">[ </span><span class="syntax-bracket"> ]</span>"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn single_elt_list_expr() {
|
||||||
|
expect_html_expr(
|
||||||
|
r#"[ 0 ]"#,
|
||||||
|
r#"<span class="syntax-bracket">[ </span><span class="syntax-number">0</span><span class="syntax-bracket"> ]</span>"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn multi_elt_list_expr() {
|
||||||
|
expect_html_expr(
|
||||||
|
r#"[ "hello", "WoRlD" ]"#,
|
||||||
|
r#"<span class="syntax-bracket">[ </span><span class="syntax-string">"hello"</span><span class="syntax-comma">, </span><span class="syntax-string">"WoRlD"</span><span class="syntax-bracket"> ]</span>"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO test record, nested records
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn function_def() {
|
||||||
|
expect_html_def(
|
||||||
|
r#"main = "Hello, World!""#,
|
||||||
|
r#"TODO"#,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue