-
-
Notifications
You must be signed in to change notification settings - Fork 16
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
feat: implement multiline support #51
Open
aurelien-brabant
wants to merge
3
commits into
hyprwm:main
Choose a base branch
from
aurelien-brabant:feat/add-multi-line-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
#include <fstream> | ||
#include <string> | ||
#include <format> | ||
#include <iostream> | ||
#include <algorithm> | ||
#include <cmath> | ||
#include <expected> | ||
|
@@ -473,8 +474,9 @@ void CConfigImpl::parseComment(const std::string& comment) { | |
|
||
CParseResult CConfig::parseLine(std::string line, bool dynamic) { | ||
CParseResult result; | ||
bool shouldPreverseLeadingWhitespace = impl->multiline.delimiter == '\\'; | ||
|
||
line = trim(line); | ||
line = shouldPreverseLeadingWhitespace ? line.substr(0, line.find_last_not_of(MULTILINE_SPACE_CHARSET) + 1) : trim(line); | ||
|
||
auto commentPos = line.find('#'); | ||
|
||
|
@@ -496,34 +498,38 @@ CParseResult CConfig::parseLine(std::string line, bool dynamic) { | |
|
||
if (!escaped) { | ||
line = line.substr(0, commentPos); | ||
// there might be trailing whitespaces after the comment that weren't previous trimmed | ||
line = line.substr(0, line.find_last_not_of(MULTILINE_SPACE_CHARSET) + 1); | ||
break; | ||
} else { | ||
line = line.substr(0, commentPos + 1) + line.substr(commentPos + 2); | ||
commentPos = line.find('#', lastHashPos); | ||
} | ||
} | ||
|
||
line = trim(line); | ||
if (line.empty()) { | ||
if (impl->multiline.active) | ||
result.setError("Found empty line while parsing multiline value"); | ||
|
||
if (line.empty()) | ||
return result; | ||
} | ||
|
||
auto equalsPos = line.find('='); | ||
|
||
if (equalsPos == std::string::npos && !line.ends_with("{") && line != "}") { | ||
if (equalsPos == std::string::npos && !line.ends_with("{") && line != "}" && !impl->multiline.active) { | ||
// invalid line | ||
result.setError("Invalid config line"); | ||
return result; | ||
} | ||
|
||
if (equalsPos != std::string::npos) { | ||
if (equalsPos != std::string::npos || impl->multiline.active) { | ||
// set value or call handler | ||
CParseResult ret; | ||
auto LHS = trim(line.substr(0, equalsPos)); | ||
auto RHS = trim(line.substr(equalsPos + 1)); | ||
auto LHS = impl->multiline.active ? impl->multiline.lhs : trim(line.substr(0, equalsPos)); | ||
auto RHS = impl->multiline.active ? line : trim(line.substr(equalsPos + 1)); | ||
|
||
if (LHS.empty()) { | ||
result.setError("Empty lhs."); | ||
result.setError("Empty lhs"); | ||
return result; | ||
} | ||
|
||
|
@@ -562,6 +568,35 @@ CParseResult CConfig::parseLine(std::string line, bool dynamic) { | |
if (ISVARIABLE) | ||
return parseVariable(LHS, RHS, dynamic); | ||
|
||
auto lastChar = RHS[RHS.size() - 1]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. .at |
||
bool isMultilineContinuation = lastChar == '\\' || lastChar == '>'; | ||
|
||
if (isMultilineContinuation && impl->multiline.active && impl->multiline.delimiter != lastChar) { | ||
impl->multiline.active = false; | ||
result.setError("Multiline continuation character mismatch. Make sure you are not mixing \\ and >"); | ||
|
||
return result; | ||
} | ||
|
||
if (impl->multiline.buffer.size() > 0 && impl->multiline.delimiter == '>') | ||
impl->multiline.buffer += " "; | ||
|
||
impl->multiline.active = isMultilineContinuation; | ||
|
||
if (isMultilineContinuation) { | ||
impl->multiline.lhs = LHS; | ||
impl->multiline.delimiter = lastChar; | ||
RHS.erase(RHS.size() - 1); | ||
impl->multiline.buffer += RHS.substr(0, RHS.find_last_not_of(MULTILINE_SPACE_CHARSET) + 1); | ||
|
||
return CParseResult{}; | ||
} | ||
|
||
if (!impl->multiline.buffer.empty()) { | ||
RHS = impl->multiline.buffer + RHS; | ||
impl->multiline.buffer.clear(); | ||
} | ||
|
||
bool found = false; | ||
for (auto& h : impl->handlers) { | ||
if (!h.options.allowFlags && h.name != LHS) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what if
line.find_last_not_of(MULTILINE_SPACE_CHARSET)
returnsstd::string::npos