1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 20:57:44 +00:00

JS repl: Fix indentation when a line starts with '})]'

This commit is contained in:
AnotherTest 2020-04-09 08:00:45 +04:30 committed by Andreas Kling
parent da9335dbec
commit 6545a74743

View file

@ -61,20 +61,27 @@ private:
bool dump_ast = false;
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()
{
StringBuilder piece;
int level = 0;
StringBuilder prompt_builder;
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);
piece.append(line);
@ -85,18 +92,18 @@ String read_next_piece()
case JS::TokenType::BracketOpen:
case JS::TokenType::CurlyOpen:
case JS::TokenType::ParenOpen:
level++;
repl_line_level++;
break;
case JS::TokenType::BracketClose:
case JS::TokenType::CurlyClose:
case JS::TokenType::ParenClose:
level--;
repl_line_level--;
break;
default:
break;
}
}
} while (level > 0);
} while (repl_line_level > 0);
return piece.to_string();
}
@ -390,8 +397,11 @@ int main(int argc, char** argv)
editor = make<Line::Editor>();
editor->initialize();
editor->on_display_refresh = [syntax_highlight](Line::Editor& editor) {
auto stylize = [&](Line::Span span, Line::Style styles) {
if (syntax_highlight)
editor->on_display_refresh = [](Line::Editor& editor) {
editor.stylize(span, styles);
};
editor.strip_styles();
StringBuilder builder;
builder.append({ editor.buffer().data(), editor.buffer().size() });
@ -399,24 +409,34 @@ int main(int argc, char** argv)
builder.append(" ");
String str = builder.build();
size_t open_indents = repl_line_level;
JS::Lexer lexer(str, false);
bool indenters_starting_line = true;
for (JS::Token token = lexer.next(); token.type() != JS::TokenType::Eof; token = lexer.next()) {
auto length = token.value().length();
auto start = token.line_column() - 2;
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()) {
case JS::TokenType::Invalid:
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;
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;
case JS::TokenType::StringLiteral:
case JS::TokenType::RegexLiteral:
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;
case JS::TokenType::BracketClose:
case JS::TokenType::BracketOpen:
@ -469,13 +489,13 @@ int main(int argc, char** argv)
case JS::TokenType::Tilde:
case JS::TokenType::UnsignedShiftRight:
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;
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;
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;
case JS::TokenType::Class:
case JS::TokenType::Const:
@ -489,7 +509,7 @@ int main(int argc, char** argv)
case JS::TokenType::Typeof:
case JS::TokenType::Var:
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;
case JS::TokenType::Await:
case JS::TokenType::Catch:
@ -502,13 +522,15 @@ int main(int argc, char** argv)
case JS::TokenType::Try:
case JS::TokenType::While:
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;
case JS::TokenType::Identifier:
default:
break;
}
}
editor.set_prompt(prompt_for_level(open_indents));
};
repl(*interpreter);
} else {