Skip to content

Commit

Permalink
it was possible to start a new line after blubb.keys()[ since ) has the
Browse files Browse the repository at this point in the history
break flag and [ is attached. in this case now the first space or
breakpoint will be used

added cfg option to keep the original keyword text instead of the
lowercase one. (FormatterKeepKeywords)
  • Loading branch information
turleypol committed Feb 15, 2024
1 parent b4fe753 commit ed5aa12
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
2 changes: 2 additions & 0 deletions pol-core/bscript/compiler/astbuilder/PrettifyBuilder.cpp
Expand Up @@ -37,6 +37,8 @@ std::string PrettifyBuilder::build( const std::string& pathname, bool is_module
else
prettify_processor.process_compilation_unit( *sf );

if ( report.error_count() )
return {};
return prettify_processor.prettify();
}

Expand Down
40 changes: 33 additions & 7 deletions pol-core/bscript/compiler/astbuilder/PrettifyFileProcessor.cpp
Expand Up @@ -28,6 +28,8 @@ antlrcpp::Any PrettifyFileProcessor::process_compilation_unit( SourceFile& sf )
{
if ( auto compilation_unit = sf.get_compilation_unit( report, source_file_identifier ) )
{
if ( report.error_count() )
return {};
collectComments( sf );
return compilation_unit->accept( this );
}
Expand All @@ -38,6 +40,8 @@ antlrcpp::Any PrettifyFileProcessor::process_module_unit( SourceFile& sf )
{
if ( auto module_unit = sf.get_module_unit( report, source_file_identifier ) )
{
if ( report.error_count() )
return {};
collectComments( sf );
return module_unit->accept( this );
}
Expand Down Expand Up @@ -202,15 +206,31 @@ void PrettifyFileProcessor::buildLine()
// start a new line if breakpoint
else if ( _line_parts[i].style & TokenPart::BREAKPOINT )
{
// if the next part is attached, dont break now and instead after the attached one
// if the next part is attached, dont break now and instead later
// eg dont split blubb[1]()
bool skip = false;
if ( i + 1 < _line_parts.size() )
{
// next one is attached
if ( _line_parts[i + 1].style & TokenPart::ATTACHED )
{
_line_parts[i + 1].style |= TokenPart::BREAKPOINT;
continue;
// search for space or breakpoint
for ( size_t j = i + 1; j < _line_parts.size(); ++j )
{
if ( _line_parts[j].style & TokenPart::ATTACHED )
skip = true;
else if ( _line_parts[j].style & TokenPart::SPACE ||
_line_parts[j].style & TokenPart::BREAKPOINT )
{
_line_parts[j].style |= TokenPart::BREAKPOINT;
skip = true;
break;
}
}
}
}
if ( skip )
continue;
lines.emplace_back( std::make_tuple( std::move( line ), false, _line_parts[i].group ) );
line.clear();
}
Expand Down Expand Up @@ -242,6 +262,8 @@ void PrettifyFileProcessor::buildLine()
}

// split based on groups
// TODO groups could be the best for variable declarations
// other option eg function declaration
if ( groups && linelength > compilercfg.FormatterLineWidth )
{
std::vector<size_t> alignmentspace = {};
Expand Down Expand Up @@ -1464,14 +1486,18 @@ void PrettifyFileProcessor::addToken( std::string&& text, const Position& pos, i
void PrettifyFileProcessor::addToken( std::string&& text, antlr4::tree::TerminalNode* terminal,
int style )
{
// TODO: terminal->getSymbol()->getText()
addToken( std::forward<std::string>( text ), Range( *terminal ).start, style );
if ( compilercfg.FormatterKeepKeywords )
addToken( terminal->getSymbol()->getText(), Range( *terminal ).start, style );
else
addToken( std::forward<std::string>( text ), Range( *terminal ).start, style );
}

void PrettifyFileProcessor::addToken( std::string&& text, antlr4::Token* token, int style )
{
// TODO: token->getText()
addToken( std::forward<std::string>( text ), Range( token ).start, style );
if ( compilercfg.FormatterKeepKeywords )
addToken( token->getText(), Range( token ).start, style );
else
addToken( std::forward<std::string>( text ), Range( token ).start, style );
}
} // namespace Pol::Bscript::Compiler

Expand Down
3 changes: 2 additions & 1 deletion pol-core/bscript/compilercfg.cpp
Expand Up @@ -63,7 +63,7 @@ void CompilerConfig::Read( const std::string& path )


FormatterLineWidth = elem.remove_unsigned( "FormatterLineWidth", 80 );

FormatterKeepKeywords = elem.remove_bool( "FormatterKeepKeywords", false );
// This is where we TRY to validate full paths from what was provided in the
// ecompile.cfg. Maybe Turley or Shini can find the best way to do this in *nix.
#ifdef WIN32
Expand Down Expand Up @@ -136,6 +136,7 @@ void CompilerConfig::SetDefaults()


FormatterLineWidth = 80;
FormatterKeepKeywords = false;
}

CompilerConfig compilercfg;
Expand Down
1 change: 1 addition & 0 deletions pol-core/bscript/compilercfg.h
Expand Up @@ -43,6 +43,7 @@ struct CompilerConfig

// Formatter
size_t FormatterLineWidth;
bool FormatterKeepKeywords;

void Read( const std::string& path );
void SetDefaults();
Expand Down

0 comments on commit ed5aa12

Please sign in to comment.