mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 06:37:44 +00:00
JS repl: Fix indentation when a line starts with '})]'
This commit is contained in:
parent
da9335dbec
commit
6545a74743
1 changed files with 149 additions and 127 deletions
|
@ -61,20 +61,27 @@ private:
|
||||||
|
|
||||||
bool dump_ast = false;
|
bool dump_ast = false;
|
||||||
static OwnPtr<Line::Editor> editor;
|
static OwnPtr<Line::Editor> editor;
|
||||||
|
static int repl_line_level = 0;
|
||||||
|
|
||||||
|
static String prompt_for_level(int level)
|
||||||
|
{
|
||||||
|
static StringBuilder prompt_builder;
|
||||||
|
prompt_builder.clear();
|
||||||
|
prompt_builder.append("> ");
|
||||||
|
|
||||||
|
for (auto i = 0; i < level; ++i)
|
||||||
|
prompt_builder.append(" ");
|
||||||
|
|
||||||
|
return prompt_builder.build();
|
||||||
|
}
|
||||||
|
|
||||||
String read_next_piece()
|
String read_next_piece()
|
||||||
{
|
{
|
||||||
StringBuilder piece;
|
StringBuilder piece;
|
||||||
int level = 0;
|
|
||||||
StringBuilder prompt_builder;
|
|
||||||
|
|
||||||
do {
|
do {
|
||||||
prompt_builder.clear();
|
|
||||||
prompt_builder.append("> ");
|
|
||||||
for (auto i = 0; i < level; ++i)
|
|
||||||
prompt_builder.append(" ");
|
|
||||||
|
|
||||||
String line = editor->get_line(prompt_builder.build());
|
String line = editor->get_line(prompt_for_level(repl_line_level));
|
||||||
editor->add_to_history(line);
|
editor->add_to_history(line);
|
||||||
|
|
||||||
piece.append(line);
|
piece.append(line);
|
||||||
|
@ -85,18 +92,18 @@ String read_next_piece()
|
||||||
case JS::TokenType::BracketOpen:
|
case JS::TokenType::BracketOpen:
|
||||||
case JS::TokenType::CurlyOpen:
|
case JS::TokenType::CurlyOpen:
|
||||||
case JS::TokenType::ParenOpen:
|
case JS::TokenType::ParenOpen:
|
||||||
level++;
|
repl_line_level++;
|
||||||
break;
|
break;
|
||||||
case JS::TokenType::BracketClose:
|
case JS::TokenType::BracketClose:
|
||||||
case JS::TokenType::CurlyClose:
|
case JS::TokenType::CurlyClose:
|
||||||
case JS::TokenType::ParenClose:
|
case JS::TokenType::ParenClose:
|
||||||
level--;
|
repl_line_level--;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while (level > 0);
|
} while (repl_line_level > 0);
|
||||||
|
|
||||||
return piece.to_string();
|
return piece.to_string();
|
||||||
}
|
}
|
||||||
|
@ -390,8 +397,11 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
editor = make<Line::Editor>();
|
editor = make<Line::Editor>();
|
||||||
editor->initialize();
|
editor->initialize();
|
||||||
|
editor->on_display_refresh = [syntax_highlight](Line::Editor& editor) {
|
||||||
|
auto stylize = [&](Line::Span span, Line::Style styles) {
|
||||||
if (syntax_highlight)
|
if (syntax_highlight)
|
||||||
editor->on_display_refresh = [](Line::Editor& editor) {
|
editor.stylize(span, styles);
|
||||||
|
};
|
||||||
editor.strip_styles();
|
editor.strip_styles();
|
||||||
StringBuilder builder;
|
StringBuilder builder;
|
||||||
builder.append({ editor.buffer().data(), editor.buffer().size() });
|
builder.append({ editor.buffer().data(), editor.buffer().size() });
|
||||||
|
@ -399,24 +409,34 @@ int main(int argc, char** argv)
|
||||||
builder.append(" ");
|
builder.append(" ");
|
||||||
String str = builder.build();
|
String str = builder.build();
|
||||||
|
|
||||||
|
size_t open_indents = repl_line_level;
|
||||||
|
|
||||||
JS::Lexer lexer(str, false);
|
JS::Lexer lexer(str, false);
|
||||||
|
bool indenters_starting_line = true;
|
||||||
for (JS::Token token = lexer.next(); token.type() != JS::TokenType::Eof; token = lexer.next()) {
|
for (JS::Token token = lexer.next(); token.type() != JS::TokenType::Eof; token = lexer.next()) {
|
||||||
auto length = token.value().length();
|
auto length = token.value().length();
|
||||||
auto start = token.line_column() - 2;
|
auto start = token.line_column() - 2;
|
||||||
auto end = start + length;
|
auto end = start + length;
|
||||||
|
if (indenters_starting_line) {
|
||||||
|
if (token.type() != JS::TokenType::ParenClose && token.type() != JS::TokenType::BracketClose && token.type() != JS::TokenType::CurlyClose) {
|
||||||
|
indenters_starting_line = false;
|
||||||
|
} else {
|
||||||
|
--open_indents;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
switch (token.type()) {
|
switch (token.type()) {
|
||||||
case JS::TokenType::Invalid:
|
case JS::TokenType::Invalid:
|
||||||
case JS::TokenType::Eof:
|
case JS::TokenType::Eof:
|
||||||
editor.stylize({ start, end }, { Line::Style::Foreground(Line::Style::Color::Red), Line::Style::Underline });
|
stylize({ start, end }, { Line::Style::Foreground(Line::Style::Color::Red), Line::Style::Underline });
|
||||||
break;
|
break;
|
||||||
case JS::TokenType::NumericLiteral:
|
case JS::TokenType::NumericLiteral:
|
||||||
editor.stylize({ start, end }, { Line::Style::Foreground(Line::Style::Color::Magenta) });
|
stylize({ start, end }, { Line::Style::Foreground(Line::Style::Color::Magenta) });
|
||||||
break;
|
break;
|
||||||
case JS::TokenType::StringLiteral:
|
case JS::TokenType::StringLiteral:
|
||||||
case JS::TokenType::RegexLiteral:
|
case JS::TokenType::RegexLiteral:
|
||||||
case JS::TokenType::UnterminatedStringLiteral:
|
case JS::TokenType::UnterminatedStringLiteral:
|
||||||
editor.stylize({ start, end }, { Line::Style::Foreground(Line::Style::Color::Red) });
|
stylize({ start, end }, { Line::Style::Foreground(Line::Style::Color::Red) });
|
||||||
break;
|
break;
|
||||||
case JS::TokenType::BracketClose:
|
case JS::TokenType::BracketClose:
|
||||||
case JS::TokenType::BracketOpen:
|
case JS::TokenType::BracketOpen:
|
||||||
|
@ -469,13 +489,13 @@ int main(int argc, char** argv)
|
||||||
case JS::TokenType::Tilde:
|
case JS::TokenType::Tilde:
|
||||||
case JS::TokenType::UnsignedShiftRight:
|
case JS::TokenType::UnsignedShiftRight:
|
||||||
case JS::TokenType::UnsignedShiftRightEquals:
|
case JS::TokenType::UnsignedShiftRightEquals:
|
||||||
editor.stylize({ start, end }, { Line::Style::Foreground(Line::Style::Color::Magenta) });
|
stylize({ start, end }, { Line::Style::Foreground(Line::Style::Color::Magenta) });
|
||||||
break;
|
break;
|
||||||
case JS::TokenType::NullLiteral:
|
case JS::TokenType::NullLiteral:
|
||||||
editor.stylize({ start, end }, { Line::Style::Foreground(Line::Style::Color::Yellow), Line::Style::Bold });
|
stylize({ start, end }, { Line::Style::Foreground(Line::Style::Color::Yellow), Line::Style::Bold });
|
||||||
break;
|
break;
|
||||||
case JS::TokenType::BoolLiteral:
|
case JS::TokenType::BoolLiteral:
|
||||||
editor.stylize({ start, end }, { Line::Style::Foreground(Line::Style::Color::Green), Line::Style::Bold });
|
stylize({ start, end }, { Line::Style::Foreground(Line::Style::Color::Green), Line::Style::Bold });
|
||||||
break;
|
break;
|
||||||
case JS::TokenType::Class:
|
case JS::TokenType::Class:
|
||||||
case JS::TokenType::Const:
|
case JS::TokenType::Const:
|
||||||
|
@ -489,7 +509,7 @@ int main(int argc, char** argv)
|
||||||
case JS::TokenType::Typeof:
|
case JS::TokenType::Typeof:
|
||||||
case JS::TokenType::Var:
|
case JS::TokenType::Var:
|
||||||
case JS::TokenType::Void:
|
case JS::TokenType::Void:
|
||||||
editor.stylize({ start, end }, { Line::Style::Foreground(Line::Style::Color::Blue), Line::Style::Bold });
|
stylize({ start, end }, { Line::Style::Foreground(Line::Style::Color::Blue), Line::Style::Bold });
|
||||||
break;
|
break;
|
||||||
case JS::TokenType::Await:
|
case JS::TokenType::Await:
|
||||||
case JS::TokenType::Catch:
|
case JS::TokenType::Catch:
|
||||||
|
@ -502,13 +522,15 @@ int main(int argc, char** argv)
|
||||||
case JS::TokenType::Try:
|
case JS::TokenType::Try:
|
||||||
case JS::TokenType::While:
|
case JS::TokenType::While:
|
||||||
case JS::TokenType::Yield:
|
case JS::TokenType::Yield:
|
||||||
editor.stylize({ start, end }, { Line::Style::Foreground(Line::Style::Color::Cyan), Line::Style::Italic });
|
stylize({ start, end }, { Line::Style::Foreground(Line::Style::Color::Cyan), Line::Style::Italic });
|
||||||
break;
|
break;
|
||||||
case JS::TokenType::Identifier:
|
case JS::TokenType::Identifier:
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
editor.set_prompt(prompt_for_level(open_indents));
|
||||||
};
|
};
|
||||||
repl(*interpreter);
|
repl(*interpreter);
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue