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

Option To Limit Code Width For Split Lines #4169

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -3193,6 +3193,11 @@ pos_shift;
extern BoundedOption<unsigned, 0, 10000>
code_width;

// When a line is split due to code_width, limit width of split lines to N columns.
// Values within [0, code_width]
extern BoundedOption<unsigned, 0, 10000>
code_split_width;

// Whether to fully split long 'for' statements at semi-colons.
extern Option<bool>
ls_for_split_full;
Expand Down
27 changes: 27 additions & 0 deletions src/width.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ struct TokenPriority

static inline bool is_past_width(Chunk *pc);

static inline bool is_past_split_width(Chunk *pc);

//! Split right after the chunk
static void split_before_chunk(Chunk *pc);
Expand Down Expand Up @@ -123,6 +124,17 @@ static inline bool is_past_width(Chunk *pc)
return(past_width);
}

static inline bool is_past_split_width(Chunk *pc)
{
size_t currCol = pc->GetColumn() + pc->Len() - 1;
bool past_split_width = currCol > options::code_split_width();

LOG_FMT(LSPLIT, "%s(%d): orig line %zu, orig col %zu, curr col %zu, text '%s', past split width %s\n",
__func__, __LINE__, pc->GetOrigLine(), pc->GetOrigCol(), currCol, pc->Text(),
past_split_width ? "YES" : "NO");
return(past_split_width);
}


static void split_before_chunk(Chunk *pc)
{
Expand Down Expand Up @@ -224,6 +236,21 @@ void do_code_width()
{
continue;
}

// We must split the line, check if we have a split width
if (options::code_split_width() > 0)
{
// Move pc back to first chunk within the split width
Chunk* prev = pc->GetPrev();
while ( prev->IsNotNullChunk()
&& !prev->IsNewline()
&& is_past_split_width(prev))
{
pc = prev;
prev = prev->GetPrev();
}
}

bool split_OK = split_line(pc);

if (split_OK)
Expand Down